Total 3D Navigation/Camera

Started by
1 comment, last by jeffersonalegria 19 years, 10 months ago
Please...Help me I need one sample or link or anything about 3D rotation and camera navigation (like Descent game) using the glLookAt and the UpVector. thanks... Jefferson
Advertisement
It isn''t too complex, if you have a fair knowledge of basic vector math. A rotation matrix can be interpreted as an orientation matrix. If this orientation matrix held a cameras orientation, it could be encoded as a 3x3 matrix like this:
<br>[v]<br>[n]<br>where u, v, and n are 3-vectors (x-y-z). If the matrix where held at the origin having not moved and pointing in the "default" direction in your 3d space, it would basically be the i, j, and k vectors, <1,0,0>, <0,1,0>, and <0,0,1>, respectively. If you rotate this camera, you''re basically rotating the i-j-k axes into a new orientation. The knew orientation is encoded in the u, v, and n vectors, where u is right, v is up, and n is forward. Sometimes, you can automatically generate an oriention for a camera given an up vector and a target to look at. This is how the look-at function is thusly named. Basically, there are two vector operations involved, subtraction and the cross product. Vector subtraction yields a vector pointing in the direction toward the first operand, from the second, reading left-to-right.<br>a-b = (a0-b0, a1-b1, a2-b2)<br>and geometrically:<br>a<——–b<br>The cross product returns a vector that is perpendicular to the two vectors crossed (mathmatically denoted axb or a^b). This is useful for defining a plane given the three vertices of a triangle or for making your camera look at something like you''re doing. The definition of cross product you''ll have to teach yourself. But given the above abstract definitions, you should be able to understand the operations involved in the following piece of code:<br> <!–STARTSCRIPT–><pre class="source"><br><font color=gray>//returns camera matrix <br></font><br><font color=gray>//note that you''ll also need the position point of your camera<br></font><br><font color=gray>//which is not part of the 3x3 matrix but part of the same struct<br></font><br>matrix3 look_at(vector3 &up, vector3 &cam_pos, vector3 &target)<br>{ <br> vector3 forward = target - cam_pos; <font color=gray>//you defined overloaded <br></font><br> <font color=gray>//operator -<br></font><br> vector3 right = forward ^ up; <font color=gray>//you defined overloaded <br></font><br> <font color=gray>//operator ^ (cross).<br></font><br> <font color=gray>//^ is not commutative, so<br></font><br> <font color=gray>//up ^ forward would return<br></font><br> <font color=gray>//"left" vector.<br></font><br> <font color=gray>//redefine up vector since it may or may not be perpendicular<br></font><br> <font color=gray>//to found forward vector:<br></font><br> up = right ^ forward;<br> <br> <font color=gray>//there are many places that you can choose to normalize your<br></font><br> <font color=gray>//uvn vectors at. Right now would work. Note that this func<br></font><br> <font color=gray>//inputs an up vector that''s not necessarily of unit length <br></font><br> <font color=gray>//like j (&lt;0,1,0&gt;) is. Unit length vectors are required for<br></font><br> <font color=gray>//some operations like rotating objects by multiplying their <br></font><br> <font color=gray>//their vertices by this camera orientation. This is where <br></font><br> <font color=gray>//judgement based on knowledge of vector math comes into play.<br></font><br> <br> <font color=green>#ifdef</font> NORMALIZE_AFTER_MATH <font color=gray>//your choice<br></font><br> normalize(up);<br> normalize(right);<br> normalize(forward);<br> <font color=green>#endif</font><br> <br> <font color=gray>//return temporary object of look-at matrix!<br></font><br> <font color=blue>return</font> matrix3(right, up, forward); <font color=gray>//you defined matrix3<br></font><br>}<br></pre><!–ENDSCRIPT–><br>I don''t know if that code works, lol <img src="tongue.gif" width=15 height=15 align=middle> <br> <br>edit: damn source tags<br><br>
"I study differential and integral calculus in my spare time." -- Karl Marx
Thank you, very much.....

This topic is closed to new replies.

Advertisement