📄 camera.htm
字号:
<HTML> <HEAD> <TITLE>Viewing Systems for 3d Engines</TITLE> </HEAD> <BODY bgcolor="#FFFFFF" TEXT="#000000" LINK="#666699" ALINK="#000000" VLINK="#666699"> <table bgcolor="#666699" BORDER=0 CELLPADDING=5 CELLSPACING=0 WIDTH=640> <TR> <TD align="left" bgcolor="#666699" width="90%"> <font face="arial" color="#FFFFFF"><b>Viewing Systems for 3d Engines</b></font> </TD> <TD bgcolor = "#000000" align="right"> <a href="math.htm"><font color="#FFFFFF" face="arial"><b>Back</b></a></font></TD> </TR> </table> <center><br> <center> <table border="0" cellpadding="10" cellspacing="0"> <tr> <td> <br><br><pre> Viewing Systems For 3D Engines by Tom Hammersley*Note* A correction to this article can be found at the bottom of thisdocument.---------------------------------------------------------------------------IntroductionOK, so perhaps you have an engine running that has a duck, rotating in themiddle of the screen. Very nice, but not much use for doing 'real' thingswith. You need a camera. You need to be able to walk around, to view theduck at different angles, to zoom in. You need a proper viewing system.---------------------------------------------------------------------------The XYZ Rotation Angle SystemSo you think, I know, I'll make a system where my camera has 3 angles, X, Yand Z rotation. Then I just add on its position. So you build a matrix,apply that transformation to a model, then put it into camera space. Itseems to work well... but it's rather limited. Specifying an orientation interms of X Y and Z angles is tricky, and can cause problems. For example,once you start rotating around a little, a rotation in the X axis may nothave yield the results you expected. You may also experience "Gimbal Lock",where rotations can cancel each other out, and so on. In short, this systemis crap.---------------------------------------------------------------------------The UVN Vectors SystemThis is quite a good system. It works by defining 3 vectors, U, V and N.These vectors are right, up, and viewing direction, respectively. If youtake your left hand, point your index finger forwards, stick your thumb upto the sky, and point your middle finger to the right, then your middlefinger is U, your thumb is V, and your index finger is N. Imagine thatthese vectors orientate the viewing system. So that to rotate your 'head'to the left, you would rotate about the V vector, to roll, you would rotateabout N, and so on. Also needed to use this system is an origin, the pointof the camera. To use this system, you will need to build a viewing matrix.This is calculated by multiplying together two matrices. The system isgiven by:Translation matrix =| 1 0 0 0 || 0 1 0 0 || 0 0 1 0 || -x -y -z 0 |Rotation matrix =| Ux Vx Nx 0 || Uy Vy Ny 0 || Uz Vz Nz 0 || 0 0 0 1 |CameraMatrix = Translation*RotationN can also be calculated as:a = azimuth anglee = elevation angleNx = sin(e)*cos(a)Ny = sin(e)*sin(a)Nz = cos(e)To point at an arbitrary position, you will then need to define N as theunit vector between the camera and the point. U and V will be guessed at,and adjusted. To adjust the vector, simply do:V = V' - (V'*N)*NWhere V' is your guess at V. U is the cross product of N and V.---------------------------------------------------------------------------Two Points And A TwistThis is another useful system. The camera is defined at one point, and ithas a focus point. A twist angle is given, which is the angle about F - C(focus - camera). To build the matrix for this system, you'll need to dothe following:a, b, c = Normalized direction XYZe, f, g = Camera point XYZa = twist angle | cos(a) sin(a) 0 0 |V1 = | -sin(a) cos(a) 0 0 | | 0 0 1 0 | | 0 0 0 1 | | b -a | | ------------- ------------- a 0 | | (1 - c^2)^1/2 (1 - c^2)^1/2 | | | | -ac -bc |V2 = | ------------- ------------- b 0 | | (1 - c^2)^1/2 (1 - c^2)^1/2 | | | | 0 (1 - c^2)^1/2 c 0 | | 0 0 0 1 | | 1 0 0 0 |V3 = | 0 1 0 0 | | 0 0 1 0 | | -e -f -g 1 |Tview = V1*V2*V3Watch out for when c^2 is 1. Then you get (1 - 1)^1/2, which is zero - anexception. This'll need a special case.Understanding How The Camera Model WorksI've had a few people talking to me on IRC, with difficulty understandinghow this system works. Well, I'll give you a full explanation, by breakingit all down into small pieces, and re-assembling it into a way you canunderstand.Part #1: UVN VectorsThis part alone has caused quite a lot of confusion. In short, UV and N are3 vectors that are used to orientate the XY and Z values of your points. Uis for the X axis, V is for the Y axis, N is for the Z axis.Now, we all may go around, merrily saying that a point is at (1, 2, 3). Butwhat do we really mean when we say this? Simple. The point is 1 unit fromthe origin in the X AXIS, the point is 2 units from the origin in theY AXIS, and the point is 3 units from the origin in the Z AXIS. Why have Iemphasised the term axis? Because that is the key to understanding therotation.When I say "X axis", what does that mean? Well, an axis can be consideredas a unit vector - a direction. An axis is infinite is usually infinite ineach direction. The X axis has the value (1, 0, 0). Now, imagine that toget a points X co-ordinate in camera space, we have to map a point onto theX axis. We would end up with something like:NewPoint.X = XAxis.x*Point.x + XAxis.y*Point.y + XAxis.z*Point.zWhich simplfies down to:NewPoint.X = 1*Point.x + 0*Point.y + 0*Point.zCan you see what has occured? We don't just take the X value, we take thedistance from the origin along the X axis. Now, as the X axis can beorientated in an arbitrary manner, then as the axis rotates, the pointtakes on new values: XY and Z are being combined in different ratios.Recall that we have similar vectors from V and N - the Y and Z axes. Again,a similar approach is taken to them: not simply taking the Y orZ co-ordinate, but taking the distance from the origin along an axis. It isfundamental that you understand that part.Now, UV and N are always perpendicular to each other, ie they will form thecorner of a cube, regardless of rotation, the angles between each otherwill remain the same; just the orientation changes. To visualise this, takeyour left hand. Point out your index finger, stick your thumb upwards tothe sky, and stick your middle finger out to the right. Your index fingeris N, your thumb V, and your middle finger U. Rotate your hand a bit. Asyou can see, the vectors rotate around, changing the rotation of thecamera, but still remain at the same angle to each other.Camera PositionCamera position has also given a little trouble. What you have to rememberis that camera position is specified in world co-ordinates, and when weperform the translation, we map the world back to the camera, not map thecamera into the world. So, when we translate, the world will be movedtowards the camera. To do that, we need to translate by a negative vector.And that vector is the camera position. Going back to my "hand" metaphor,imagine that the position of your hand is the position of the cameraSpace, (0, 0, 0), And Other Such ThingsConsider what happens when we apply -camera.pos to every co-ordinate in thespace. They all get translated by the same amount, including the camera,which gets placed at (0, 0, 0) -- the origin. So, you can think of thecamera position as being the origin of the world. However world spaceco-ordinates are not defined relative to that point, they are definedrelative to (0, 0, 0) (NOT THE CAMERA POINT MIND!). So, the translation by-camera.pos moves the co-ordinates to be relative to the camera; in fact,you could make any point the centre of the world using such a system.Also, consider what the rotation is doing. Try to think not so much of thecamera itself rotating; rather the world surrounding it rotating, beingbackward mapped into the universe. IE we don't take the horse to market, webring the market to the horse.I hope this explains it clearly enough for you; bear in mind though thatI can't put the thoughts directly into your brain, you have to work out thebits inbetween for yourself!Tom Hammersley, tomh@globalnet.co.ukThis information was derived from 3D Computer Graphics, by Alan Watt---------------------------------------------------------------------------Correction:Visit the following document :http://www.tc.cornell.edu/Visualization/Education/cs417/They are the ones that posted the sheet. I have tried to contact Alan Wattat the University of Sheffield but to no avail - I can't find his emailaddress to ask him specific questions about the matrix in question. The article Tom posted contains information from pages 61-63 of "3dcomputer graphics by Alan Watt" - and the matrix on p62 which is in Tom'sarticle is incorrect. I have run it by several people I used to work with( all in the computer graphics industry ) and have tried several ways toprove it myself - no one could come up with the same matrix. If this matrix truely represented a "rotation" then it would beorthonormal and the cross product of any two column vectors would yeildthe third column vector of the matrix. The matrix on p.62 of "3dcomputer graphics by Alan Watt" which appears in Tom's article does notadhear to this property therefore does not represent a rotation and isincorrect. I have looked for a possible answer to this matrix presented onp.62 for the last year wondering if there was something perhaps I wasmissing but after taking it to several people, looking in several computergraphics and mathematics books, and trying to prove it to myself - I havecome to the conclusion it is wrong - the final blow came when someone onthe usenet told me about the erata sheet at cornell - once I saw that, itput the nail in the coffin - and ended my doubt. I just wonder howsuch a major mistake made it into a book - which is why I kept trying tofigure it out for so long. I'm posting this because I don't want anyone to go through the frustrationthat I have gone through trying to prove the stupid thing. Conclusion -books aren't perfect. Oh well.I hope this helps,Don ;)--------------------------------------------------------------------------Corrections from the book 3D Computer Graphics, by Alan WattErrata for Watt, "3D Computer Graphics"Second Edition, Addison WesleyThe bulleted numbers refer to page number.18 The first equation on the page has two mistakes. V^2 should read |V|^2. Also VW should read V dot W.60 The third row, second column entry of T_view should be sin(phi) not sin(theta).62 The first 4x4 matrix is incorrect. It is not orthonormal and thus cannot be the product of two rotations.72 The second constraint in the middle of the page should read: "Normalizing the range of zs values so that the range of d<=zv<=f maps into the range 0<=zs<=1."73 Matrix equation (3.1) has an incorrect last COLUMN. It should read [0,0,1,0].100 H=(L+V)/2 should read H=norm(L+V).116 Equation (4.8) should have a set of brackets so that the "1/2" multiplies the entire expression.117 Figure 4.27 is confusing at best. I think that the N vector should be H and the R vector should be V.118 The third equation on the page gives mu as a function of Fo. The mu should be eta, the index of refraction. The equation is just a rearranged version of the one above it.118 The second numbered bullet should read: "These values of eta ... "120 Note that in the code the index of refraction is refered to as "mu".130 In figure 5.4 the notation "Nt dot (P(t)-X)<0 implies S in inside" should read "F is inside".138 In Equation (5.2), the last "y2" in the equation for Nb should be "ys". The "Ns" on the right of the equation for Ns should be "Na".187 In Figure 6.21 the parametric directions u and v are reversed. ----------------------------------------------------------------------</pre> </td> </tr> </table> </center> </BODY> </HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -