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

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

问题描述

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

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

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) 来获得视图方向.这是通过以下方式完成的.最终的查看方向存储在'view'中:

 public Vec3f getViewingAngle(){//首先旋转观察点//然后找到从那里到中心的向量Vec3f 视图=新 Vec3f(0,0,-1);浮动新Z=0;浮动比率=(浮动)(Math.PI/180);浮动 vA=(float) (-1f*rotations.y*(ratio));浮动 hA=(float) (-1f*rotations.x)*ratio;//先绕x轴旋转浮动 newY=(float) (view.y*Math.cos(vA)-view.z*Math.sin(vA));newZ=(float) (view.y*Math.sin(vA)+view.z*Math.cos(vA));视图=新 Vec3f(view.x,newY,newZ);//绕Y轴旋转浮动 newX=(float) (view.z*Math.sin(hA)+view.x*Math.cos(hA));newZ=(float) (view.z*Math.cos(hA)-view.x*Math.sin(hA));视图=新 Vec3f(newX,view.y,newZ);view=new Vec3f(view.x*-1f,view.y*-1f,view.z*-1f);//返回最终确定的法线观察方向视图=Vec3f.normalized(视图);返回视图;}

现在我将这个程序转移到一个更大的项目中,其中相机旋转由第 3 方图形库处理.我没有旋转矢量.有什么方法可以让我从以下位置获取视图方向向量:

GLfloat 矩阵[16];glGetFloatv (GL_MODELVIEW_MATRIX, 矩阵);

我正在查看这个以供参考

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?

解决方案

You'll want to look at this picture @ 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:

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

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