c - OpenGL ES 2.0 : Can't seem to render second VBO?

标签 c opengl-es-2.0 vbo

我在 OpenGL ES 2.0 中有 2 个 VBO 和 2 个 IBO。我在渲染第一个 VBO 时没有任何问题,但没有渲染第二个。我也没有收到错误。

第一个 VBO 包含一个具有 9 个顶点的 GL_STATIC_DRAW 对象。第二个 VBO 包含 4 个顶点来创建一个简单的 2D 平面,以便我可以在其中渲染用户界面。

这是我的 C 代码的相关部分。renderBG() 呈现第一个 VBO,renderUI() 呈现第二个 VBO。我这样做的方式正确吗?

void renderBG(OGL_STATE_T *state) {

    matIdentity(modelViewMatrix);   
    matTranslate(modelViewMatrix, 0, 0, -1.0);

    _currentRotation = _currentRotation + 0.5;
    _currentRotation = fmod(_currentRotation, 360); 
    matRotate(modelViewMatrix, 0, 0, 45);
    matRotate(modelViewMatrix, 0, _currentRotation, 0);

    const GLfloat *mvMat = modelViewMatrix;
    const GLfloat *pMat = projectionMatrix;

    glClearColor(0, 0.05, 0, 1.0);
    glClearDepthf(10.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glViewport(0,0,state->screen_width, state->screen_height);

    glBindBuffer(GL_ARRAY_BUFFER, state->nsVB);

    glUniformMatrix4fv(_projectionUniform, 1, 0, pMat);
    glUniformMatrix4fv(_modelViewUniform, 1, 0, mvMat);

    glVertexAttribPointer(_positionSlot, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
    glVertexAttribPointer(_colorSlot, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex),
            (GLvoid *) (sizeof(float) * 3));

    glEnableVertexAttribArray(_positionSlot);
    glEnableVertexAttribArray(_colorSlot);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, state->nsIB);
    glDrawElements(GL_TRIANGLES, sizeof(nsIndices)/sizeof(nsIndices[0]),
            GL_UNSIGNED_BYTE, 0);

}

void renderUI(OGL_STATE_T *state) {

    matIdentity(modelViewMatrix);
    matTranslate(modelViewMatrix, 0, 0, -1.0);

    const GLfloat *mvMat2 = modelViewMatrix;
    const GLfloat *pMat2 = projectionMatrix;

    glViewport(0,0,state->screen_width, state->screen_height);

    glBindBuffer(GL_ARRAY_BUFFER, state->uiVB);

    glUniformMatrix4fv(_projectionUniform, 1, 0, pMat2);
    glUniformMatrix4fv(_modelViewUniform, 1, 0, mvMat2);

    glVertexAttribPointer(_positionSlot, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
    glVertexAttribPointer(_colorSlot, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex),
            (GLvoid *) (sizeof(float) * 3));

    glEnableVertexAttribArray(_positionSlot);
    glEnableVertexAttribArray(_colorSlot);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, state->uiIB);
    glDrawElements(GL_TRIANGLES, uiIndicesArraySize / uiIndicesElementSize,
            GL_UNSIGNED_BYTE, 0);   

    GLenum err;

    if ((err = glGetError()) != GL_NO_ERROR)
        printf("There was an error");
}

void render(OGL_STATE_T *state) {

    renderBG(state);
    renderUI(state);

    eglSwapBuffers(state->display, state->surface);
    check();
}

编辑:

这里有一些额外的信息。这是在第二个 VBO 中填充顶点和索引数组的方式:

GLfloat _width = uiWidth;
GLfloat _height = uiHeight;

Vertex uiVertices[] = {
    {{-_width,_height,0}, {1,1,1,1}},       // Top left
    {{_width,_height,0}, {1,1,1,1}},        // Top right
    {{-_width,-_height,0}, {1,1,1,1}},      // Bottom left
    {{_width,-_height,0}, {1,1,1,1}}        // Bottom right
};

GLubyte uiIndices[] = {
    0,1,2,
    2,1,3
};

uiVerticesArraySize = sizeof(uiVertices);
uiVerticesElementSize = sizeof(uiVertices[0]);
uiIndicesArraySize = sizeof(uiIndices);
uiIndicesElementSize = sizeof(uiIndices[0]);

printf("%f %f\n", uiVertices[0].Position[0], uiVertices[0].Position[1]);

glGenBuffers(1, &state->uiVB);
glBindBuffer(GL_ARRAY_BUFFER, state->uiVB);
glBufferData(GL_ARRAY_BUFFER, sizeof(uiVertices), uiVertices, GL_STATIC_DRAW);

glGenBuffers(1, &state->uiIB);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, state->uiIB);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(uiIndices), uiIndices, GL_STATIC_DRAW);

最佳答案

事实证明我按顺时针顺序声明了我的索引(认为这将面向相机的法线)并启用了 GL_CULL_FACE。调换指数,世界一切都好。

关于c - OpenGL ES 2.0 : Can't seem to render second VBO?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22122700/

相关文章:

c - 二进制 * 的无效操作数(有 ‘int’ 和 ‘int *’ )

c - PC lint 错误 19 "useless declaration "

android - Adreno 220 GLSL 漏洞

ios - iOS 上的 OpenGL ES 2.0 对象拾取

opengl-es - OpenGL,是否值得在绘制调用后显式解除绑定(bind)?

C++ 纹理显示不正确 : Merging into 1 colour

c - Linux semaphore.h 挂起/等待队列策略

c - 如何打开在 dev 中创建的文件并在用户空间程序中使用它?

android - 在 OpenGL ES 2.0 中将图像映射到正方形(2 个三角形带)的最简单方法是什么?

java - OpenGL VBO - 数组步幅错误?