Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 87 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
87
Dung lượng
6,31 MB
Nội dung
Transformations What is a Transformation? • Maps points (x, y) in one coordinate system to points (x', y') in another coordinate system x' = ax+ by + c y' = dx+ ey+ f • Simple Transformations – can be combined Transformations are used • • • • • Position objects in a scene (modeling) Change the shape of objects Create multiple copies of objects Projection for virtual cameras Animations How are Transforms Represented? • Biểu diễn dạng ma trận p' = Mp + t 2D transformations Affine Transformations • Want transformations which preserve geometry – lines, polygons, quadrics • Affine = line preserving – Rotation, translation, scaling – Projection – Concatenation (composition) (Nonuniform) Scale sx � Scale(s x , s y ) � �0 sx � �0 � �0 � sy 1 x 0� � s 1 S � � �0 sy � � � 1 � sy � � 0� sx x � �x � � � � � � � 0� y s y y �� � � �z � �s z � sz � � � � �z � (Nonuniform) Scale • Scale S= S-1 = Shear • Shear = S-1 = Rotations • 2D simple, 3D complicated [Derivation? Examples?] • 2D? x '� � cos � � � � y '� �sin � • Linear • Commutative sin ��� x � �� cos ��� y R(X+Y) = R(X)+R(Y) 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); … 78 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 79 Perspective projection • Characteristic of perspective projection is foreshortening: – The farther an object is from the camera, the smaller it appears in the final image 80 Perspective projection • glFrustum(left, right, bottom, top, zNear, zFar) 81 Perspective projection gluPerspective(fovy, aspect, zNear, zFar) – fovy = 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 82 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: 83 Viewport Transformation 84 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) 85 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 86 Common Transformation Usage • examples of reshape() routine – restate projection & viewing transformations • Usually called when window resized 87 reshape(): Perspective & LookAt public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { GL2 gl = drawable.getGL().getGL2(); if (height == 0) height = 1; // prevent divide by zero float aspect = (float) width / height; // Set the view port (display area) to cover the entire window gl.glViewport(0, 0, width, height); // Setup perspective projection, gl.glMatrixMode(GL_PROJECTION); gl.glLoadIdentity(); glu.gluPerspective(45.0, aspect, 0.1, 100.0); // Enable the model-view transform gl.glMatrixMode(GL_MODELVIEW); gl.glLoadIdentity(); glu.gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); 88 reshape(): Perspective & Translate • Same effect as previous LookAt public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { GL2 gl = drawable.getGL().getGL2(); if (height == 0) height = 1; // prevent divide by zero float aspect = (float) width / height; gl.glViewport(0, 0, width, height); gl.glMatrixMode(GL_PROJECTION); gl.glLoadIdentity(); glu.gluPerspective(45.0, aspect, 0.1, 100.0); gl.glMatrixMode(GL_MODELVIEW); gl.glLoadIdentity(); gl.glTranslatef(0.0, 0.0, -5.0); } 89 reshape(): Ortho public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { GL2 gl = drawable.getGL().getGL2(); double aspect = (double) width / height; double left = -2.5, right = 2.5; double bottom = -2.5, top = 2.5; gl.glViewport(0, 0, width, height); gl.glMatrixMode(GL_PROJECTION); gl.glLoadIdentity(); if (aspect < 1.0) { left *= aspect; right *= aspect; } else { bottom /= aspect; top /= aspect; } gl.glOrtho(left, right, bottom, top, near, far); gl.glMatrixMode(GL_MODELVIEW); gl.glLoadIdentity(); } 90 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 < 91 Reversing Coordinate Projection • Screen space back to world space glGetIntegerv(GL_VIEWPORT, int viewport[4]) glGetDoublev(GL_MODELVIEW_MATRIX, double mvmatrix[16]) glGetDoublev(GL_PROJECTION_MATRIX, double projmatrix[16]) gluUnProject(GLdouble winx, winy, winz, mvmatrix[16], projmatrix[16], int viewport[4], double[] objx, double[] objy, double[] objz) • gluProject goes from world to screen space 92 ... Transformation? • Maps points (x, y) in one coordinate system to points (x'', y'') in another coordinate system x'' = ax+ by + c y'' = dx+ ey+ f • Simple Transformations – can be combined Transformations. .. Mp + t 2D transformations Affine Transformations • Want transformations which preserve geometry – lines, polygons, quadrics • Affine = line preserving – Rotation, translation, scaling – Projection... � � � 27 Transformations in OpenGL 28 Transformations in OpenGL • Modeling • Viewing – orient camera – projection • Animation • Map to screen 29 Camera Analogy • 3D is just like taking a photograph