Note that there are some explanatory texts on larger screens.

plurals
  1. POGLKit: Drawing Points using GL_POINTS
    text
    copied!<p>I am picking up the opengl es 2.0 with GLKit. I can successfully draw shapes like squares, triangles, cube etc.</p> <p>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.</p> <pre><code>- (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); } </code></pre> <p>and here is my update method just for ref:</p> <pre><code>- (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; } </code></pre> <p>and following is how I am defining the effect:</p> <pre><code>self.effect = [[GLKBaseEffect alloc] init]; self.effect.useConstantColor = GL_TRUE; self.effect.constantColor = GLKVector4Make(0.0, 0.0, 1.0, 1.0); //Blue color </code></pre> <p>So that the points are visible on white background.</p> <p>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.</p> <p><strong>Edit:</strong></p> <p>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:</p> <p>I have <strong>VertexShader.glsl</strong> and <strong>FragmentShader.glsl</strong> files:</p> <p><strong>VertexShader.glsl</strong></p> <pre><code>void main() { gl_PointSize = 10.0; } </code></pre> <p><strong>FragmentShader.glsl</strong></p> <pre><code>void main() { } </code></pre> <p>I am setting up the shader just before I create the effect and just after I set EAGLContext:</p> <pre><code>// 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, &amp;linked); if (linked == 0) { glDeleteProgram(_program); return; } </code></pre> <p>And here is the createShaderWithFile method:</p> <pre><code>- (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, &amp;shaderSource, NULL); glCompileShader(shader); GLint success = 0; glGetShaderiv(shader, GL_COMPILE_STATUS, &amp;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; } </code></pre> <p>I have also updated the drawRect method above.</p> <p>Here is the link to this project: <a href="http://dl.dropbox.com/u/36638938/Template.zip" rel="nofollow">http://dl.dropbox.com/u/36638938/Template.zip</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload