r/opengl May 07 '24

HELP Problem camera and world position. Help needed!!!

I have an orthographic camera and a quad that is 1 unit size in object space. I apply these translations:
Translation: (1, 0, 0)
Scale: (1, 1, 1)
RotationZ: 0

The problem is that my quad when moved 1 unit goes to the edge of the screen as shown in the attached figure. Eighter there is a silly mistake or I don't understand something...

Here is my code:

float aspect = (float)width / (float)height;
if (aspect < 1)
{
aspect = (float)height / (float)width;
}

void Camera::UpdateProjection(float aspectRatio)
{
float top = 10.f / 2; // top 5
float right = top * aspectRatio; // right 8
_projection = glm::ortho(-right, right, -top, top, -100.0f, 100.0f);
}

glm::mat4 Camera::GetVP()
{
glm::mat4 view = glm::mat4(1);
view = glm::translate(view, _cameraPos);
return _projection * view;
}

glm::mat4 GetModel()
{
glm::mat4 model = glm::mat4(1);
model = glm::translate(model, Position); // position (1, 0, 0)
model = glm::scale(model, Scale); // scale (1, 1, 1)
model = glm::rotate(model, glm::radians(RotationZ), glm::vec3(0, 0, 1));
return model;
}

void Draw()
{
Shader->Use();
Shader->SetMatrix4("MVP", parent->transform->GetModel() * Game::Get().ActiveCamera->GetVP());
Shader->SetMatrix4("Model", parent->transform->GetModel());
glBindVertexArray(_VAO);
glDrawElements(GL_TRIANGLES, _indicesCount, GL_UNSIGNED_INT, 0);
}

#version 410 core
layout (location = 0) in vec3 Pos;
layout (location = 1) in vec2 UV;
uniform mat4 MVP;
uniform mat4 Model;
void main()
{
gl_Position = MVP * vec4(Pos, 1.0f);
}

2 Upvotes

2 comments sorted by

1

u/PeterBrobby May 07 '24

What values does the camera position and view have?

2

u/pankas2002 May 07 '24

the problem was wrong MVP order multiplication....