打开GL ES 2.0渲染

时间:2013-12-28 07:06:15

标签: ios opengl-es-2.0 render

我遇到一个问题,我有一个基于GL ES 2.0的开放视图,它首先绘制一个纹理,然后将该纹理渲染到屏幕上。

我将此视图实例化为带有其他按钮的主UIView的子视图。由于某种原因,纹理在绘制时不会立即渲染,而是仅在按下其中一个按钮后才渲染。

有没有人经历过这样的事情?先感谢您 Uba Duba

以下是绘图和渲染代码:

// Drawings a line onscreen based on where the user touches
- (void)renderLineFromPoint:(CGPoint)start toPoint:(CGPoint)end
{
    static GLfloat*     vertexBuffer = NULL;
    static NSUInteger   vertexMax = 64;
    NSUInteger          vertexCount = 0,
    count,
    i;

    [EAGLContext setCurrentContext:context];
    glBindFramebuffer(GL_FRAMEBUFFER, fbTemp);
    // Convert locations from Points to Pixels
    CGFloat scale = self.contentScaleFactor;
    start.x *= scale;
    start.y *= scale;
    end.x *= scale;
    end.y *= scale;

    // Allocate vertex array buffer
    if(vertexBuffer == NULL)
        vertexBuffer = malloc(vertexMax * 2 * sizeof(GLfloat));

    // Add points to the buffer so there are drawing points every X pixels
    count = MAX(ceilf(sqrtf((end.x - start.x) * (end.x - start.x) + (end.y - start.y) * (end.y - start.y)) / kBrushPixelStep), 1);
    for(i = 0; i < count; ++i) {
        if(vertexCount == vertexMax) {
            vertexMax = 2 * vertexMax;
            vertexBuffer = realloc(vertexBuffer, vertexMax * 2 * sizeof(GLfloat));
        }

        vertexBuffer[2 * vertexCount + 0] = start.x + (end.x - start.x) * ((GLfloat)i / (GLfloat)count);
        vertexBuffer[2 * vertexCount + 1] = start.y + (end.y - start.y) * ((GLfloat)i / (GLfloat)count);
        vertexCount += 1;
    }

    // Load data to the Vertex Buffer Object
    glBindBuffer(GL_ARRAY_BUFFER, vboId);
    glBufferData(GL_ARRAY_BUFFER, vertexCount*2*sizeof(GLfloat), vertexBuffer, GL_DYNAMIC_DRAW);

    glEnableVertexAttribArray(pointShader.positionLoc);
    glVertexAttribPointer(pointShader.positionLoc, 2, GL_FLOAT, GL_FALSE, 0, 0);

    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, brushTexture.id);
    // Draw
    glUseProgram(pointShader.programObject);

    // the brush texture will be bound to texture unit 0
    glUniform1i(pointShader.textureLoc, 0);

    // viewing matrices
    GLKMatrix4 projectionMatrix = GLKMatrix4MakeOrtho(0, backingWidth, 0, backingHeight, -1, 1);
    GLKMatrix4 modelViewMatrix = GLKMatrix4Identity; // this sample uses a constant identity modelView matrix
    GLKMatrix4 MVPMatrix = GLKMatrix4Multiply(projectionMatrix, modelViewMatrix);

    glUniformMatrix4fv(pointShader.MVPLoc, 1, GL_FALSE, MVPMatrix.m);

    // point size
    glUniform1f(pointShader.pointSizeLoc, brushTexture.width / kBrushScale);

    // initialize brush color
    glUniform4fv(pointShader.vertexColorLoc, 1, brushColor);

    glDrawArrays(GL_POINTS, 0, vertexCount);

    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
    glBindTexture(GL_TEXTURE_2D, 0);

    [self drawResult];


}

和执行渲染的drawResult函数:

// Draw current result at the present render buffer.
- (void) drawResult
{

    NSLog(@"Calling Draw Result");

    // Bind the master frame buffer.
    glBindFramebuffer(GL_FRAMEBUFFER, fbMaster);
    // Clear the buffer.
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glClear(GL_COLOR_BUFFER_BIT);

    // Draw the background board.
    [self drawTexture:masterTexture ];
    // Draw the temporary (drawing) board.
    [self drawTexture:tempTexture];


    // Bind the render buffer and present it to the view's context.
    glBindRenderbuffer(GL_RENDERBUFFER, renderBuffer);
    [context presentRenderbuffer:GL_RENDERBUFFER];
}

绑定纹理的绘制纹理函数

// Draw a texture at the full screen quad
- (void) drawTexture:(GLuint) textureID
{
    // Positions to draw a texture at the full screen quad.
    const GLfloat vVerticesStraight[] = {
        -1.0f,  1, 0.0f,  // Position 0
        0.0f,  1.0f,        // TexCoord 0
        -1.0f, -1.0f, 0.0f,  // Position 1
        0.0f,  0.0f,        // TexCoord 1
        1.0f, -1.0f, 0.0f,  // Position 2
        1.0f,  0.0f,        // TexCoord 2
        1.0f,  1.0f, 0.0f,  // Position 3
        1.0f,  1.0f         // TexCoord 3
    };

    // Index order for draw element.
    const GLushort indices[] = { 0, 1, 2, 0, 2, 3 };

    // Use the program object
    glUseProgram ( drawTexture.programObject );

    // Load the vertex position
    glVertexAttribPointer ( drawTexture.a_positionLoc, 3, GL_FLOAT,
                           GL_FALSE, 5 * sizeof(GLfloat), vVerticesStraight );

    // Load the texture coordinate
    glVertexAttribPointer ( drawTexture.a_texCoordLoc, 2, GL_FLOAT,
                           GL_FALSE, 5 * sizeof(GLfloat), &vVerticesStraight[3] );

    // Enable attribute for the vertex array.
    glEnableVertexAttribArray ( drawTexture.a_positionLoc );
    glEnableVertexAttribArray ( drawTexture.a_texCoordLoc );

    // Bind the texture.
    glActiveTexture(GL_TEXTURE0);
    glBindTexture ( GL_TEXTURE_2D,  textureID);
    glUniform1i ( drawTexture.s_textureLoc, 0 );

    // Draw the texture.
    glDrawElements ( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices );

    // Bind the current texture to the default texture.
    glBindTexture(GL_TEXTURE_2D, 0);


}

没有背景线程,非常奇怪的是第一张图似乎呈现给纹理而不是屏幕,但是在按下视图上的另一个按钮后,纹理呈现在屏幕上。

0 个答案:

没有答案