Sign in to follow this  
Slushy

OpenGL Camera Up Problem.

Recommended Posts

Slushy    168

Hello! So I recently went from OpenGL 2.X (using now deprecated functions like, glTranslate etc.) to using OpenGL 3.2. This also meant implementing matrices manually for all of my transformation. While this is extremely fun and very enjoyable, it's hard.

 

My problem is that when I move my Camera (Viewmatrix) in the +Y direction, my OBJ that I loaded into my game is moving upwards (hence im moving the camera "downwards" relative to the object). This also affects moving in +/- X. + Goes left, - Goes right.

 

Why is it that I have this problem? I've checked my Projectionmatrix and it seems to be correct, according to http://ogldev.atspace.co.uk/www/tutorial12/12_11.png

 

Projectionmatrix

perspectiveMatrix.setIdentity();

float aspectRatio = WIDTH / HEIGHT;
float zRange = zNear - zFar;

perspectiveMatrix.m00 = (float) (1.0f / (aspectRatio * Math.tan(degreesToRadians(fieldOfView / 2.0f))));
perspectiveMatrix.m11 =  (float) (1.0f / (Math.tan(degreesToRadians(fieldOfView / 2.0f))));
perspectiveMatrix.m22 = (-zNear - zFar) / zRange;
perspectiveMatrix.m23 = 1;
perspectiveMatrix.m32 = (2 * zFar * zNear) / zRange;

 

 

Modelmatrix is Identity.

 

Viewmatrix is changed when I move:

if(moveUp)
    Matrices.viewMatrix.translate(new Vector3f(0, 1.0f * moveSpeed, 0));

if(moveLeft)
    Matrices.viewMatrix.translate(new Vector3f(-1.0f * moveSpeed, 0, 0));

if(moveDown)
    Matrices.viewMatrix.translate(new Vector3f(0, -1.0f * moveSpeed, 0));

if(moveRight)
    Matrices.viewMatrix.translate(new Vector3f(1.0f * moveSpeed, 0, 0));

 

 

Vertex Shader:

#version 330

uniform mat4 perspectiveMatrix;
uniform mat4 viewMatrix;
uniform mat4 modelMatrix;

layout(location = 0) in vec3 position;

out vec4 color;

void main(){
    gl_Position = perspectiveMatrix * viewMatrix * modelMatrix * vec4(position, 1.0);
    color = vec4(position, 1.0);
}

 

 

All sources:
http://pastebin.com/yZExLWpp - Window.java

http://pastebin.com/yBRcdnRK - Matrices.java

http://pastebin.com/mJa1j5ri - Camera.java

http://pastebin.com/UM7BQdrR - Cube.java

 

Tell me if you need more. I don't believe you should though.

 

Thanks!, Slushy.

 

Share this post


Link to post
Share on other sites
Slushy    168

Well, stupid me. I had just set the View Matrix to Identity and thought that'd take care of it. Obviously that was not correct, I used this to setup a View Matrix and now it works like a charm!

 

Thanks!

Share this post


Link to post
Share on other sites
Slushy    168

I'll use  this as a bump post and hope that somebody answers me again. My problem didn't get solved.. I posted my earlier post to fast. My problem still exists unfortunately..

 

Here is my Camera class.

http://pastebin.com/UmRvTrQN (NOTE: This is modified to be moving "correct", pressing D makes me go right and pressing A goes left etc. Though it shouldn't.. really..?)

 

Hope somebody can solve this for me.. Feels like I've tried everything. Ofcourse I could just move the camera in the opposite direction that it's supposed to be moved. But I want it to be correct.

Edited by Slushy

Share this post


Link to post
Share on other sites
VladR    722

When you are troubleshooting camera, matrices  -view/world, you need to do it gradually, step by step.

 

1. No Rotations

2. No Translations

3. Render a cube that has , pretransformed, world coordinates so that its center is 0,0,0 and its size is, say, 10 units

4. Set View matrix so that the look,up,right vector are all identical to its respective z,y,x axis - e..g (0,0,1), (0,1,0), (1,0,0)

5. Make sure you handle left-handed vs right-handed coord.system correctly

 

 

Only when this works, start adding rotations, world transformation and inspect which piece of code messes your view/world matrix.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this