使用opengles 2.0绘制背景正方形和点



我正在学习适用于iPhone的OpenGL 2.0。

首先,我试着画一个有质感的背景。它只有在我不画其他东西的时候才有效。

下面是绘制背景的代码:

[EAGLContext setCurrentContext:context];
// Clear the buffer
glBindFramebuffer(GL_FRAMEBUFFER, viewFramebuffer);
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
//Draw something here
if (backgroundTextureCreated == YES) {
    static const GLfloat squareVertices[] = {
        -1.0f, -1.0f,
        1.0f, -1.0f,
        -1.0f,  1.0f,
        1.0f,  1.0f,
    };
    static const GLfloat textureVertices[] = {
        1.0f, 1.0f,
        1.0f, 0.0f,
        0.0f,  1.0f,
        0.0f,  0.0f,
    };
    #warning BG drawing here.
    /*_ddp_positionSlot = glGetAttribLocation(_ddp_program, "position");
    _ddp_inputTextureCoordinateSlot = glGetAttribLocation(_ddp_program, "inputTextureCoordinate");
    glEnableVertexAttribArray(_ddp_positionSlot);
    glEnableVertexAttribArray(_ddp_inputTextureCoordinateSlot);
    _ddp_TextureUniform = glGetUniformLocation(_ddp_program, "bg");*/
    glBindTexture(GL_TEXTURE_2D, BackgroundTexture.id);
    glUniform1i(_ddp_TextureUniform, 0);
    glVertexAttribPointer(_ddp_positionSlot, 2, GL_FLOAT, 0, 0, squareVertices);
    glEnableVertexAttribArray(_ddp_positionSlot);
    glVertexAttribPointer(_ddp_inputTextureCoordinateSlot, 2, GL_FLOAT, 0, 0, textureVertices);
    glEnableVertexAttribArray(_ddp_inputTextureCoordinateSlot);
    glUseProgram(_ddp_program);
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
// Display the buffer
glBindRenderbuffer(GL_RENDERBUFFER, viewRenderbuffer);
[context presentRenderbuffer:GL_RENDERBUFFER];

这里有代码来画点:

- (void)renderLineFromPoint:(CGPoint)start toPoint:(CGPoint)end
{
static GLfloat*     vertexBuffer = NULL;
static NSUInteger   vertexMax = 128;
NSUInteger          vertexCount = 0,
                    count,
                    i;
[EAGLContext setCurrentContext:context];
glBindFramebuffer(GL_FRAMEBUFFER, viewFramebuffer);
glBindTexture(GL_TEXTURE_2D, brushTexture.id);
// 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);
    NSLog(@"Vertex coords: %f,%f",
          start.x + (end.x - start.x) * (GLfloat)i / (GLfloat)count,
            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(ATTRIB_VERTEX);
glVertexAttribPointer(ATTRIB_VERTEX, 2, GL_FLOAT, GL_FALSE, 0, 0);
// Draw
glUseProgram(program[PROGRAM_POINT].id);
glDrawArrays(GL_POINTS, 0, vertexCount);
//glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
// Display the buffer
glBindRenderbuffer(GL_RENDERBUFFER, viewRenderbuffer);
[context presentRenderbuffer:GL_RENDERBUFFER];
}

renderLineFromPoint函数取自apple GLPaint示例代码。

当我从点绘制线时,背景绘制停止工作。

怎么了?

我也遇到过类似的问题。我能够画一个纹理作为背景。每当我开始用手指在画布上作画时,背景就消失了(这似乎也是你的问题)。

我的绘图代码是正确的(你的似乎也是)。问题是,在设置背景后,在用手指作画之前,调用了方法resizeFromLayer:,然后调用了renderBufferStorage:fromDrawable:。后一种方法刷新后备缓冲区(请参见renderBufferStorage:fromDrawable)。

由于您也使用GLPaint作为起点,我想这也可能是您问题的原因。

最新更新