Presentation is loading. Please wait.

Presentation is loading. Please wait.

6/15/2015©Zachary Wartell 1 OpenGL Transforms with Object-Oriented Framework Revision 1.1 Copyright Zachary Wartell, University of North Carolina at Charlotte,

Similar presentations


Presentation on theme: "6/15/2015©Zachary Wartell 1 OpenGL Transforms with Object-Oriented Framework Revision 1.1 Copyright Zachary Wartell, University of North Carolina at Charlotte,"— Presentation transcript:

1 6/15/2015©Zachary Wartell 1 OpenGL Transforms with Object-Oriented Framework Revision 1.1 Copyright Zachary Wartell, University of North Carolina at Charlotte, 2008 All Rights Reserved Reading: - Hearn & Baker - Chapter 5 - OpenGL Programming Guide - Chapter 3 – emphasis on "Manipulating the Matrix Stacks"OpenGL Programming Guide Chapter 3"Manipulating the Matrix Stacks" "Examples of Composing Several Transformations"

2 OpenGL Modeling Transformations 6/15/2015©Zachary Wartell 2 glBegin (GL_QUADS); glVertex2i(-0.5,-0.5); glVertex2i( 0.5,-0.5); glVertex2i( 0.5, 0.5); glVertex2i(-0.5, 0.5); glEnd(); coordinates in object coordinate space coordinates in eye coordinate space coordinates in window coordinate space Vertex commands GL_MODELVIEW matrix GL_PROJECTION matrix + glViewport matrix M MV = M eye←object = M eye←world M world ← object M P = M clip←eye …other magic… M VP = M win←ndev

3 2D Example 6/15/2015©Zachary Wartell 3 O W=E M MV = M eye←object = M eye←world M world ← object = I · M world ← object View Window (2.5,2.5) W (-2.5,-2.5) W M P = M clip←eye - gluOrtho2D (1,1) Clip (-1,-1) Clip

4 6/15/2015©Zachary Wartell 4 Matrix Transform Functions glMatrixMode({GL_MODELVIEW,GL_PROJECTION}); glLoadMatrix[fd] (…) glLoadIdentity () glTranslate[fd] (…) glRotate[fd] (…) glScale[fd] (…) glMultMatrix[fd] (…)

5 6/15/2015©Zachary Wartell 5 glMatrixMode(GL_MODELVIEW); glLoadIdentity(); DrawUnitBox(RED); glTranslatef(0,1.5,0); DrawUnitBox(BLUE); glLoadIdentity(); glTranslatef(0,2.5,0); glRotatef(45,0,0,1); DrawUnitBox(GREEN); glLoadIdentity(); glTranslatef(1.5,2.5,0) glScalef(0.5,2.0,1); DrawUnitBox(ORANGE); Matrix Example x y M MV = I M MV = M MV T1 M MV = I M MV = M MV ·T2 M MV = M MV ·R1 M MV = I M MV = M MV ·T3 M MV = M MV ·S1 I · T1 · (T2·R1) · (T3·S1) · (-0.5,-0.5) ( 0.5,0.5) W

6 6/15/2015©Zachary Wartell 6 Matrix Stack OpenGL uses one stack for each matrix mode –all matrix operations alter the top matrix –vertex coordinates transformed by the top stack –glPushMatrix – push a new matrix onto the stack; the pushed matrix is a copy of previous stack top –glPopMatrix – pop the stack

7 6/15/2015©Zachary Wartell 7 Matrix Stack Operation glLoadMatrixf (Q); glTranslate3f(tx,ty,tz); glScale3f(sx,sy,sz); glPushMatrix(); glRotatef(90.0,0.0,0.0,1.0); glPopMatrix(); glTranslate(tx2,ty2,tz2); M0M0 M MV =Q=Q·T1 =Q·T1·S1 M1M1 =Q·T1·S1·R1 M MV =Q·T1·S1·T2

8 6/15/2015©Zachary Wartell 8 glLoadIdentity(); glTranslatef(2,2,0); DrawUnitBox(BLUE); for (angle=0;angle < 360;angle += 90) { glPushMatrix(); glRotatef(angle,0,0,1); glTranslatef(1,0,0); glScalef(0.5,0.5,1); DrawUnitBox(RED); glPopMatrix(); } Procedure Approach – Hierarchical Objects x y (-0.5,-0.5) ( 0.5,0.5) W

9 Object-Oriented Approach (Non-hierarchical) 6/15/2015©Zachary Wartell 9 class PosableBox { Point2 origin; float orientation; float scale[2]; … }; void PosableBox::render() const { glPushMatrix(); glTranslatef(origin[0],origin[1],0); glRotatef(orientation,0,0,1); glScalef(scale[0],scale[1],1); DrawUnitBox(BLUE); glPopMatrix(); } x y (-0.5,-0.5) ( 0.5,0.5) W M MV = T B ·R B ·S B draw T B ·R B ·S B B ( 0.5,0.5) B, (1.8,2.6) W `

10 Change of Coordinates – Collision/Boundary Violation 6/15/2015©Zachary Wartell 10 class PosableBox { Point2 origin; float orientation; float scale[2]; … }; void PosableBox::render() const { glPushMatrix(); glTranslatef(origin[0],origin[1],0); glRotatef(orientation,0,0,1); glScalef(scale[0],scale[1],1); DrawUnitBox(BLUE); glPopMatrix(); } x y (-0.5,-0.5) ( 0.5,0.5) W M MV = T B ·R B ·S B draw T B ·R B ·S B B ( 0.5,0.5) B, M W ← B = T B ·R B ·S B (1.8,2.6) W

11 Change of Coordinates – Mouse Click 6/15/2015©Zachary Wartell 11 class PosableBox { Point2 origin; float orientation; float scale[2]; … }; void PosableBox::render() const { glPushMatrix(); glTranslatef(origin[0],origin[1],0); glRotatef(orientation,0,0,1); glScalef(scale[0],scale[1],1); DrawUnitBox(BLUE); glPopMatrix(); } x y (-0.5,-0.5) ( 0.5,0.5) W M MV = T B ·R B ·S B draw T B ·R B ·S B B ( 0.5,0.5) B, M B ← W = ? M W ← B = T B ·R B ·S B M B ← W = M W ← B -1 = (T B ·R B ·S B ) -1 = S B -1 ·R B -1 ·T B -1 (1.8,2.6) W (2.3,0.8) W (0,-0.25) B

12 Object-Oriented – Hierarchical – Step #1 6/15/2015©Zachary Wartell 12 class CoordinateSystem { Point2 origin; float orientation; float scale[2]; … }; class Shape { … }; CS1 UB1UD1P1 class Polygon : Shape; attachment shapes (0..*) parent (0..1) class UnitBox : Shape; class UnitDisc : Shape; World Coordinates ( W ) M W ← CS1

13 Object-Oriented – Hierarchical – Step #1 6/15/2015©Zachary Wartell 13 void CoordinateSystem::render() { glPushMatrix(); …post multiply M World ← Local onto M MV … …call Shape::render on all attached Shapes… glPopMatrix(); } void UnitDisc::render () { …send GL the vertex coordinates describing a unit disc measured in local coordinates … } CS1 UB1UD1P1 x y W M MV = M W ← CS1 =T CS1 ·R CS1 ·S CS1 render M W ← CS1 · render M W ← CS1

14 Object-Oriented – Hierarchical – Step #2 6/15/2015©Zachary Wartell 14 class CoordinateSystem { Point2 origin; float orientation; float scale[2]; … }; CS1 UB1UD1P1 Attachment children (0..*) parent (0..1) CS2 UB2 M CS1 ← CS2 M W ← CS1

15 Object-Oriented – Hierarchical – Step #2 6/15/2015 15 void CoordinateSystem::render() { glPushMatrix(); …post multiply M Parent ← Local onto M MV … for all attached Shapes Shape::render for all attached CoordinateSystems CoordinateSystem::render glPopMatrix(); } CS1 UB1UD1P1 CS2 UB2 CS3 UD2 M CS1 ← CS3 M CS1 ← CS2 M W ← CS1 W

16 Object-Oriented – Hierarchical – Step #2 6/15/2015©Zachary Wartell 16 void CoordinateSystem::render() { glPushMatrix(); …post multiply M Parent ← Local onto M MV … for all attached Shapes Shape::render for all attached CoordinateSystems CoordinateSystem::render glPopMatrix(); } M MV =IM 0 =I M MV =I M 0 =I M MV =I · T CS1 ·R CS1 ·S CS1 M W ← CS1 M 0 =I M 1 =I · T CS1 ·R CS1 ·S CS1 M W ← CS1 M MV =I · T CS1 ·R CS1 ·S CS1 M 0 =I M 1 =I · T CS1 ·R CS1 ·S CS1 M W ← CS1 M MV =I · T CS1 ·R CS1 ·S CS1 · T CS2 ·R CS2 ·S CS2 M CS1 ← CS2 M W ← CS2 M 0 =I M MV =I · T CS1 ·R CS1 ·S CS1 M W ← CS1 M 0 =I M 1 =I · T CS1 ·R CS1 ·S CS1 M W ← CS1 M MV =I · T CS1 ·R CS1 ·S CS1 M 0 =I M 1 =I · T CS1 ·R CS1 ·S CS1 M W ← CS1 M MV =I · T CS1 ·R CS1 ·S CS1 · T CS3 ·R CS3 ·S CS3 M CS1 ← CS3 M W ← CS3 M 0 =I M MV =I · T CS1 ·R CS1 ·S CS1 M W ← CS1 M MV =I CS1 UB1UD1P1 CS2 UB2 CS3 UD2 M CS1 ← CS3 M CS1 ← CS2 x y W CS1 CS2 CS3 M W ← CS1 W

17 6/15/2015©Zachary Wartell 17 Tricky Bit OpenGL specifications and documents use column-vector notation: But in C/C++, GL follows the row-major memory organization for matrices: GLfloat translate[4][4] = {{1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}, {tx,ty,tz,1}}; When building matrices to transfer to GL or examining matrices retrieved from GL, you must remember the array layout is the transpose of the mathematical convention. Alternatively, you can use the “transpose” versions of GL matrix routines which accept and return matrix arrays organized using the conventional mathematical layout (see glLoadTransposeMatrix, etc.) row column


Download ppt "6/15/2015©Zachary Wartell 1 OpenGL Transforms with Object-Oriented Framework Revision 1.1 Copyright Zachary Wartell, University of North Carolina at Charlotte,"

Similar presentations


Ads by Google