根据位置和欧拉角计算注视向量
我实现了一个 FPS 风格的相机,该相机由位置矢量、欧拉角、俯仰角和偏航角(x 和 y 旋转)组成。 设置投影矩阵后,我通过旋转转换为相机坐标,然后转换为相机位置的倒数:
// Load projection matrix
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Set perspective
gluPerspective(m_fFOV, m_fWidth/m_fHeight, m_fNear, m_fFar);
// Load modelview matrix
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// Position camera
glRotatef(m_fRotateX, 1.0, 0.0, 0.0);
glRotatef(m_fRotateY, 0.0, 1.0, 0.0);
glTranslatef(-m_vPosition.x, -m_vPosition.y, -m_vPosition.z);
现在我设置了几个视口,每个视口都有自己的相机,并且从每个相机渲染其他摄像机的位置(作为一个简单的盒子)。 我还想绘制这些相机的视图向量,但我不知道如何从位置和欧拉角计算观看向量。 我尝试将原始相机矢量 (0, 0, -1) 乘以表示相机旋转的矩阵 然后将相机位置添加到转换后的矢量中,但这根本不起作用(很可能是因为我偏离了基地):
vector v1(0, 0, -1);
matrix m1 = matrix::IDENTITY;
m1.rotate(m_fRotateX, 0, 0);
m1.rotate(0, m_fRotateY, 0);
vector v2 = v1 * m1;
v2 = v2 + m_vPosition; // add camera position vector
glBegin(GL_LINES);
glVertex3fv(m_vPosition);
glVertex3fv(v2);
glEnd();
我想要的是从相机向观察方向绘制一条线段。 我到处寻找这方面的例子,但似乎找不到任何东西。
多谢!
I've implemented an FPS style camera, with the camera consisting of a position vector, and Euler angles pitch and yaw (x and y rotations).
After setting up the projection matrix, I then translate to camera coordinates by rotating, then translating to the inverse of the camera position:
// Load projection matrix
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Set perspective
gluPerspective(m_fFOV, m_fWidth/m_fHeight, m_fNear, m_fFar);
// Load modelview matrix
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// Position camera
glRotatef(m_fRotateX, 1.0, 0.0, 0.0);
glRotatef(m_fRotateY, 0.0, 1.0, 0.0);
glTranslatef(-m_vPosition.x, -m_vPosition.y, -m_vPosition.z);
Now I've got a few viewports set up, each with its own camera, and from every camera I render the position of the other cameras (as a simple box).
I'd like to also draw the view vector for these cameras, except I haven't a clue how to calculate the lookat vector from the position and Euler angles.
I've tried to multiply the original camera vector (0, 0, -1) by a matrix representing the camera rotations
then adding the camera position to the transformed vector, but that doesn't work at all (most probably because I'm way off base):
vector v1(0, 0, -1);
matrix m1 = matrix::IDENTITY;
m1.rotate(m_fRotateX, 0, 0);
m1.rotate(0, m_fRotateY, 0);
vector v2 = v1 * m1;
v2 = v2 + m_vPosition; // add camera position vector
glBegin(GL_LINES);
glVertex3fv(m_vPosition);
glVertex3fv(v2);
glEnd();
What I'd like is to draw a line segment from the camera towards the lookat direction.
I've looked all over the place for examples of this, but can't seem to find anything.
Thanks a lot!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我刚刚想通了。当我回去添加答案时,我看到 Ivan 刚刚告诉了我同样的事情:)
基本上,为了绘制相机矢量,我这样做:
这会从相机位置在观看方向上绘制一条 10 个单位的线。
I just figured it out. When I went back to add the answer, I saw that Ivan had just told me the same thing :)
Basically, to draw the camera vector, I do this:
This draws a line from the camera position for 10 units in the lookat direction.