蔡侑庭 (Yu-Ting Tsai) Department of Computer Science National Chiao Tung University, Taiwan Prof. Wen-Chieh Lin s CG Slides OpenGL 2.1 Specification OpenGL Programming Guide, Chap. 3 & Appendix F
2 OpenGL Buffers Five Basic Steps for GLUT Initialize GLUT Initialize Buffers Create Display Window Set Event Routines Start Event Parsing Loop GLUT Event Routines
3 Sample Code http://caig.cs.nctu.edu.tw/course/cg2007/files/ week_ 3.zip void MyDisplay() {... } void MyReshape( int nwidth, int nheight ) {... }
4 Eye Coordinates Normalized Device Coordinates Clip C di t Coordinates
5 y y z x x Left-Handed Coordinates z Right-Handed Coordinates OpenGL
6 Unified Way to Treat OpenGL Coordinates In most cases, w o is 1 (default value) Camera is always at the origin [0, 0, 0, 1] T Viewing direction always points toward negative z-axis Transformations are applied only to object coordinates Vice versa
7 Transformation matrices remain in effect until you change them OpenGL is a state t machine Call glloadidentity to reset transformation matrices if needed Each transformation at o function call Multiplies its result with current matrix Changes current matrix
8 g glmatrixmode( ( GL_MODELVIEW ); glloadidentity(); gltranslate( tx, tx ty, ty tz ); glrotate( θ, rx, ry, rz ); glscale( sx, sy, sz ); Eye Coordinates
9 Transformations are applied to object coordinates in reverse order glmatrixmode( GL_MODELVIEW ); gltranslate( tx, ty, tz ); glrotate( θ, rx, ry, rz ); glscale( sx, sy, sz ); glbegin( GL_POLYGON ); glend();
10 Rotations are applied to object coordinates with respect to the origin gltranslate( tx, ty, tz ); glrotate( θ, rx, ry, rz ); glrotate( θ, rx, ry, rz ); gltranslate( tx, ty, tz ); glbegin( GL_POLYGON ); glbegin( GL_POLYGON ); y y x x z z
11 g glmatrixmode( ( GL_PROJECTION ); glloadidentity(); glortho( l, l rr, b b, tt, n n, f ); [ glfrustum( l, r, b, t, n, f ); ] [ gluperspective( θ, aspect, n, f ); ] Eye Coordinates Clip Coordinates
12 glortho( l, r, b, t, n, f ) y l t z Camera x b n r f
13 glfrustum( l, r, b, t, n, f ) y l t z Camera x b n r f
14 gluperspective( θ, aspect = w / h, n, f ) y w z Camera x θ n h f
15 Automatically Performed by OpenGL (Graphics Hardware) Clip Coordinates Normalized Device Coordinates
16... gldepthrange( l, l u ); glviewport( x, y, w, h ); Normalized Device Coordinates
17 glviewport( x, y, w, h )& gldepthrange( l, u ) h [x, y] w
18 glviewport( x, y, w, h )& gldepthrange( l, u ) The y-axis of window coordinates points upward Normalized device coordinates [-1, 1] [-1, 1] [-1, 1] Mapped to [x, x + w] [y, y + h] [l, u]
19 Uniform Spacing (Orthographic) v e [-n, -f] v c [-1, 1] v d [-1, 1] v w [l, u] Camera
20 Non-Uniform Spacing (Perspective) v e [-n, -f] v c [-n, f] v d [-1, 1] v w [l, u] Camera
21 OpenGL Coordinate System Right-Handed Coordinates Basic OpenGL Transformations Modelview Transformations Translation, rotation, & scale Projection Transformations Orthographic & perspective projection Viewport Transformation
22 More on OpenGL Transformations Discussion on Assignment I Recitation of Selected Topics?
23 How to achieve non-uniform depth spacing for orthographic projection?
24 How to achieve uniform depth spacing for perspective projection?
25 Prof. Wen-Chieh Lin s CG Slides OpenGL Specifications http://www.opengl.org/documentation/specs/ OpenGL Programming Guide, Chap. 3 & Appendix F Sudhansu K. Semwal s OpenGL Tutorial http://www.cs.uccs.edu/~semwal/indexgltutorial. html
26 Thank You! Any Questions?