abcd869d157e1655b07c146e0ebdb277.ppt
- Количество слайдов: 41
Open. GL Computer Graphics Programming with Transformations
Topics Transformations in Open. GL l Saving Current Transformation l Drawing 3 D Scenes with Open. GL l Open. GL Functions for Modeling and Viewing l 2
Transformations in Open. GL CT: current transformation l Simplified graphics pipeline l V CT V Window-to-Viewport Transformation Q Model (Master) Coordinate System l Q World Window World Coordinate System S S Viewport Screen Coordinate System Open. GL maintains so-called modelview matrix • Every vertex passed down the graphics pipeline is multiplied by this matrix 3
Transformations in Open. GL l Open. GL is a 3 D graphics package • Transformations are 3 D l How does it work in 2 D? • 2 D drawing is done in the xy-plane, z y coordinate is 0. • Translation: dz = 0 • Scaling: Sz = 1 • Rotation: z-roll z x 4
Transformations in Open. GL l Fundamental Transformations • Translation: gl. Translated(dx, dy, dz) for 2 D: gl. Translated(dx, dy, 0) • Scaling: gl. Scaled(sx, sy, sz) for 2 D: gl. Scaled(sx, sy, 1. 0) • Rotation: gl. Rotated(angle, ux, uy, uz) for 2 D: gl. Rotated(angle, 0, 0, 1) l Transformations does not set CT directly, a matrix is postmultiplied to CT • CT = CT M 5
Transformations in Open. GL l Canvas functions • void Canvas: : init. CT(void) { gl. Matrix. Mode(GL_MODELVIEW); gl. Load. Identity(); } • void Canvas: : scale 2 D(double sx, double sy) { gl. Matrix. Mode(GL_MODELVIEW); gl. Scaled(dx, dy, 1. 0); } 6
Transformations in Open. GL l Canvas functions • void Canvas: : translate 2 D(double dx, double dy) { gl. Matrix. Mode(GL_MODELVIEW); gl. Translated(dx, dy, 0); } • void Canvas: : rotate 2 D(double angle) { gl. Matrix. Mode(GL_MODELVIEW); gl. Rotated(angle, 0. 0, 1. 0); } 7
Transformations Example l Draw a house. Draw another house by rotating it through -30° and then translating it through (32, 25) • cvs. init. CT(); house(); cvs. translate 2 D(32, 25); cvs. rotate 2 D(-30. 0); house(); 8
Transformations Example 9
Transformations Example l Think of it in two different ways • Q =T(32, 25)R(-30)P CT = CT T(32, 25) R(-30) • Translate the coordinate system through (32, 25) and then rotate it through – 30° l The code generated by these two ways is identical. 10
Saving Current Transformation We can save and restore CTs using gl. Push. Matrix() and gl. Pop. Matrix() l Manipulation of a stack of CT l Before After push. CT() After pop. CT() After rotate 2 D() = CT 3 Rot CT 4 CT 3 CT 2 CT 1 11
Saving Current Transformation l Canvas functions • void Canvas: : push. CT(void) { gl. Matrix. Mode(GL_MODELVIEW); gl. Push. Matrix(); } • void Canvas: : pop. CT(void) { gl. Matrix. Mode(GL_MODELVIEW); gl. Pop. Matrix(); } 12
Saving CT Examples • Master coordinate system: where an object is defined • Modeling transformation: transforms an object from its master coordinate system to world coordinate system to produce an instance • Instance: a picture of an object in the scene 13
Drawing 3 D Scenes with Open. GL The concept of “camera” (eye) is used for 3 D viewing l Our 2 D drawing is a special case of 3 D drawing y far plane l view volume near plane z eye x window Viewport 14
Drawing 3 D Scenes with Open. GL l Camera to produce parallel view of a 3 D scene 15
Drawing 3 D Scenes with Open. GL l Simplified Open. GL graphics pipeline VM modelview matrix P projection matrix clip Vp viewport matrix 16
Drawing 3 D Scenes with Open. GL l Modelview matrix = CT • Object transformation + camera transformation • Applying model matrix M then viewing matrix V 17
Drawing 3 D Scenes with Open. GL l Projection matrix • Shifts and scales view volume into a standard cube (extension from – 1 to 1) • Distortion can be compensated by viewport transformation later 18
Drawing 3 D Scenes with Open. GL l Viewport matrix • Maps surviving portion of objects into a 3 D viewport after clipping is performed • Standard cube block w/ x and y extending across viewport and z from 0 to 1 19
Open. GL Modeling and Viewing Functions l Modeling transformation • Translation: gl. Translated(dx, dy, dz) • Scaling: gl. Scaled(sx, sy, sz) • Rotation: gl. Rotated(angle, ux, uy, uz) l Camera for parallel projection • gl. Matrix. Mode(GL_PROJECTION); gl. Load. Identity(); gl. Ortho(left, right, bottom, top, near, far) • Example • near=2: near plane is 2 units in front of eye far=20: far plane is 20 units in front of eye 20
Open. GL Modeling and Viewing Functions l Positioning and aiming camera • gl. Matrix. Mode(GL_MODELVIEW); gl. Load. Identity(); glut. Look. At(eye. x, eye. y, eye. z, // eye position look. x, look. y, look. z, // look at point up. x, up. y, up. z) // up vector • Up vector is often set to (0, 1, 0) l glut. Look. At() builds a matrix that converts world coordinates into eye coordinates. 21
Set up a Typical Camera - Example l gl. Matrix. Mode(GL_PROJECTION); gl. Load. Identity(); gl. Ortho(-3. 2, -2. 4, 1, 50) gl. Matrix. Mode(GL_MODELVIEW); gl. Load. Identity(); (4, 4, 4) glut. Look. At(4, 4, 4, 0, 1, 0) (0, 1, 0) 22
Transformation Matrix for Look. At l Camera coordinate system • Axes: u, v, n n = eye – look u = up n v=n u • Origin: eye (looking in the direction –n) l Transformation matrix 23
Transformation Matrix for Look. At 24
Elementary 3 D Shapes Provided by Open. GL l Cube • glut. Wire. Cube(GLdouble size) • size = length of a side l Sphere • glut. Wire. Sphere(GLdouble radius, GLint n. Slices, GLint n. Stacks) • Approximated by polygonal faces • n. Slices = #polygons around z-axis • n. Stacks = #bands along z-axis 25
Elementary 3 D Shapes Provided by Open. GL l Torus • glut. Wire. Torus(GLdouble in. Rad, GLdouble out. Rad, GLint n. Slices, GLint n. Stacks) • Approximated by polygonal faces l Teapots • glut. Wire. Teapot(GLdouble size) l There are solid counterparts of the wire objects 26
Plantonic Solids Provided by Open. GL l Tetrahedron • glut. Wire. Tetrahedron() l Octahedron • glut. Wire. Octahedron() l Dodecahedron • glut. Wire. Dodecahedron() l Icosahedron • glut. Wire. Icosahedron() l All of them are centered at the origin 27
Plantonic Solids Provided by Open. GL 28
Cone Provided by Open. GL l Cone • glut. Wire. Cone(GLdouble base. Rad, GLdouble height, GLint n. Slices, GLint n. Stacks) Axis coincides with the z-axis l Base rests on xy-plane and extends to z = height l base. Rad: radius at z = 0 l 29
Tapered Cylinder Provided by Open. GL l Tapered cylinder • glu. Cylinder(GLUquadric. Obj *qobj, GLdouble base. Rad, GLdouble top. Rad, GLdouble height, GLint n. Slices, GLint n. Stacks) Axis coincides with the z-axis l Base rests on xy-plane and extends to z = height l base. Rad: radius at z = 0 l top. Rad: radius at z = height l 30
Tapered Cylinder Provided by Open. GL A family of shapes distinguished by the value of top. Rad l To draw, we have to l • Deifne a new quadric object • Set drawing style • GLU_LINE: wire frame • GLU_FILL: solid rendering • Draw the object 31
Tapered Cylinder Provided by Open. GL l Example – wire frame cylinder • GLUquadric. Obj *qobj; qobj = glu. New. Quadric(); glu. Quadric. Draw. Style(qobj, GLU_LINE); glu. Cylinder(qobj, base. Rad, top. Rad, height, n. Slices, n. Stacks); 32
33
34
#include
//<<<<<<< display. Wire >>>>>>> void display. Wire(void) { gl. Matrix. Mode(GL_PROJECTION); // set the view volume shape gl. Load. Identity(); gl. Ortho(-2. 0*64/48. 0, -2. 0, 0. 1, 100); gl. Matrix. Mode(GL_MODELVIEW); // position and aim the camera gl. Load. Identity(); glu. Look. At(2. 0, 0. 0, 1. 0, 0. 0); // to obtain the picture shown in Figure 5. 59 we have to // change the eye location as follows // glu. Look. At(1. 0, 2. 0, 0. 0, 1. 0, 0. 0); 36
gl. Clear(GL_COLOR_BUFFER_BIT); // clear the screen gl. Color 3 d(0, 0, 0); // draw black lines axis(0. 5); // z-axis gl. Push. Matrix(); gl. Rotated(90, 0, 1, 0); axis(0. 5); // x-axis gl. Rotated(-90, 1, 0, 0); axis(0. 5); // y-axis gl. Pop. Matrix(); gl. Push. Matrix(); gl. Translated(0. 5, 0. 5); // big cube at (0. 5, 0. 5) glut. Wire. Cube(1. 0); gl. Pop. Matrix(); 37
gl. Push. Matrix(); gl. Translated(1. 0, 0); // sphere at (1, 1, 0) glut. Wire. Sphere(0. 25, 10, 8); gl. Pop. Matrix(); gl. Push. Matrix(); gl. Translated(1. 0, 0, 1. 0); // cone at (1, 0, 1) glut. Wire. Cone(0. 2, 0. 5, 10, 8); gl. Pop. Matrix(); gl. Push. Matrix(); gl. Translated(1, 1, 1); glut. Wire. Teapot(0. 2); // teapot at (1, 1, 1) gl. Pop. Matrix(); 38
gl. Push. Matrix(); gl. Translated(0, 1. 0 , 0); // torus at (0, 1, 0) gl. Rotated(90. 0, 1, 0, 0); glut. Wire. Torus(0. 1, 0. 3, 10); gl. Pop. Matrix(); gl. Push. Matrix(); gl. Translated(1. 0, 0 , 0); // dodecahedron at (1, 0, 0) gl. Scaled(0. 15, 0. 15); glut. Wire. Dodecahedron(); gl. Pop. Matrix(); 39
gl. Push. Matrix(); gl. Translated(0, 1. 0); // small cube at (0, 1, 1) glut. Wire. Cube(0. 25); gl. Pop. Matrix(); gl. Push. Matrix(); gl. Translated(0, 0 , 1. 0); // cylinder at (0, 0, 1) GLUquadric. Obj * qobj; qobj = glu. New. Quadric(); glu. Quadric. Draw. Style(qobj, GLU_LINE); glu. Cylinder(qobj, 0. 2, 0. 4, 8, 8); gl. Pop. Matrix(); gl. Flush(); } 40
//<<<<<<<< main >>>>>>>> void main(int argc, char **argv) { glut. Init(&argc, argv); glut. Init. Display. Mode(GLUT_SINGLE | GLUT_RGB ); glut. Init. Window. Size(640, 480); glut. Init. Window. Position(100, 100); glut. Create. Window("Transformation testbed - wireframes"); glut. Display. Func(display. Wire); gl. Clear. Color(1. 0 f, 0. 0 f); // background is white gl. Viewport(0, 0, 640, 480); glut. Main. Loop(); } 41


