1
\$\begingroup\$

I'm trying to convert my game's camera system to use world space coordinates rather than OpenGL's default normalized device coordinates, however in doing so my sprites are being rendered improperly as you will see in the image below. Using GLM as my math library I have created a 4x4 orthographic matrix with the following code:

m_orthoMatrix = glm::ortho(0.0f, (float) m_screenWidth, (float) m_screenHeight, 0.0f);

I am then performing some translations (and scaling if by chance that is messing with anything) with the following code (m_cameraMatrix is a 4x4 matrix)

vec3 translation(-m_position.x, -m_position.y, 0.0f);
    m_cameraMatrix = glm::translate(m_orthoMatrix, translation);
vec3 scale(m_scale, m_scale, 0.0f);
    m_cameraMatrix = glm::scale(mat4(1.0f), scale) * m_cameraMatrix;

m_scale is a float currently with my testing at value 1.0f. m_position is a vec2 where in the image, the sprite on the right has values (0, 0) and the sprite on the left as values (screen width / 2, 20).

The issue, as you will notice, is that the sprites are rendered upside down, and I can't seem to fix it without getting rid of my translation math and going back to normalized coordinates.

screenshot showing the flipped sprites

\$\endgroup\$
0
\$\begingroup\$

Try this.

m_orthoMatrix = glm::ortho(0.0f, (float) m_screenWidth, 0.0f, (float) m_screenHeight);

Looking at the documentation it says glm::ortho takes left, right, bottom, top, in that order as it's parameters. You are saying that y-value m_screenWidth should be at the bottom of the screen while 0.0f should be at the top. This has the effect of flipping the y-axis if you expect the coordinates to have their orgin (point 0,0) in the bottom left. The bottom left now have the coordinates (0, m_screenWidth) instead.

If you want the origin to be top left however, then you are already doing it correctly. So then what you are probably doing wrong is either drawing the quad upside-down, have upside-down texture coordinates, or loading the image into a texture upside-down.

\$\endgroup\$
  • \$\begingroup\$ that fixes the flippage, but the coordinates still arent right.. or perhaps im confused by "world space coordinates" I want 0, 0 to be the top left and width, height to be the bottom right (this is simply what im used to with other languages)... with my google research that is what world space coordinates were \$\endgroup\$ – BossLetsPlays Dec 3 '15 at 16:59
  • \$\begingroup\$ @BossLetsPlays updated my answer. \$\endgroup\$ – Christer Dec 3 '15 at 17:50
  • \$\begingroup\$ ah ok thanks, that led me to take another look at my texture coords, with my old camera system I had been (1 - uvcoord.y) to render the sprite properly, all I had to do was remove that inversion \$\endgroup\$ – BossLetsPlays Dec 3 '15 at 18:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.