1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Projection Matrices modified by Ray Wisman Ed Angel Professor of Computer.

Slides:



Advertisements
Similar presentations
Computer Graphics - Viewing -
Advertisements

1 Computer Graphics Chapter 8 3D Transformations.
1 View Shandong University Software College Instructor: Zhou Yuanfeng
Projection Matrices CS4395: Computer Graphics 1 Mohan Sridharan Based on slides created by Edward Angel.
Based on slides created by Edward Angel
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Computer Viewing Ed Angel Professor of Computer Science, Electrical and Computer Engineering,
Projection Matrices Ed Angel
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Representation Ed Angel Professor of Computer Science, Electrical and Computer Engineering,
Objectives Derive the perspective projection matrices used for standard OpenGL projections Derive the perspective projection matrices used for standard.
Viewing Doug James’ CG Slides, Rich Riesenfeld’s CG Slides, Shirley, Fundamentals of Computer Graphics, Chap 7 Wen-Chieh (Steve) Lin Institute of Multimedia.
OpenGL (II). How to Draw a 3-D object on Screen?
1 Chapter 5 Viewing. 2 Perspective Projection 3 Parallel Projection.
CS 480/680 Computer Graphics Representation Dr. Frederick C Harris, Jr. Fall 2012.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
Computer Graphics (fall 2009)
1 Projection Matrices. 2 Objectives Derive the projection matrices used for standard OpenGL projections Introduce oblique projections Introduce projection.
Viewing Angel Angel: Interactive Computer Graphics5E © Addison-Wesley
1 E. Angel and D. Shreiner : Interactive Computer Graphics 6E © Addison-Wesley 2012 Classical Viewing Sai-Keung Wong ( 黃世強 ) Computer Science National.
Chapter 5 Viewing.
C O M P U T E R G R A P H I C S Guoying Zhao 1 / 67 C O M P U T E R G R A P H I C S Guoying Zhao 1 / 67 Computer Graphics Three-Dimensional Graphics III.
Computer Graphics Bing-Yu Chen National Taiwan University.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
CAP 4703 Computer Graphic Methods Prof. Roy Levow Chapter 5.
Demetriou/Loizidou – ACSC330 – Chapter 5 Viewing Dr. Giorgos A. Demetriou Computer Science Frederick Institute of Technology.
CAP4730: Computational Structures in Computer Graphics 3D Transformations.
Computer Graphics I, Fall 2010 Computer Viewing.
Viewing and Projection. 2 3 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009  Introduce the classical views  Compare and contrast image.
Graphics Graphics Korea University kucg.korea.ac.kr Viewing 고려대학교 컴퓨터 그래픽스 연구실.
Review on Graphics Basics. Outline Polygon rendering pipeline Affine transformations Projective transformations Lighting and shading From vertices to.
Chapters 5 2 March Classical & Computer Viewing Same elements –objects –viewer –projectors –projection plane.
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Classical Viewing Ed Angel Professor of Computer Science, Electrical and Computer Engineering,
1 Graphics CSCI 343, Fall 2015 Lecture 18 Viewing III--More Projection.
Classical Viewing Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts University of New Mexico.
Viewing and Projection
Viewing and Projection. The topics Interior parameters Projection type Field of view Clipping Frustum… Exterior parameters Camera position Camera orientation.
Classical Viewing Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts University of New Mexico.
Viewing Angel Angel: Interactive Computer Graphics5E © Addison-Wesley
CS5500 Computer Graphics March 20, Computer Viewing Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts.
1 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Computer Viewing Isaac Gang University of Mary Hardin-Baylor.
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Transformations Ed Angel Professor of Computer Science, Electrical and Computer Engineering,
OpenGL LAB III.
Viewing. Classical Viewing Viewing requires three basic elements - One or more objects - A viewer with a projection surface - Projectors that go from.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
Viewing 고려대학교 컴퓨터 그래픽스 연구실 kucg.korea.ac.kr.
Viewing.
Computer Graphics - Chapter 5 Viewing
Computer Viewing.
Isaac Gang University of Mary Hardin-Baylor
CSC461: Lecture 20 Parallel Projections in OpenGL
Projections and Normalization
Introduction to Computer Graphics with WebGL
CSC461: Lecture 19 Computer Viewing
Introduction to Computer Graphics with WebGL
Introduction to Computer Graphics with WebGL
Introduction to Computer Graphics with WebGL
Isaac Gang University of Mary Hardin-Baylor
Projections and Hidden Surface Removal
Introduction to Computer Graphics with WebGL
Chap 3 Viewing Pipeline Reading:
Introduction to Computer Graphics with WebGL
Introduction to Computer Graphics with WebGL
University of New Mexico
Computer Graphics Computer Viewing
Computer Viewing Ed Angel Professor Emeritus of Computer Science
Viewing and Projection
Viewing and Projection
Presentation transcript:

1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Projection Matrices modified by Ray Wisman Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts University of New Mexico

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 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 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 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Distort (Normalize) (a)Perspective view (b)Orthographic projection of distorted object

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 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 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 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 =

Orthogonal Matrix P = ST = *= Note error in text switching signs

Orthogonal Matrix WRONG P = TS = *= WRONG

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 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 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 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Shear of x =* =* x p =x+y x p =x-2z

16 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 General Shear top view down y axis side view down x axis Oblique projection

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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Normalization Transformation original clipping volume original object new clipping volume distorted object projects correctly

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 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 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 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 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 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 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)

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 Transforms (1,1,-2,1) to (1,1,1,2) After perspective division: (½, ½, ½, 1) After orthogonal projection: (½, ½, 0, 1)

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 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 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 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 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 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) * 2 = 0  = Where  is perspective division by w=0.3

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 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?