Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted
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.

share|improve this answer
    
So sorry it's a PerspectiveCamera –  TM3P Mar 30 '13 at 14:32
    
And, yes, I'm inverting the product of the two matrices. –  TM3P Mar 30 '13 at 14:38
2  
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 –  sarahm Mar 30 '13 at 14:41
    
Awesome, it solved my problem. Thanks. –  TM3P Mar 30 '13 at 19:09
1  
@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. –  dreta Mar 31 '13 at 12:24
show 3 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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