2
\$\begingroup\$

I use this code in my GLSL vertex shader, where v_position is the vertex position:

gl_Position = v_position * u_camera;

and set u_camera as follows:

meshShader.setUniformMatrix("u_camera", 
camera.view.mul(camera.projection).inv());

With the camera being an PerspectiveCamera of fov 45, width = aspect ratio and height = 1

This code is working when translating the camera over the z axis:

if (Gdx.input.isKeyPressed(Keys.W)) {
        camera.translate(0, 0, CAMERA_SPEED * Gdx.graphics.getDeltaTime());
} else if (Gdx.input.isKeyPressed(Keys.S)) {
        camera.translate(0, 0, -CAMERA_SPEED * Gdx.graphics.getDeltaTime());
}

But not when doing the same thing as above on the x axis, with the A or D keys.

The mesh (a triangle) looks like this before i press A or D (Translating with W and S is fine)

Normal

But when translating over the X axis it it looks like this:

Wrong

I don't know anything of matrix math, and I made this code experimenting with the various matrices of the Camera class

EDIT : This code camera.projection.mul(camera.view.inv()) does a similiar thing

\$\endgroup\$
1
\$\begingroup\$
meshShader.setUniformMatrix("u_camera", 
camera.view.mul(camera.projection).inv());

Well first of all, you're (probably, judging by the notation) inverting the view AND projection matrices. This isn't correct. The inversion is only a part of the creation of a view matrix.

Also, an orthographic camera doesn't have a FOV, so i'm not sure if you haven't gotten some things mixed up because of that as well.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ So sorry it's a PerspectiveCamera \$\endgroup\$ – UberLambda Mar 30 '13 at 14:32
  • \$\begingroup\$ And, yes, I'm inverting the product of the two matrices. \$\endgroup\$ – UberLambda Mar 30 '13 at 14:38
  • 2
    \$\begingroup\$ Aside from that, gl_Position = v_position * u_camera; should be gl_Position = u_camera * v_position; since OpenGL uses column matrices. I'd recommend you to read the Matrices tutorial on opengl-tutorial.org/beginners-tutorials/tutorial-3-matrices \$\endgroup\$ – Exilyth Mar 30 '13 at 14:41
  • \$\begingroup\$ @sarahm No it doesn't, GLSL will use what ever multiplication is appropriate for the notation. If you'll write vector * matrix, then the vector will be treated as a row vector, if you'll write matrix * vector then the vector will be treated as a column vector. I do all my 3D calculations using row vectors, because it's more natural to write it like that for me. It's up to you to set up your matrices for row or column multiplication, OpenGL doesn't care. \$\endgroup\$ – dreta Mar 30 '13 at 21:12
  • 1
    \$\begingroup\$ @TM3P I was talking to sarahm, his comment is incorrect, i have no idea why it has 2 upvotes. It applies to your situation because that's how your matrices are constructed, but it's not a rule. It's alright if you don't understand this at all, but he should if he's commenting on it. BTW, if you'd like to learn linear algebra in a very accessible manner, try Game Engine Architecture by Jason Gregory, the information is presented in a VERY accessible manner and it's by a game developer for game developers. \$\endgroup\$ – dreta Mar 31 '13 at 12:24

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.