我正在学习用于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示例代码。
当我从点画线时,背景图停止工作。
怎么了?
答案 0 :(得分:0)
我有一个非常类似的问题。 我能够绘制纹理作为背景。每当我用手指在画布上画画时,背景就会消失(似乎也是你的问题)。
我的绘图代码没问题(你的代码似乎也是如此)。问题是,在设置背景之后,在用手指画画之前,方法resizeFromLayer:
被调用,然后调用renderBufferStorage:fromDrawable:
。后一种方法刷新后备缓冲区(参见renderBufferStorage: fromDrawable)。
由于你也使用GLPaint作为起点,我想这也可能是你问题的原因。