Search code examples
iosopengl-eslinesglkit

iOS simulator shows flickering lines


In my app, I draw a series of points (stars) and then a series of lines (constellation figures) but the lines are flickering on the simulator and when I run the app on my iPhone there are no lines at all. Any ideas?

(I'm running OpenGL 2.0 ES in a GLKView under a GLKViewController.)

Edit: Here is the drawing code where I use a custom shader for the stars but I don't really need it for the lines. Not sure how to return to a default shader. (I've tried glUseProgram(0); which is supposed to work but it makes no difference.)

- (void)drawRect:(CGRect)rect {
    // Drawing code
    //    NSLog(@"GBStarField drawRect invoked!");

    /*********************************/
    // Draw all stars
    /*********************************/

    // Clear the framebuffer
    glClearColor(0.0, 0.0f, 0.0f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);

    // Calculate OpenGL to GLKit View coordinate transformation

    // Calculate perspective transformation
    float aspect = fabsf(self.bounds.size.width / self.bounds.size.height);
    GLKMatrix4 projectionMatrix = GLKMatrix4MakePerspective(GLKMathDegreesToRadians(90.0f), aspect, 0.0f, 10.0f);

    self.effect.transform.projectionMatrix = projectionMatrix;

    // Calculate translation transformation
#ifdef USE_RA_DEC
    GLKMatrix4 modelViewMatrix = GLKMatrix4MakeTranslation(0.0f, 0.0f, 0.0f);
#else
    GLKMatrix4 modelViewMatrix = GLKMatrix4MakeTranslation(0.0f, 0.0f, -2.0f);
#endif

    // Calculate rotation transformation
    // remember the right hand rule for rotation vector
    modelViewMatrix = GLKMatrix4Rotate(modelViewMatrix, GLKMathDegreesToRadians(rotation/10), 0, 1, 0);
    //modelViewMatrix = GLKMatrix4Rotate(modelViewMatrix, GLKMathDegreesToRadians(-90), 0, 0, 1); // polaris
    //modelViewMatrix = GLKMatrix4Rotate(modelViewMatrix, GLKMathDegreesToRadians(180-279.2346), 0, 1, 0); // vega
    //modelViewMatrix = GLKMatrix4Rotate(modelViewMatrix, GLKMathDegreesToRadians(-38.783692), 0, 0, 1); // vega
    //modelViewMatrix = GLKMatrix4Rotate(modelViewMatrix, GLKMathDegreesToRadians(180-78.63447), 0, 1, 0); // rigel
    //modelViewMatrix = GLKMatrix4Rotate(modelViewMatrix, GLKMathDegreesToRadians(8.20164), 0, 0, 1); // rigel

    self.effect.transform.modelviewMatrix = modelViewMatrix;

    [self.effect prepareToDraw];

    // Get uniform locations (must be after linking)
    GLint u_projectionMatrixLocation;
    u_projectionMatrixLocation = glGetUniformLocation(programBrightStar, (const GLchar*) "u_projectionMatrix");
    GLint u_modelViewMatrixLocation;
    u_modelViewMatrixLocation = glGetUniformLocation(programBrightStar, (const GLchar*) "u_modelViewMatrix");

    // Get attribute locations (must be after linking)
    GLint a_positionLocation;
    a_positionLocation =  glGetAttribLocation(programBrightStar, (const GLchar*) "a_position");
    GLint a_colorLocation;
    a_colorLocation =  glGetAttribLocation(programBrightStar, (const GLchar*) "a_color");
    GLint a_pointsizeLocation;
    a_pointsizeLocation =  glGetAttribLocation(programBrightStar, (const GLchar*) "a_pointsize");

    // Working with the star buffers
    glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
    glGetBufferParameteriv(GL_ARRAY_BUFFER, GL_BUFFER_SIZE, &vertexBufferSize);
    //NSLog(@"vertexBufferSize/sizeof(Vertex) = %lu", vertexBufferSize/sizeof(Vertex));

    // Use custom vertex/fragment shader (must be after binging the buffers
    glUseProgram(programBrightStar);

    // Set uniforms for shader (must be after "glUseProgram")
    glUniformMatrix4fv(u_projectionMatrixLocation, 1, GL_FALSE, projectionMatrix.m);
    glUniformMatrix4fv(u_modelViewMatrixLocation, 1, GL_FALSE, modelViewMatrix.m);

    // Set attributes for shader (must be after "glUseProgram")
    glEnableVertexAttribArray(a_positionLocation);
    glVertexAttribPointer(a_positionLocation, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid *) offsetof(Vertex, Position));
    glEnableVertexAttribArray(a_colorLocation);
    glVertexAttribPointer(a_colorLocation, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid *) offsetof(Vertex, Color));
    glEnableVertexAttribArray(a_pointsizeLocation);
    glVertexAttribPointer(a_pointsizeLocation, 1, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid *) offsetof(Vertex, Pointsize));

    // Draw it as points defined by the vertex and index buffer data
    glDrawArrays(GL_POINTS, 0, vertexBufferSize/sizeof(Vertex));

    /*********************************/
    // Draw all constellations
    /*********************************/

    // Working with the constellation buffers
    glBindBuffer(GL_ARRAY_BUFFER, constellationVertexBuffer[0]);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, constellationIndexBuffer[0]);
    glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &indexBufferSize);
    //NSLog(@"indexBufferSize/sizeof(GLuint) = %lu", indexBufferSize/sizeof(GLuint));

    // Use custom vertex/fragment shader (must be after binging the buffers
    glUseProgram(programBrightStar);

    // Set uniforms for shader (must be after "glUseProgram")
    glUniformMatrix4fv(u_projectionMatrixLocation, 1, GL_FALSE, projectionMatrix.m);
    glUniformMatrix4fv(u_modelViewMatrixLocation, 1, GL_FALSE, modelViewMatrix.m);

    // Set attributes for shader (must be after "glUseProgram")
    glEnableVertexAttribArray(a_positionLocation);
    glVertexAttribPointer(a_positionLocation, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid *) offsetof(Vertex, Position));
    glEnableVertexAttribArray(a_colorLocation);
    glVertexAttribPointer(a_colorLocation, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid *) offsetof(Vertex, Color));
    glEnableVertexAttribArray(a_pointsizeLocation);
    glVertexAttribPointer(a_pointsizeLocation, 1, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid *) offsetof(Vertex, Pointsize));

    // Draw it as lines defined by the vertex and index buffer data
    glDrawElements(GL_LINES, indexBufferSize/sizeof(GLuint), GL_UNSIGNED_INT, 0);
}

Solution

  • I found the problem. It was my shader. I had a shader for stars that made circles from large square points. This was rejecting the lines as the image was rotated. I solved the problem by adding a second shader to draw the lines.