Z-Knight

07-22-2008, 11:19 AM

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.

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.