0
votes

I got an orthographic camera working however I wanted to try and implement a perspective camera so I can do some parallax effects later down the line. I am having some issues when trying to implement it. It seems like the depth is not working correctly. I am rotating a 2d image along the x-axis to simulate it laying somewhat down so I get see the projection matrix working. It is still showing as an orthographic perspective though.

Here is some of my code:

CameraPersp::CameraPersp() :
    _camPos(0.0f,0.0f,0.0f), _modelMatrix(1.0f), _viewMatrix(1.0f), _projectionMatrix(1.0f)

Function called init to setup the matrix variables:

void CameraPersp::init(int screenWidth, int screenHeight)
{
    _screenHeight = screenHeight;
    _screenWidth = screenWidth;

    _modelMatrix = glm::translate(_modelMatrix, glm::vec3(0.0f, 0.0f, 0.0f));
    _modelMatrix = glm::rotate(_modelMatrix, glm::radians(-55.0f), glm::vec3(1.0f, 0.0f, 0.0f));
    _viewMatrix = glm::translate(_viewMatrix, glm::vec3(0.0f, 0.0f, -3.0f));
    _projectionMatrix = glm::perspective(glm::radians(45.0f), static_cast<float>(_screenWidth) / _screenHeight, 0.1f, 100.0f);
}

Initializing a texture to be loaded in with x,y,z,width,height,src

_sprites.back()->init(-0.5f, -0.5f, 0.0f, 1.0f, 1.0f, "src/content/sprites/DungeonCrawlStoneSoupFull/monster/deep_elf_death_mage.png");

Sending in the matrices to the vertexShader:

    GLint mLocation = _colorProgram.getUniformLocation("M");
    glm::mat4 mMatrix = _camera.getMMatrix();
    //glUniformMatrix4fv(mLocation, 1, GL_FALSE, &(mMatrix[0][0]));
    glUniformMatrix4fv(mLocation, 1, GL_FALSE, glm::value_ptr(mMatrix));

    GLint vLocation = _colorProgram.getUniformLocation("V");
    glm::mat4 vMatrix = _camera.getVMatrix();
    //glUniformMatrix4fv(vLocation, 1, GL_FALSE, &(vMatrix[0][0]));
    glUniformMatrix4fv(vLocation, 1, GL_FALSE, glm::value_ptr(vMatrix));

    GLint pLocation = _colorProgram.getUniformLocation("P");
    glm::mat4 pMatrix = _camera.getPMatrix();
    //glUniformMatrix4fv(pLocation, 1, GL_FALSE, &(pMatrix[0][0]));
    glUniformMatrix4fv(pLocation, 1, GL_FALSE, glm::value_ptr(pMatrix));

Here is my vertex shader:

#version 460
//The vertex shader operates on each vertex

//input data from VBO. Each vertex is 2 floats
in vec3 vertexPosition;
in vec4 vertexColor;
in vec2 vertexUV;

out vec3 fragPosition;
out vec4 fragColor;
out vec2 fragUV;

//uniform mat4 MVP;
uniform mat4 M;
uniform mat4 V;
uniform mat4 P;

void main() {
    //Set the x,y position on the screen
    //gl_Position.xy = vertexPosition;
    gl_Position = M * V * P * vec4(vertexPosition, 1.0);

    //the z position is zero since we are 2d
    //gl_Position.z = 0.0;

    //indicate that the coordinates are nomalized
    gl_Position.w = 1.0;

   fragPosition = vertexPosition;
   fragColor = vertexColor;
   // opengl needs to flip the coordinates
   fragUV = vec2(vertexUV.x, 1.0 - vertexUV.y);
}

I can see the image "squish" a little because it is still rendering the perspective as orthographic. If I remove the rotation on the x-axis, it is not longer squished because it isn't laying down at all. Any thoughts on what I am doing wrong? I can supply more info upon request but I think I put in most of the meat of things.

Picture:

Projection Matrix showing as Orthographic

1
Comments are not for extended discussion; this conversation has been moved to chat.Jean-François Fabre♦

1 Answers

1
votes

You shouldn't modify gl_Position.w

    gl_Position = M * V * P * vec4(vertexPosition, 1.0); // gl_Position is good

    //indicate that the coordinates are nomalized < not true
    gl_Position.w = 1.0; // Now perspective divisor is lost, projection isn't correct