3

I am writing a volume render program that constantly adjusts some plane geometry so it always faces the camera. The plane geometry rotates whenever the camera rotates in order to appear as if it doesn't move--relative to everything else in the scene. (I use the camera's viewing direction as a normal vector to these plane geometries.)

Currently I am manually storing a custom rotation vector ('rotations') and applying its affects as follows in the render function:

gl2.glRotated(rotations.y, 1.0, 0.0, 0.0);
gl2.glRotated(rotations.x, 0.0, 1.0, 0.0);

Then later on I get the viewing direction by rotating the initial view direction (0,0,-1) around the x and y axes with the values from rotation. This is done in the following manner. The final viewing direction is stored in 'view':

     public Vec3f getViewingAngle(){
        //first rotate the viewing POINT
        //then find the vector from there to the center
        Vec3f view=new Vec3f(0,0,-1);
        float newZ=0;
        float ratio=(float) (Math.PI/180);
        float vA=(float) (-1f*rotations.y*(ratio));
        float hA=(float) (-1f*rotations.x)*ratio;

        //rotate about the x axis first
        float newY=(float) (view.y*Math.cos(vA)-view.z*Math.sin(vA));
        newZ=(float) (view.y*Math.sin(vA)+view.z*Math.cos(vA));
        view=new Vec3f(view.x,newY,newZ);

        //rotate about Y axis
        float newX=(float) (view.z*Math.sin(hA)+view.x*Math.cos(hA));
        newZ=(float) (view.z*Math.cos(hA)-view.x*Math.sin(hA));
        view=new Vec3f(newX,view.y,newZ);
        view=new Vec3f(view.x*-1f,view.y*-1f,view.z*-1f);

        //return the finalized normal viewing direction
        view=Vec3f.normalized(view);
        return view;
}

Now I am moving this program to a larger project wherein the camera rotation is handled by a 3rd party graphics library. I have no rotations vector. Is there some way I can get my view direction vector from:

GLfloat matrix[16]; 
glGetFloatv (GL_MODELVIEW_MATRIX, matrix);

I am looking at this for reference http://3dengine.org/Modelview_matrix but I still don't get how to come up with the view direction. Can someone explain to me if it is possible and how it works?

AAB
  • 674
  • 3
  • 14
  • 27

2 Answers2

13

You'll want to look at this picture @ http://db-in.com/images/local_vectors.jpg http://db-in.com/images/local_vectors.jpg

The Direction-of-Flight ( DOF) is the 3rd row.

GLfloat matrix[16]; 
glGetFloatv( GL_MODELVIEW_MATRIX, matrix );

float DOF[3];
DOF[0] = matrix[  2 ]; // x
DOF[1] = matrix[  6 ]; // y
DOF[2] = matrix[ 10 ]; // z

Reference:

Michaelangel007
  • 2,798
  • 1
  • 25
  • 23
1

Instead of trying to follow the modelview matrix, to adjust your volume rasterizer's fragment impostor, you should just adjust the modelview matrix to your needs. OpenGL is not a scene graph, it's a drawing system and you can, and should change things however they suit you best.

Of course if you must embedd the volume rasterization into a larger scene, it may be neccessary to extract certain info from the modelview matrix. The upper left 3×3 submatrix contains the composite rotation of models and view. The 3rd column contains the view rotated Z vector.

datenwolf
  • 159,371
  • 13
  • 185
  • 298
  • I see. I agree with you that I should just control the modelview matrix. Unfortunately I have to add the volume to a pre-existing scenegraph that handles the camera. I see that the upper left 3x3 is the rotation matrix. I don't understand what you mean by 'rotated z vector', is this what I'm looking for? – AAB Mar 29 '13 at 18:32
  • ....I guess I would just rotate the original orientation by the rotation, matrix, but I can't be sure what the original orientation is. Is it somehow stored in the matrix? – AAB Mar 29 '13 at 18:39
  • Well, OpenGL is not a scene graph, so the modelview matrix contains the whole transformation chain. Trying to coax usefull information out of OpenGL is this way is a bit unreliable. However, given a certain modelview matrix, the first 3 columns are the base vectors of the model coordinate system in view space. I.e. the 1st column is local X (right), the 2nd column is local Y (up) and the 3rd column is local Z (into). – datenwolf Mar 29 '13 at 18:57