簡體   English   中英

openGL 索引渲染問題

[英]openGL indices rendering issues

我希望有人能幫助我了解我遺漏了什么或做錯了什么。 我正在嘗試制作一個簡單的金字塔,但由於某種原因,金字塔的底部不會呈現(構成底部的兩個三角形)。

GLfloat verts[] = {

        // Vertex Positions    // Colors (r,g,b,a)

        //front triangle
         0.0f,  1.0f, 0.0f,   1.0f, 0.0f, 0.0f, 1.0f, // Top Right Vertex 0
        -1.0f, -1.0f, 1.0f,   0.0f, 1.0f, 0.0f, 1.0f, // Bottom Right Vertex 1
         1.0f, -1.0f, 1.0f,   0.0f, 0.0f, 1.0f, 1.0f, // Bottom Left Vertex 2

        //right triangle
         0.0f,  1.0f, 0.0f,   1.0f, 0.0f, 1.0f, 1.0f, // Top Right vertex 3
         1.0f, -1.0f, 1.0f,   0.5f, 0.5f, 1.0f, 1.0f, // 4 
         1.0f,  -1.0f, -1.0f,  1.0f, 1.0f, 0.5f, 1.0f, //  5 

         //back triangle
         0.0f,  1.0f, 0.0f,   0.2f, 0.2f, 0.5f, 1.0f, //  6  
         1.0f, -1.0f, -1.0f,  1.0f, 0.0f, 1.0f, 1.0f, //  7 
        -1.0f, -1.0f, -1.0f,  1.0f, 0.0f, 1.0f, 1.0f, // 8

        //left triangle
        0.0f,  1.0f,  0.0f,  1.0f, 0.0f, 1.0f, 0.5f, // 9
       -1.0f, -1.0f, -1.0f,  0.0f, 0.0f, 1.0f, 1.0f, // 10
       -1.0f, -1.0f,  1.0f,  0.5f, 0.5f, 1.0f, 1.0f // 11

       //bottom triangle
       - 1.0f, -1.0f,  1.0f,  0.0f, 0.0f, 0.0f, 0.0f, //12
        1.0f, -1.0f,  1.0f,  0.0f, 0.0f, 0.0f, 0.0f, //13
       -1.0f, -1.0f, -1.0f,  0.0f, 0.0f, 0.0f, 0.0f, //14

        //bottom triangle right
       -1.0f, -1.0f, -1.0f,  0.0f, 0.0f, 0.0f, 0.0f, //15
        1.0f, -1.0f, -1.0f,  0.0f, 0.0f, 0.0f, 0.0f, //16 
        1.0f, -1.0f, -1.0f,  0.0f, 0.0f, 0.0f, 0.0f, //17

    };

以下是為每個三角形列出的索引

GLushort indices[] = {
        0, 1, 2,     // Triangle 1
        3, 4, 5,     // Triangle 2
        6, 7, 8,     // Triangle 3
        9, 10, 11,   // Triangle 4
        12, 13, 14,  // Triangle 5
        15, 16, 17,  // Triangle 6
    }

在下面,您可以看到我根據索引的大小定義頂點數並創建 VBO 和緩沖區的位置

 const GLuint floatsPerVertex = 3;
    const GLuint floatsPerColor = 4;

    //mesh.nVertices = sizeof(verts) / (sizeof(verts[0]) * (floatsPerVertex + floatsPerColor));
    mesh.nVertices = sizeof(indices) / (sizeof(indices[0]));
    glGenVertexArrays(1, &mesh.vao); // we can also generate multiple VAOs or buffers at the same time
    glBindVertexArray(mesh.vao);

    // Create VBO
    glGenBuffers(2, mesh.vbo);
    glBindBuffer(GL_ARRAY_BUFFER, mesh.vbo[0]); // Activates the buffer
    glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW); // Sends vertex or coordinate data to the GPU

    //buffer for indices
    //mesh.nVertices = sizeof(indices) / sizeof(indices[0]);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mesh.vbo[1]);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

    // Strides between vertex coordinates
    GLint stride = sizeof(float) * (floatsPerVertex + floatsPerColor);

    // Create Vertex Attribute Pointers
    glVertexAttribPointer(0, floatsPerVertex, GL_FLOAT, GL_FALSE, stride, 0);
    glEnableVertexAttribArray(0);

    glVertexAttribPointer(1, floatsPerColor, GL_FLOAT, GL_FALSE, stride, (char*)(sizeof(float) * floatsPerVertex));
    glEnableVertexAttribArray(1);

在我激活網格以繪制元素的位置下方

// Activate the VBOs contained within the mesh's VAO
    glBindVertexArray(gMesh.vao);

    glDrawElements(GL_TRIANGLES, gMesh.nVertices, GL_UNSIGNED_SHORT, NULL); // draws the triangle for pyramids
    glBindVertexArray(0);

發現一個錯誤:


        //bottom triangle right
       -1.0f, -1.0f, -1.0f,  0.0f, 0.0f, 0.0f, 0.0f, //15
        1.0f, -1.0f, -1.0f,  0.0f, 0.0f, 0.0f, 0.0f, //16 
        1.0f, -1.0f, -1.0f,  0.0f, 0.0f, 0.0f, 0.0f, //17

編號為 16 和 17 的頂點具有相同的值。 您確定應該為三角形打印相同的頂點嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM