The aim of my program is to render a simple colored triangle in rotation. The rotations are correct if all Z values of my vertices are equals to 0.0f. If one of these values are different of 0.0f the transformations are not correct (if I retreat the camera, the triangle seems to be in the same place).
The following declaration works correctly :
static GLfloat vertices[9] =
{
-1.000000f, 0.000000f, 0.000000f,
0.000000f, 1.000000f, 0.000000f,
1.000000f, 0.000000f, 0.000000f
};
But the following one not works correctly :
static GLfloat vertices[9] =
{
-1.000000f, 1.000000f, 1.000000f,
-1.000000f, 1.000000f, -1.000000f,
-1.000000f, -1.000000f, -1.000000f
};
So if the polygon is coplanar with the plane (x0y) it's ok, but in the other case it's not correct. I searched to find the problem in my matrix usage in vain. Here's a part of my code :
//Main loop
while (continuer)
{
eventListener(&event, &continuer);
glClearDepth(1.0f);
glClearColor(0.13f, 0.12f, 0.13f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(programID);
//Projection matrix
glm::mat4 projection = glm::perspective(70.0f, ((float)WIDTH)/(float)HEIGHT, 1.0f, 1000.0f);
//View matrix
glm::mat4 view = glm::lookAt(glm::vec3(0.0f, 0.0f, trans), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f));
//Model matrix
glm::mat4 model = glm::mat4(1.0f);
model *= glm::translate(model, glm::vec3(0.0f, 0.0f, 0.0f));
model *= glm::rotate(model, angle, glm::vec3(1.0f, 1.0f, 1.0f));
glm::mat4 ModelViewMatrix = view * model;
glm::mat4 ModelViewProjectionMatrix = projection * ModelViewMatrix;
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, vertices);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, colors);
glUniformMatrix4fv(glGetUniformLocation(programID, "MV"), 1, GL_TRUE, glm::value_ptr(ModelViewMatrix));
glUniformMatrix4fv(glGetUniformLocation(programID, "MVP"), 1, GL_TRUE, glm::value_ptr(ModelViewProjectionMatrix));
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableVertexAttribArray(1);
glDisableVertexAttribArray(0);
glUseProgram(0);
angle += 0.050000f;
trans += 0.00010f;
glFlush();
SDL_GL_SwapBuffers();
}
and my vertex shader code :
#version 330
in vec3 VertexPosition;
in vec3 VertexColor;
uniform mat4 MV;
uniform mat4 MVP;
out vec3 Color;
void main()
{
Color = VertexColor;
gl_Position = MVP * vec4(VertexPosition, 1.0f);
}
I tried several combinations of code without any success. I'm lost. Does anyone can help me, please ?
Thanks very much in advance for your help.