1

我正在尝试使用 IBO 渲染两个不同的三角形。我将六个顶点存储在一个 VBO 中,并尝试通过两个独立的 IBO 访问它们。问题是第一个 IBO 渲染但第二个没有。createVertices 和 createIndices 在初始化时被调用。

void createVertices()
{
    //Vertex Data
    GLfloat v[] = { 0.95f,  0.75f, 0.0f, 1.0f,
                    0.75f, -0.75f, 0.0f, 1.0f,
                    0.0f,  -0.75f, 0.0f, 1.0f, // END OF TRIANGLE 1
                   -0.75,  0.75f, 0.5f, 1.0f,
                   -0.75, -0.75f, 0.5f, 1.0f,
                    0.0f, -0.75f, 0.5f, 1.0f }; // END OF TRIANGLE 2

    glGenVertexArrays(1, &VAO);
    glBindVertexArray(VAO);
    glGenBuffers(1, &VBO);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);

    glBufferData(GL_ARRAY_BUFFER, sizeof(v), v, GL_STATIC_DRAW);
    glEnableVertexAttribArray(0);

    glBindBuffer(GL_ARRAY_BUFFER, 0);
}

//////////

void createIndices()
{
    GLushort i[] = { 0,1,2};

    glGenBuffers(2, IBO);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO[0]);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(i), i, GL_STATIC_DRAW);
    size = (sizeof(i)/sizeof(GLushort)); // USED IN DRAWELEMENTS


    GLushort w[] = { 3,4,5};

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO[1]);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(w), w, GL_STATIC_DRAW);
    size2 = (sizeof(i)/sizeof(GLushort)); // USED IN DRAWELEMENTS


    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}

/////////

void Render()
{
    glClear(GL_COLOR_BUFFER_BIT);

    glUseProgram(shader.SProgram);           
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glVertexAttribPointer(0,4, GL_FLOAT, GL_FALSE, 0, 0);


    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO[0]);
    glDrawElements(GL_TRIANGLES,size,GL_UNSIGNED_SHORT,(GLvoid*)IBO[0]);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO[1]);
    glDrawElements(GL_TRIANGLES,size2,GL_UNSIGNED_SHORT,(GLvoid*)IBO[1]);

    glUseProgram(0);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
4

1 回答 1

1

这没有意义:

glDrawElements(GL_TRIANGLES,size,GL_UNSIGNED_SHORT,(GLvoid*)IBO[0]);
                                                   ^^^^^^^^^^^^^^

具有 IBO 限制的 glDrawElements 的参数是缓冲区的偏移量,就像 gl…Pointer 和 VBO 一样。你可能只是想要这个

glDrawElements(GL_TRIANGLES,size,GL_UNSIGNED_SHORT,(GLvoid*)0);

对于您的 IBO,只需将 IBO 本身与

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO[…]);
于 2013-03-31T22:46:57.170 回答