# 3D Cameras

This topic is 4866 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Okay, I have a nice little class called CD3DCamera. I will use it for different types of cameras, a more or less base class so I don’t hae to keep writing the same code over and over again. I haven’t tested it yet because I don’t want to go to all the trouble and have something not work right, I want to make sure the math is correct first. I’m not that good and 3D math, a bummer because I use Direct3D all the time. 1. ) I need to make sure I’m calculating the UP vector correctly. I use the D3DXVec4Cross helper function to get the UP vector. I use the cross product of the look-at-target vector and the original up (0,1,0) vector. 2. ) To rotate, I use D3DXRotationAxis to rotate the camera on any axis I choose. I apply the rotation and use D3DXVec4TransformCoord with the look-at-target vector as the source and destination vector. Then I apply the cross product again to achieve the UP vector. Is this all correct?

##### Share on other sites
Hmm... no replys, I guess that means I'm correct then.

##### Share on other sites
Quote:
 Original post by sakky1. ) I need to make sure I’m calculating the UP vector correctly. I use the D3DXVec4Cross helper function to get the UP vector. I use the cross product of the look-at-target vector and the original up (0,1,0) vector.

Well... if you take the cross product of the look-at-target vector (I'm assuming this is a vector from the position of the camera to the look-at target?) and the original up vector. How can you get the new up-vector??
The cross product of two vectors gives you a vector that is perpendicular to both of those vectors.

##### Share on other sites
Which is saying. If you are at the origon and looking(0,0,1) with up(0,1,0) the cross product will return the right vector(1,0,0)

##### Share on other sites

If your getting your look at vector by transforming a base vector by a matrix, then you can do the same for your up vector

vlook = {1,0,0}; //with no rotation look in the +x direction
vup = {0,1,0}; //with no rotation up is +y

vlook.tranform3(viewChangeMatrix);
vup.tranform3(viewChangeMatrix);

##### Share on other sites
I have three vectors, position, look and up. I use the three in the D3DXMatrixLookAt function. I need the new up vector for the look. How do I get that then?

##### Share on other sites
Generally people use either the world up vector or the camera up vector

You can get the camera up vector by cross product of the forward and right vectors.

##### Share on other sites
Quote:
 Original post by sakkyI have three vectors, position, look and up. I use the three in the D3DXMatrixLookAt function. I need the new up vector for the look. How do I get that then?

You say you have three vectors but then you ask how to calculate the 3rd? So what you mean is you have 2 points (which is really 1 vector) and you have to ask yourself how did you arrive at this vector. You need to take the same steps and apply them to your up vector to get accurate results. Or just set it to up (0,1,0) always and never look straight up or down.

If you calculate a matrix to get you to your final view vectors this is easy. Just transform your base view vectors by the same matrix and they are all the correct vectors. No cross product or anything.

As far as I know its mathematicaly imposible to calculate the up vector given only the lookFrom and lookAt positions. There is not enough data present.

##### Share on other sites
Quote:
 Original post by sakky2. ) To rotate, I use D3DXRotationAxis to rotate the camera on any axis I choose.

You mean D3DXMatrixRotationAxis? Then the resulting matrix contains all the vectors you need. If I remember correctly first row is right vector, second is up vector and third is look-at vector.

##### Share on other sites
Quote:
Original post by Zook
Quote:
 Original post by sakky2. ) To rotate, I use D3DXRotationAxis to rotate the camera on any axis I choose.

You mean D3DXMatrixRotationAxis? Then the resulting matrix contains all the vectors you need. If I remember correctly first row is right vector, second is up vector and third is look-at vector.

Really? Like I said, I'm not to good with the math. I've been studying though.

Okay, I start off with Position(0,0,0), Look(0,0,1) and Up(0,1,0). So the camera is located at the origin looking straigh down the +Z axis, with the up vector, up, as usual. So to translate or rotate, I apply the same to all three vectors. When I translate, I add a vecctor or three floats to the position of the camera. I'm also suppose to do this to the look and up vectors too?

Okay then, so what ever I do to the one, I must to do to all others.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

(You must login to your GameDev.net account.)

• 14
• 12
• 29
• 11
• 44
• ### Forum Statistics

• Total Topics
634855
• Total Posts
3019653
×