OpenGL 中的视线向量 [英] Sight vector in OpenGL

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

问题描述

我遇到的问题是我无法在 OpenGL 中获得视线"向量.我做了一些研究,发现转换后应该是Z向量,但它不想工作.我有这段代码来检索块的速度(我希望它从相机"向前移动),但它一直移动与相机无关,但与渲染世界相比始终相同:

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;

我做错了什么?

推荐答案

好的,现在在你澄清了你真正想要做什么之后,我很确定这是正确的答案:

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

您可能已经习惯了称为 ModelView 矩阵的东西.它本质上是由两部分组合而成的,对您来说是不是很奇怪?嗯,它是给我的,经过我思考了一段时间,它是有道理的.最终的顶点位置是这样计算的:

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;

你看,无论是通过 [x,y,z] 从原点移动相机"还是通过 [-x,-y,-z 移动对象,OpenGL 都没有区别] - 你会得到相同的结果.然而,将相机"位置区分为可能与原点不同的东西是很有用的.

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;

我认为最自然的方法是,正如我所说,将计算分成两个矩阵:ModelView.现在场景中的每个对象都必须更改 Model 矩阵,并且通过更改 View 矩阵来设置相机位置.有道理吗?

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?

我给你举个例子.如果您的相机位于 [5,0,0](对应于 Translate(-5,0,0)),并且您的对象位于 [-5,0,0],它将从相机降落 10 个单位.现在,当您将相机移离原点更远时(增加第一个平移距离),相机"与对象之间的距离会增加.

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.

所以不难得出结论,如果你想忽略相机位置,只需从等式中去除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;

我们的假设模型现在将从视口沿 X 轴降落 5 个单位,无论您当前看"什么,我认为这几乎是您想要实现的.

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天全站免登陆