简体   繁体   中英

openGL indices rendering issues

I was hoping someone could help me understand what it is I am missing or doing wrong. I am trying to make a simple pyramid but for some reason the bottom of the pyramid will not render (the two triangles that make up the bottom).

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

    };

and below are the indices being listed for each triangle

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
    }

below you can see where I define the number of vertices based on the size of indices and create VBO's and buffers

 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);

and below where i activate the mesh to draw the elements

// 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);

Found a error over:


        //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

the 16 and 17 numbered vertices have the same value . Are you sure you are supposed to print the same vertices for a triangle?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM