openglopengl-4

OpenGL Render Half Polygon


I am getting half polygon rendered while using Indices in OpenGL. Any idea what's going wrong?

    float vertices[] = {
    -0.5f, 0.5f, 0,
    -0.5f, -0.5f, 0,
    0.5f, -0.5f, 0,
    0.5f, -0.5f, 0,
    0.5f, 0.5f, 0,
    -0.5f, 0.5f, 0
    };

    uint32_t Indices[] = {
        0,1,3,
        3,1,2
    };

enter image description here


Solution

  • When you draw with indices you just need 4 vertices not 6. The index is the "address" of the vertex:

    float vertices[] = {
        -0.5f, -0.5f, 0,    // vertex 0
         0.5f, -0.5f, 0,    // vertex 1
         0.5f,  0.5f, 0,    // vertex 2
        -0.5f,  0.5f, 0     // vertex 3
    };
    
    uint32_t Indices[] = {
        0, 1, 3,            // 1. triangle, vertices 0, 1, 2 
        0, 2, 3             // 2. triangle, vertices 0, 2, 3
    };