如何从OpenGL ModelView矩阵获取视图方向? [英] How can I get view direction from the OpenGL ModelView Matrix?

查看:157
本文介绍了如何从OpenGL ModelView矩阵获取视图方向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个体积渲染程序,该程序会不断调整某些平面的几何形状,使其始终面向相机.每当相机旋转以使其看起来好像不动时,平面几何都会旋转-相对于场景中的所有其他物体. (我将相机的观看方向用作这些平面几何的法线向量.)

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

目前,我正在手动存储自定义旋转矢量("rotations"),并在渲染函数中按如下所示应用其效果:

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

然后,我通过使用旋转值绕x和y轴旋转初始视图方向(0,0,-1)来获得视图方向.这是通过以下方式完成的.最终的观看方向存储在视图"中:

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

我正在寻找它作为参考 http://3dengine.org/Modelview_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?

推荐答案

您将要看这张图片@ http://db-in.com/images/local_vectors.jpg

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

飞行方向(DOF)是第三行.

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

参考:

这篇关于如何从OpenGL ModelView矩阵获取视图方向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆