9
votes

I'm attempting to set up an orthographic projection in OpenGL, but can't seem to find why this triangle is not rendering correctly (it isn't visible). I have used perspective projection with the same code (apart from my vertex coordinates and projection matrix, of course) and it works fine. I construct the triangle vertices as:

Vertex vertices[] = { Vertex(glm::vec3(0, 600, 0.0),        glm::vec2(0.0, 0.0)),
                      Vertex(glm::vec3(300, 0, 0.0),        glm::vec2(0.5, 1.0)),
                      Vertex(glm::vec3(800 , 600, 0.0),     glm::vec2(1.0, 0.0)) };

My camera constructor is:

Camera::Camera(const glm::vec3& pos, int width, int height) {

    ortho = glm::ortho(0, width, height, 0, 0, 1000);
    this->position = pos;
    this->up = glm::vec3(0.0f, 1.0f, 0.0f);
    this->forward = glm::vec3(0.0f, 0.0f, 1.0f);

}

I call this as:

camera = Camera(glm::vec3(0, 0, 2), window->getSize().x, window->getSize().y);

Where the window is 800 by 600 pixels. I am uploading a transform to the shader via the function:

 void Shader::update(const Transform& transform, const Camera& camera) {

     glm::mat4 model = camera.getProjection() * transform.getModel();

     glUniformMatrix4fv(uniforms[TRANSFORM_U], 1, GL_FALSE, &model[0][0]);

 }

In which camera.getProjection() is:

glm::mat4 Camera::getProjection() const {

    return ortho * glm::lookAt(position, glm::vec3(0, 0, 0), up);

}

And transform.getModel() is:

glm::mat4 Transform::getModel() const {

    glm::mat4 posMat = glm::translate(pos);

    glm::quat rotQuat = glm::quat(glm::radians(rot));
    glm::mat4 rotMat = glm::toMat4(rotQuat);

    glm::mat4 scaleMat = glm::scale(scl);

    return posMat * rotMat * scaleMat;

}

Though I suspect the problem lies in my set up of orthographic projection rather than my transforms, as this worked fine for perspective projection. Can anyone see why the triangle rendered with these coordinates is not visible? I am binding my shader and uploading the projection matrix to it before rendering the mesh. If it helps, my vertex shader is:

#version 120

attribute vec3 position;
attribute vec2 texCoord;

varying vec2 texCoord0;

uniform mat4 transform;

void main()
{
    gl_Position = transform * vec4(position, 1.0);
    texCoord0 = texCoord;
}
1
Could you check your culling settings? Maybe the triangle is just culled because you look at its backface.Nico Schertler
Honestly I would be unsure how to do so (glDisable(BACK_FACE_CULLING) or similar?) however, if I change the position of glm::lookAt from glm::vec3(0, 0, 2) to glm::vec3(0, 0, -2), I believe this would solve the issue if the face were being culled, but it does not. Edit: Changing the vertex order does not work either.FinalFanatic

1 Answers

10
votes

For anyone interested in the issue, it was with:

ortho = glm::ortho(0, width, height, 0, 0, 1000);

Where the arguments are supplied as integers, not floats. Therefore the integer division applied within glm::ortho was creating an incorrect orthographic projection matrix.