Author: Kagula
Date: 2010-03-23
本文是文章后半部分所列教材的学习笔记。
How a point from the 3d cartesian space project to window space. I explanation it used Matlab language symantics.I assume the reader be familiar with matlab and linear algebra.
Here I suppose a point is in the 3d cartesian space,other words is in the world space. It’s already undergo the linear transform of some rotation , translation , uniform scales and the shear.
Now I stand in the original point and want view the star in the sky, how can I see it.
Assume the star is a point in the world space I call it Xworld. The same point I see with my eye is in my view space(also name it camera space).How to transform the point from world space to camera space.
First I assume the point in the 4d tuple.The coordinate I defined here is, A Axis Left to right is positive direction I call it x axis. Down to up and perpendicular to x axis is y axis. Another axis perpendicular with x and y axis is z axis,From your eye to the monitor is positive direction. Where they cross is original point,I assigned it to(0,0,0). The fourth component of the point I defined 1,why be 1 I discuss later.
Xworld=[x1,y1,z1,1]’
Xview =[x2,y2,z2,1]’
Mview=[a11 a12 a13 a14;a21 a22 a23 a24;a31 a32 a33 a34]
So the identity should be like this
Xview=Mview*Xworld. (1-1)
Mview is a function how to evaluate it.
You can imagine you stand in the world center of 2d cartesian but how about if you stand left offset 1 unit distance from the original of the x axis. Yes the object like move to right a one unit distance. So x component of the Xview should plus one unit.
I define E is where you stand, So the equality should be like it
Xview = Xworld – E (1-2)
Now I think the matrix Mview=[eye(3) -eye(3)*E;0 1],which from the reference [2].Substituting the symbol with you particular value,you will find it equal to (1-2)
Below snippet is the example,where Ex、Ey、Ez is component of the E
-[1 0 0;0 1 0;0 0 1]*[Ex Ey Ez]’=-[Ex Ey Ez]’
[eye(3) –[Ex Ey Ez]’;0 0 0 1]=[ 1 0 0 –Ex; 0 1 0 –Ey;0 0 1 –Ez;0 0 0 1]=Mview (1-3)
Also show (1-1) with substituting Mview with (1-3) equal to (1-2) left the fourth component of the Xview is 1.
Before dive the paragraph you should know what is the view frustum and what is the orthographic projection,about it reference [2] Page94 Figure 4.19,Page 89.
Step1: Projection
Assume a point P in the view frustum, P project to the point q in the view plane(near plane),we can get the below equation
qx/px = qz/pz => qx = qz*(px/pz) (2-1) then we get the matrix below
Mproj=[ 1 0 0 0;0 1 0 0;0 0 1 0;0 0 1/qz 0] (2-2) OpenGL
Mproj’=[1 0 0 0; 0 1 0 0;0 0 1 1/qz;0 0 0 0] (2-3) DirectX
Step2: To normalized device coordinate
I express it in terms of the six-tuple(l,r,b,t,n,f),denoting the left,right,bottom,top,near and far.l is you can see the most left edge,r is you can see the most right edge,the other is so on.
Before projection,[input-1]we determined the depth we see is near plane from the far plane. [input-2] fovy ,Field of view in the y direction, in radians. [input-3]As pect ratio, The ratio defined as view space width divided by height.
Then we evaluated [Output](l,r,b,t,n,f)
Reference《D3DXMatrixPerspectiveFovLH Function》http://msdn.microsoft.com/en-us/library/bb205350(VS.85).aspx
Because
X_output = 2/(l-r)(n*x_input) (2-4)
Y_output = 2/(b-t)(n*y_input) (2-5)
Z_output = ( n/(f-n) )*(z_input-n) (2-6)
So the matrix is which D3DXMatrixPerspectiveFovLH is.Detail see R1P55Equation(2.68)
The R1P55Equation(2.67) have some complicate,You should be familiar with “Lines Project to Lines”
Below matlab code will show the relationship about the concept
%begin
S=0:1/100:1;
W0 = 0.1;w1=1; %w0 is Near,w1 is Far
S2=(W1.*s)./(w0+(w1-w0).*s) %R1P37Equation(2.44)
Plot(s,s2,’-r’,s,s,’-b’);
Xlabel(‘s’),ylabel(‘s2’);
Axis(‘square’)
grid
%end
Now the objects is in the unit cube,left to right is 1 to -1,top from bottum is 1 to -1,near to far is 0 to 1(DirectX) or -1 to 1(OpenGL)
After perspective divide (Reference 2-1 )the point in 3d space can project to 2d space(also can be windows coordinate)
I have not considered the view frustum screw,because I can not understand it now, whatever in directX9 is no concern.
[1]《3D Game Engine Design-Apractical Approach to Real-Time Computer Graphics》(Second Edition) David H. Eberly 2007
[2]《Real-Time Rendering》(Third Edition) Tomas Akenine-Moller ,Eric Haines,Naty Hoffman 2008
[3]《Linear Algebra:An Interactive Approach》Jain,S.K. , Gunawardena,A.D. 2003
[4]《DirectX Documentation for C++》(Microsoft DirectX SDK (November 2008))45ret