Hi,
I can see my first “OpenGL 3.2 core profile”-triangle
Now I tried to add perspective using the nice glm library.
But somehow the triangle disappears when using the
perspective stuff. Here is my code:
#version 150 core
uniform mat4 ModelViewProjectionMatrix;
in vec3 in_Position;
void main(void)
{
gl_Position= ModelViewProjectionMatrix * vec4(in_Position, 1.0);
}
#version 150 core
out vec4 out_Color;
void main(void)
{
out_Color = vec4(1.0, 0.0, 0.0, 1.0);
}
// setup projection matrix
ProjectionMatrix= glm::mat4(1.0f);
ProjectionMatrix*= glm::perspective(45.0f, 1024.0f / 768.0f, 0.1f, 200.0f);
// setup modelview matrix (look down the negative z-axis)
ModelViewMatrix= glm::mat4(1.0f);
ModelViewMatrix*= glm::lookAt(glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, -1.0f), glm::vec3(0.0f, 1.0f, 0.0f));
// create and upload modelviewprojection matrix
ModelViewProjectionMatrix= ProjectionMatrix * ModelViewMatrix;
glUniformMatrix4fv(glGetUniformLocation(m_handle, "ModelViewProjectionMatrix"), 1, false, glm::value_ptr(ModelViewProjectionMatrix));
// setup triangle using VBO and VAO
float* vert = new float[9];
vert[0] = 0.0; vert[1] = 1.0; vert[2]= -10.0;
vert[3] =-1.0; vert[4] =-1.0; vert[5]= -10.0;
vert[6] = 1.0; vert[7] =-1.0; vert[8]= -10.0;
unsigned int m_vaoID;
glGenVertexArrays(1, &m_vaoID);
glBindVertexArray(m_vaoID);
glGenBuffers(1, &m_vboID);
glBindBuffer(GL_ARRAY_BUFFER, m_vboID);
glBufferData(GL_ARRAY_BUFFER, 9 * sizeof(GLfloat), vert, GL_STATIC_DRAW); glVertexAttribPointer((GLuint)ATTRIBUTE_LOCATION_WORLDVERTEX, 3, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(ATTRIBUTE_LOCATION_WORLDVERTEX);
// render
glDrawArrays(GL_TRIANGLES, 0, 3);
// cleanup
glBindVertexArray(0);
delete[] vert;
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glDeleteBuffers(1, &m_vboID);
glDeleteVertexArrays(1, &m_vaoID);
I can see this triangle…
…when I apply the following changes to the code above:
glUniformMatrix4fv(glGetUniformLocation(m_handle, "ModelViewProjectionMatrix"), 1, false, glm::value_ptr(glm::mat4(1.0f)/*ModelViewProjectionMatrix*/));
vert[0] = 0.0; vert[1] = 1.0; vert[2]= -1.0;
vert[3] =-1.0; vert[4] =-1.0; vert[5]= -1.0;
vert[6] = 1.0; vert[7] =-1.0; vert[8]= -1.0;
So the change is that I upload an identity (no projection)
matrix for the modelviewprojection matrix and display the
triangle at z == -1.0f instead of -10.0f.
I have disabled back-face culling and depth test.
Where is my mistake? I can’t get the triangle to work
with perspective. I’m really stuck
Help is really appreciated!