Hello!
So im a fresh beginner of opengl programming and im confronted with a simple problem.
I have a cube with a texture. Now i want it to react to lighting. For that i have to give the cube some normals.
Here are my cube functions:
GLuint VertexArrayIDSolidCube = 0;
static void createCube()
{
GLuint vertexbuffer;
GLuint normalbuffer;
glGenVertexArrays(1, &VertexArrayIDSolidCube);
glBindVertexArray(VertexArrayIDSolidCube);
// Our vertices. Tree consecutive floats give a 3D vertex; Three consecutive vertices give a triangle.
// A cube has 6 faces with 2 triangles each, so this makes 6*2=12 triangles, and 12*3 vertices
static const GLfloat g_vertex_buffer_data[] = {
-1.0f,-1.0f,-1.0f, -1.0f,-1.0f, 1.0f, -1.0f, 1.0f, 1.0f, // Left Side
-1.0f,-1.0f,-1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f,-1.0f, // Left Side
1.0f, 1.0f,-1.0f, -1.0f,-1.0f,-1.0f, -1.0f, 1.0f,-1.0f, // Back Side
1.0f,-1.0f, 1.0f, -1.0f,-1.0f,-1.0f, 1.0f,-1.0f,-1.0f, // Bottom Side
1.0f, 1.0f,-1.0f, 1.0f,-1.0f,-1.0f, -1.0f,-1.0f,-1.0f, // Back Side
1.0f,-1.0f, 1.0f, -1.0f,-1.0f, 1.0f, -1.0f,-1.0f,-1.0f, // Bottom Side
-1.0f, 1.0f, 1.0f, -1.0f,-1.0f, 1.0f, 1.0f,-1.0f, 1.0f, // Front Side
1.0f, 1.0f, 1.0f, 1.0f,-1.0f,-1.0f, 1.0f, 1.0f,-1.0f, // Right Side
1.0f,-1.0f,-1.0f, 1.0f, 1.0f, 1.0f, 1.0f,-1.0f, 1.0f, // Right Side
1.0f, 1.0f, 1.0f, 1.0f, 1.0f,-1.0f, -1.0f, 1.0f,-1.0f, // Top Side
1.0f, 1.0f, 1.0f, -1.0f, 1.0f,-1.0f, -1.0f, 1.0f, 1.0f, // Top Side
1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f,-1.0f, 1.0f // Front Side
};
glGenBuffers(1, &vertexbuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);
static const GLfloat normals[] = {
-1.0f, 0.0f, 0.0f, // Left Side
0.0f, 0.0f, -1.0f, // Back Side
0.0f,-1.0f, 0.0f, // Bottom Side
0.0f, 0.0f, -1.0f, // Back Side
-1.0f, 0.0f, 0.0f, // Left Side
0.0f, -1.0f, 0.0f, // Bottom Side
0.0f, 0.0f, 1.0f, // front Side
1.0f, 0.0f, 0.0f, // right Side
1.0f, 0.0f, 0.0f, // right Side
0.0f, 1.0f, 0.0f, // top Side
0.0f, 1.0f, 0.0f, // top Side
0.0f, 0.0f, 1.0f, // front Side
};
glGenBuffers(1, &normalbuffer);
glBindBuffer(GL_ARRAY_BUFFER, normalbuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(normals), normals, GL_STATIC_DRAW);
glEnableVertexAttribArray(0); //
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer(
0, // attribute. No particular reason for 0, but must match the layout in the shader.
3, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);
// 2nd attribute buffer : normals
glBindBuffer(GL_ARRAY_BUFFER, normalbuffer);
glEnableVertexAttribArray(1); //
glVertexAttribPointer(
1, // attribute. No particular reason for 1, but must match the layout in the shader.
3, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);
glBindVertexArray(0);
}
void drawCube()
{
if (!VertexArrayIDSolidCube)
{
createCube();
}
// Draw the triangles !
glBindVertexArray(VertexArrayIDSolidCube);
glDrawArrays(GL_TRIANGLES, 0, 12*3); // 12*3 indices starting at 0 -> 12 triangles
}
The result is, that it somewhat messes up my texture and it still doesnt react to lighting.
Now when i try setting the attribute number of the normals from 1 to 2, my texture disappears, and only some triangles show light and definetly not how its supposed to be.
Im assuming i have to set up some indices?
But i have no clue how that works.
I’d really appreciate any help.
Greetings.