Jump to content
  • Advertisement
Sign in to follow this  
Z-Knight

OpenGL ? View Matrix: 1st person to 3rd person ?

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've posted this question on the OpenGL forum but I'm posting here so I can spread the love. http://www.opengl.org/discussion_boards/ubbthreads.php?ubb=showflat&Number=242100#Post242100 I am writing a fly-through code to move through a scene in first person view but I also want to be able to stop and look at a desired point/object and then rotate around it and also be able to zoom and pan the view. So, I started with the following code for First Person Shooter: http://www.codesampler.com/oglsrc/oglsrc_5.htm#ogl_fps_controls The code works great and I got the First Person view to work but I'm having trouble switching to the third person because I don't understand how to alter the view matrix. The code uses the following view matrix:
     |  rx   ry   rz  -(r.e) |
     |  ux   uy   uz  -(u.e) |
     | -lx  -ly  -lz   (l.e) |
     |   0    0    0     1   |
   
    Where r = Right vector
          u = Up vector
          l = Look vector
          e = Eye position in world space
          . = Dot-product operation
The method to update the view looks like this (note this is in JOGL, but it is the basically the same as OpenGL).
  private void updateView() {
    // Normalize the look vector. setLook() method does this already but 
    // we can't assume that the reference wasn't changed so this is likely
    // precautionary and redundant....but can't take chances.
    // Make sure the look vector has some length
    if (Math.abs(look.length()) < 1.0e-6){
      throw new RuntimeException("Look vector cannot have zero length.");
    }    
    look.normalize();

    // Cross product of the 'look' and 'up' vectors gets the 'right' vector.
    Vector3d.cross(right, look, up);
    
    // Make sure the right vector has some length
    if (Math.abs(right.length()) < 1.0e-6){
      throw new RuntimeException("Right vector cannot have zero length.");
    }    
    right.normalize();

    // Cross product of the 'right' and 'look' vectors gets the 'up' vector.
    Vector3d.cross(up, right, look);
    
    // Make sure the up vector has some length
    if (Math.abs(up.length()) < 1.0e-6){
      throw new RuntimeException("Up vector cannot have zero length.");
    }    
    up.normalize();

    // Place the results into a matrix format for use with  
    // the OpenGL call to glMultMatrixd().
    
    view[0] = right.x;
    view[1] = up.x;
    view[2] = -look.x;
    view[3] = 0.0;

    view[4] = right.y;
    view[5] = up.y;
    view[6] = -look.y;
    view[7] = 0.0;

    view[8] = right.z;
    view[9] = up.z;
    view[10] = -look.z;
    view[11] = 0.0;    
    
      view[12] = -Vector3d.dot(right, eye);
      view[13] = -Vector3d.dot(up, eye);
      view[14] = Vector3d.dot(look, eye);
      view[15] = 1.0;
  }
So this piece of code lets me fly through and in this code the 'eye' point gets moved through the scene and everything else (look vector, up vector, right vector) gets adjusted accordingly. So, now my problem...in order to now change to 3rd person mode where I focus on a particular point and rotate around it is what I can't figure out. Actually, let me say that if I change the position portion of the view matrix to the following code then I am now rotating my view around the 0,0,0 point but my 'eye' point never moves and so I can no longer do zooming and panning as before (where I would simply move the eye point to do the zoom/pan).
      view[12] = -eye.x;
      view[13] = -eye.y;
      view[14] = -eye.z;
      view[15] = 1.0; 
Basically I want to figure out how I can add zoom/panning to my code for the 3rd person view...I realize this is kind of complicated concept so I'm not expecting much help, but if by chance someone has done this I would appreciate any info. [Edited by - Z-Knight on July 22, 2008 12:40:56 PM]

Share this post


Link to post
Share on other sites
Advertisement
Here's the link to my example code:
http://www.zaczek.com/jogl/TestFPS_Camera.jnlp

It is JOGL code but the same as OpenGL.

Pressing 'O' (letter o) switches between modes....by default it starts in the 3rd person view mode.

Press Mouse LEFT button to rotate the camera...note the blue sphere attached by the blue line...that represents the EYE point.

With the LEFT mouse button down to rotate the view, you can use the UP/DOWN/LEFT/RIGHT keys to fly through the scene when in the 1st person view....you'll see the eye point coordinates changing when you do this fly through.

In the 3rd person view the eye changes as well when you use the UP/DOWN/LEFT/RIGHT keys but it doesn't change as I would expect.

Share this post


Link to post
Share on other sites
never mind, I fixed it...the example is still up so you can try it. Check out the OpenGL forum for my long winded response as to how I fixed it.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!