Jump to content
  • Advertisement
Sign in to follow this  

OpenGL OpenGL 3.2 Core Profile - First Triangle

This topic is 3016 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts


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 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);

glGenBuffers(1, &m_vboID);
glBindBuffer(GL_ARRAY_BUFFER, m_vboID);
glBufferData(GL_ARRAY_BUFFER, 9 * sizeof(GLfloat), vert, GL_STATIC_DRAW);

// render

glDrawArrays(GL_TRIANGLES, 0, 3);

// cleanup


delete[] vert;

glBindBuffer(GL_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!

Share this post

Link to post
Share on other sites
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+.

Share this post

Link to post
Share on other sites
- 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]

Share this post

Link to post
Share on other sites
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]!

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!