iphone - 使用 VBO 上传后更改顶点的颜色

标签 iphone objective-c opengl-es opengl-es-2.0 vbo

编辑: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);
}

最佳答案

视情况而定。 (很棒的词,不是吗?适用于所有情况。)

如果您确实需要以不可预测的方式改变每个顶点的颜色,每次绘制它们时,那​​么您将需要上传每帧的顶点颜色。将顶点位置放在自己的(静态)VBO 中是一个不错的选择,因为这样您就可以重复使用,而无需每次都上传(系统内存 <-> GPU 内存带宽始终是宝贵的商品),但是任何东西都会有改变,好吧,必须改变。

如果您可以通过编程方式计算每个顶点的颜色,那么 vertex shaders 就是这样的。派上用场。 (确实,它们是为了什么而设计的!)希望每个顶点的颜色都是以下组合的函数:

  1. 静态每顶点数据。您可以在VBO中上传一次。并重复使用。
  2. 整个数组中全局的变量数据。您使用shader uniforms为此,每次抽奖只需上传一次即可。
  3. 也许 vertex textures ,您的平台是否支持?

然后,您可以编写一个顶点着色器来计算这些输入的颜色,并且每一帧您只需重新指定着色器制服 - 通常总共大约有十几个 float 。

所以这取决于您希望颜色如何变化。如果您可以使用顶点着色器,那么您确实可以跳过每次绘制的上传。如果您做不到,并且/或突变太复杂或任意,无法在顶点着色器中完成,那么您只需在 CPU 端执行该操作,然后咬紧牙关上传即可。

关于iphone - 使用 VBO 上传后更改顶点的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14574660/

相关文章:

iphone - 在Three20 TTLauncherView中更改页面控制点的颜色

java - Libgdx:不支持的格式组合

iphone - iPhone Appstore批准错误

iphone - 在 objective-c 中, "2011-05-10T14:30:00 0000"的正确日期格式是什么?

ios - 如何找到内存警告的真正原因以及如何在iOS应用程序中解决

ios - UIToolbar 标准尺寸

iphone - 我可以在不手动转换所有顶点的情况下进行 openGL 几何批处理吗?

iPhone openGLES 性能调优

iphone - "Unable to read symbols for..."导致设备上的 iPhone 应用程序崩溃(不是模拟器)

ios - 如何从 NSAttributedString 的末尾修剪(删除)空格