OpenGL 3.2 Core Profile - First Triangle

Started by
2 comments, last by Schnulla 13 years, 9 months ago
Hi,

I can see my first "OpenGL 3.2 core profile triangle"
but somehow it disappears when adding perspective.

Here is my code... not much so I hope someone can help me...
Back-face culling and depth test are disabled.

#version 150 coreuniform mat4 ModelViewProjectionMatrix;in vec3 in_Position;void main(void){  gl_Position= ModelViewProjectionMatrix * vec4(in_Position, 1.0);}


#version 150 coreout vec4 out_Color;void main(void){  out_Color = vec4(1.0, 0.0, 0.0, 1.0);  }


// setup projection matrixProjectionMatrix= 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 matrixModelViewProjectionMatrix= ProjectionMatrix * ModelViewMatrix;glUniformMatrix4fv(glGetUniformLocation(m_handle, "ModelViewProjectionMatrix"), 1, false, glm::value_ptr(ModelViewProjectionMatrix));// setup triangle using VBO and VAOfloat* 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);// renderglDrawArrays(GL_TRIANGLES, 0, 3);// cleanupglBindVertexArray(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 basically I upload an identity (no projection) matrix for the
modelviewprojection matrix and display the triangle at z == -1.0f
instead of -10.0f.

Where is my mistake? I can't get the triangle to work
with perspective. I'm really stuck :(

Help is very appreciated!
Advertisement
I have tried your codes and got expected results in both two cases. So I think there is no mistake. What's your development environment? Mine is openSUSE 11.2 with GTX260+.
- Windows 7 x64
- Radeon HD 5850

And I use Visual Studio 2010 Ultimate to code my stuff in.

edit: sorry forgot to say thanks for your effort! :)

[Edited by - Schnulla on July 20, 2010 9:08:24 AM]
I just found the problem. While converting stuff from OpenGL 1.X to 3.2
I had to comment out some old code in a utility class to avoid that
glGetError throws errors. Unfortunately in this code also some arrays
were allocated. Later there was a write operation on these arrays
(which I forgot to comment out too) that now wrote into undefined
memory locations overwriting my glm matrices. I don't know why
this did not crash my application. I figured this out by putting
the matrix calculations directly in front of the glDrawElements
command. So yes nothing is wrong with the code above and thanks
god I finally found the problem. Also thanks for your effort!


Topic can be marked as [SOLVED]!

This topic is closed to new replies.

Advertisement