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 w开发者_如何学Goant 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.
If you consider a matrix as consisting of 4 rows.
Side vector Up Vector Forward Vector Position
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.
So lets take the assumption that the up vector is 0, 1, 0.
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.
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 );
If you want the simplest rotation matrix, this is about the best you can do:
ρ = sqrt(x2 + y2)
r = sqrt(x2 + y2 + z2)
M = x/ρ -y/ρ x/r
y z/(r ρ) x/ρ y/r
-ρ/r 0 z/r
The simplest rotation would actually involve a more complicated matrix.
精彩评论