【发布时间】:2013-01-29 02:24:08
【问题描述】:
编辑:Apple 在此链接中找到的工作编码示例: http://developer.apple.com/library/ios/#documentation/3DDrawing/Conceptual/OpenGLES_ProgrammingGuide/TechniquesforWorkingwithVertexData/TechniquesforWorkingwithVertexData.html
在为 200 x 200 方格的固定网格创建顶点后,我通过使用 VBO 将顶点发送到 GPU 来渲染它。然后如何更新顶点的颜色?数百个顶点的颜色会频繁变化 - 每隔几帧。
我之前没有使用 VBO 解决了这个问题。我创建了顶点和颜色数组,并使用以下代码来渲染顶点。我通过更新数组triangleColours改变了顶点的颜色:
-(void)render {
glClearColor(clearColor.r, clearColor.g, clearColor.b, clearColor.a);
glClear(GL_COLOR_BUFFER_BIT);
[effect prepareToDraw];
glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition, 2,
GL_FLOAT, GL_FALSE, 0, triangleVertices);
glEnableVertexAttribArray(GLKVertexAttribColor);
glVertexAttribPointer(GLKVertexAttribColor, 4,
GL_FLOAT, GL_FALSE, 0, triangleColours);
glDrawArrays(GL_TRIANGLES, 0, numTriangleVertices);
glDisableVertexAttribArray(GLKVertexAttribPosition);
glDisableVertexAttribArray(GLKVertexAttribColor);
}
虽然上面的代码有效,但它消耗了很多电池,所以现在我尝试用 VBO 来做。顶点不会改变位置,所以我知道 VBO 是一个不错的选择,但是改变顶点的颜色呢?
这是我当前简化为仅绘制一个正方形的代码:
编辑:我将如何更新下面的代码,以便在它们更改时只上传顶点颜色? VBO 设置代码和绘制代码应该如何更改? 更新我的 verticesColours 颜色数组然后调用 glBufferSubData() 怎么样?
// Setup vertices and VBO
// Vertices for a single square
const Vertex Vertices[] = {
// position and colour
{{-20, -20}, {1, 1, 1, 1}},
{{-20, -20}, {1, 1, 1, 1}},
{{-20, -20}, {1, 1, 1, 1}},
{{-20, -20}, {1, 1, 1, 1}},
};
const GLubyte Indices[] = {
0, 1, 2,
2, 3, 0,
};
numIndices = sizeof(Indices)/sizeof(Indices[0]);
[EAGLContext setCurrentContext:self.context];
glEnable(GL_CULL_FACE);
self.effect = [[GLKBaseEffect alloc] init];
self.effect.transform.projectionMatrix = GLKMatrix4MakeOrtho(-50.0, 50.0,
-50.0, 50.0,
0.0, 1.0);
glGenVertexArraysOES(1, &_vertexArray);
glBindVertexArrayOES(_vertexArray);
glGenBuffers(1, &_vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);
glGenBuffers(1, &_indexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(Indices), Indices, GL_STATIC_DRAW);
glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid *) offsetof(Vertex, Position));
glEnableVertexAttribArray(GLKVertexAttribColor);
glVertexAttribPointer(GLKVertexAttribColor, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid *) offsetof(Vertex, Color));
glBindVertexArrayOES(0);
// Render
- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect {
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
[self.effect prepareToDraw];
glBindVertexArrayOES(_vertexArray);
glDrawElements(GL_TRIANGLES, numIndices, GL_UNSIGNED_BYTE, 0);
}
【问题讨论】:
标签: iphone objective-c opengl-es opengl-es-2.0 vbo