I have run into a problem using C and OpenGL (GLFW and GLAD). When I use GL_CULL_FACE, it only hides one triangle and it hides it from the front of my cubes. If I use GL_FRONT or GL_BACK, the same thing happens but it only shows the opposite triangles.
These are my vertices and indices that I'm currently using:
static GLfloat vertices[] = {
// front face
0.5, 0.5, -0.5, // 0
-0.5, 0.5, -0.5, // 1
-0.5, -0.5, -0.5, // 2
0.5, -0.5, -0.5, // 3
// right face
0.5, 0.5, -0.5, // 4
0.5, 0.5, 0.5, // 5
0.5, -0.5, 0.5, // 6
0.5, -0.5, -0.5, // 7
// left face
-0.5, 0.5, -0.5, // 8
-0.5, 0.5, 0.5, // 9
-0.5, -0.5, 0.5, // 10
-0.5, -0.5, -0.5, // 11
// back face
0.5, 0.5, 0.5, // 12
-0.5, 0.5, 0.5, // 13
-0.5, -0.5, 0.5, // 14
0.5, -0.5, 0.5, // 15
// top face
0.5, 0.5, -0.5, // 16
-0.5, 0.5, -0.5, // 17
0.5, 0.5, 0.5, // 18
-0.5, 0.5, 0.5, // 19
// bottom face
0.5, -0.5, -0.5, // 20
-0.5, -0.5, -0.5, // 21
0.5, -0.5, 0.5, // 22
-0.5, -0.5, 0.5, // 23
};
static GLuint indices[] = {
20, 23, 22,
23, 21, 20,
16, 19, 18,
19, 17, 16,
12, 14, 15,
14, 13, 12,
8, 10, 11,
10, 9, 8,
4, 6, 7,
6, 5, 4,
0, 2, 3,
2, 1, 0
};
This is how i'm drawing everything:
for (int x = 0; x < 50; x )
{
for (int z = 0; z < 50; z )
{
camera.Model = translate(mat4(1.0), vec3(x, 0, z));
glUniformMatrix4fv(matrixID, 1, GL_FALSE, &(camera.Projection * camera.View * camera.Model)[0][0]);
glDrawElements(GL_TRIANGLES, sizeof(indices), GL_UNSIGNED_INT, 0);
}
}
These are my buffers:
GLuint vertexbuffer;
glGenBuffers(1, &vertexbuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
GLuint indicesbuffer;
glGenBuffers(1, &indicesbuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indicesbuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
How would I fix this?
CodePudding user response:
Your mesh definition lacks consistency. Taking your 'top' face as an example, one of your triangles is defined in clockwise (CW) order, the other is counter-clockwise (CCW):
You should fix that. Stick to CCW throughout:
Adding to that, some of your face vertices are numbered in a C order, some are in Z order. This isn't a problem per se, but makes it more confusing.
CodePudding user response:
When Face Culling is enabled, all triangles must have the same winding order. For a closed three-dimensional volume, the winding order of the triangles is the same if all triangles have the same winding order when viewed from the outside of the mesh. So if you look at the mesh from one side, the back faces have a different winding order than the front faces. The winding order of your triangles is mixed. Some are clockwise and some are counterclockwise.
e.g.: The triangles with indices 0, 2, 3
and 2, 1, 0
have a different winding order. However, the triangles with indices 0, 2, 3
and 0, 1, 2
have the same winding order.