Jump to content
  • Advertisement
Sign in to follow this  
AlanSmithee

OpenGL Problem with perspective in opengl

This topic is 1398 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

Hello.

 

I am experiencing a perspective problem when rendering a model in OpenGL.

I am pretty sure there is some problem with my MVP matrix, but I am unable to deduce what it could be.

 

I am using the identity matrix for my model and the projection matrix and view matrix is defined as below:

glm::mat4 _projectionMatrix = glm::perspective(45.0f, _screenDimensions.x / _screenDimensions.y, 0.1f, 100.0f);
glm::mat4 _viewMatrix = glm::lookAt(glm::vec3(4,0,0), glm::vec3(0,0,0), glm::vec3(0,1,0));

they are multiplied and sent to the vertex shader:

auto mvpMatrix =  _projectionMatrix * _viewMatrix * glm::mat4(1.0f);
glUniformMatrix4fv(mvpUniformHandle, 1, GL_FALSE, glm::value_ptr(mvpMatrix));

and used in the shaders:

// Vertex shader
#version 330
 
layout (location = 0) in vec3 Position;
layout (location = 1) in vec2 TexCoord;
 
uniform mat4 MVP;
 
out vec2 TexCoord0;
 
void main()
{
    gl_Position = MVP * vec4(Position, 1);
    TexCoord0 = TexCoord;
}
 
// Fragment shader

#version 330
 
precision highp float;
 
in vec2 TexCoord0;
 
uniform sampler2D TextureSampler;
 
out vec4 FragColor;
 
void main()
{
    FragColor = texture2D(TextureSampler, TexCoord0.xy);
}

And the result I am getting can be seen in attached file "wrong.png"

 

However, when searching for answers I found a post at SO where someone was trying to do something with a raycastand I noticed that the asker had defined his/her projection matrix with the inverse of the aspect ratio, so I tried doing that:

glm::mat4 _projectionMatrix = glm::perspective(45.0f, -(_screenDimensions.x / _screenDimensions.y), 0.1f, 100.0f);

and the results actually seem to be correct! (see attached file "right.png")

 

I also added files "wrong2.png" and "right2.png" which is the model with some rotation / scaling / translation

 

But all this makes absolutely 0 sense to me, and makes me think that there is something else that is wrong with my code.

Why would passing a negative aspect ratio make it look as it should (if it even is as it should).

 

I am fairly certain there is a  problem in either the MVP matrix or how it is used in the shader.

I have used the model data in a webgl application and it rendered just fine there so the vertecies position and texture coord data is fine.

 

Here is the model loading / passing code for completion sake

// Vertecies, position / texture coord
Vertex vertices[] =
                       {
                           // Front face
                           Vertex(glm::vec3(-1.0, -1.0,  1.0), glm::vec2(0.0, 0.0)),
                           Vertex(glm::vec3( 1.0, -1.0,  1.0), glm::vec2(1.0, 0.0)),
                           Vertex(glm::vec3( 1.0,  1.0,  1.0), glm::vec2(1.0, 1.0)),
                           Vertex(glm::vec3(-1.0,  1.0,  1.0), glm::vec2(0.0, 1.0)),
 
                           // Back face
                           Vertex(glm::vec3(-1.0, -1.0, -1.0), glm::vec2(1.0, 0.0)),
                           Vertex(glm::vec3(-1.0,  1.0, -1.0), glm::vec2(1.0, 1.0)),
                           Vertex(glm::vec3( 1.0,  1.0, -1.0), glm::vec2(0.0, 1.0)),
                           Vertex(glm::vec3( 1.0, -1.0, -1.0), glm::vec2(0.0, 0.0)),
 
                           // Top face
                           Vertex(glm::vec3(-1.0,  1.0, -1.0), glm::vec2(0.0, 1.0)),
                           Vertex(glm::vec3(-1.0,  1.0,  1.0), glm::vec2(0.0, 0.0)),
                           Vertex(glm::vec3( 1.0,  1.0,  1.0), glm::vec2(1.0, 0.0)),
                           Vertex(glm::vec3( 1.0,  1.0, -1.0), glm::vec2(1.0, 1.0)),
 
                           // Bottom face
                           Vertex(glm::vec3(-1.0, -1.0, -1.0), glm::vec2(1.0, 1.0)),
                           Vertex(glm::vec3( 1.0, -1.0, -1.0), glm::vec2(0.0, 1.0)),
                           Vertex(glm::vec3( 1.0, -1.0,  1.0), glm::vec2(0.0, 0.0)),
                           Vertex(glm::vec3(-1.0, -1.0,  1.0), glm::vec2(1.0, 0.0)),
 
                           // Right face
                           Vertex(glm::vec3( 1.0, -1.0, -1.0), glm::vec2(1.0, 0.0)),
                           Vertex(glm::vec3( 1.0,  1.0, -1.0), glm::vec2(1.0, 1.0)),
                           Vertex(glm::vec3( 1.0,  1.0,  1.0), glm::vec2(0.0, 1.0)),
                           Vertex(glm::vec3( 1.0, -1.0,  1.0), glm::vec2(0.0, 0.0)),
 
                           // Left face
                           Vertex(glm::vec3(-1.0, -1.0, -1.0), glm::vec2(0.0, 0.0)),
                           Vertex(glm::vec3(-1.0, -1.0,  1.0), glm::vec2(1.0, 0.0)),
                           Vertex(glm::vec3(-1.0,  1.0,  1.0), glm::vec2(1.0, 1.0)),
                           Vertex(glm::vec3(-1.0,  1.0, -1.0), glm::vec2(0.0, 1.0))
                       };
 
        glGenBuffers(1, &VBO);
        glBindBuffer(GL_ARRAY_BUFFER, VBO);
        glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
 

        unsigned int Indices[] =
                     {
                         0,  1,  2,    0,  2,  3, // Front face
                         4,  5,  6,    4,  6,  7, // Back face
                         8,  9, 10,    8, 10, 11, // Top face
                        12, 13, 14,   12, 14, 15, // Bottom face
                        16, 17, 18,   16, 18, 19, // Right face
                        20, 21, 22,   20, 22, 23  // Left face
                     };
 
        glGenBuffers(1, &IBO);
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
        glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(Indices), Indices, GL_STATIC_DRAW);
 
// Draw function

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
        glUniformMatrix4fv(mvpUniform, 1, GL_FALSE, glm::value_ptr(mvpMatrix));
 
        glEnableVertexAttribArray(0);
        glEnableVertexAttribArray(1);
 
        glBindBuffer(GL_ARRAY_BUFFER, VBO);
 
        glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid*)0);
        glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid*)12);
 

        glActiveTexture(_textureUnit);
        glBindTexture(_textureTarget, _textureId);

 
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
        glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0);
 
        glDisableVertexAttribArray(0);
        glDisableVertexAttribArray(1);


Anyone have any suggestion as to what could be wrong?

 

Thanks in advance for your help!

Share this post


Link to post
Share on other sites
Advertisement

wow..

 

I was passing GL_CW instead of GL_CCW to glFrontFace()... it works fine now!

 

I can also tell that when i thought i was rendering it correctly (using inverse of aspect ration) it was acutally rendering it from behind, which I guess is why glFrontFace(GL_CW) worked.

 

Thanks a lot!

 

(for any future reader having this problem, these are the flags I set to get the model to render properly)

 

glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glFrontFace(GL_CCW);
glCullFace(GL_BACK);

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!