Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 60 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
60
Dung lượng
4,69 MB
Nội dung
Transformations Transformations in OpenGL • Modeling • Viewing – orient camera – projection • Animation • Map to screen Camera Analogy • 3D is just like taking a photograph (lots of photographs!) viewing volume camera tripod model Camera Analogy and Transformations • Viewing transformations – tripod–define position and orientation of the viewing volume in the world • Modeling transformations – moving the model • Projection transformations – adjust the lens of the camera • Viewport transformations – enlarge or reduce the physical photograph Coordinate Systems and Transformations • Steps in Forming an Image – – – – Specify geometry (world coordinates) Specify camera (camera coordinates) Project (window coordinates) Map to viewport (screen coordinates) • Each step uses transformations • Every transformation is equivalent to a change in coordinate systems (frames) Affine Transformations • Want transformations which preserve geometry – lines, polygons, quadrics • Affine = line preserving – Rotation, translation, scaling – Projection – Concatenation (composition) Homogeneous Coordinates • Each vertex is a column vector • If a is nonzero, then (x, y, z, w)T and (ax, ay, az, aw)T represent the same homogeneous vertex • A 3D Euclidean space point (x, y, z)T becomes the homogeneous vertex (x, y, z, 1.0)T • As w is nonzero, the homogeneous vertex (x, y, z, w)T corresponds to the 3D point (x/w, y/w, z/w)T • Directions (directed line segments) can be represented with w = 0.0 Vertex transformations • Vertex transformations (rotations, translations, scaling, and shearing) and projections (such as perspective and orthographic) can all be represented by applying an appropriate x matrix to the vertex coordinates • all affine operations are matrix multiplications • all matrices are stored column-major in OpenGL • matrices are always post-multiplied • product of matrix and vector is = Specifying Transformations Programmer has two styles of specifying transformations • Specify matrices (glLoadMatrix, glMultMatrix) • Specify operation (glRotate, glOrtho) – Programmer does not have to remember the exact matrices Programming Transformations • Prior to rendering, view, locate, and orient: – Eye / camera position – 3D geometry • Manage the matrices – Including matrix stack • Combine (composite) transformations 10 Orthographic Projection in OpenGL • This matrix is constructed with the following OpenGL call: glOrtho(left, right, bottom, top, zNear,zFar) • And the 2D version (another GL utility function): gluOrtho2D(left, right, bottom, top) – Just a call to glOrtho() with near = -1 and far = +1 • Usually, the following code is part of the initialization routine: glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(left, right, bottom, top, near, far); glMatrixMode(GL_MODELVIEW); … Perspective Projections • Artists (Donatello, Brunelleschi, and Da Vinci) during the renaissance discovered the importance of perspective for making images appear realistic • Parallel lines intersect at a point Perspective projection • Characteristic of perspective projection is foreshortening: – The farther an object is from the camera, the smaller it appears in the final image 48 Perspective projection • glFrustum(left, right, bottom, top, zNear, zFar) 49 Perspective projection gluPerspective(fovy, aspect, zNear, zFar) – fov = vertical field of view in degrees – aspect = image width / height at near depth – Can only specify symmetric viewing frustums where the viewing window is centered around the –z axis 50 OpenGL Perspective Matrix • Mapping the perspective viewing frustum in OpenGL to clip space involves some affine transformations • OpenGL uses a clever composition of these transformations with the perspective projection matrix: Viewport Transformation Viewport Transformation • The viewport is the rectangular region of the window where the image is drawn • Defining the Viewport – glViewport(GLint x, GLint y, GLsizei width, GLsizei height) Mapping the Viewing Volume to the Viewport • The aspect ratio of a viewport should equal the aspect ratio of the viewing volume – If the two ratios are different, the projected image will be distorted when mapped to the viewport Common Transformation Usage • examples of resize() routine – restate projection & viewing transformations • Usually called when window resized 55 resize(): Perspective & LookAt void resize( int w, int h ) { glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(65.0, (GLfloat) w / h, 1.0, 100.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); } 56 resize(): Perspective & Translate • Same effect as previous LookAt void resize(int w, int h) { glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(65.0, (GLfloat) w/h, 1.0, 100.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0.0, 0.0, -5.0); } 57 resize(): Ortho void resize( int width, int height) { GLdouble aspect = (GLdouble) width / height; GLdouble left = -2.5, right = 2.5; GLdouble bottom = -2.5, top = 2.5; glViewport(0, 0, (GLsizei) w, (GLsizei) h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); if (aspect < 1.0) { left /= aspect; right /= aspect; } else { bottom *= aspect; top *= aspect; } glOrtho(left, right, bottom, top, near, far); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); 58 Additional Clipping Planes • At least more clipping planes available • Good for cross-sections • Modelview matrix moves clipping plane clipped – glEnable(GL_CLIP_PLANEi) – glClipPlane(GL_CLIP_PLANEi, Gldouble[] coeff) Ax + By + Cz + D < 59 Reversing Coordinate Projection • Screen space back to world space glGetIntegerv(GL_VIEWPORT, GLint viewport[4]) glGetDoublev(GL_MODELVIEW_MATRIX, Gldouble mvmatrix[16]) glGetDoublev(GL_PROJECTION_MATRIX, GLdouble projmatrix[16]) gluUnProject( GLdouble winx, winy, winz, mvmatrix[16], projmatrix[16], GLint viewport[4], GLdouble *objx, *objy, *objz) • gluProject goes from world to screen space 60 ... Analogy and Transformations • Viewing transformations – tripod–define position and orientation of the viewing volume in the world • Modeling transformations – moving the model • Projection transformations. .. (screen coordinates) • Each step uses transformations • Every transformation is equivalent to a change in coordinate systems (frames) Affine Transformations • Want transformations which preserve geometry... transformations – adjust the lens of the camera • Viewport transformations – enlarge or reduce the physical photograph Coordinate Systems and Transformations • Steps in Forming an Image – – – – Specify