Public Group

# OpenGL Camera Up Problem.

This topic is 1919 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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));


#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 on other sites

How are you constructing your View Matrix?

##### Share on other sites

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 on other sites

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 on other sites

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.

1. 1
2. 2
Rutin
24
3. 3
4. 4
JoeJ
16
5. 5

• 14
• 29
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631773
• Total Posts
3002265
×