PDA

View Full Version : glm::lookat upside down



sixtyonetwo
12-26-2015, 02:12 PM
I'm trying to create my projection and view matrices using glm's lookat and perspective function but can't figure out why it ends up upside down.

Here's the drawing code

void draw(float dt)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

auto size = window->getSize();
glm::mat4 view = glm::lookAt(glm::vec3(0.0, 0.0, 10.0), glm::vec3(0.0, 0.0, 0.0), glm::vec3(0.0, 1.0, 0.0));
glm::mat4 proj = glm::perspective(100.0f, (float)size.x / (float)size.y, 1.0f, 100.0f);


shader->bindShader();
shader->sendUniform4x4("mvp", &(proj * view)[0][0]);
shader->sendUniformVec4("a_Color", &glm::vec4(1,0,0,1)[0]);

glBegin(GL_TRIANGLES);

glVertex3f(-10, -10, 0);
glVertex3f(10, -10, 0);
glVertex3f(0, 10, 0);

glEnd();

window->swapBuffers();
}
and vertex shader

#version 130

uniform mat4 mvp;
in vec3 a_Vertex;
in vec4 a_Color;
out vec4 color;

void main(void)
{
color = a_Color;
gl_Position = mvp * vec4(a_Vertex, 1.0);
}


The output of that code looks like this:

2207

Changing the glm::lookat line to have a negative y axis up vector like:

glm::mat4 view = glm::lookAt(glm::vec3(0.0, 0.0, 10.0), glm::vec3(0.0, 0.0, 0.0), glm::vec3(0.0, -1.0, 0.0));

solves the problem but it doesn't make sense to me why the original code is upside down.

GClements
12-26-2015, 02:36 PM
I'm trying to create my projection and view matrices using glm's lookat and perspective function but can't figure out why it ends up upside down.

Here's the drawing code


glm::mat4 proj = glm::perspective(100.0f, (float)size.x / (float)size.y, 1.0f, 100.0f);


You're requesting a field-of-view of 100 radians, which is around 5730 degrees. Modulo 360, that's 330 degrees, i.e. +/- 165 degrees, which is more than 90 degrees. This results in cotan(fovy/2) being negative, resulting in the X and Y scale factors being negative, which is equivalent to rotation by 180 degrees about the centre of the viewport.

sixtyonetwo
12-26-2015, 02:45 PM
You're requesting a field-of-view of 100 radians, which is around 5730 degrees. Modulo 360, that's 330 degrees, i.e. +/- 165 degrees, which is more than 90 degrees. This results in cotan(fovy/2) being negative, resulting in the X and Y scale factors being negative, which is equivalent to rotation by 180 degrees about the centre of the viewport.

Aha! Thanks a bunch, always forget things being in radians.