29
Hierarchy, Modeling, and Scene Graphs Angel: Chapter 10 OpenGL Programming and Reference Guides, other sources. ppt from Angel, AW, etc. CSCI 6360/4360

CSCI 6360/4360

  • Upload
    psyche

  • View
    71

  • Download
    1

Embed Size (px)

DESCRIPTION

Hierarchy, Modeling, and Scene Graphs Angel: Chapter 10 OpenGL Programming and Reference Guides, other sources. ppt from Angel, AW, etc. CSCI 6360/4360. Overview. Angel chapter on hierarchy and modeling … - PowerPoint PPT Presentation

Citation preview

Page 1: CSCI 6360/4360

Hierarchy, Modeling,and

Scene GraphsAngel: Chapter 10

OpenGL Programming and Reference Guides, other sources.

ppt from Angel, AW, etc.

CSCI 6360/4360

Page 2: CSCI 6360/4360

Overview

• Angel chapter on hierarchy and modeling …– Presents ideas essentially “intuitive” to those trained in use of object oriented

paradigm

• Detailed explication of translation of procedural OpenGL ideas to hierarchical (object oriented) formulation

– … pretty straightforward

• Scene graphs -

– From c/c++ perspective of OpenGL - “bleeding edge” of software• Lacks standards• Open source formulation of OpenGL - …• Vendors have their own “toolkits” – nVidia, …

– From truly object oriented perspective – “couldn’t be more natural”

• Only way to do it in Java – Java3d

Page 3: CSCI 6360/4360

3

Overview

• Examine the limitations of linear modeling– Symbols and instances

• Introduce hierarchical models– Articulated models– Robots

• Introduce Tree and DAG models

• But, don’t worry it (almost) all goes away with the various scene graph implementations and tools

Page 4: CSCI 6360/4360

Instances

• Start with a prototype object (a symbol)

• Each appearance of the object in the model is an instance– Must scale, orient, position– Defines instance transformation

• Can store a model by assigning a number to each symbol and storing the parameters for the instance transformation– Symbol-instance table

Page 5: CSCI 6360/4360

Structure Through Function Calls

• Symbol-instance table does not show relationships between parts of model

• Consider model of car– Chassis + 4 identical wheels– Two symbols

• Rate of forward motion determined by rotational speed of wheels

• Structure Through Function Calls

car(speed){

chassis() wheel(right_front); wheel(left_front); wheel(right_rear); wheel(left_rear);

}

• Fails to show relationships well

• Look at problem using a graph

Page 6: CSCI 6360/4360

Graphs, Trees, Models• Graph

– Set of nodes and edges (links)– Edge connects a pair of nodes

• Directed or undirected– Cycle: directed path that is a

loop

• Tree– Graph in which each node

(except the root) has exactly one parent node

• May have multiple children• Leaf or terminal node: no children

• Tree Model of Car

• DAG Model– If we use the fact that all the wheels are

identical, we get a directed acyclic graph• Not much different than dealing

with tree

loop

root node

leaf node

Page 7: CSCI 6360/4360

Modeling with Trees

• Must decide what information to place in nodes and what to put in edges

• Nodes– What to draw– Pointers to children

• Edges

– May have information on incremental changes to transformation matrices (can

also store in nodes)

Page 8: CSCI 6360/4360

Angel Example: Robot Arm

robot arm parts in their own coodinate systems

• Robot arm is an example of an articulated mode• Parts connected at joints

– Can specify state of model by

giving all joint angles

Page 9: CSCI 6360/4360

Relationships in Robot Arm• Base rotates independently

– Single angle determines position

• Lower arm attached to base– Its position depends on rotation of base– Must also translate relative to base and rotate about connecting joint

• Upper arm attached to lower arm– Its position depends on both base and lower arm– Must translate relative to lower arm and rotate about joint connecting to lower arm

• Rotation of base: Rb

– Apply M = Rb to base

• Translate lower arm relative to base: Tlu

• Rotate lower arm around joint: Rlu

– Apply M = Rb Tlu Rlu to lower arm

• Translate upper arm relative to upper arm: Tuu

• Rotate upper arm around joint: Ruu

– Apply M = Rb Tlu Rlu Tuu Ruu to upper arm

Page 10: CSCI 6360/4360

Required Matrices and Code• Rotation of base: Rb

– Apply M = Rb to base

• Translate lower arm relative to base: Tlu

• Rotate lower arm around joint: Rlu

– Apply M = Rb Tlu Rlu to lower arm

• Translate upper arm relative to upper arm: Tuu

• Rotate upper arm around joint: Ruu

– Apply M = Rb Tlu Rlu Tuu Ruu to upper arm

robot_arm(){ glRotate(theta, 0.0, 1.0, 0.0); base(); // draw glTranslate(0.0, h1, 0.0); glRotate(phi, 0.0, 1.0, 0.0); lower_arm(); // draw glTranslate(0.0, h2, 0.0); glRotate(psi, 0.0, 1.0, 0.0); upper_arm(); // draw}

Page 11: CSCI 6360/4360

Tree Model of Robot

• Code shows relationships between parts of model

• Can change “look” of parts easily without altering relationships

• Simple example of tree model

• Want a general node structure for nodes

robot_arm(){ glRotate(theta, 0.0, 1.0, 0.0); base(); // draw glTranslate(0.0, h1, 0.0); glRotate(phi, 0.0, 1.0, 0.0); lower_arm(); // draw glTranslate(0.0, h2, 0.0); glRotate(psi, 0.0, 1.0, 0.0); upper_arm(); // draw}

Page 12: CSCI 6360/4360

Possible Node Structure

Code for drawing part orpointer to drawing function

linked list of pointers to children

matrix relating node to parent

robot_arm(){ glRotate(theta, 0.0, 1.0, 0.0); base(); // draw glTranslate(0.0, h1, 0.0); glRotate(phi, 0.0, 1.0, 0.0); lower_arm(); // draw glTranslate(0.0, h2, 0.0); glRotate(psi, 0.0, 1.0, 0.0); upper_arm(); // draw}

Page 13: CSCI 6360/4360

Generalizations

• Need to deal with multiple children– How to represent a more general tree– How to traverse such a data structure

• Animation– How to use dynamically– How to create and delete nodes during execution

• Will consider building a model – of a humanoid figure

Page 14: CSCI 6360/4360

Building the Model

• Simple implementation using quadrics: ellipsoids and cylinders

• Access parts through functions– torso(), left_upper_arm()

• Matrices describe position of node with respect to its parent

– Mlla positions left lower leg with respect to left upper arm

Page 15: CSCI 6360/4360

Building the Model

• Simple implementation using quadrics: ellipsoids and cylinders

• Access parts through functions– torso(), left_upper_arm()

• Matrices describe position of node with respect to its parent

– Mlla positions left lower leg with respect to left upper arm

Page 16: CSCI 6360/4360

Display and Traversal

• The position of the figure is determined by 11 joint angles (two for the head and one for each other part)

• Display of the tree requires a graph traversal– Visit each node once

– Display function at each node that describes the part associated with the node, applying the correct transformation matrix for position and orientation

Page 17: CSCI 6360/4360

Transformation MatricesDetail

• There are 10 relevant matrices:– M positions and orients entire figure through the torso which is the root node

– Mh positions head with respect to torso

– Mlua, Mrua, Mlul, Mrul position arms and legs with respect to torso

– Mlla, Mrla, Mlll, Mrll position lower parts of limbs with respect to corresponding upper limbs

Page 18: CSCI 6360/4360

Stack-based TraversalDetail

• Set model-view matrix to M and draw torso

• Set model-view matrix to MMh and draw head

• For left-upper arm need MMlua and so on

• Rather than recomputing MMlua from scratch or using an inverse matrix, we can use the matrix stack to store M and other matrices as we traverse the tree

Page 19: CSCI 6360/4360

Traversal CodeDetail

figure() { glPushMatrix() // save present model-view matrix torso(); glRotate3f(…); // update model-view matrix for head head(); glPopMatrix(); // recover original model-view matrix glPushMatrix(); // save it again

glTranslate3f(…); // update model-view matrix for left upper arm glRotate3f(…); left_upper_arm();

glPopMatrix(); // recover and save original model-view matrix gain glPushMatrix();

:

Page 20: CSCI 6360/4360

Preorder TraversalDetail

void traverse(treenode *root){ if(root == NULL) return; glPushMatrix(); glMultMatrix(root->m); root->f(); if(root->child != NULL) traverse(root->child); glPopMatrix(); if(root->sibling != NULL) traverse(root->sibling);}

Page 21: CSCI 6360/4360

NotesDetails

• Must save model-view matrix before multiplying it by node matrix – Updated matrix applies to children

of node but not to siblings which contain their own matrices

• Traversal program applies to any left-child right-sibling tree– The particular tree is encoded in

the definition of individual nodes

• Order of traversal matters because of possible state changes in the functions

figure() { glPushMatrix() torso(); glRotate3f(…); head(); glPopMatrix(); glPushMatrix();

glTranslate3f(…); glRotate3f(…); left_upper_arm();

glPopMatrix(); glPushMatrix();

:

Page 22: CSCI 6360/4360

OpenGL and Objects• OpenGL lacks an object orientation

• Consider, for example, a green sphere– We can model the sphere with polygons or use OpenGL

quadrics– Its color is determined by the OpenGL state and is not a

property of the object

• Defies our notion of a physical object

• We can try to build better objects in code using object-oriented languages/techniques

Page 23: CSCI 6360/4360

Imperative vs. Object-Oriented Programming Models

• Imperative programming model– Example: rotate a cube

– The rotation function must know how the cube is represented• Vertex list• Edge list

• In object-oriented model, the representation is stored with the object

– The application sends a message to the object– The object contains functions (methods) which allow it to “transform itself”

Application glRotate

cube data

results

Application glRotate

cube data

results

Page 24: CSCI 6360/4360

Angel - C/C++

• Can try to use C structs to build objects

• C++ provides better support– Use class construct– Can hide implementation using public, private, and protected members in a class– Can also use friend designation to allow classes to access each other

• Chapter has long example of creating graphics objects

• In fact would use a tool

Page 25: CSCI 6360/4360

Scene Graphs

• Recall figure model …– Could describe model either by tree or by equivalent code– Could write a generic traversal to display

• Can represent all elements of a scene (cameras, lights, materials, geometry) as C++ objects and should be able to show them in a tree

– Render scene by traversing this tree– Scene graph

Page 26: CSCI 6360/4360

Scene Graph API

• Open Scene Graph (OSG), – Cross-platform, supports culling, sorting, level of detail

• Inventor, Java3D, Nvidia scene graph (NVSG), VRML

• Scene graphs can also be described by a file (text or binary)– Implementation independent way of transporting scenes– Supported by scene graph APIs

• However, primitives supported should match capabilities of graphics systems

– Hence most scene graph APIs are built on top of OpenGL or DirectX (for PCs)

Page 27: CSCI 6360/4360

OSG and Viewer

Page 28: CSCI 6360/4360

OSG

Page 29: CSCI 6360/4360

End

• .