6

Click here to load reader

Introduction to Scene Graphs Scene Graph - IRISA to Scene Graphs Kévin Boulanger November 2006 • When developing big projects lots of data have to be managedWhen developing big

Embed Size (px)

Citation preview

Page 1: Introduction to Scene Graphs Scene Graph - IRISA to Scene Graphs Kévin Boulanger November 2006 • When developing big projects lots of data have to be managedWhen developing big

Introduction toScene Graphs

Kévin BoulangergNovember 2006

• When developing big projects lots of data have to be managedWhen developing big projects, lots of data have to be managed• It quickly becomes a big mess !• A robust and extensible data structure is required

Scene Graphp

• Hierarchical data structureE b ild• Easy to build

• Easy to extend• Allows work by many developers at the same time

Some examples

• Scene graph is a commonly used data structure (web, video games)

• Examples:• OpenSceneGraph (www.openscenegraph.org)• OpenSG (www opensg org)• OpenSG (www.opensg.org)• nVSG (developer.nvidia.com/object/nvsg_home.html)• Java3D (java.sun.com/products/java-media/3D)

VRML X3D ( 3d )• VRML, X3D (www.x3d.org)• SGI Open Inventor (oss.sgi.com/projects/inventor)• OpenMASK (www.irisa.fr/siames/OpenMASK)• etc.

• Many scene graph designs exist, each adapted to its respective use

Definition

• Objects, items in a 3D scene are rarely independent:• physical connection• interactioninteraction• influence (light)• requirement (data)

A scene graph is a hierarchical data structure, based on a graph:• the leaf nodes represent objects to render,

th i t l d t f ti ti• the internal nodes are groups, transformations, properties

• Two types:• tree• DAG (Directed Acyclic Graph)

Page 2: Introduction to Scene Graphs Scene Graph - IRISA to Scene Graphs Kévin Boulanger November 2006 • When developing big projects lots of data have to be managedWhen developing big

Tree

A tree contains no loop and each node has a single parent node

car

translation translation translationtranslationpaintp

tire material tire material tire material tire materialbodyy

wheel wheel wheel wheel

DAG (Directed Acyclic Graph)( y )

A DAG contains no loop and each node can have several parent nodes

car

paint tire material

translation translation translationtranslation

p

bodyy

wheelwheel

Comparison

• Tree (one parent, several children per node):• very simple to build and manage• very simple to build and manage,• simple to traverse,• however, duplication of data

i i i din memory is required

• DAG (several parents, several children per node):• more complicated to build,• slightly more complicated to traverse,g y p• great advantage: no duplication of data

in memory, sharing of resources instancing instancing

Renderingg

• Rendering consists of traversing the graph:• a geometry node has to be rendered (primitives meshes)• a geometry node has to be rendered (primitives, meshes),• a transform node has to modify the transformation matrices,• a state node has to change a render state (color, material, shader, …)

• Depth-first traversal:• simple for a tree,• for a DAG, do as if it is a tree, nodes will be traversed several times if necessary

Page 3: Introduction to Scene Graphs Scene Graph - IRISA to Scene Graphs Kévin Boulanger November 2006 • When developing big projects lots of data have to be managedWhen developing big

Traversal of the graph (example 1)g ( )

Execution equivalent to the depth-first traversalvoid RenderCar(){

setMaterial(paint);renderBody();renderBody();

setMaterial(tire);

// Wheel 1glPushMatrix();glTranslatefv(transWheel1);

car

i t ti t i l glTranslatefv(transWheel1);renderWheel();glPopMatrix();

// Wheel 2glPushMatrix();glTranslatefv(transWheel2);translation translation translationtranslation

paint

body

tire material

renderWheel();glPopMatrix();

// Wheel 3glPushMatrix();glTranslatefv(transWheel3);

wheel

renderWheel();glPopMatrix();

// Wheel 4glPushMatrix();glTranslatefv(transWheel4);renderWheel();renderWheel();glPopMatrix();

}

Transform node

1. Save the current modelview matrix: glPushMatrix()

2 A l th t f ti2. Apply the transformation: glTranslatef(), glRotatef(), glMultMatrixf()

3. Render every children

4. Restore the modelview matrix: glPopMatrix()g p ()

Second example

H d d d f ti

transArmorigin point

Hand-coded function

glPushMatrix();glRotatef(-angle1);

angle1

Right arm

Left arm

render(arm1);

glTranslatef(transArm);glRotatef(-angle2);

render(arm2);angle2

render(arm2);

glPopMatrix();

glPushMatrix();glRotatef(angle1);

Direct traversal of the graph will render the same object but not

render(arm1);

glTranslatef(transArm);glRotatef(angle2);

render(arm2);

optimallyglPopMatrix();

Traversal of the graph (example 2)g ( )

Execution equivalent to

glPushMatrix();glRotatef(-angle1);

render(arm1);

robot claw the depth-first traversalglPushMatrix();glRotatef(-angle1);

render(arm1);render(arm1);

glPushMatrix();glTranslatef(transArm);

glPushMatrix();glRotatef(-angle2);

rotation-angle1

rotationangle1

render(arm1);

glPushMatrix();glTranslatef(transArm);

glPushMatrix();glRotatef(-angle2);

rotation-angle1

glRotatef( angle2);

render(arm2);

glPopMatrix();glPopMatrix();glPopMatrix();

translationtransArmarm1 translation

transArm

glRotatef( angle2);

render(arm2);

glPopMatrix();glPopMatrix();glPopMatrix();

glPushMatrix();glRotatef(angle1);

render(arm1);rotation-angle2

rotationangle2

glPushMatrix();glRotatef(angle1);

render(arm1);

glPushMatrix();glTranslatef(transArm);

glPushMatrix();glRotatef(angle2);

render(arm2);arm2

glPushMatrix();glTranslatef(transArm);

glPushMatrix();glRotatef(angle2);

render(arm2);render(arm2);

glPopMatrix();glPopMatrix();glPopMatrix();

render(arm2);

glPopMatrix();glPopMatrix();glPopMatrix();

Page 4: Introduction to Scene Graphs Scene Graph - IRISA to Scene Graphs Kévin Boulanger November 2006 • When developing big projects lots of data have to be managedWhen developing big

Comparison

Hand-coded function Graph traversal

glPushMatrix();glRotatef(-angle1);

render(arm1);

Hand-coded function

glPushMatrix();glRotatef(-angle1);

render(arm1);

Graph traversal

render(arm1);

glTranslatef(transArm);glRotatef(-angle2);

render(arm2);

render(arm1);

glPushMatrix();glTranslatef(transArm);

glPushMatrix();glRotatef(-angle2);

glPopMatrix();

glPushMatrix();glRotatef(angle1);

render(arm1);

glRotatef( angle2);

render(arm2);

glPopMatrix();glPopMatrix();glPopMatrix();

glTranslatef(transArm);glRotatef(angle2);

render(arm2);

glPushMatrix();glRotatef(angle1);

render(arm1);

glPopMatrix(); glPushMatrix();glTranslatef(transArm);

glPushMatrix();glRotatef(angle2);

render(arm2);render(arm2);

glPopMatrix();glPopMatrix();glPopMatrix();

Optimization

• Direct traversal of the graph for rendering is not optimal when there are many transform and render state nodes:

• useless glPushMatrix() and glPopMatrix() calls,• useless changes of render states (two times the same color)

• Usage of a structure in parallel, dedicated to rendering:Usage of a structure in parallel, dedicated to rendering:• the scene graph is traversed,• each encountered node is stored in a queue or a tree,• sorting is done to group render state changes• sorting is done to group render state changes,• if possible, matrix modifications are reduced,• finally, the queue or tree is traversed for rendering

• Sometimes quite difficult to do the sorting because of constraints, as for transparency (polygons have to be rendered from back to front)

Cullingg

• Culling consists in rendering parts of the 3D scene for optimization purposespurposes

• frustum culling allows to render only what is in the viewer field-of-view,

titi i th d i i i th i f th t• space partitioning methods minimize the size of the spaces to manage (BSP trees, octrees, etc.),

• Scene graphs allow efficient culling by removing full branches when traversing. If a node is considered out of view, it is not rendered, neither the children

• Bounding boxes or spheres are commonly associated with the nodes• the leaf nodes have associated bounding volumesthe leaf nodes have associated bounding volumes• the internal node bounding volumes enclose every bounding volume of the children• the closer to the root node the bigger are the bounding volumes• the closer to the root node, the bigger are the bounding volumes

Collision detection

• Scene graphs are well adapted to collision detection

• Approach similar to culling:pp g

• intersection points of rays with the scene have to be computed,

• if a ray does not collide the bounding volume of a node, there is no need to test the intersection with the children, a whole branch of the graph is eliminatedgraph is eliminated

• if a ray collides the bounding volume of a node, the intersection has to be found by testing the child nodesto be found by testing the child nodes

Page 5: Introduction to Scene Graphs Scene Graph - IRISA to Scene Graphs Kévin Boulanger November 2006 • When developing big projects lots of data have to be managedWhen developing big

Base node: groupg

group

• Connects a parent to several children• Used in graphs where every nodes have a single childg p y g

• When traversing for rendering,simply traverse every children for renderingsimply traverse every children for rendering

Base node: transform

transform transform

• Transform the child or every children(translation, rotation, scale, projection)( p j )

• Must save the current transformation matrix before rendering the childrenand restore it afterand restore it after

Base node: render state

render staterender state

• Change one of the current render states(color, material, lighting, transparency, shader, etc.)( g g p y )

• Implemented different ways depending on the graph design

• Must save the current render state before rendering the childrenMust save the current render state before rendering the childrenand restore it after

Base node: switch

switch

• Does a selection by rendering only the desired child• Often used for levels-of-detail management

(selection of the mesh quality depending on the distance from the(selection of the mesh quality depending on the distance from the camera for example)

Page 6: Introduction to Scene Graphs Scene Graph - IRISA to Scene Graphs Kévin Boulanger November 2006 • When developing big projects lots of data have to be managedWhen developing big

Scene graph designg g

• There are at least many different scene graph design approaches as the number of existing libraries

• Common examples:• usage of nodes for everything, with only one childusage of nodes for everything, with only one child

(group nodes have to be used)• usage of nodes for everything, with several children per node• usage of only transform nodes (several children) and geometry nodes• usage of only transform nodes (several children) and geometry nodes (leaves). Additional data are associated with each node.