Not logged in, Join Here! or Log In Below:  
 
News Articles Search    
 

 Home / 3D Theory & Graphics / 3d camera code and theory... Account Manager
 
Archive Notice: This thread is old and no longer active. It is here for reference purposes. This thread was created on an older version of the flipcode forums, before the site closed in 2005. Please keep that in mind as you view this thread, as many of the topics and opinions may be outdated.
 
charly

July 22, 1999, 06:35 PM

Well once again a questions about camera.
I haven't yet found a working camera matrix stuff and havent got
camera working.

I have now working matrix systems and the normal matrix rotation
works, but now I need a camera matrix.
I think I know the basic idea of camera, but I don't know how to
make the matrix.

Could somebody tell me how can I make camera matrix from camera
position and camera target please?

thanks.

 
7ig3r

July 23, 1999, 03:12 PM



charly wrote:
>> Well once again a questions about camera.
>> I haven't yet found a working camera matrix stuff and havent got
>> camera working.
>>
>> I have now working matrix systems and the normal matrix rotation
>> works, but now I need a camera matrix.
>> I think I know the basic idea of camera, but I don't know how to
>> make the matrix.
>>
>> Could somebody tell me how can I make camera matrix from camera
>> position and camera target please?
>>
>> thanks.
>>

Check out Allegro 3.0/3.1 at www.talula.demon.co.uk. Allegro has code for basic 3d (including 6DOF cameras). I have used some of its code to write my own 3d engine. It is really useful. Although it doesn't really give you much theory, it will provide you with the starter code. I hope this helps.

7ig3r

 
Raven

July 23, 1999, 03:36 PM

charly wrote:
>> Well once again a questions about camera.
>> I haven't yet found a working camera matrix stuff and havent got
>> camera working.
>>
>> I have now working matrix systems and the normal matrix rotation
>> works, but now I need a camera matrix.
>> I think I know the basic idea of camera, but I don't know how to
>> make the matrix.
>>
>> Could somebody tell me how can I make camera matrix from camera
>> position and camera target please?
>>
>> thanks.
>>

Check out my postings about cameras eariler on the message board. the date is 11 Jul 1999 - Sun [02:02PM]. I gave the guy a theoretical account of 6 degrees of freedom(DOF) cameras plus an answer to the most common error when you implement them. He's got it working now

Hope this helps,
- Raven

Oh and please answer my post about containment tests for concavities. I figured out a fix for the algorithm i talked about, but i wanna check if it's right before i go tearing up my Object-Oriented heirarchy to accomodate it(you know what a pain it could be:)

 
Raven

July 23, 1999, 03:36 PM

charly wrote:
>> Well once again a questions about camera.
>> I haven't yet found a working camera matrix stuff and havent got
>> camera working.
>>
>> I have now working matrix systems and the normal matrix rotation
>> works, but now I need a camera matrix.
>> I think I know the basic idea of camera, but I don't know how to
>> make the matrix.
>>
>> Could somebody tell me how can I make camera matrix from camera
>> position and camera target please?
>>
>> thanks.
>>

Check out my postings about cameras eariler on the message board. the date is 11 Jul 1999 - Sun [02:02PM]. I gave the guy a theoretical account of 6 degrees of freedom(DOF) cameras plus an answer to the most common error when you implement them. He's got it working now

Hope this helps,
- Raven

Oh and please answer my post about containment tests for concavities. I figured out a fix for the algorithm i talked about, but i wanna check if it's right before i go tearing up my Object-Oriented heirarchy to accomodate it(you know what a pain it could be:)

 
charly

July 23, 1999, 07:24 PM



7ig3r wrote:
>>
>>
>>charly wrote:
>>>> Well once again a questions about camera.
>>>> I haven't yet found a working camera matrix stuff and havent got
>>>> camera working.
>>>>
>>>> I have now working matrix systems and the normal matrix rotation
>>>> works, but now I need a camera matrix.
>>>> I think I know the basic idea of camera, but I don't know how to
>>>> make the matrix.
>>>>
>>>> Could somebody tell me how can I make camera matrix from camera
>>>> position and camera target please?
>>>>
>>>> thanks.
>>>>
>>
>>Check out Allegro 3.0/3.1 at www.talula.demon.co.uk. Allegro has code for basic 3d (including 6DOF cameras). I have used some of its code to write my own 3d engine. It is really useful. Although it doesn't really give you much theory, it will provide you with the starter code. I hope this helps.
>>
>>7ig3r

Thank you. I actually know something about the theory, but I'm not quite sertain if I'm
right.. Well if I get working code I might even fully understand the theory..
But thanks for the URL..

charly

 
charly

July 23, 1999, 07:33 PM



Raven wrote:
>>Check out my postings about cameras eariler on the message board. the date is 11 Jul 1999 - Sun [02:02PM]. I gave the guy a theoretical account of 6 degrees of freedom(DOF) cameras plus an answer to the most common error when you implement them. He's got it working now
>>
>> Hope this helps,
>> - Raven
>>
>>Oh and please answer my post about containment tests for concavities. I figured out a fix for the algorithm i talked about, but i wanna check if it's right before i go tearing up my Object-Oriented heirarchy to accomodate it(you know what a pain it could be:)

I checked your posting before I posted this. Well I didn't get that much out of it..
for example I'm not quite sure about the camera's point. I mean I have the point and
the target.. do I build a new point before substracting or what? That is the first thing
I have troubles with.. and the second is the yaw,pitch and roll. Well I know what they are
and what they do etc. but I don't know how do I get them?
Well this would be a good point to say that I'm trying to get camera from 3ds file and use it
in my own engine u know =)
I know I get the angles somehow doing some dot- or cross products, but but..
Well I have a theory(based on one camera code) that I get the angles by doing those products
between the camera and up vector, but I don't know what is the up vector etc.

Hope you can help me more with this =)
thanks anyway..

charly

 
Raven

July 23, 1999, 08:58 PM

>>I checked your posting before I posted this. Well I didn't get that much out of it..
>>for example I'm not quite sure about the camera's point. I mean I have the point and
>>the target.. do I build a new point before substracting or what? That is the first thing
>>I have troubles with.. and the second is the yaw,pitch and roll. Well I know what they are
>>and what they do etc. but I don't know how do I get them?
>>Well this would be a good point to say that I'm trying to get camera from 3ds file and use it
>>in my own engine u know =)
>>I know I get the angles somehow doing some dot- or cross products, but but..
>>Well I have a theory(based on one camera code) that I get the angles by doing those products
>>between the camera and up vector, but I don't know what is the up vector etc.
>>
>>Hope you can help me more with this =)
>>thanks anyway..
>>
>>charly

Ok. You are trying to make a camera look at a point. You have a camera position vector and a target vector and you want the camera to sit at the position and to be rotated in such a way that it's "pointing" or "looking" at the target. Right so far? Ok then

I'm not sure about this i've never done a target based camera but this is how i'd do it(i just made this up). You can take up the idea and adjust it to your needs. Also, this is kinda long. Sorry:)

We've got 2 vectors:

Vp=(Xp, Yp, Zp) Vt=(Xt, Yt, Zt)

Vp is the position vector and Vt is the target vector(they are all vectors since we are talking linear algebra here, so we are in a vector space). Small note, i won't use matrices. They take too long to type. You can convert equations to matrices yourself

I'm assuming the camera has been translated to Vp already, however it's yaw, pitch and roll are 0. Therefore it essentially looks down z(right-handed coordinate system). Now, we build a vector Vc=(Xc, Yc, Zc) which represents the direction the camera is looking in(the up vector). If you have ignored yaw, pitch and roll(ie they are 0) then for a right-handed coordinate system(z increases out of the screen) we have:

Vc=(0,0,-1)

Note this vector is normalized. Now, we have where the camera is looking and we have where it's supposed to look. We need to find the transformation that will make the dotproduct between Vc and Vt zero(ie the angle will be 0). With matrices we have:

Vc*||Aij||=Vt

You have to solve this for ||Aij||(4x4 matrix). This means inverting ||Aij||, taking Vc to be a column vector and Vt a row vector, a matrix product between them, and then your camera rotation matrix will be the inverse of ||Aij||. Too long:) We'll use other methods instead. Lets try breaking up a complex 3D case into 2 simple 2D cases. We project Vc and Vt onto the XZ plane. We now have:

Vcp=(Xc, Zc) Vtp=(Xp, Zp)

Normalize them: Vcp=|Vcp| Vtp=|Vtp|
After normalizing the dotproduct Vcp.Vtp is equal to cos(Theta) where Theta is the angle between the vectors. Needless to say that:

cos(Theta)=Vcp.Vtp => acos(Vcp.Vtp)=Theta

We got one angle. Back to 3D this angle is the rotation about the Y-axis since we projected onto the XZ plane. Rotate the vector Vc(the 3D camera vector) by it. Notice that now we can project it onto the XY plane. If we were to take the XY plane first then the length of Vcp for XY would be 0, since the X and Y components of it are 0! Now we can project it and do the same thing as with XZ. We have:

Vcp=(Xc, Yc) Vtp=(Xp, Yp)

Vcp=|Vcp| Vtp=|Vtp|

acos(Vcp.Vtp)=Theta'

Theta' is the rotation about the(you guessed it!) Z-axis. We can now compose the transformation by concatenating the matrices of rotation:

||Aij|| and ||Bkl|| where ||Aij|| is rotation around Y-axis by Theta and ||Bkl|| is rotation around Z-axis by Theta'. ||Aij||*||Bkl||=||Cmn||

NOTE!!! You HAVE to concatenate ||Aij||*||Bkl||, NOT ||Bkl||*||Aij||. This will be equivalent as taking the XY plane first in the notes above. As i said earlier the length of Vcp on XY is 0! Don't fool yourself by thinking that you already got the angle and the rotation is there. Notice that any rotation of the vector (0,0,-1) around the Z-axis will ALWAYS return (0,0,-1)!!!

Another NOTE!!! The actual camera matrix is ||Cmn||^-1(the inverse of ||Cmn||). This is because if you will perform all this you will find yaw and roll. Pitch was not used in this, and i'm sure we can derive a theorem to say that at least one axis of rotation will not be used to transform this vector(ie we have an ambiguity along one axis, though this is not important). If you have yaw and roll the real matrix will be to rotate by -yaw and -roll! Hence the matrix to be used on the points in world coordinates is ||Cmn||^-1.

NOTE, the revenge!!! The order of concatenation is IMPORTANT!!! Don't forget that. That is the most common mistake about cameras(i did it, the other guy did it, EVERYBODY did it). First goes the translation of the camera from it's position to (0,0,0). Then go the rotations about the X, Y and Z axees(english???). THAT is the order it goes in, and NO OTHER! This might seem like overkill to say but so many people made the mistake you won't believe it.

This is kinda plump for real-time rendering so i would do everything on paper using nothing but the variables stated here and express ||Cmn|| in terms of the original Vt and Vp. This will be a matrix with formulas in it's cells. I would solve the formulas in realtime and get the numbers. All these normilzations and dotproducts might resolve themselves on paper to something simpler, though it's too long so i won't do it myself. Make sure you don't make a mistake though, or you're in for it. Or get a book where the matrix with the formulas is written down for you:)

Good luck,
- Raven

P.S. If you got questions about the math used here email me, otherwise post a message

 
Raven

July 23, 1999, 08:58 PM

>>I checked your posting before I posted this. Well I didn't get that much out of it..
>>for example I'm not quite sure about the camera's point. I mean I have the point and
>>the target.. do I build a new point before substracting or what? That is the first thing
>>I have troubles with.. and the second is the yaw,pitch and roll. Well I know what they are
>>and what they do etc. but I don't know how do I get them?
>>Well this would be a good point to say that I'm trying to get camera from 3ds file and use it
>>in my own engine u know =)
>>I know I get the angles somehow doing some dot- or cross products, but but..
>>Well I have a theory(based on one camera code) that I get the angles by doing those products
>>between the camera and up vector, but I don't know what is the up vector etc.
>>
>>Hope you can help me more with this =)
>>thanks anyway..
>>
>>charly

Ok. You are trying to make a camera look at a point. You have a camera position vector and a target vector and you want the camera to sit at the position and to be rotated in such a way that it's "pointing" or "looking" at the target. Right so far? Ok then

I'm not sure about this i've never done a target based camera but this is how i'd do it(i just made this up). You can take up the idea and adjust it to your needs. Also, this is kinda long. Sorry:)

We've got 2 vectors:

Vp=(Xp, Yp, Zp) Vt=(Xt, Yt, Zt)

Vp is the position vector and Vt is the target vector(they are all vectors since we are talking linear algebra here, so we are in a vector space). Small note, i won't use matrices. They take too long to type. You can convert equations to matrices yourself

I'm assuming the camera has been translated to Vp already, however it's yaw, pitch and roll are 0. Therefore it essentially looks down z(right-handed coordinate system). Now, we build a vector Vc=(Xc, Yc, Zc) which represents the direction the camera is looking in(the up vector). If you have ignored yaw, pitch and roll(ie they are 0) then for a right-handed coordinate system(z increases out of the screen) we have:

Vc=(0,0,-1)

Note this vector is normalized. Now, we have where the camera is looking and we have where it's supposed to look. We need to find the transformation that will make the dotproduct between Vc and Vt zero(ie the angle will be 0). With matrices we have:

Vc*||Aij||=Vt

You have to solve this for ||Aij||(4x4 matrix). This means inverting ||Aij||, taking Vc to be a column vector and Vt a row vector, a matrix product between them, and then your camera rotation matrix will be the inverse of ||Aij||. Too long:) We'll use other methods instead. Lets try breaking up a complex 3D case into 2 simple 2D cases. We project Vc and Vt onto the XZ plane. We now have:

Vcp=(Xc, Zc) Vtp=(Xp, Zp)

Normalize them: Vcp=|Vcp| Vtp=|Vtp|
After normalizing the dotproduct Vcp.Vtp is equal to cos(Theta) where Theta is the angle between the vectors. Needless to say that:

cos(Theta)=Vcp.Vtp => acos(Vcp.Vtp)=Theta

We got one angle. Back to 3D this angle is the rotation about the Y-axis since we projected onto the XZ plane. Rotate the vector Vc(the 3D camera vector) by it. Notice that now we can project it onto the XY plane. If we were to take the XY plane first then the length of Vcp for XY would be 0, since the X and Y components of it are 0! Now we can project it and do the same thing as with XZ. We have:

Vcp=(Xc, Yc) Vtp=(Xp, Yp)

Vcp=|Vcp| Vtp=|Vtp|

acos(Vcp.Vtp)=Theta'

Theta' is the rotation about the(you guessed it!) Z-axis. We can now compose the transformation by concatenating the matrices of rotation:

||Aij|| and ||Bkl|| where ||Aij|| is rotation around Y-axis by Theta and ||Bkl|| is rotation around Z-axis by Theta'. ||Aij||*||Bkl||=||Cmn||

NOTE!!! You HAVE to concatenate ||Aij||*||Bkl||, NOT ||Bkl||*||Aij||. This will be equivalent as taking the XY plane first in the notes above. As i said earlier the length of Vcp on XY is 0! Don't fool yourself by thinking that you already got the angle and the rotation is there. Notice that any rotation of the vector (0,0,-1) around the Z-axis will ALWAYS return (0,0,-1)!!!

Another NOTE!!! The actual camera matrix is ||Cmn||^-1(the inverse of ||Cmn||). This is because if you will perform all this you will find yaw and roll. Pitch was not used in this, and i'm sure we can derive a theorem to say that at least one axis of rotation will not be used to transform this vector(ie we have an ambiguity along one axis, though this is not important). If you have yaw and roll the real matrix will be to rotate by -yaw and -roll! Hence the matrix to be used on the points in world coordinates is ||Cmn||^-1.

NOTE, the revenge!!! The order of concatenation is IMPORTANT!!! Don't forget that. That is the most common mistake about cameras(i did it, the other guy did it, EVERYBODY did it). First goes the translation of the camera from it's position to (0,0,0). Then go the rotations about the X, Y and Z axees(english???). THAT is the order it goes in, and NO OTHER! This might seem like overkill to say but so many people made the mistake you won't believe it.

This is kinda plump for real-time rendering so i would do everything on paper using nothing but the variables stated here and express ||Cmn|| in terms of the original Vt and Vp. This will be a matrix with formulas in it's cells. I would solve the formulas in realtime and get the numbers. All these normilzations and dotproducts might resolve themselves on paper to something simpler, though it's too long so i won't do it myself. Make sure you don't make a mistake though, or you're in for it. Or get a book where the matrix with the formulas is written down for you:)

Good luck,
- Raven

P.S. If you got questions about the math used here email me, otherwise post a message

 
This thread contains 8 messages.
 
 
Hosting by Solid Eight Studios, maker of PhotoTangler Collage Maker.