Camera view matrix inverted?

I recently switched from a computer running an Intel HD3000 to one with an nVidia 970m and Intel HD 5600 integrated. In porting my old code from OpenGL 3.1 to 3.3 and up, I’ve run into a strange issue. My view matrix seems to be inverting things on the y and x axes; I can’t for the life of my figure out why, since that part of the code isn’t OpenGL version specific, it’s just math. I set up a test scenario with a single triangle positioned at 1 on the z axis, pointing up. What I see is a triangle at z=1 pointing down, with the vertices along the x axis reversed. The relevant code is below:

glm::mat4 testprojMat = glm::perspective(60.0f, 16.0f/9.0f, 0.001f, 1000.0f);
glm::mat4 testviewMat = 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));
testShader.updateUniform("modelMat", glm::mat4(1.0f));
testShader.updateUniform("viewMat", testviewMat);
testShader.updateUniform("testprojMat", testprojMat);

// Triangle:
std::vector<glm::vec3> testVerts;
    std::vector<unsigned short> testInds;
    testVerts.push_back(glm::vec3(-1.0f, 0.0f, 1.0f));
    testVerts.push_back(glm::vec3(1.0f, 0.0f, 1.0f));
    testVerts.push_back(glm::vec3(0.0f, 1.0f, 1.0f));
    testInds.push_back(0);    testInds.push_back(1);    testInds.push_back(2);
    glGenVertexArrays(1, &testVao);    glBindVertexArray(testVao);
    glGenBuffers(1, &testVbo);    glGenBuffers(1, &testIbo);
    glBindBuffer(GL_ARRAY_BUFFER, testVbo);    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, testIbo);

    glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec3)*testVerts.size(), testVerts.data(), GL_STATIC_DRAW);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned short)*testInds.size(), testInds.data(), GL_STATIC_DRAW);

    glEnableVertexAttribArray(0);// positions
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(glm::vec3), (GLvoid*)0);

My vertex and fragment shaders:

//vertex shader
#version 330

//shaderName testShader

layout (location = 0) in vec3 in_Position;

uniform mat4 projMat;
uniform mat4 viewMat;
uniform mat4 modelMat;

out vec3 ex_Pos;

void main(void)
{
        ex_Pos = in_Position;
	gl_Position = (projMat * viewMat * modelMat) * vec4(in_Position, 1.0);
}

//fragment shader
#version 330

in vec3 ex_Pos;

out vec4 out_Color;

void main(void)
{
	out_Color = vec4(1.0, 0.0, 0.0, 1.0);
        // Differentiate edge vertices of triangle, to tell which way it's oriented:
	if (ex_Pos.x < 0.0){
		out_Color = vec4(0.0, 1.0, 0.0, 1.0);
	}
}

Switching the up vector in glm::lookat to (0,-1,0) gives the triangle the right way around, but that’s contrary to the results the code gave me on my old machine. Was there some shift between OGL 3.1 and 3.3 I’m overlooking?

You’re creating a perspective projection with a field-of-view angle of 60 radians, which is equivalent to -162 degrees (note: negative). This results in the x and y scale factors being negative (i.e. the result is as if you had used +162 degrees then concatenated a 180-degree rotation about the Z axis).

Older versions of GLM used degrees for functions which mimicked OpenGL or GLU functions which used degrees (e.g. gluPerspective). Later versions allowed this to be changed to radians by setting GLM_FORCE_RADIANS. Current versions always use radians; angles in degrees must be explicitly converted to radians with e.g. glm::radians()).

1 Like

This is super old, but thank you so much I had my camera wrong for months