旋转的球体,使对相机的极头 [英] Rotate a sphere so that its pole heads towards the camera

查看:191
本文介绍了旋转的球体,使对相机的极头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个球,其北极在(0,0,1)和中心(0,0,0)。我也有一个照相机任意定位在场景和看着(0,0,0)。我想旋转球体,使得其北极位于该线从(0,0,0),以相机的位置。我在一个顶点着色器执行这项任务,我希望它是尽可能简单,以避免FPS下降。没有人知道任何简单的方法如何计算旋转矩阵,甚至一个简单的方法如何执行球体顶点的旋转?

I have a sphere whose north pole is at (0, 0, 1) and center at (0, 0, 0). I also have a camera arbitrarily positioned in the scene and looking at (0, 0, 0). I want to rotate the sphere so that its north pole lies on the line from (0, 0, 0) to camera position. I am performing this task in a vertex shader and I would like it to be as simple as possible to avoid FPS drop. Does anyone know any simple method how to calculate the rotation matrix or even a simpler way how to perform the rotation of sphere vertices?

推荐答案

旋转矩阵是很容易完成。

The rotation matrix is quite easy to perform.

如果你考虑一个矩阵组成的4行。

If you consider a matrix as consisting of 4 rows.

边矢量 截至矢量 前进矢量 位置

Side vector Up Vector Forward Vector Position

您计算矢量0,0,0到相机。这简直是​​(CAMX,CAMY,camZ)(其计算为摄像机位置 - 对象原点)。

You calculate a vector from 0, 0, 0 to the camera. This is simply (camX, camY, camZ) (Its calculates as camera position - object origin).

从那里,你现在有你的前进载体。首先归一化。

From there you now have your forward vector. Firstly normalise it.

所以,让我们假设向上矢量为0,1,0。

So lets take the assumption that the up vector is 0, 1, 0.

您可以简单地积这​​件事向量和归一化推进载体。这使您可以一边载体。最后,跨产品的侧面和前向矢量以真实了载体。你有前3排的你矩阵。最后一行将在你的榜样反正被0,0,0,1。

You can simply cross product this up vector and the normalised forward vector. This gives you the side vector. Finally cross product the side and forward vectors to give a TRUE up vector. and you have the top 3 rows of you matrix. The final row will be 0,0,0,1 anyway in your example.

因此​​,使用D3DX你会计算,如下所示:

So using D3DX you'd calculate it as follows:

 D3DXVECTOR3 toCam = camPos - spherePos;
 D3DXVECTOR3 fwdVector;
 D3DXVec3Normalize( &fwdVector, &toCam );

 D3DXVECTOR3 upVector( 0.0f, 1.0f, 0.0f );
 D3DXVECTOR3 sideVector;
 D3DXVec3CrossProduct( &sideVector, &upVector, &fwdVector );
 D3DXVec3CrossProduct( &upVector, &sideVector, &fwdVector );

 D3DXVec3Normalize( &upVector, &toCam );
 D3DXVec3Normalize( &sideVector, &toCam );

 D3DXMATRIX orientation( sideVector.x, sideVector.y, sideVector.z, 0.0f,
                         upVector.x,   upVector.y,   upVector.z,   0.0f,
                         fwdVector.x,  fwdVector.y,  fwdVector.z,  0.0f,
                         spherePos.x,  spherePos.y,  spherePos.z,  1.0f );

这篇关于旋转的球体,使对相机的极头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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