0
\$\begingroup\$

I'm trying to set up perspective projection in OpenGL but it doesn't work

void Camera::setViewportSize(float left, float right, float bottom, float top)
{
    constexpr float fov = glm::radians(45.0f);
    m_projectionMatrix = glm::perspective(fov, (right - left) / (top - bottom), 0.1f, 10.0f);
    recalculateViewMatrix();
}

void Camera::recalculateViewMatrix() 
{
    glm::mat4 translate = glm::translate(glm::mat4(1.0f), glm::vec3(m_position, 0.0f));
    glm::mat4 rotate = glm::rotate(glm::mat4(1.0f), m_rotation, glm::vec3(0.0f, 0.0f, 1.0f));
    glm::mat4 scale = glm::scale(glm::mat4(1.0f), glm::vec3(m_scale, m_scale, 1.0f));

    m_viewMatrix = translate * rotate * scale;
    m_viewMatrix = glm::inverse(m_viewMatrix);

    m_viewProjectionMatrix = m_projectionMatrix * m_viewMatrix;
}

I upload viewProjectionMatrix to shader and use it like this:

gl_Position = u_viewProjectionMatrix * vec4(a_position, 1.0);

It just doesn't show anything. I double checked if I pass good left, right, bottom, top values and it should be all good. Playing with zNear and zFar didn't change anything. Note - When I use ortho, it works like a charm:

    m_projectionMatrix = glm::ortho(left, right, bottom, top, 1.0f, -1.0f);

enter image description here

Update I did add model matrix and I translated it -2 units on z axis. I can see the lines: enter image description here But they are disorted.

They should be at positions:

line1: (0.0f, 0.0f), (100.0, 100.0)
line2: (0.0f, 0.0f), (600.0f, 800.0f)

It seem like the centre of screen is at (0, 0)???

\$\endgroup\$
2
  • \$\begingroup\$ Keep in mind that you can use the Edit link at the bottom to modify an existing question instead of deleting and posting a new one. Then you can flag any comments that are no longer needed for us moderators to delete. \$\endgroup\$
    – DMGregory
    Commented Jan 21 at 14:45
  • \$\begingroup\$ @DMGregory OK, no problem \$\endgroup\$
    – DDD
    Commented Jan 21 at 15:11

1 Answer 1

0
\$\begingroup\$

Check camera settings, viewport dimensions, OpenGL errors, object positions, and shader code for perspective projection in OpenGL.

\$\endgroup\$
1
  • \$\begingroup\$ If it works for otho, then why it doesn't work for perspective? I mean should I change shader code or my object positions? \$\endgroup\$
    – DDD
    Commented Jan 21 at 19:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .