hierarchical modelinghofstra university1 hierarchical modeling: tree of transformations, display...

40
Hierarchical Modeling Hofstra University 1 Hierarchical Modeling: Tree of Transformations, Display Lists and Functions, Matrix and Attribute Stacks,

Post on 21-Dec-2015

221 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Hierarchical ModelingHofstra University1 Hierarchical Modeling: Tree of Transformations, Display Lists and Functions, Matrix and Attribute Stacks,

Hierarchical Modeling Hofstra University 1

Hierarchical Modeling:Tree of Transformations,

Display Lists and Functions,Matrix and Attribute Stacks,

Page 2: Hierarchical ModelingHofstra University1 Hierarchical Modeling: Tree of Transformations, Display Lists and Functions, Matrix and Attribute Stacks,

Hierarchical Modeling Hofstra University 2

Modeling complex objects/motion

Decompose object hierarchically into parts

Model each part in local frame, and use affine transformations to position it at its proper place in the part above it in the hierarchy

Page 3: Hierarchical ModelingHofstra University1 Hierarchical Modeling: Tree of Transformations, Display Lists and Functions, Matrix and Attribute Stacks,

Hierarchical Modeling Hofstra University 3

The pipeline conceptually Construct shapes from geometric primitives

Arrange the objects in three dimensional space and select the desired vantage point for viewing the composed scene.

Calculate the color of all the objects. The color might

be explicitly assigned by the application, determined from specified lighting conditions, or obtained by pasting a texture onto the objects.

Convert the mathematical description of objects and their associated color information to pixels on the screen.

During these stages, an API might perform other operations, such as eliminating parts of objects that are hidden by other objects.

Page 4: Hierarchical ModelingHofstra University1 Hierarchical Modeling: Tree of Transformations, Display Lists and Functions, Matrix and Attribute Stacks,

Hierarchical Modeling Hofstra University 4

The Rendering

Page 5: Hierarchical ModelingHofstra University1 Hierarchical Modeling: Tree of Transformations, Display Lists and Functions, Matrix and Attribute Stacks,

Hierarchical Modeling Hofstra University 5

World Coordinates and Model Coordinates

Page 6: Hierarchical ModelingHofstra University1 Hierarchical Modeling: Tree of Transformations, Display Lists and Functions, Matrix and Attribute Stacks,

Hierarchical Modeling Hofstra University 6

World Coordinates and Model Coordinates

Page 7: Hierarchical ModelingHofstra University1 Hierarchical Modeling: Tree of Transformations, Display Lists and Functions, Matrix and Attribute Stacks,

Hierarchical Modeling Hofstra University 7

Viewing the World

Page 8: Hierarchical ModelingHofstra University1 Hierarchical Modeling: Tree of Transformations, Display Lists and Functions, Matrix and Attribute Stacks,

Hierarchical Modeling Hofstra University 8

Viewing Transformation

Page 9: Hierarchical ModelingHofstra University1 Hierarchical Modeling: Tree of Transformations, Display Lists and Functions, Matrix and Attribute Stacks,

Hierarchical Modeling Hofstra University 9

MVT, Lights, Material, Shading,…Rendering

Page 10: Hierarchical ModelingHofstra University1 Hierarchical Modeling: Tree of Transformations, Display Lists and Functions, Matrix and Attribute Stacks,

Hierarchical Modeling Hofstra University 10

Coordinate Systems

Object/Model World Camera/eye Image/Normalized View Volume Screen, Pixel

Page 11: Hierarchical ModelingHofstra University1 Hierarchical Modeling: Tree of Transformations, Display Lists and Functions, Matrix and Attribute Stacks,

Hierarchical Modeling Hofstra University 12

Possible approaches

Symbols and instancing, instancing transforms: can be represented by a table.

Hierarchical modeling. Scene graphs (SG). (Scene) Trees DAGs

Page 12: Hierarchical ModelingHofstra University1 Hierarchical Modeling: Tree of Transformations, Display Lists and Functions, Matrix and Attribute Stacks,

Hierarchical Modeling Hofstra University 13

Instancing in OpenGL

In OpenGL, instancing is created by modifying the model-view matrix:

glMatrixMode( GL_MODELVIEW ); glLoadIdentity(); glPushMatrix( ); glTranslatef( ... ); glRotatef( ... ); glScalef( ... ); symbol(); glPopMatrix( );

Page 13: Hierarchical ModelingHofstra University1 Hierarchical Modeling: Tree of Transformations, Display Lists and Functions, Matrix and Attribute Stacks,

Hierarchical Modeling Hofstra University 14

Symbols & Instances

Page 14: Hierarchical ModelingHofstra University1 Hierarchical Modeling: Tree of Transformations, Display Lists and Functions, Matrix and Attribute Stacks,

Hierarchical Modeling Hofstra University 15

Village code glMatrixMode(GL_MODELVIEW); glLoadIdentity(); setview(); //instance 1 Basic simple house

glPushMatrix(); house();

glPopMatrix(); //simple house instance 2 Big house Translated

glPushMatrix();glTranslatef(6, 0, 0);glScalef(2,2,2);

house();glPopMatrix();

//simple house instance 3 Intermed. house Rotated 15deg and Translated glPushMatrix();glTranslatef(0, 6, 0);glRotated(15, 0,0,1);glScalef(2,1,1);

house();glPopMatrix();

//david house Instance 1 glPushMatrix(); glTranslatef(-6,0,0); glScalef(2,2,2);

david_house(); glPopMatrix();

Page 15: Hierarchical ModelingHofstra University1 Hierarchical Modeling: Tree of Transformations, Display Lists and Functions, Matrix and Attribute Stacks,

Hierarchical Modeling Hofstra University 16

Hierarchical Modeling

DAG allow high-level sharing of objects but often make the memory costs and code complexity too high. DAGs are natural in many applications, e.g., modeling polygonal meshes where many edges and vertices are shared by higher level objects.

Page 16: Hierarchical ModelingHofstra University1 Hierarchical Modeling: Tree of Transformations, Display Lists and Functions, Matrix and Attribute Stacks,

Hierarchical Modeling Hofstra University 17

DAG example: polygonal mesh

Vertex 1

surface

Polygon 1 Polygon 2 Polygon 3 Polygon n

Edge 1 Edge 2 Edge 3

Vertex 2 Vertex 3

Page 17: Hierarchical ModelingHofstra University1 Hierarchical Modeling: Tree of Transformations, Display Lists and Functions, Matrix and Attribute Stacks,

Hierarchical Modeling Hofstra University 18

Hierarchical Modelingusing Trees

Primitives that can be drawn directly, at leaves

Internal nodes become instances Transformations that position instances

of objects label the edges Traverse in pre-order (visit parent, visit

children left to right). Stack based implementation Model independent traversal algorithm

Page 18: Hierarchical ModelingHofstra University1 Hierarchical Modeling: Tree of Transformations, Display Lists and Functions, Matrix and Attribute Stacks,

Hierarchical Modeling Hofstra University 19

Example: simple 2D drawing

Page 19: Hierarchical ModelingHofstra University1 Hierarchical Modeling: Tree of Transformations, Display Lists and Functions, Matrix and Attribute Stacks,

Hierarchical Modeling Hofstra University 20

Hierarchical modeling: tree of transformations

Final picture, obtainedby traversing in pre-orderthe tree

T1.T2.T5.T7.T9(LINE)

Page 20: Hierarchical ModelingHofstra University1 Hierarchical Modeling: Tree of Transformations, Display Lists and Functions, Matrix and Attribute Stacks,

Hierarchical Modeling Hofstra University 21

Compress the tree

If an edge (A,B) is labeled with identity transformation, remove it , and move any drawings from node B into A

If edges (A,B) and (A,C) have the same labels, merge the nodes B and C

Example: in the previous tree T8 will be removed, and the unit square moved to the parent node

Page 21: Hierarchical ModelingHofstra University1 Hierarchical Modeling: Tree of Transformations, Display Lists and Functions, Matrix and Attribute Stacks,

Hierarchical Modeling Hofstra University 22

From the tree model to OpenGL code

Start from the root, traverse tree in pre-order

When visiting a node, if there is a primitive at the node that can be drawn, draw it

If going down from a node which has more then one unvisited child, store(push) CTM and attributes before continuing down

If coming back up an edge to a node which has unvisited children, pop CTM and attributes

Page 22: Hierarchical ModelingHofstra University1 Hierarchical Modeling: Tree of Transformations, Display Lists and Functions, Matrix and Attribute Stacks,

Hierarchical Modeling Hofstra University 23

Stack implementation of the compressed tree

glScalef(1.0,-1.0,1.0); //T1glPushMatrix(); glTranslatef(1.5, 3.0, 0.0); //T2glScalef(1.0, 2.0, 1.0); //T5 glBegin(GL_LINE_LOOP); //BOX

glVertex3f(0.0,0.0,0.0); glVertex3f(1.0,0.0,0.0); glVertex3f(1.0,1.0,0.0); glVertex3f(0.0,1.0,0.0);

glEnd();glTranslatef(0.5,0.0, 0.0); //T7glRotatef(90.0,0.0,0.0,1.0); //T9glBegin(GL_LINES); //LINE

glVertex3f(0.0,0.0,0.0); glVertex3f(1.0,0.0,0.0);

glEnd();

glPopMatrix();glPushMatrix();glTranslatef(2.0,1.0,0.0); //T3glScalef(0.5, 0.5, 1.0); //T6drawUnitCircle(NUM_SLICES);glPopMatrix();glScalef(4.0,5.0,1.0); // T4glBegin(GL_LINE_LOOP); //

BOX glVertex3f(0.0,0.0,0.0); glVertex3f(1.0,0.0,0.0); glVertex3f(1.0,1.0,0.0); glVertex3f(0.0,1.0,0.0);

glEnd();glFlush();

Page 23: Hierarchical ModelingHofstra University1 Hierarchical Modeling: Tree of Transformations, Display Lists and Functions, Matrix and Attribute Stacks,

Hierarchical Modeling Hofstra University 24

Display Lists

Need to draw the display at a rate sufficient to avoid noticeable flicker

Page 24: Hierarchical ModelingHofstra University1 Hierarchical Modeling: Tree of Transformations, Display Lists and Functions, Matrix and Attribute Stacks,

Hierarchical Modeling Hofstra University 25

Display Processor Display processor – special purpose, limited

instruction set, oriented toward drawing graphics primitives

Display memory holds lists of instructions called display lists

Display list executed repeatedly at a rate sufficient to avoid flicker

Page 25: Hierarchical ModelingHofstra University1 Hierarchical Modeling: Tree of Transformations, Display Lists and Functions, Matrix and Attribute Stacks,

Hierarchical Modeling Hofstra University 26

Display Modes Immediate Mode – as soon as the program

executes a statement that defines a primitive, it is sent to the server

Retained Mode – define the object once, then put its description in a display list. Redisplayed with a simple function call

Reduced network traffic

Page 26: Hierarchical ModelingHofstra University1 Hierarchical Modeling: Tree of Transformations, Display Lists and Functions, Matrix and Attribute Stacks,

Hierarchical Modeling Hofstra University 27

OpenGL Display Lists

#define BOX 1glNewList(BOX, GL_COMPILE); glBegin(GL_POLYGON); glColor3f(0.0, 1.0, 0.0); glVertex2f(-1.0, -1.0); glVertex2f( 1.0, -1.0); glVertex2f( 1.0, 1.0); glVertex2f(-1.0, 1.0); glEnd( );glEndList( );

glCallList(BOX);

Send list to server but don’t display contentsGL_COMPILE_AND_EXECUTE

This can change the original state and leave it altered

Page 27: Hierarchical ModelingHofstra University1 Hierarchical Modeling: Tree of Transformations, Display Lists and Functions, Matrix and Attribute Stacks,

Hierarchical Modeling Hofstra University 28

Definition and Execution#define BOX 1glNewList(BOX, GL_COMPILE); glPushAttrib(GL_ALL_ATTRIB_BITS); glPushMatrix( ); glBegin(GL_POLYGON); glColor3f(0.0, 1.0, 0.0); glVertex2f(-1.0, -1.0); glVertex2f( 1.0, -1.0); glVertex2f( 1.0, 1.0); glVertex2f(-1.0, 1.0); glEnd( ); glPopAttrib( ); glPopMatrix( );glEndList( );

Push and pop attributes and coordinates to the stacks

Page 28: Hierarchical ModelingHofstra University1 Hierarchical Modeling: Tree of Transformations, Display Lists and Functions, Matrix and Attribute Stacks,

Hierarchical Modeling Hofstra University 29

Stack implementation of the tree of transformations

glScalef(1.0,-1.0,1.0);

glPushMatrix();glTranslatef(1.5, 3.0, 0.0);

glScalef(1.0, 2.0, 1.0);

glCallList(BOX); glTranslatef(0.5,0.0, 0.0);

glRotatef(90.0,0.0,0.0,1.0);glBegin(GL_LINES); glVertex3f(0.0,0.0,0.0); glVertex3f(1.0,0.0,0.0);

glEnd();

glPopMatrix();

glPushMatrix();glTranslatef(2.0,1.0,0.0);glScalef(0.5, 0.5, 1.0);drawUnitCircle(NUM_SLICES);glPopMatrix();glScalef(4.0,5.0,1.0);glCallList(BOX);

glFlush();

Page 29: Hierarchical ModelingHofstra University1 Hierarchical Modeling: Tree of Transformations, Display Lists and Functions, Matrix and Attribute Stacks,

Hierarchical Modeling Hofstra University 30

Example: cart with two wheels

Page 30: Hierarchical ModelingHofstra University1 Hierarchical Modeling: Tree of Transformations, Display Lists and Functions, Matrix and Attribute Stacks,

Hierarchical Modeling Hofstra University 31

Example: wheel

glNewList(WHEEL,GL_COMPILE); // red wheel with WHEEL_RAD glPushMatrix(); glPushAttrib(GL_COLOR);

glColor3f(1.0,0.0,0.0); // red glScalef(WHEEL_RAD, WHEEL_RAD, 1.0); // scale to wheel radius

glBegin(GL_LINES); // cross, each bar length 2 glVertex2f( -1.0, 0.0); glVertex2f(1.0, 0.0); glVertex2f( 0.0, -1.0); glVertex2f( 0.0, 1.0); glEnd(); drawUnitCircle(NUM_SLICES); // unit circle

glPopAttrib(); glPopMatrix();glEndList();

Model a red wheel of radius WHEEL_RAD, consisting ofa circle and two crossed spikes. Primitive components are unit circle andcrossed orthogonal spikes of length 2.

Page 31: Hierarchical ModelingHofstra University1 Hierarchical Modeling: Tree of Transformations, Display Lists and Functions, Matrix and Attribute Stacks,

Hierarchical Modeling Hofstra University 32

Example: rod with 2 fixed wheels

glNewList(CART,GL_COMPILE); glPushMatrix(); glPushAttrib(GL_COLOR); glColor3f(0.0,1.0,0.0); // green glBegin(GL_LINES); // rod glVertex2f(-HALF_AXIS_LENGTH, 0.0); glVertex2f( HALF_AXIS_LENGTH, 0.0); glEnd(); glPushMatrix(); glTranslatef(-HALF_AXIS_LENGTH, 0.0, 0.0); //position front WHEEL at axis glRotatef(TH, 0.0,0.0,1.0); // rotate a WHEEL by TH glCallList(WHEEL); glPopMatrix(); glTranslatef(HALF_AXIS_LENGTH, 0.0, 0.0); //position back WHEEL at axis glRotatef(TH+INCR, 0.0,0.0,1.0); // rotate a WHEEL by TH+INCR glCallList(WHEEL);

glPopAttrib(); glPopMatrix();glEndList();

Model the cart consisting of two red wheels of radius WHEEL_RAD, positioned at the ends of a green rod of length 2*HALF_AXIS_LENGTH.The two wheels are rotated at different angles.

Page 32: Hierarchical ModelingHofstra University1 Hierarchical Modeling: Tree of Transformations, Display Lists and Functions, Matrix and Attribute Stacks,

Hierarchical Modeling Hofstra University 33

Example: cart with 2 wheels void display(){ glClear(GL_COLOR_BUFFER_BIT); glCallList(CART); glFlush();}

void myinit(){ glClearColor(1.0,1.0,1.0,0.0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(-225.0, 225.0, -225, 225.0); glMatrixMode(GL_MODELVIEW);

defineParts(); // WHEEL and CART are display lists defined here, }

main() …. As usual, negotiate with Windows Sys, register callbacks, run loop to process events

Page 33: Hierarchical ModelingHofstra University1 Hierarchical Modeling: Tree of Transformations, Display Lists and Functions, Matrix and Attribute Stacks,

Hierarchical Modeling Hofstra University 34

Example: spin the wheels

Limitations of display lists Use of functions in drawing instances

of objects at tree nodes Continuously keep changing the

value of rotation angle for turning a wheel

Need of double buffering

Page 34: Hierarchical ModelingHofstra University1 Hierarchical Modeling: Tree of Transformations, Display Lists and Functions, Matrix and Attribute Stacks,

Hierarchical Modeling Hofstra University 35

Example: spin the wheels,2void draw_cart(){glPushMatrix();glPushAttrib(GL_COLOR); glColor3f(0.0,1.0,0.0); // draw axis in greenglBegin(GL_LINES); glVertex2f(-HALF_AXIS_LENGTH,0.0); glVertex2f(HALF_AXIS_LENGTH,0.0);glEnd();glPushMatrix();glTranslatef(-HALF_AXIS_LENGTH,0.0, 0.0); // position front wheel at

axis, T1 glRotatef(theta,0.0,0.0,1.0); // spin front WHEEL by theta glCallList(WHEEL); // draw wheel glPopMatrix();glTranslatef(HALF_AXIS_LENGTH,0.0, 0.0); // position back wheel at

axis, T2 glRotatef(theta, 0.0,0.0,1.0); // spin back WHEEL by theta glCallList(WHEEL); // draw wheelglPopAttrib();glPopMatrix();}

Page 35: Hierarchical ModelingHofstra University1 Hierarchical Modeling: Tree of Transformations, Display Lists and Functions, Matrix and Attribute Stacks,

Hierarchical Modeling Hofstra University 36

Open GL interactive program structure

int main(int argc, char** argv){ glutInit(&argc,argv);

glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);glutCreateWindow(”generic example");

myinit (); glutReshapeFunc (myReshape); glutMouseFunc (mouse); glutMotionFunc(do_on_motion); glutIdleFunction(idle);

glutDisplayFunc(display);

glutMainLoop();

}

Page 36: Hierarchical ModelingHofstra University1 Hierarchical Modeling: Tree of Transformations, Display Lists and Functions, Matrix and Attribute Stacks,

Hierarchical Modeling Hofstra University 37

Example: spin the wheels,3, the Idle Function

void idle(void) // IDLE callback function updates { // rotation angle that spins a wheel theta += INCR_ANGLE; glutPostRedisplay();}void mouseMotion(int x, int y) // MOTION callback function updates{ // translation vector that moves whole transx = x; transy = y; glutPostRedisplay();}void display() // DISPLAY callback draws whole object { // in back buffer then swaps buffers glClear(GL_COLOR_BUFFER_BIT); // translate whole object axis with spinning wheels glTranslatef( (float)transx, (float)(WINDOWH-transy), 0.0); draw_cart(); glutSwapBuffers(); // SWAP BUFFERS}

Page 37: Hierarchical ModelingHofstra University1 Hierarchical Modeling: Tree of Transformations, Display Lists and Functions, Matrix and Attribute Stacks,

Hierarchical Modeling Hofstra University 38

Example: spin the wheels, 4, the Idle Function

int main(…){ glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB); // double buffering ... glutDisplayFunc(display); glutMotionFunc(mouseMotion); glutIdleFunc(idle); glutMainLoop(); …}

Page 38: Hierarchical ModelingHofstra University1 Hierarchical Modeling: Tree of Transformations, Display Lists and Functions, Matrix and Attribute Stacks,

Hierarchical Modeling Hofstra University 39

HW: Model hierarchically a 3D scene consisting of a circular road, a car, at least 2 trees along the road. Each component has a different color than the rest.

Allow for interactively changing the camera position, allowing the camera to rotate around the horizontal and vertical axis of the coordinate system attached to the scene, and also for interactively choosing an option to move the car forward or backward

A car has a rectangular body and 4 wheels A tree has a trunk (cylinder) and a crown (sphere) The road is a circular loop Model each object (object part) in its own local coordinate

system; use transformations to position all objects in the scene. Trees of transformations should help in modeling the individual

objects (tree, car), and also in putting the whole scene together.

Use functions or display lists to model the components HW is due in 2 weeks. Pace yourself well: complete the

modeling in a week, so the week after you could deal with the interaction and the animation. Model and test each object individually , first.

Page 39: Hierarchical ModelingHofstra University1 Hierarchical Modeling: Tree of Transformations, Display Lists and Functions, Matrix and Attribute Stacks,

Hierarchical Modeling Hofstra University 40

Example: model circular road

// ROAD_RAD1, ROAD_RAD2, ROAD_SL, ROAD_RINGS defined globally

void draw_road(){ GLUquadric* q; // dynamic array to hold vertices of road q = gluNewQuadric(); // creates and returns a ptr to new quadric object

glPushMatrix(); glPushAttrib(GL_ALL_ATTRIB_BITS); glColor3f(0.5,0.5,0.5); // road is gray

gluDisk(q, ROAD_RAD1, ROAD_RAD2, ROAD_SL, ROAD_RINGS);

glPopMatrix(); glPopAttrib();

gluDeleteQuadric(q); }

Page 40: Hierarchical ModelingHofstra University1 Hierarchical Modeling: Tree of Transformations, Display Lists and Functions, Matrix and Attribute Stacks,

Hierarchical Modeling Hofstra University 41

Example: model circular road

void display()

{

glClear(GL_COLOR_BUFFER_BIT);

// glRotatef(45,1.0,0.0,0.0);

//gluLookAt(0.0,0.0,0.0, -1.0,-1.0,-1.0, 0.0 ,0.0,1.0);

draw_road();

glFlush();

}