Download presentation
Presentation is loading. Please wait.
Published byRandolph Armstrong Modified over 9 years ago
1
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Projection Matrices modified by Ray Wisman rwisman@ius.edu Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts University of New Mexico
2
2 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Objectives Derive the projection matrices used for standard OpenGL projections Introduce oblique projections Introduce projection normalization
3
3 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Normalization Rather than derive a different projection matrix for each type of projection, we can convert all projections to orthogonal projections with the default view volume Default view volume (or canonical view volume): 2x2x2 cube centered at origin left, bottom, near = -1, -1, -1 right, top, far = 1,1,1 This strategy allows us to use standard transformations in the pipeline and makes for efficient clipping
4
4 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 OpenGL Orthogonal Parallel Viewing glOrtho(left,right,bottom,top,near,far) near and far measured from camera
5
5 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Distort (Normalize) (a)Perspective view (b)Orthographic projection of distorted object
6
6 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Pipeline View modelview transformation projection transformation perspective division clippingprojection nonsingular 4D 3D against default cube 3D 2D
7
7 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Notes We stay in four-dimensional homogeneous coordinates through both the modelview and projection transformations Both these transformations are nonsingular Default to identity matrices (orthogonal view) Normalization lets us clip against simple cube regardless of type of projection Delay final orthogonal projection until end Important for hidden-surface removal to retain depth information as long as possible
8
8 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Orthogonal Projection Normalization glOrtho(left,right,bottom,top,near,far) glOrtho(-1, 1, -1, 1, -1, 1) normalization find transformation to convert specified clipping volume to default Mapping a view volume to the canonical view volume.
9
9 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Orthogonal Matrix Two steps Translate view cube center to origin T(-(left+right)/2, -(bottom+top)/2,(near+far)/2)) Scale to have sides of length 2 S(2/(left-right),2/(top-bottom),2/(near-far)) P = ST =
10
Orthogonal Matrix P = ST = *= Note error in text switching signs
11
Orthogonal Matrix WRONG P = TS = *= WRONG
12
12 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Orthogonal Matrix * = Near and far are distances measured from the camera. glOrtho(2, 6, 4, 6, 3, 5) glVertex3f( 4, 5, 4)
13
13 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Final Orthogonal Projection Set z =0 Equivalent to the homogeneous coordinate transformation Hence, general orthogonal projection in 4D is M orth = P = M orth ST
14
14 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Oblique (Slanting) Projections The OpenGL projection functions cannot produce general parallel projections such as oblique Only those that are orthogonal to projection plane However if we look at the example of the cube it appears that the cube has been sheared Oblique Projection = Shear + Orthogonal Projection
15
15 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Shear of x =* =* x p =x+y x p =x-2z
16
16 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 General Shear top view down y axis side view down x axis Oblique projection
17
17 Shear Matrix top view side view What is x p when = 90? What happens to x p as increases 90 to 135? What happens to x p as x increases? What happens to x p as z is more negative? cot(45)=cot( /4)=1 cot(90)=cot( /2)=0 cot(135)=cot(3 /4)=-1
18
18 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Shear Matrix xy shear ( z values unchanged) Projection matrix General case (canonical view volume): H( , ) = P = M orth H( , ) P = M orth STH( , )
19
19 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Shear Matrix Effect H( , ) = P = M orth H( , ) Implement oblique projection by first shearing objects then an orthographic projection. Must insert projection normalization for canonical view volume. M orth P = M orth STH( , )
20
20 Shear Matrix Effect =*=* Effect is x p = x - z 2x2x2 cube centered at origin with red front, right blue sides (1,1,1) (0,1,1) cot(45)=cot( /4)=1 cot(90)=cot( /2)=0 cot(135)=cot(3 /4)=-1 Give x p for x = (1,1,-1).
21
21 Shear Matrix Effect =*=* Effect is: x p = x - z y p = y - z 2x2x2 cube centered at origin with red front, right blue sides. (1,1,1) (0,0,1)
22
22 Equivalency Oblique view volume transformed by Volume becomes orthogonal to view plane Cube becomes oblique by same transformation Still needs normalized (scaled and translated) P = M orth H( , ) What we want OpenGL to do.
23
23 Effect on Clipping The projection matrix P = STH transforms the original clipping volume to the canonical (default) view volume top view DOP near plane far plane object clipping volume z = -1 z = 1 x = -1 x = 1 distorted object (projects orthogonally correctly)
24
24 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Simple Perspective Consider a simple perspective with the COP at the origin, the near clipping plane at z = -1, and a 90 degree field of view determined by the planes x = z, y = z
25
25 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Simple Perspective COP at the origin, pointing to negative z Projection plane z = d, d < 0 Clips values > d d=-1 clips values > -1
26
26 Perspective Equations Consider top and side views with origin COP x p =Similarly: y p =z p = d COP Slope=tan( ) tan(45°)=1. How are x p, y p affected by values of z?
27
27 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 General Perspective Transformation consider q = Mp where M = q = Mp = Perspective division by w=z/d = Mp = =
28
28 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Perspective Transformation to d=-1 plane Simple projection matrix to d=-1 in homogeneous coordinates Note that this matrix is independent of the far clipping plane and w’=z*(-1), front clipped M = * =
29
29 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Perspective Normalization Simple perspective not normalized to canonical clipping volume Will find values of and transforming perspective projection into canonical clipping volume N = M =
30
30 Perspective Normalization N = after multiplication by N, the point (x, y, z, 1) goes to x’ = x y’ = y z’ = z+ w’ = -z After dividing by w’ x’’ = -x/z y’’ = -y/z z’’ = -( + /z) which projects orthogonally to the desired point (z=0) regardless of and
31
31 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Math Example with arbitrary and x’ = x y’ = y z’ = z+ w’ = -z x’’ = -x/z y’’ = -y/z z’’ = -( + /z) w’’ = -w’/z * = Divide by w’ =-1 =3 (1,1,1) transformed to (-1,-1,0) for display. x’’ = -1/1 = -1 y’’ = -1/1 = -1 z’’ = -( + /1) = -2 w’’ = w’/z = -(-1/1) = 1
32
32 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Picking and If we pick = = the near plane is mapped to z = 1 the far plane is mapped to z = -1 and the sides are mapped to x = 1, y = 1 Hence the new clipping volume is the default or canonical clipping volume
33
33 Picking and =N= Projection normalization to a canonical viewing volume = = Note: the author differs from OpenGL and contradicts himself in the text but this still captures the spirit of the transformation.
34
34 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Picking and =N= Perspective projection normalization to a canonical viewing volume = = Scales z from original clipping volume Translates z from original clipping volume
35
35 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Normalization Transformation original clipping volume original object new clipping volume distorted object projects correctly
36
36 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Normalization and Hidden-Surface Removal Although our selection of the form of the perspective matrices may appear somewhat arbitrary, it was chosen so that if z 1 > z 2 in the original clipping volume then the for the transformed points z 1 ’ > z 2 ’ Thus hidden surface removal works if we first apply the normalization transformation However, the formula z’’ = -( + /z) implies that the distances are distorted by the normalization which can cause numerical problems especially if the near distance is small
37
37 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 OpenGL Perspective glFrustum(left,right,bottom,top,near,far) glFrustum allows for an unsymmetric viewing frustum (although gluPerspective does not)
38
38 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 OpenGL Perspective glFrustum(left,right,bottom,top,near,far) glFrustum allows for an unsymmetric viewing frustum (although gluPerspective does not) glFrustum(-1,4,-1,1,2,10); glFrustum(-1,1,-1,1,2,10); glFrustum(-1,1,-1,4,2,10);
39
39 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 OpenGL Perspective Matrix The normalization in glFrustum requires an initial shear H to form a right viewing pyramid Followed by a scaling S to get the normalized perspective volume. Finally, the perspective matrix results in needing only a final orthogonal transformation P = NSH our previously defined perspective matrix shear and scale H( , ) = H(cot -1 (left+right/-2far), cot -1 (top+bottom/-2far))
40
40 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Authors’s Perspective Matrix =N= S= P=NSH= H= Scale x,y to cube size 2 Perspective projection normalization to a canonical viewing volume Skew x,y into a symmetric frustum Perspective viewing transformation
41
41 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 OpenGL Perspective Matrix P OpenGL = The author’s approach is better for understanding the derivation of the perspective or projection matrix. The following is that used by OpenGL.
42
42 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 OpenGL Perspective Matrix Example The following Octave is that used by OpenGL. function result=MOpenGL(l,r,t,b,n,f) result=[ (2*abs(n))/(r-l),0,(r+l)/(r-l),0; 0,(2*abs(n))/(t-b),(t+b)/(t-b),0; 0,0,(abs(n)+abs(f))/(abs(n)-abs(f)),(2*abs(n)*abs(f))/(abs(n)-abs(f)); 0,0,-1,0]; endfunction MOpenGL(-1, 1,-1,1,1,3) 1 0 0 0 0 1 0 0 0 0 -2 -3 0 0 -1 0
43
43 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 OpenGL Perspective Matrix Example glFrustum(left, right, bottom, top, near, far); The following was generated by the OpenGL: glMatrixMode(GL_PROJECTION); glLoadIdentity(); glFrustum(-1, 1, -1, 1, 1, 3); glGetFloatv(GL_PROJECTION_MATRIX, pm); printf("Projection Matrix\n"); for(int i=0;i<4;i++){ for(int j=0;j<4;j++){ printf("%f\t",pm[4*j+i]); } printf("\n"); } Projection Matrix 1 0 0 0 0 1 0 0 0 0 -2 -3 0 0 -1 0 Transforms (1,1,-2,1) to (1,1,1,2) After perspective division: (½, ½, ½, 1) After orthogonal projection: (½, ½, 0, 1)
44
44 OpenGL – Two equivalent examples GLfloat P[4][4] = { {1,0, 0, 0}, {0,1, 0, 0}, {0,0, -2, -3}, {0,0, -1, 0}}; GLfloat T[4][4] = { {1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1,-3}, {0, 0, 0, 1}}; GLfloat ma[16]; glMatrixMode(GL_PROJECTION); glLoadIdentity(); Transpose(P, ma); glMultMatrixf(ma); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); Transpose(T, ma); glMultMatrixf(ma); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glFrustum(-1,1,-1,1,1,3); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0,0,-3);
45
45 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Why do we do it this way? Normalization allows for a single pipeline for both perspective and orthogonal viewing We stay in four dimensional homogeneous coordinates as long as possible to retain three-dimensional information needed for hidden-surface removal and shading We simplify clipping
46
46 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Shadows Perspective projection can create shadows when surface is essentially flat or far away Light source at: x l,y l,z l Shadow at y=0 Method: Draw object (optional). Translate light to origin: T(-x l,-y l,-z l ) Multiply by perspective projection Translate back: T(x l, y l, z l ) Set shadow color Draw object in shadow color
47
47 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Shadows with light source above Perspective projection y l controls perspective of shadow Translate shadow to origin = MT(-x l,-y l,-z l )T(x l,y l,z l ) M=M=
48
48 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Shadows with light source above [x, y, z, 1] T =MT(-x l,-y l,-z l )T(x l,y l,z l ) = T(0,3,0)MT(0,-3,0)[2,2,2,1] T =[6,0,6,1] T Transformation of [x, y, z, 1] T What effect on x p of increasing y l ?
49
49 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Shadows with light source above [x, y, z, 1] T =MT(-x l,-y l,-z l )T(x l,y l,z l ) = T(0,3,0)M(3)T(0,-3,0) 1.0 0.0 0.0 0.0 2 2 6 0.0 0.0 0.0 0.0 * 2 = 0 0 0.0 0.0 1.0 0.0 2 2 6 0.0 -0.3 0.0 1.0 1 0.3 1 = Where is perspective division by w=0.3
50
50 Shadows Directly beneath Cube projected to y=0 GLfloat yl=3.0, ma[16], M[4][4] = { {1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}, {0,-1/yl, 0, 0}}; glPushMatrix(); glTranslatef(0, 2, 0); // Elevate cube(); glPopMatrix(); Transpose(M, ma); glTranslatef(0,yl,0); // 3) Return glMultMatrixf(ma); // 2) project glTranslatef(0,-yl,0); // 1) light y=0 cube(); // Project cube What is the effect on the shadow by increasing yl?
51
51 Shadows on Left Cube projected to x=0 GLfloat xl=2.0, ma[16], M[4][4] = { {1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}, {-1/xl, 0,0, 0}}; glPushMatrix(); glTranslatef(2, 0, 0); // Right cube(); glPopMatrix(); Transpose(M, ma); glTranslatef(xl,0,0); // 3) Return glMultMatrixf(ma); // 2) project glTranslatef(-xl, 0,0); // 1) light x=0 cube(); // Project cube What is the effect on the shadow by increasing xl?
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.