根据位置和欧拉角计算注视向量

发布于 2024-08-31 20:55:06 字数 1027 浏览 5 评论 0原文

我实现了一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

来世叙缘 2024-09-07 20:55:06

我刚刚想通了。当我回去添加答案时,我看到 Ivan 刚刚告诉了我同样的事情:)

基本上,为了绘制相机矢量,我这样做:

glPushMatrix();

// Apply inverse camera transform
glTranslatef(m_vPosition.x, m_vPosition.y, m_vPosition.z);
glRotatef(-m_fRotateY, 0.0, 1.0, 0.0); 
glRotatef(-m_fRotateX, 1.0, 0.0, 0.0); 

// Then draw the vector representing the camera
glBegin(GL_LINES);
glVertex3f(0, 0, 0);
glVertex3f(0, 0, -10);
glEnd();

glPopMatrix();

这会从相机位置在观看方向上绘制一条 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:

glPushMatrix();

// Apply inverse camera transform
glTranslatef(m_vPosition.x, m_vPosition.y, m_vPosition.z);
glRotatef(-m_fRotateY, 0.0, 1.0, 0.0); 
glRotatef(-m_fRotateX, 1.0, 0.0, 0.0); 

// Then draw the vector representing the camera
glBegin(GL_LINES);
glVertex3f(0, 0, 0);
glVertex3f(0, 0, -10);
glEnd();

glPopMatrix();

This draws a line from the camera position for 10 units in the lookat direction.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文