在OpenGL视矢量 [英] Sight vector in OpenGL

查看:243
本文介绍了在OpenGL视矢量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是,我不能得到载体视线在OpenGL。我做了一些研究,发现它应该是转换之后ž载体,但它并不想工作。我这有code以检索块(我希望它从摄像头动盼着)的速度,但所有的时间它的动作无关的摄像头,但所有的时间同样的方式相比,呈现的世界:

  GLfloat矩阵[16]。
    glGetFloatv(GL_MODELVIEW_MATRIX,矩阵);
    GLfloat D = SQRT(矩阵[8] *矩阵[8] +矩阵[9] *矩阵[9] +矩阵[10] *矩阵[10]);
    xmov =矩阵[8] / D;
    ymov =矩阵[9] / D;
    zmov =矩阵[10] / D;
 

我做了什么错?

解决方案

好了,现在你澄清后,你真正想做的事,我是pretty的肯定,这是正确的答案:

您可能已经习惯的东西叫做模型视图矩阵。没有它似乎奇怪吧,它的本质结合两部分组成?嗯,这是对我来说,之后我想了一会儿,这是有道理。最终的顶点位置的计算方法如下:

  GL_POSITION = ProjectionMat * ModelViewMat * VertexPos;
 

您看看,有没有你是否从原点动摄像头的区别对OpenGL [X,Y,Z] 移动对象[-x,-y,-z] - 你会得到相同的结果。然而有用区分的东西,可以是从原点不同一个照相机的位置。

  GL_POSITION = ProjectionMat * ViewMat * ModelMat * VertexPos;
 

我觉得最自然的方式来做到这一点是,正如我所说,拆分计算成两个矩阵:型号查看。在现场的每一个对象现在已经改变了型号矩阵,摄像机的位置是通过改变查看矩阵设置。有道理?

我给你举个例子。如果您的相机在 [5,0,0] (相当于翻译(-5,0,0)),并在 [ - 5,0,0] ,它会降落10台相机。现在,当你移动你的相机远离原点(增加第一平移距离)之间的摄像头,距离和对象的增长。

对象翻译的模型的,相机翻译的查看的。

所以,不难得出结论,如果你想要的忽略的摄像机位置,只去掉了查看部分从方程;每个对象将绘制的没有的考虑摄像机的位置考虑,只有这样,相对于你的口中。

  GL_POSITION = ProjectionMat * ModelMat * VertexPos;
 

我们的假设模型现在登陆5个单位沿X轴从视口中的无论的你当前的看,那就是,我认为,pretty的很多东西你想达到。

不管怎样,你很可能使用 <一个很好的教程/ P>

The problem I have is that I cant get "line of sight" vector in OpenGL. I've done some research and found that it should be Z vector after transformations, but it doesn't want to work. I've got this code to retrive velocity of the block( i want it to move foward from "camera"), but all the time it moves irrelevant to camera, but all the time same way compared to rendered world:

GLfloat matrix[16]; 
    glGetFloatv (GL_MODELVIEW_MATRIX, matrix);
    GLfloat d = sqrt( matrix[8]*matrix[8] + matrix[9]*matrix[9] + matrix[10]*matrix[10]);
    xmov = matrix[8]/d;
    ymov = matrix[9]/d;
    zmov = matrix[10]/d;

What I've done wrong?

解决方案

Okay, now after you clarified what you really want to do, I'm pretty sure this is the correct answer:

You are probably used to something called ModelView matrix. Didn't it seem strange for you that it's essentially combined of two parts? Well, it was for me, and after I thought about it for a while, it made sense. The final vertex position is calculated like this:

gl_Position = ProjectionMat * ModelViewMat * VertexPos;

You see, there's no difference for OpenGL whether you move "camera" from origin by [x,y,z] or move objects by [-x,-y,-z] - you'll get the same results. It is however useful to distinguish a "camera" position as something that can be different from the origin.

gl_Position = ProjectionMat * ViewMat * ModelMat * VertexPos;

I think the most natural way to do it is, as I said, split the calculations into two matrices : Model and View. Every object on the scene now has to change the Model matrix, and camera position is set by changing View matrix. Makes sense?

I'll give you an example. If your camera is in [5,0,0] (which corresponds to Translate(-5,0,0)), and your object in [-5,0,0], it will land 10 units from the camera. Now, when you move your camera further away from origin (increasing the first translation distance), distance between "camera" and the object grows.

The object translation is the Model, the camera translation is the View.

So it's not hard to come to conclusion, that if you want to ignore camera position, just strip the View part from the equation; every object will now be drawn without taking camera position into account, thus relative only to your viewport.

gl_Position = ProjectionMat * ModelMat * VertexPos;

Our hypothetical model will now land 5 units along the X axis from the viewport regardless of what you're currently "looking at", and that's, I think, pretty much what you wanted to achieve.

Anyway, you could probably use a nice tutorial about it

这篇关于在OpenGL视矢量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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