1

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

genpfault
  • 51,148
  • 11
  • 85
  • 139
  • The data is correct, which makes this an unanswerable question (because no one knows how you are submitting the geometry to openGL) – robthebloke May 24 '22 at 03:14
  • The data is **not** correct, vertices 2 and 3 are identical which means that the second triangle is an infinitely thin line. Additionally the shared vertices (1 and 3) produce a line parallel to the x axis, if you're trying to draw a rectangle they would need to be the vertices of the hypotenuse. – vandench May 24 '22 at 12:46

2 Answers2

2

Are you drawing with indices ? If that's the case you just need 4 vertices

If you're drawing with indices then the data is incorrect index 2 and 3 have the same value supplying the same two positions won't do anything, You're supplying two vertices to draw a triangle which requires 3 vertices

alabdaly891
  • 159
  • 1
  • 8
2

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
};
Rabbid76
  • 202,892
  • 27
  • 131
  • 174