Camera Idea

Started by
10 comments, last by BornToCode 18 years, 5 months ago
Here's my camera code from a GLUT based app (not a game, more like a 3D editor)

void SetLookAt() {// do orbit camera	vEye.x = fEyeRadius * cosf(fLat) * cosf(fLon);	vEye.y = fEyeRadius * cosf(fLat) * sinf(fLon);	vEye.z = fEyeRadius * sinf(fLat);    glLoadIdentity();    gluLookAt(vEye.x, vEye.y, vEye.z, 0, 0, 0, 0, 0, -1);}


'fLat' and 'fLon' are updated as the mouse moves around the screen with the middle mouse button held down (using
glutMotionFunc). fLat is constrained to the range (-0.75, 0.75). The 'up' axis is along the negative z-axis, the object being looked at is always at (0, 0, 0).

It's essentially the same as your method except I calculate the vector on the fly. 'sin' and 'cos' aren't so expensive that it's worth precalculating and storing them for results I need at most 60 times a second.
John BlackburneProgrammer, The Pitbull Syndicate
Advertisement
This is how i do it in my application

void CameraRotation(D3DXMATRIX Final,float rx,float ry,float rz,
D3DXVECTOR3& right,D3DXVECTOR3& up,D3DXVECTOR3& look,D3DXVECTOR3& position)
{
D3DXMATRIX T;
D3DXMATRIX T2;
D3DXMATRIX T3;
D3DXMatrixRotation(&T,D3DXToRadian(x));
D3DXMatrixRotation(&T2,D3DXToRadian(y));
D3DXMatrixRotation(&T3,D3DXToRadian(z));
Final=T*T2*T3;
//----------------------------------
//if you are using Open Gl instead of D3D just take
//The transpose Matrix of the Final
//-----------------------------------
right=D3DXVECTOR3(Final._11,Final._12,Final._13);
up=D3DXVECTOR3(Final._21,Final._22,Final._23);
look=D3DXVECTOR3(Final._31,Final._32,Final._33);
position=D3DXVECTOR3(Final._41,Final._42,Final._43);
D3DXVec3Normalize(&look,&look);

D3DXVec3Cross(&right,&up,&look);
D3DXVec3Normalize(&right,&right);
D3DXVec3Cross(&up,&look,&right);
D3DXVec3Normalize(&up,&up);
}

PS: Once this Function return you can used the vectors along with position to move camera around anywhere you want

This topic is closed to new replies.

Advertisement