Sign in to follow this  
synthetix

OpenGL Problem with matrix math for camera

Recommended Posts

synthetix    192
I've been struggling with getting the matrix math down for translating and rotating a camera through 3D space. I've got an OpenGL program that places the camera in the middle of a box at 0,0,0. I want to be able to both translate and rotate (first person shooter-style) through the box using the keyboard. I've got it working except I can't translate/rotate and keep the coordinates the same for both (only one or the other). For example, I can move through the box just fine (using the WSAD keys), but if I rotate my view to the left by 90 degrees, "forward" now goes to the right.

The problem is, I multiply the modelview matrix by the translation matrix first, and then by the rotation matrix. This works except that by rotating the scene 90 degrees, it throws the translation coordinates off by 90 degrees! In other words, translation occurs under the assumption that the scene has [i]not been rotated.[/i]

I only need to rotate the scene on the Y axis, Wolfenstein/Doom style. Here is my code so far:

[CODE]
mat4x4 mat_model,mat_tran,mat_rot,mat_temp;

mat4x4_identity(mat_model);
mat4x4_identity(mat_tran);
mat4x4_identity(mat_rot);
mat4x4_identity(mat_temp);

//create translation matrix
mat4x4_translate(mat_tran, strafe, 0.0, dolly);

//create rotation matrix
mat4x4_rotate_Y(mat_rot,mat_temp,-rot_y);

//apply the matrices to the modelview matrix
mat4x4_mul(mat_temp,mat_tran,mat_rot);
mat4x4_dup(mat_model,mat_temp);


[/CODE]

What am I missing?

Share this post


Link to post
Share on other sites
[quote name='Synthetix' timestamp='1341224731' post='4954855']
I can move through the box just fine (using the WSAD keys), but if I rotate my view to the left by 90 degrees, "forward" now goes to the right.
[/quote]

"Forward" should modify a direction vector in local coordinates, which can then be transformed by the rotation matrix to generate the approprite translation vector.

Share this post


Link to post
Share on other sites
synthetix    192
[quote name='Goran Milovanovic' timestamp='1341268329' post='4955084']
"Forward" should modify a direction vector in local coordinates, which can then be transformed by the rotation matrix to generate the approprite translation vector.
[/quote]

Thanks. I tried that, but now the scene only rotates around the origin (0,0,0). Actually, this gives the same result as pre-multiplying the rotation/translation matrices.

Let me just confirm: the "eye point" is the X,Y (assuming Z-up) coordinates of the camera, looking down, correct? And the "look at" point, or the camera's orientation, is the eye point vector multiplied by the rotation matrix, yes?

I'm sorry I can't be more helpful. I am really having a hard time grasping this. Edited by Synthetix

Share this post


Link to post
Share on other sites
[quote name='Synthetix' timestamp='1341285643' post='4955150']

Thanks. I tried that, but now the scene only rotates around the origin (0,0,0). Actually, this gives the same result as pre-multiplying the rotation/translation matrices.
[/quote]

It sounds like you're not translating other objects in the scene. You have to transform everything in your scene to create the illusion of a "camera" moving through space. Typically, you would have a 4x4 transform matrix that describes orientation/translation of the camera. You would also have a 4x4 matrix for every object in the scene. Then, to create the illusion of a camera moving through space, you have to transform the matrix of each and every object, by the inverse matrix of the camera.

Why inverse? -> In the real world, you would have an actual camera that you would rotate left (if you wanted to see the portion of the scene on the left). However, in the OpenGL world, in order to look left, you have to "rotate the world" to the right, creating the illusion of a camera that is rotating to the left.

I don't know if you had the chance to research this in more depth, but I think this page should still be helpful: http://www.opengl.org/wiki/Viewing_and_Transformations

[quote name='Synthetix' timestamp='1341285643' post='4955150']
Let me just confirm: the "eye point" is the X,Y (assuming Z-up) coordinates of the camera, looking down, correct? And the "look at" point, or the camera's orientation, is the eye point vector multiplied by the rotation matrix, yes
[/quote]

In the context of gluLookAt? Both the "eye point" and the "look at point" should be vectors in object/world space coordinates.

Share this post


Link to post
Share on other sites
synthetix    192
Okay, I can grab both the camera's current position vector and direction (the direction it's pointing) vector from the modelview matrix. Assuming I have this data on each loop iteration, how do I get the camera to rotate around its current position as opposed to 0,0,0? I have been reading a lot of tutorials that say you have to rotate the direction vector by the rotation matrix used to rotate the scene so when you apply the translation, it goes in the correct direction. I think I understand that part perfectly well, as I'm able to derive the forward/back direction vector from the modelview matrix, normalize it, and add it to the translation matrix along with the speed value. I do the same for the strafe vector, which is the cross product of the fwd/back vector and the Y direction (currently -1.0).

Example:

[CODE]
//calculate strafe vector using cross product of Z and Y
vec4 direction_strafe;
vec4_cross(direction_strafe,direction_move,(vec4){0.0,-1.0,0.0,0.0});

camera_position[z] += (direction_move[z] * speed); //WS keys (fwd/back)
camera_position[x] += (direction_strafe[x] * speed); //AD keys (strafe)

//construct translation matrix
mat4x4 translate;
mat4x4_translate(translate, camera_position[x], 0.0, camera_position[z]); //Y is 0.0 since we never go up/down
[/CODE]

I then multiply the translation matrix by the rotation matrix and drop the result into the modelview matrix. Problem is, when I rotate the scene, it is always rotating around 0,0,0 so when I move around, the camera always rotates around the world's origin and not its own. Edited by Synthetix

Share this post


Link to post
Share on other sites
synthetix    192
[quote name='dpadam450' timestamp='1341507089' post='4956025']
direction_move should be calculated by using the sin/cos of the camera angle. Looks like you have a cross product calculating it?
[/quote]

I'm calculating the strafe vector using the cross product of the foward (Z) direction and the up (Y) direction. So, the strafe vector is basically 90 degrees from the forward vector. I then add this to the translation matrix when the user presses one of the WSAD move keys, and then multiply the modelview matrix with it before rendering the scene.

I'm using a rotation matrix which contains the rotation angle when the user rotates the camera view left/right on the Y/up axis.

All of this works perfectly for either movement or rotation only, but I can't get the two working together! [img]http://public.gamedev.net//public/style_emoticons/default/sad.png[/img]

Share this post


Link to post
Share on other sites
scniton    252
Did you look at Goran Milovanovic's second post about using the inverse?

You've defined your camera transformation as:
Translate * Rotation
Which means your view matrix should be:
Rotation' * Translation' (i.e. the inverse)

So modifying your original code:

[source lang="cpp"]
mat4x4 invTran_mat, invRot_mat, temp_mat, modelView_mat;

mat4x4_identity(temp_mat);
mat4x4_identity(invTran_mat);

// Inverse of your original rotation matrix
mat4x4_rotate_Y(invRot_mat, temp_mat, rot_y);

// Inverse of your original translation matrix
mat4x4_translate(invTran_mat, -strafe, 0.0, -dolly);

//apply the matrices to the model matrix
mat4x4_mul(temp_mat, invRot_mat, invTran_mat);
mat4x4_mul(modelView_mat, temp_mat, model_mat);
[/source] Edited by scniton

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  

  • Partner Spotlight

  • Similar Content

    • By pseudomarvin
      I assumed that if a shader is computationally expensive then the execution is just slower. But running the following GLSL FS instead just crashes
      void main() { float x = 0; float y = 0; int sum = 0; for (float x = 0; x < 10; x += 0.00005) { for (float y = 0; y < 10; y += 0.00005) { sum++; } } fragColor = vec4(1, 1, 1 , 1.0); } with unhandled exception in nvoglv32.dll. Are there any hard limits on the number of steps/time that a shader can take before it is shut down? I was thinking about implementing some time intensive computation in shaders where it would take on the order of seconds to compute a frame, is that possible? Thanks.
    • By Arulbabu Donbosco
      There are studios selling applications which is just copying any 3Dgraphic content and regenerating into another new window. especially for CAVE Virtual reality experience. so that the user opens REvite or CAD or any other 3D applications and opens a model. then when the user selects the rendered window the VR application copies the 3D model information from the OpenGL window. 
      I got the clue that the VR application replaces the windows opengl32.dll file. how this is possible ... how can we copy the 3d content from the current OpenGL window.
      anyone, please help me .. how to go further... to create an application like VR CAVE. 
       
      Thanks
    • By cebugdev
      hi all,

      i am trying to build an OpenGL 2D GUI system, (yeah yeah, i know i should not be re inventing the wheel, but this is for educational and some other purpose only),
      i have built GUI system before using 2D systems such as that of HTML/JS canvas, but in 2D system, i can directly match a mouse coordinates to the actual graphic coordinates with additional computation for screen size/ratio/scale ofcourse.
      now i want to port it to OpenGL, i know that to render a 2D object in OpenGL we specify coordiantes in Clip space or use the orthographic projection, now heres what i need help about.
      1. what is the right way of rendering the GUI? is it thru drawing in clip space or switching to ortho projection?
      2. from screen coordinates (top left is 0,0 nd bottom right is width height), how can i map the mouse coordinates to OpenGL 2D so that mouse events such as button click works? In consideration ofcourse to the current screen/size dimension.
      3. when let say if the screen size/dimension is different, how to handle this? in my previous javascript 2D engine using canvas, i just have my working coordinates and then just perform the bitblk or copying my working canvas to screen canvas and scale the mouse coordinates from there, in OpenGL how to work on a multiple screen sizes (more like an OpenGL ES question).
      lastly, if you guys know any books, resources, links or tutorials that handle or discuss this, i found one with marekknows opengl game engine website but its not free,
      Just let me know. Did not have any luck finding resource in google for writing our own OpenGL GUI framework.
      IF there are no any available online, just let me know, what things do i need to look into for OpenGL and i will study them one by one to make it work.
      thank you, and looking forward to positive replies.
    • By fllwr0491
      I have a few beginner questions about tesselation that I really have no clue.
      The opengl wiki doesn't seem to talk anything about the details.
       
      What is the relationship between TCS layout out and TES layout in?
      How does the tesselator know how control points are organized?
          e.g. If TES input requests triangles, but TCS can output N vertices.
             What happens in this case?
      In this article,
      http://www.informit.com/articles/article.aspx?p=2120983
      the isoline example TCS out=4, but TES in=isoline.
      And gl_TessCoord is only a single one.
      So which ones are the control points?
      How are tesselator building primitives?
    • By Orella
      I've been developing a 2D Engine using SFML + ImGui.
      Here you can see an image
      The editor is rendered using ImGui and the scene window is a sf::RenderTexture where I draw the GameObjects and then is converted to ImGui::Image to render it in the editor.
      Now I need to create a 3D Engine during this year in my Bachelor Degree but using SDL2 + ImGui and I want to recreate what I did with the 2D Engine. 
      I've managed to render the editor like I did in the 2D Engine using this example that comes with ImGui. 
      3D Editor preview
      But I don't know how to create an equivalent of sf::RenderTexture in SDL2, so I can draw the 3D scene there and convert it to ImGui::Image to show it in the editor.
      If you can provide code will be better. And if you want me to provide any specific code tell me.
      Thanks!
  • Popular Now