© tamara munzner university of british columbia hierarchical modelling recap: composing transforms...

35
© © Tamara Munzner Tamara Munzner University of University of British Columbia British Columbia Hierarchical modelling Hierarchical modelling recap: composing transforms recap: composing transforms finish: rendering pipeline finish: rendering pipeline matrix hierarchies and stacks matrix hierarchies and stacks display lists display lists

Post on 20-Dec-2015

216 views

Category:

Documents


0 download

TRANSCRIPT

© © Tamara MunznerTamara Munzner

University ofUniversity ofBritish ColumbiaBritish Columbia

Hierarchical modellingHierarchical modelling

• recap: composing transformsrecap: composing transforms• finish: rendering pipelinefinish: rendering pipeline• matrix hierarchies and stacksmatrix hierarchies and stacks• display listsdisplay lists

• recap: composing transformsrecap: composing transforms• finish: rendering pipelinefinish: rendering pipeline• matrix hierarchies and stacksmatrix hierarchies and stacks• display listsdisplay lists

© © Tamara MunznerTamara Munzner

University ofUniversity ofBritish ColumbiaBritish Columbia

NewsNews

Project 0:Project 0:• depth testdepth test

– glEnable(GL_DEPTH_TEST); // before loopglEnable(GL_DEPTH_TEST); // before loop– glClear(GL_COLOR_BUFFER_BIT |glClear(GL_COLOR_BUFFER_BIT |

GL_DEPTH_BUFFER_BIT); // in display GL_DEPTH_BUFFER_BIT); // in display

• avoid blob effect: try changing color for each faceavoid blob effect: try changing color for each face

• glutDodecahedron glutDodecahedron not not the intended solutionthe intended solution– good for checking your approachgood for checking your approach

© © Tamara MunznerTamara Munzner

University ofUniversity ofBritish ColumbiaBritish Columbia

Composing transforms recapComposing transforms recap

Order mattersOrder matters• 4x4 matrix multiplication not commutative!4x4 matrix multiplication not commutative!

Moving to origin Moving to origin • Transformation of geometry into coordinate system Transformation of geometry into coordinate system

where operation becomes simplerwhere operation becomes simpler• Perform operationPerform operation• Transform geometry back to original coordinate Transform geometry back to original coordinate

systemsystem

© © Tamara MunznerTamara Munzner

University ofUniversity ofBritish ColumbiaBritish Columbia

The Rendering PipelineThe Rendering Pipeline

GeometryGeometryDatabaseDatabaseGeometryGeometryDatabaseDatabase

Model/ViewModel/ViewTransform.Transform.Model/ViewModel/ViewTransform.Transform. LightingLightingLightingLighting PerspectivePerspective

Transform.Transform.PerspectivePerspectiveTransform.Transform. ClippingClippingClippingClipping

ScanScanConversionConversion

ScanScanConversionConversion

DepthDepthTestTest

DepthDepthTestTest

TexturingTexturingTexturingTexturing BlendingBlendingBlendingBlendingFrame-Frame-bufferbuffer

Frame-Frame-bufferbuffer

© © Tamara MunznerTamara Munzner

University ofUniversity ofBritish ColumbiaBritish Columbia

DiscussionDiscussion

Advantages of a pipeline structureAdvantages of a pipeline structure• Logical separation of the different components, Logical separation of the different components,

modularitymodularity• Easy to parallelize:Easy to parallelize:

– Earlier stages can already work on new data while Earlier stages can already work on new data while later stages still work with previous datalater stages still work with previous data

– Similar to pipelining in modern CPUsSimilar to pipelining in modern CPUs– But much more aggressive parallelization possible But much more aggressive parallelization possible

(special purpose hardware!)(special purpose hardware!)– Important for hardware implementations!Important for hardware implementations!

• Only local knowledge of the scene is necessaryOnly local knowledge of the scene is necessary

© © Tamara MunznerTamara Munzner

University ofUniversity ofBritish ColumbiaBritish Columbia

DiscussionDiscussion

Disadvantages:Disadvantages:• Limited flexibilityLimited flexibility• Some algorithms would require different ordering of Some algorithms would require different ordering of

pipeline stagespipeline stages– Hard to achieve while still preserving compatibilityHard to achieve while still preserving compatibility

• Only local knowledge of scene is availableOnly local knowledge of scene is available– ShadowsShadows– Global illuminationGlobal illumination

© © Tamara MunznerTamara Munzner

University ofUniversity ofBritish ColumbiaBritish Columbia

Matrix hierarchies and stacksMatrix hierarchies and stacks

© © Tamara MunznerTamara Munzner

University ofUniversity ofBritish ColumbiaBritish Columbia

Matrix Operations in OpenGLMatrix Operations in OpenGL

Direct matrix specification:Direct matrix specification:• loadload

– glLoadMatrixglLoadMatrix– glLoadIdentityglLoadIdentity

• multiplymultiply– glMultMatrixglMultMatrix

Transformations:Transformations:• glRotate*glRotate*• glTranslate*glTranslate*• glScale*glScale*

© © Tamara MunznerTamara Munzner

University ofUniversity ofBritish ColumbiaBritish Columbia

Matrix Operations in OpenGLMatrix Operations in OpenGL

1000

0cos)cos1(sin)cos1(sin)cos1(

0sin)cos1(cos)cos1(sin)cos1(

0sin)cos1(sin)cos1(cos)cos1(

2

2

2

zxyzyxz

xyzyzyx

yxzzxyx

zyx

zyx

,,

),,,(glRotate*

: ccw rotation angle, looking along vector towards origin: ccw rotation angle, looking along vector towards origin

: vector along which rotation occurs: vector along which rotation occurs

equivalent toequivalent to AA ),ix(glMultMatr

© © Tamara MunznerTamara Munzner

University ofUniversity ofBritish ColumbiaBritish Columbia

Matrix Operations in OpenGLMatrix Operations in OpenGL

2 Matrices:2 Matrices:• Model/view matrix Model/view matrix MM• Projective matrix Projective matrix PP

Example:Example:glMatrixMode( GL_MODELVIEW );glMatrixMode( GL_MODELVIEW );

glLoadIdentity(); // M=IdglLoadIdentity(); // M=Id

glRotatef( angle, x, y, z ); // M=Id*R(glRotatef( angle, x, y, z ); // M=Id*R())

glTranslatef( x, y, z ); // M= Id*R (glTranslatef( x, y, z ); // M= Id*R ()*T(x,y,z))*T(x,y,z)

glMatrixMode( GL_PROJECTION );glMatrixMode( GL_PROJECTION );

glRotatef( … ); // P= …glRotatef( … ); // P= …

© © Tamara MunznerTamara Munzner

University ofUniversity ofBritish ColumbiaBritish Columbia

Matrix Operations in OpenGLMatrix Operations in OpenGL

Semantics:Semantics:• matrixMode sets the matrix that is to be affected by all matrixMode sets the matrix that is to be affected by all

following transformationsfollowing transformations• Whenever primitives are rendered with glBegin(), the Whenever primitives are rendered with glBegin(), the

vertices are transformed with whatever the current vertices are transformed with whatever the current model/view and perspective matrix ismodel/view and perspective matrix is– OpenGL as state machineOpenGL as state machine

• Multiplication order:Multiplication order:– post: right to left, top to bottompost: right to left, top to bottom

© © Tamara MunznerTamara Munzner

University ofUniversity ofBritish ColumbiaBritish Columbia

Transformation HierarchiesTransformation Hierarchies

scene graphscene graphroadroad

stripe1stripe1 stripe2stripe2 ...... car1car1 car2car2 ......

w1w1 w3w3w2w2 w4w4

© © Tamara MunznerTamara Munzner

University ofUniversity ofBritish ColumbiaBritish Columbia

Transformation HierarchiesTransformation Hierarchies

torsotorso

headheadRUarmRUarm

RLarmRLarm

RhandRhand

RUlegRUleg

RLlegRLleg

RfootRfoot

LUarmLUarm

LLarmLLarm

LhandLhand

LUlegLUleg

LLlegLLleg

LfootLfoot

worldworld

trans(0.30,0,0) rot(z, )trans(0.30,0,0) rot(z, )

© © Tamara MunznerTamara Munzner

University ofUniversity ofBritish ColumbiaBritish Columbia

Matrix StacksMatrix Stacks

Challenge:Challenge:• Avoid extra computationAvoid extra computation

– using inverse to return to originusing inverse to return to origin

– computing incremental Tcomputing incremental T1 1 -> T-> T22Object coordinatesObject coordinates

World coordinatesWorld coordinates

TT11(x)(x)TT22(x)(x)

TT33(x)(x)

© © Tamara MunznerTamara Munzner

University ofUniversity ofBritish ColumbiaBritish Columbia

Transformation HierarchiesTransformation Hierarchies

Matrix StackMatrix Stack

glPushMatrix()glPushMatrix()

glPopMatrix()glPopMatrix()

AA

BB

CC

AA

BB

CC

AA

BB

CC

CC

glScale3f(2,2,2)glScale3f(2,2,2)

D = C scale(2,2,2) trans(1,0,0)D = C scale(2,2,2) trans(1,0,0)

AA

BB

CC

DD

DrawSquare()DrawSquare()

glTranslate3f(1,0,0)glTranslate3f(1,0,0)

DrawSquare()DrawSquare()

glPushMatrix()glPushMatrix()

glPopMatrix()glPopMatrix()

© © Tamara MunznerTamara Munzner

University ofUniversity ofBritish ColumbiaBritish Columbia

Composing TransformationsComposing Transformations

OpenGL exampleOpenGL example

FFWW

FFhh

FFhh

glLoadIdentity();glLoadIdentity();glTranslatef(4,1,0);glTranslatef(4,1,0);glPushMatrix();glPushMatrix();glRotatef(45,0,0,1);glRotatef(45,0,0,1);glTranslatef(0,2,0);glTranslatef(0,2,0);glScalef(2,1,1);glScalef(2,1,1);glTranslate(1,0,0);glTranslate(1,0,0);glPopMatrix();glPopMatrix();

FF11

FF hh

FF hh

FF hh

FF hh

FFhh

© © Tamara MunznerTamara Munzner

University ofUniversity ofBritish ColumbiaBritish Columbia

Transformation HierarchiesTransformation Hierarchies

ExampleExample

4

15 3

2

xx

yy

glTranslate3f(x,y,0);glTranslate3f(x,y,0);glRotatef( ,0,0,1);glRotatef( ,0,0,1);DrawBody();DrawBody();glPushMatrix();glPushMatrix(); glTranslate3f(0,7,0);glTranslate3f(0,7,0); DrawHead();DrawHead();glPopMatrix();glPopMatrix();glPushMatrix();glPushMatrix(); glTranslate(2.5,5.5,0);glTranslate(2.5,5.5,0); glRotatef( ,0,0,1);glRotatef( ,0,0,1); DrawUArm();DrawUArm(); glTranslate(0,-3.5,0);glTranslate(0,-3.5,0); glRotatef( ,0,0,1);glRotatef( ,0,0,1); DrawLArm();DrawLArm();glPopMatrix();glPopMatrix();... (draw other arm)... (draw other arm)

1

2

3

© © Tamara MunznerTamara Munzner

University ofUniversity ofBritish ColumbiaBritish Columbia

Hierarchical Modeling:Hierarchical Modeling:Matrix StacksMatrix Stacks

Advantages:Advantages:• Matrix stacks make it feasible to have multiple copies Matrix stacks make it feasible to have multiple copies

of one objectof one object• No need to compute inverse matrices all the timeNo need to compute inverse matrices all the time• Avoids incremental changes to coordinate systemsAvoids incremental changes to coordinate systems

– Accumulation of numerical errorsAccumulation of numerical errors

Practical issues:Practical issues:• In graphics hardware, depth of matrix stacks is limited In graphics hardware, depth of matrix stacks is limited

– (typically 16 for model/view and about 4 for (typically 16 for model/view and about 4 for projective matrix)projective matrix)

© © Tamara MunznerTamara Munzner

University ofUniversity ofBritish ColumbiaBritish Columbia

Example: modularizationExample: modularization

Drawing a scaled squareDrawing a scaled square

void drawBlock(float k) {void drawBlock(float k) { glPushMatrix();glPushMatrix();

glScalef(k,k,k);glScalef(k,k,k); glBegin(GL_LINE_LOOP);glBegin(GL_LINE_LOOP); glVertex3f(0,0,0);glVertex3f(0,0,0); glVertex3f(1,0,0);glVertex3f(1,0,0); glVertex3f(1,1,0);glVertex3f(1,1,0); glVertex3f(0,1,0);glVertex3f(0,1,0); glEnd();glEnd();

glPopMatrix();glPopMatrix();}}

© © Tamara MunznerTamara Munzner

University ofUniversity ofBritish ColumbiaBritish Columbia

Example: appletsExample: applets

http://www.cs.brown.edu/exploratories/http://www.cs.brown.edu/exploratories/freeSoftware/catalogs/scenegraphs.htmlfreeSoftware/catalogs/scenegraphs.html

© © Tamara MunznerTamara Munzner

University ofUniversity ofBritish ColumbiaBritish Columbia

Display listsDisplay lists

© © Tamara MunznerTamara Munzner

University ofUniversity ofBritish ColumbiaBritish Columbia

Display ListsDisplay Lists

Concept:Concept:• If multiple copies of an object are required, it can be If multiple copies of an object are required, it can be

compiled into a display list:compiled into a display list:

glNewList( listId, GL_COMPILE );glNewList( listId, GL_COMPILE );

glBegin( …);glBegin( …);

… … // geometry goes here// geometry goes here

glEndList();glEndList();

// render two copies of geometry offset by 1 in z-direction:// render two copies of geometry offset by 1 in z-direction:

glCallList( listId );glCallList( listId );

glTranslatef( 0.0, 0.0, 1.0 );glTranslatef( 0.0, 0.0, 1.0 );

glCallList( listId) ;glCallList( listId) ;

© © Tamara MunznerTamara Munzner

University ofUniversity ofBritish ColumbiaBritish Columbia

Display ListsDisplay Lists

Advantages:Advantages:• More efficient than individual function calls for every More efficient than individual function calls for every

vertex/attributevertex/attribute• Can be cached on the graphics board (bandwidth!)Can be cached on the graphics board (bandwidth!)• Display lists exist across multiple framesDisplay lists exist across multiple frames

– Represent static objects in an interactive Represent static objects in an interactive applicationapplication

© © Tamara MunznerTamara Munzner

University ofUniversity ofBritish ColumbiaBritish Columbia

Display ListsDisplay Lists

Example: 36 SnowmenExample: 36 Snowmenhttp://www.lighthouse3d.com/opengl/displaylists/http://www.lighthouse3d.com/opengl/displaylists/

efficiency issuesefficiency issues

© © Tamara MunznerTamara Munzner

University ofUniversity ofBritish ColumbiaBritish Columbia

Snowmen: no listsSnowmen: no lists// Draw 36 Snowmen // Draw 36 Snowmen

for(int i = -3; i < 3; i++) for(int i = -3; i < 3; i++)

for(int j=-3; j < 3; j++) { for(int j=-3; j < 3; j++) {

glPushMatrix(); glPushMatrix();

glTranslatef(i*10.0,0,j * 10.0); glTranslatef(i*10.0,0,j * 10.0);

// Call the function to draw a snowman // Call the function to draw a snowman

drawSnowMan(); drawSnowMan();

glPopMatrix(); glPopMatrix();

} }

© © Tamara MunznerTamara Munzner

University ofUniversity ofBritish ColumbiaBritish Columbia

Snowmen: no listsSnowmen: no listsvoid drawSnowMan() { void drawSnowMan() {

glColor3f(1.0f, 1.0f, 1.0f);glColor3f(1.0f, 1.0f, 1.0f); // Draw Body // Draw Body glTranslatef(0.0f ,0.75f, 0.0f); glTranslatef(0.0f ,0.75f, 0.0f); glutSolidSphere(0.75f,20,20); glutSolidSphere(0.75f,20,20);

// Draw Head // Draw Head glTranslatef(0.0f, 1.0f, 0.0f); glTranslatef(0.0f, 1.0f, 0.0f); glutSolidSphere(0.25f,20,20); glutSolidSphere(0.25f,20,20);

// Draw Nose // Draw Nose glColor3f(1.0f, 0.5f , 0.5f); glColor3f(1.0f, 0.5f , 0.5f); glRotatef(0.0f,1.0f, 0.0f, 0.0f); glRotatef(0.0f,1.0f, 0.0f, 0.0f); glutSolidCone(0.08f,0.5f,10,2); glutSolidCone(0.08f,0.5f,10,2); }}

// Draw Eyes // Draw Eyes glPushMatrix(); glPushMatrix(); glColor3f(0.0f,0.0f,0.0f); glColor3f(0.0f,0.0f,0.0f); glTranslatef(0.05f, 0.10f, 0.18f); glTranslatef(0.05f, 0.10f, 0.18f); glutSolidSphere(0.05f,10,10); glutSolidSphere(0.05f,10,10); glTranslatef(-0.1f, 0.0f, 0.0f); glTranslatef(-0.1f, 0.0f, 0.0f); glutSolidSphere(0.05f,10,10); glutSolidSphere(0.05f,10,10); glPopMatrix(); glPopMatrix();

© © Tamara MunznerTamara Munzner

University ofUniversity ofBritish ColumbiaBritish Columbia

Snowmen: no listsSnowmen: no lists// Draw 36 Snowmen // Draw 36 Snowmen

for(int i = -3; i < 3; i++) for(int i = -3; i < 3; i++)

for(int j=-3; j < 3; j++) { for(int j=-3; j < 3; j++) {

glPushMatrix(); glPushMatrix();

glTranslatef(i*10.0,0,j * 10.0); glTranslatef(i*10.0,0,j * 10.0);

// Call the function to draw a snowman // Call the function to draw a snowman

drawSnowMan(); drawSnowMan();

glPopMatrix(); glPopMatrix();

}}

© © Tamara MunznerTamara Munzner

University ofUniversity ofBritish ColumbiaBritish Columbia

Display ListsDisplay Lists

Example: 36 SnowmenExample: 36 Snowmenhttp://www.lighthouse3d.com/opengl/displaylists/http://www.lighthouse3d.com/opengl/displaylists/

benchmarks of 36K polygonsbenchmarks of 36K polygons

• 55 FPS: no display list55 FPS: no display list

© © Tamara MunznerTamara Munzner

University ofUniversity ofBritish ColumbiaBritish Columbia

Snowmen: display listsSnowmen: display lists

GLuint createDL() { GLuint createDL() { GLuint snowManDL; GLuint snowManDL; // Create the id for the list // Create the id for the list snowManDL = glGenLists(1); snowManDL = glGenLists(1); // start list // start list glNewList(snowManDL,GL_COMPILE); glNewList(snowManDL,GL_COMPILE); // call the function that contains the rendering commands // call the function that contains the rendering commands drawSnowMan(); drawSnowMan(); // endList // endList glEndList(); glEndList(); return(snowManDL); } return(snowManDL); }

© © Tamara MunznerTamara Munzner

University ofUniversity ofBritish ColumbiaBritish Columbia

Snowmen: no listsSnowmen: no lists

// Draw 36 Snowmen // Draw 36 Snowmen

for(int i = -3; i < 3; i++) for(int i = -3; i < 3; i++)

for(int j=-3; j < 3; j++) { for(int j=-3; j < 3; j++) {

glPushMatrix(); glPushMatrix();

glTranslatef(i*10.0,0,j * 10.0); glTranslatef(i*10.0,0,j * 10.0);

// Call the function to draw a snowman // Call the function to draw a snowman

glCallList(Dlid); glCallList(Dlid);

glPopMatrix(); glPopMatrix();

} }

© © Tamara MunznerTamara Munzner

University ofUniversity ofBritish ColumbiaBritish Columbia

Display ListsDisplay Lists

Example: 36 SnowmenExample: 36 Snowmenhttp://www.lighthouse3d.com/opengl/displaylists/http://www.lighthouse3d.com/opengl/displaylists/

benchmarks of 36K polygonsbenchmarks of 36K polygons

• 55 FPS: no display list55 FPS: no display list• 153 FPS: 1 snowman display list, called 36 times153 FPS: 1 snowman display list, called 36 times

© © Tamara MunznerTamara Munzner

University ofUniversity ofBritish ColumbiaBritish Columbia

Snowmen: one big listSnowmen: one big listGLuint createDL() { GLuint createDL() { GLuint snowManDL; GLuint snowManDL; snowManDL = glGenLists(1); snowManDL = glGenLists(1); glNewList(snowManDL,GL_COMPILE); glNewList(snowManDL,GL_COMPILE); for(int i = -3; i < 3; i++) for(int i = -3; i < 3; i++) for(int j=-3; j < 3; j++) { for(int j=-3; j < 3; j++) { glPushMatrix(); glPushMatrix(); glTranslatef(i*10.0,0,j * 10.0); glTranslatef(i*10.0,0,j * 10.0); drawSnowMan(); drawSnowMan(); glPopMatrix(); glPopMatrix(); } } glEndList(); glEndList(); return(snowManDL); return(snowManDL); } }

© © Tamara MunznerTamara Munzner

University ofUniversity ofBritish ColumbiaBritish Columbia

Display ListsDisplay Lists

Example: 36 SnowmenExample: 36 Snowmenhttp://www.lighthouse3d.com/opengl/displaylists/http://www.lighthouse3d.com/opengl/displaylists/

benchmarks of 36K polygonsbenchmarks of 36K polygons

• 55 FPS: no display list55 FPS: no display list• 153 FPS: 1 snowman display list, called 36 times153 FPS: 1 snowman display list, called 36 times• 108 FPS: single 36 snowman display list108 FPS: single 36 snowman display list

© © Tamara MunznerTamara Munzner

University ofUniversity ofBritish ColumbiaBritish Columbia

Snowmen: nested listsSnowmen: nested listsGLuint createDL() { GLuint createDL() { GLuint snowManDL,loopDL; GLuint snowManDL,loopDL; snowManDL = glGenLists(1); snowManDL = glGenLists(1); loopDL = glGenLists(1); loopDL = glGenLists(1); glNewList(snowManDL,GL_COMPILE); glNewList(snowManDL,GL_COMPILE); drawSnowMan(); drawSnowMan(); glEndList(); glEndList(); glNewList(loopDL,GL_COMPILE); glNewList(loopDL,GL_COMPILE); for(int i = -3; i < 3; i++) for(int i = -3; i < 3; i++) for(int j=-3; j < 3; j++) { for(int j=-3; j < 3; j++) { glPushMatrix(); glPushMatrix(); glTranslatef(i*10.0,0,j * 10.0); glTranslatef(i*10.0,0,j * 10.0); glCallList(snowManDL); glCallList(snowManDL); glPopMatrix(); glPopMatrix(); } } glEndList(); glEndList(); return(loopDL); } return(loopDL); }

© © Tamara MunznerTamara Munzner

University ofUniversity ofBritish ColumbiaBritish Columbia

Display ListsDisplay Lists

Example: 36 SnowmenExample: 36 Snowmenhttp://www.lighthouse3d.com/opengl/displaylists/http://www.lighthouse3d.com/opengl/displaylists/

benchmarks of 36K polygonsbenchmarks of 36K polygons

• 55 FPS: no display list55 FPS: no display list• 153 FPS: 1 snowman display list, called 36 times153 FPS: 1 snowman display list, called 36 times• 108 FPS: single 36 snowman display list108 FPS: single 36 snowman display list• 153 FPS: nested display lists153 FPS: nested display lists