Asymptote
Asymptote

Reputation: 1150

GLKit: Drawing Points using GL_POINTS

I am picking up the opengl es 2.0 with GLKit. I can successfully draw shapes like squares, triangles, cube etc.

I have problem drawing points, here is my code which isn't working on device (iPod 4), though I can see the Points/dots in retina simulator.

- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect {

     //White background color
    glClearColor(1.0, 1.0, 1.0, 1.0);
    glClear(GL_COLOR_BUFFER_BIT);

    [self.effect prepareToDraw];  

    //USING _program
    glUseProgram(_program);

    GLfloat vert[] = {0,0, 0.1,0.1, 0.2,0.2, 0.3,0.3, 0.4,0.4, 0.5,0.5};
    glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
    glEnableVertexAttribArray(GLKVertexAttribPosition);
    glVertexAttribPointer(GLKVertexAttribPosition, 2, GL_FLOAT, GL_FALSE, 0, vert);
    glDrawArrays(GL_POINTS, 0, 5);
    glDisableVertexAttribArray(GLKVertexAttribPosition);
}

and here is my update method just for ref:

- (void)update {
    float aspect = fabsf(self.view.bounds.size.width / self.view.bounds.size.height);
    GLKMatrix4 projectionMatrix = GLKMatrix4MakePerspective(GLKMathDegreesToRadians(65.0f), aspect, 4.0f, 10.0f);    
    self.effect.transform.projectionMatrix = projectionMatrix;

    //Since visible range is 4 to 10 so have to mave back in z
    GLKMatrix4 modelViewMatrix = GLKMatrix4MakeTranslation(0.0f, 0.0f, -6.0f);
    self.effect.transform.modelviewMatrix = modelViewMatrix;   
}

and following is how I am defining the effect:

self.effect = [[GLKBaseEffect alloc] init];
self.effect.useConstantColor = GL_TRUE;
self.effect.constantColor = GLKVector4Make(0.0, 0.0, 1.0, 1.0); //Blue color

So that the points are visible on white background.

Can someone point any flaw in this code? I am not sure why it isn't showing on device since triangles, squares and cubes where all visible. Any help is appreciated.

Edit:

I have not used shaders before so this is my first time. As Ben mentioned the example code. Here's now what I am doing:

I have VertexShader.glsl and FragmentShader.glsl files:

VertexShader.glsl

void main()
{
    gl_PointSize = 10.0;
}

FragmentShader.glsl

void main()
{

}

I am setting up the shader just before I create the effect and just after I set EAGLContext:

// Setup shader
GLuint vertexShader = [self createShaderWithFile:@"VertexShader.glsl" type:GL_VERTEX_SHADER];
GLuint fragmentShader = [self createShaderWithFile:@"FragmentShader.glsl" type:GL_FRAGMENT_SHADER];
_program = glCreateProgram();

glAttachShader(_program, vertexShader);
glAttachShader(_program, fragmentShader);
glLinkProgram(_program);

GLint linked = 0;
glGetProgramiv(_program, GL_LINK_STATUS, &linked);
if (linked == 0) {
    glDeleteProgram(_program);
    return;
}

And here is the createShaderWithFile method:

- (GLuint)createShaderWithFile:(NSString *)filename type:(GLenum)type 
{
    GLuint shader = glCreateShader(type);

    if (shader == 0) {
        return 0;
    }

    NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:filename];
    NSString *shaderString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    const GLchar *shaderSource = [shaderString cStringUsingEncoding:NSUTF8StringEncoding];

    glShaderSource(shader, 1, &shaderSource, NULL);
    glCompileShader(shader);

    GLint success = 0;
    glGetShaderiv(shader, GL_COMPILE_STATUS, &success);

    if (success == 0) {
        char errorMsg[2048];
        glGetShaderInfoLog(shader, sizeof(errorMsg), NULL, errorMsg);
        NSString *errorString = [NSString stringWithCString:errorMsg encoding:NSUTF8StringEncoding];
        NSLog(@"Failed to compile %@: %@", filename, errorString);
        glDeleteShader(shader);
        return 0;
    }

    return shader;
}

I have also updated the drawRect method above.

Here is the link to this project: http://dl.dropbox.com/u/36638938/Template.zip

Upvotes: 0

Views: 2459

Answers (1)

Ben Ruijl
Ben Ruijl

Reputation: 5123

Try setting the gl_PointSize variable in the vertex shader to a larger value. Maybe you can't see a single pixel on the device.

Upvotes: 1

Related Questions