Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Modelview Stack Lecture 9 Wed, Sep 12, 2007. The Modelview Stack OpenGL maintains a stack of matrices. The matrix on top of the stack is the current.

Similar presentations


Presentation on theme: "The Modelview Stack Lecture 9 Wed, Sep 12, 2007. The Modelview Stack OpenGL maintains a stack of matrices. The matrix on top of the stack is the current."— Presentation transcript:

1 The Modelview Stack Lecture 9 Wed, Sep 12, 2007

2 The Modelview Stack OpenGL maintains a stack of matrices. The matrix on top of the stack is the current matrix. The function glPushMatrix() will push a copy of the current matrix onto the stack. The function glPopMatrix() will pop the top matrix off the stack.

3 The Modelview Stack Pushing and popping are used to “remember” previous transformations. The basic pattern is Push the current matrix onto the stack (to remember it). Perform a series of geometric transformations and draw an object. Pop the current matrix off the stack, thereby restoring the former “current matrix.”

4 Manipulating the Stack glMatrixMode(GL_MODELVIEW); glLoadIdentity(); drawCylinder(); // Along pos. z-axis glPushMatrix(); glRotatef(90.0, 0.0, 1.0, 0.0); drawCylinder(); glTranslatef(0.0, 0.0, 2.0); drawCylinder(); glPopMatrix(); glRotatef(90.0, 1.0, 0.0, 0.0); drawCylinder();

5 Manipulating the Stack Modelview StackActionDrawing ILoad identity IDraw cylinderAlong z-axis, 0 to 1 I, IPush matrix I, R y Rotate I, R y Draw cylinderAlong x-axis, 0 to 1 I, R y TTranslate I, R y TDraw cylinderAlong x-axis, 2 to 3 IPop matrix R x Rotate R x Draw cylinderAlong y-axis, 0 to -1

6 Manipulating the Stack The identity matrix is placed on the stack. The first cylinder is drawn along the positive z-axis. The current matrix is pushed onto the stack. The next cylinder is drawn along positive z- axis and then rotated into the positive x-axis.

7 Manipulating the Stack The third cylinder is drawn along the positive z-axis, shifted forward along the z-axis, then rotated into the x-axis. The current matrix is popped off the stack, The fourth cylinder is drawn along the positive z-axis and the rotated into the negative y-axis.

8 Using the Stack to Draw a Person Our person:

9 The Basic Components We see that the person is constructed of rectangles and ovals (including circles). Therefore, we will write two basic functions: drawCircle() – draws unit circle at the origin. drawSquare() – draws unit square at the origin in the first quadrant.

10 Drawing the Hand The hand is a circle, 6 inches in diameter.

11 Drawing the Hand void drawHand() { glPushMatrix(); glScalef(0.25, 0.25, 0.0); drawCircle(); glPopMatrix(); return; }

12 Drawing the Arm The arm is a rectangle 2 feet, 6 inches long and 3 inches wide, with the hand attached at the end.

13 Drawing the Arm void drawArm() { glPushMatrix(); glScalef(0.25, 2.5, 1.0); drawSquare(); glPopMatrix(); glTranslatef(0.25, 0.0, 0.0); drawHand(); glPopMatrix(); return; }

14 Drawing the Foot The foot is an oval, one foot long and 6 inches high.

15 Drawing the Foot void drawFoot() { glPushMatrix(); glScalef(0.5, 0.25, 0.0); drawCircle(); glPopMatrix(); return; }

16 Drawing the Leg The leg is a rectangle 2 feet, 6 inches long and 6 inches wide, with the foot attached at the end.

17 Drawing the Leg void drawLeg() { glPushMatrix(); glScalef(0.5, 2.5, 1.0); drawSquare(); glPopMatrix(); glTranslatef(0.5, 0.0, 0.0); drawFoot(); glPopMatrix(); return; }

18 Drawing the Head The head is a circle of diameter 1 foot.

19 Drawing the Head The head is a circle of diameter 1 foot. void drawHead() { glPushMatrix(); glScalef(0.5, 0.5, 1.0); drawCircle(); glPopMatrix(); return; }

20 Draw the Torso The torso is 2.5 feet high and 1.5 feet wide.

21 Draw the Torso void drawTorso() { glPushMatrix(); glScalef(1.25, 2.5, 1.0); drawSquare(); glPopMatrix(); return; }

22 Draw the Person

23 void drawPerson() { glPushMatrix(); glTranslatef(0.0, 2.75, 0.0); glPushMatrix(); glTranslatef(-0.625, 0.0, 0.0); drawTorso(); glPopMatrix(); glPushMatrix(); glTranslatef(0.0, 3.0, 0.0); drawHead(); glPopMatrix(); :

24 Draw the Person : glPushMatrix(); glTranslatef(0.625, 2.5, 0.0); glRotatef(20.0, 0.0, 0.0, 1.0); glTranslatef(-0.25, -2.5, 0.0); drawArm(); // Right arm glPopMatrix(); glPushMatrix(); glScalef(-1.0, 1.0, 1.0); glTranslatef(0.625, 2.5, 0.0); glRotatef(20.0, 0.0, 0.0, 1.0); glTranslatef(-0.25, -2.5, 0.0); drawArm(); // Left arm glPopMatrix(); :

25 Draw the Person : glPushMatrix(); glTranslatef(0.125, 0.25, 0.0); drawLeg(); // Right leg glPopMatrix(); glPushMatrix(); glScalef(-1.0, 1.0, 1.0); glTranslatef(0.125, 0.25, 0.0); drawLeg(); // Left leg glPopMatrix(); return; }

26 Draw a Person Read Run

27 Drawing a Crowd of People Read Run

28 Example – An Orrery Suppose we want to draw the sun at the origin, Jupiter orbiting the sun, and four of Jupiter’s moons orbiting Jupiter. Procedure Draw the sun. Rotate by Jupiter’s angle. Translate by Jupiter’s radius. Draw Jupiter.

29 Example – An Orrery For each of Jupiter’s moons, Push the modelview matrix. Rotate through the moon’s angle. Translate by the moon’s radius. Draw the moon. Pop the modelview matrix.

30 Example – An Orrery Read Run

31 Specifying a Transformation Matrix OpenGL allows the programmer to specify his own transformation matrix and multiply it by the current transformation.

32 Specifying a Transformation Matrix Create a 4  4 matrix as a 16-member one- dimensional array of floats or doubles, in row-major order. Use glLoadMatrix*() to make it the current transformation. Use glMultMatrix*() to multiply the current transformation by it.

33 Example: Creating a Shear Suppose we want to create the shear transformation in 2D that sends (0, 0) to (0, 0), (1, 0) to (1, 0), (0, 1) to (1, 1), (1, 1) to (2, 1). In general, it sends (x, y) to (x + y, y).

34 Example: Creating a Shear The transformation matrix is 1100 0100 0010 0001

35 Example: Creating a Shear The following code will perform this transformation. // Store in column-major order float mat[16] = {1.0, 0.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0}; glMultMatrix(mat);

36 Shearing the People Read Run


Download ppt "The Modelview Stack Lecture 9 Wed, Sep 12, 2007. The Modelview Stack OpenGL maintains a stack of matrices. The matrix on top of the stack is the current."

Similar presentations


Ads by Google