iOS模拟器显示闪烁的线条

时间:2015-02-19 23:22:42

标签: ios opengl-es lines glkit

在我的应用程序中,我绘制了一系列点(星星),然后是一系列线条(星座图),但线条在模拟器上闪烁,当我在iPhone上运行应用程序时根本没有线条。有什么想法吗?

(我在GLKViewController下的GLKView中运行OpenGL 2.0 ES。)

编辑:这是绘图代码,我使用自定义着色器为星星,但我真的不需要它的线条。不确定如何返回默认着色器。 (我已经尝试过glUseProgram(0);这应该可以工作,但没有区别。)

- (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);
}

1 个答案:

答案 0 :(得分:0)

我发现了问题。这是我的着色器。我有一个明星着色器,可以从大的方形点制作圆圈。这是在图像旋转时拒绝线条。我通过添加第二个着色器来绘制线条来解决问题。

相关问题