sixtyonetwo

12-26-2015, 03: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.

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.