Download Scene Graphs

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
Transcript
Scene Graphs
• In 3D Graphics programming the
structure used is a scene graph which is
special tree structure designed to store
information about a scene.
• Typical elements include
– geometries
– positional information
– lights
– fog
Recap
Camera paradigm for 3D viewing
• 3D viewing is similar to taking picture
with camera:
2D picture
– 2D view of 3D scene
Camera
3D scene
Content of 2D picture will depend on:
• camera parameters (position,
direction, field of view, ...),
• properties of scene objects,
• illumination, ...
Viewing Pipeline
Coordinate transformations:
2D picture
– generation of 3D view involves sequence
(pipeline) of coordinate transformationsyv
yw
ym
ym
xm
zm
zm
zm
zw
zv
xm
ym
xv
Camera
Device
coordinates
Viewing coordinates
xm
3D object
Modelling coordinates
xw
World coordinates
A scene graph is a data structure used to hold the
elements that make up a scene. It may be either a tree or
a Directed Acyclic
Graph (DAG).
The tree and DAG are similar, except that in the DAG the
branches may possibly grow back together.
Trees
• Start at the root and move
outward towards the leaves.
Normally shown with the root at
the top and the branches at the
bottom.
• A node is a part of this tree that
may have other nodes or leaves
underneath it, a leaf cannot have
other nodes or leaves under it.
• There is only one path from a leaf
to the root of the tree. There are
no "cycles", if you move outward
along the branches, you never
loop back around to end up at the
root again.
Directed acyclic graph (DAG)
• Directed means that the
parent-child relationship is
one-way, Acyclic means that
there can’t be loops, i.e.
child cant be one of its own
ancestors
• Like a tree, except maybe
the branches grow back
together sometimes, so that
following a different
sequence of branches
outwards from the root might
lead you to the exact same
leaf.
• Branches never grow in a
loop, though, so as long as
you keep moving outwards,
you always end up at a leaf
eventually:
Nodes
• The scene graph contains 'nodes' such as shape,
light, camera, etc.
• The tree structure is important because it allows the
scope of influence of scene parameters to be clear
and unambiguous.
• Nodes which have an unspecified number of children
below them are known as Group nodes. One of the
most important type of nodes is a Transform Group,
this modifies all of the shapes below it by
transforming them via a 4x4 matrix.
Simple scene graph
Root
node
Light
node
Fog
node
Group
node
Xform
node
Geom
node
Scene Graph Nodes
• Content Nodes
– contain basic elements of
a scene
•
•
•
•
Parent
Parent
geometry
light
position
fog
• Group Nodes
– no content
– link the hierarchy
– allow grouping of nodes
sharing a common state
Child #1
Child #2
Example Hierarchy
Root
Light
Group
Group
“Lampost”
“Dog”
Xform
Geom
Xform
Geom
T1
Dog
T2
Lampost
The Scene Graph
• The scene graph captures transformations and
object-object relationships in a suitable structure:
World
Objects
Robot
Head
Mouth
Instancing
(i.e, a matrix)
Body
Eye
Leg
Trunk
Arm
Legend
Traversing the Scene Graph
• Traverse the scene graph in depth-first order,
concatenating and undoing transforms:
– For example, to render a robot
•Apply robot -to-head matrix
•Apply head -to-mouth matrix
–Render mouth
•Un-apply head-to-mouth matrix
•Apply head-to-left eye matrix
–Render eye
•Un-apply head-to-left eye matrix
•Apply head-to-right eye matrix
–Render eye
•Un-apply head-to-right eye matrix
•Un-apply robot-to-head matrix
•Apply robot-to-body matrix
The Scene Graph in OpenGL
• OpenGL maintains a matrix stack of
modeling and viewing transformations:
Robot
Visited
Head
Body
Unvisited
Active
Matrix
Stack
Mouth
Eye
Leg
Foot
Trunk
Arm
OpenGL: The Matrix Stack
• The user can save the current
transformation matrix by pushing it onto
the stack with glPushMatrix()
• The user can later restore the most
recently pushed matrix with
glPopMatrix()
• These commands really only make
sense when in GL_MODELVIEW matrix
mode
OpenGL: Matrix Stack Example
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(…);
// save translation matrix:
glPushMatrix();
glRotatef(…);
// render something translated & rotated:
glCallList(…);
// restore pushed matrix, undoing rotation:
glPopMatrix();
// render something else, no rotation:
glCallList(…);
Data Structures
• Let’s have a look at the data structures
employed in more detail
• Selection of data structures for computer
graphics often driven by need for efficiency
– storage
– computation
• Trade-off between storage and
computational efficiency often applied
Data Structures
• Data structures are required for:
– scene specification
• object, polygon, point / vertex, ...
– mathematical manipulations
• vector, matrix, …
– graphical display
• buffer, ...
• Typical data structures: trees / scene
graphs, linked lists, arrays
Data Structures
Computer graphics
often use hierarchical
data structures, e.g.
Note:
other possible levels: object groups,
facet groups (surfaces), edges
vertex may also link back to facets
which share vertex (for shading)
Linked list
of objects
Scene
Linked lists
of facets
Objects
Linked lists
of vertices
Facets
Structures
with x, y, z
coordinates
Vertices
Data Structures
Object 1
Object 2
Object N
Object list
Facet 1
Facet M
Facet lists
Vertex array
Vertices
Facet list and
vertex array
Possible architecture
Data Structures
/* 3D point or vertex with integer coordinates */
typedef struct structTag3DiPoint
{
int xCoordinate,
/* x coordinate */
yCoordinate,
/* y coordinate */
zCoordinate;
/* z coordinate */
}
int3DPoint,
/* 3D point */
* pInt3DPoint,
/* pointer to a 3D point */
int3DVertex,
/* 3D vertex */
* pInt3DVertex;
/* pointer to a 3D vertex */
Possible structure for 3D point or vertex
Data Structures
/* Polygon in 3D space */
typedef struct structTag3DiPolygon
{
int3DVertex
i3SidedPoly[3];
int
colour,
visibilityFlag;
float
magNormal;
struct structTag3DiPolygon * link2NextPolygon;
/* Other attributes can go here */
}
int3DPolygon,
/* 3D Polygon */
* pInt3DPolygon,
/* pointer to a 3D Polygon */
int3DFacet,
/* 3D facet */
* pInt3DFacet;
/* pointer to a 3D facet */
Possible structure for polygon
Data Structures
/* Object in 3D space */
typedef struct structTag3DiObject
{
pInt3DFacet
pFacetList;
pInt3DVertex
pVertexArray;
int
numVertices;
int3DPoint
worldPosition;
struct structTag3DiObject *
link2NextObject;
/* Other attributes can go here */
}
int3DObject,
/* 3D Object */
* pInt3DObject;
/* pointer to a 3D Object */
Possible structure for 3D object
Data Structures
• To synthesise copies of an object
– master / instance architecture
• master defines generic attributes of object
• instance defines attribute values of particular copy
Instances
Master
Data Structures
Object 1
Instances
Object 2
tm att
tm att
Master 1
Masters
tm: transf. matrix
att: attributes
Object N
tm att
Master M
car
ball
Facet list
Facet list
Edge list
Edge list
Vertex list
Vertex list
Possible architecture
Background: linear algebra
• Quick review of important concepts
• Point: location (x, y, z)
• Vector: direction and magnitude
<x, y, z>
Vectors
• Magnitude of a vector: |v|
^
• Direction of a vector, unit vector: v
• Affine sum:
P = a Q + (1-a) R
Dot Product
• Def: u • v = ux vx + uy vy+ uz vz
• u • v = |u| |v| cos θ
• Uses:
– Angle between two vectors?
– Are two vectors perpendicular?
– Do two vectors form
acute or obtuse
angle?
– Is a face visible?
(backface culling)
Cross Product
• u  v = <uyvz - uzvy, uzvx - uxvz, uxvy - uyvx>
• Direction: normal to plane containing u, v (using
right-hand rule in right-handed coordinate system)
• Magnitude: |u||v| sin θ
• Uses:
– Face outward normal?
– Angle between vectors?
– Do two line segments
intersect?
Face outward normals
• How to find the outward normal of a face?
– Assume that vertices are listed in a standard
order when viewed from the outside -- counterclockwise
– Cross product of the first two edges is outward
normal vector
– Note that first corner must be convex
Surface Normals
• For a polygon , a surface normal
can be calculated as the vector
cross product of two (nonparallel) edges of the polygon.
• For a plane given by the equation
ax + by + cz = d, the vector
(a,b,c) is a normal. For a plane
given by the equation
r = a + αb + βc, where a is a
vector to get onto the plane and b
and c are non-parallel vectors
lying on the plane, the normal to
the plane defined is given by
b × c (the cross product of the
vectors lying on the plane).
Coordinate systems and frames
• Hierarchical modeling
• May deal with many coordinate systems:
viewer, model, world, viewport
• Frame: origin + basis vectors (axes)
• Need to transform between frames
• E.g. reading in a world description with
several objects…
Transformations
• Changes in coordinate systems usually involve
– Translation
– Rotation
– Scale
• Rotation and scale can be represented as 3x3
matrices, but not translation
• We're also interested in a perspective
transformation
• We use 4D "Homogeneous coordinates"
Homogeneous Coordinates
• A point: (x, y, z, w) where w is a "scale factor"
• Converting a 3D point to homogeneous
coordinates: (x, y, z)  (x, y, z, 1)
• Transforming back to 3-space: divide by w
– (x, y, z, w)  (x/w, y/w, z/w)
• (3, 2, 1): same as
(3, 2, 1, 1) = (6, 4, 2, 2) = (1.5, 1, 0.5, 0.5)
• Where is the point (3, 2, 1, 0)?
– Point at infinity or "pure direction."
– Used for vectors (vs. points)
Homogeneous transformations
• Most important reason for using homogeneous
coordinates:
– All affine transformations (line-preserving: translation,
rotation, scale, perspective, skew) can be represented as
a matrix multiplication
– You can concatenate several such transformations by
multiplying the matrices together. Just as fast as a
single transform!
– Modern graphics cards implement homogeneous
transformations in hardware
Using Modelling Software
• Maya
• 3d Studio Max
• VRML generators
Features
•
•
•
•
•
•
•
•
Primitives
Groups
Complex , irregular shapes
Lines, Points, Facets
Curved surfaces
Texture mapped surfaces
Lighting and Shading
Interactions and Behaviours
Primitives
• Facets constructed from known
geometric relationships
• Uses polygons to map to standard
mesh structure
• Scaling , shearing, rotation and
translation used to modify vertex
information, vertex ordering remains
same
Complex or Irregular objects
• Manual construction
– Lines and vertices positioned by hand/ eye
– Modification of primitives
– Extrusions
• Curved surfaces
–
–
–
–
B-splines
Bezier curves
Parametric meshes
etc
Scene view
• Scene hierarchy required
• Must have mechanism to store results
• Output file structure must link to internal structure
–
–
–
–
–
–
–
Hierarchy
Relationship between hierarchical nodes
Vertex list
Vertex ordering list
Lighting information
Texture mapping
May also hold recent viewing parameters
3DS File structure
•
•
•
•
May be ASCII output
Tags outline structures
Must read between Tags
Comma delimitation usually to separate
vertex list and ordering information
The 3ds File Structure
• Sometimes more efficient to read binary
output
• Consists of chunks of binary data
Chunks
• A chunk is composed of 4 fields:
– Identifier: a hexadecimal number of two byte of length that
identify the chunk. With this information we can immediately
realise if the chunk is useful for our purpose. If we need the
chunk we extrapolate the contained information in it and, if
necessary, in its children, if instead the chunk is useless we
jump it using the following information...
– Length of the chunk: another number, this time of 4 byte, that
is the sum of the chunk length and all the lengths of every
contained sub-chunks.
– Chunk data: this field has a variable length. The real data of
the chunk are contained in this field.
– Sub-Chunks:
Typical structure
Offset
Length
0
2
Chunk identifier
2
4
Chunk length: chunk data + subchunks(6+n+m)
6
n
Data
6+n
m
Sub-chunks
Example
• MAIN CHUNK 0x4D4D
3D EDITOR CHUNK 0x3D3D
OBJECT BLOCK 0x4000
TRIANGULAR MESH 0x4100
VERTICES LIST 0x4110
FACES DESCRIPTION 0x4120
FACES MATERIAL 0x4130
MAPPING COORDINATES LIST 0x4140
SMOOTHING GROUP LIST 0x4150
LOCAL COORDINATES SYSTEM 0x4160
LIGHT 0x4600
SPOTLIGHT 0x4610
CAMERA 0x4700
MATERIAL BLOCK 0xAFFF
Traversal
• If we for example want to reach the
chunk VERTICES LIST
– we have to read the MAIN CHUNK,
– the 3D EDITOR CHUNK,
– the OBJECT BLOCK
– and finally the TRIANGULAR MESH.
Chunk Identification
MAIN CHUNK
Identifier
0x4D4D
Length
0 + sub-chunks length
Chunk father
None
Sub chunks
3D EDITOR CHUNK
Data
None
3D EDITOR CHUNK
Identifier
0x3D3D
Length
0 + sub-chunks length
Chunk father
MAIN CHUNK
Sub chunks
OBJECT BLOCK, MATERIAL BLOCK, KEYFRAMER CHUNK
Data
None
OBJECT BLOCK
Identifier
0x4000
Length
Object name length + sub-chunks length
Chunk father
3D EDITOR CHUNK
Sub chunks
TRIANGULAR MESH, LIGHT, CAMERA
Data
Object name
TRIANGULAR MESH
Identifier
0x4100
Length
0 + sub-chunks length
Chunk father
OBJECT BLOCK
Sub chunks
VERTICES LIST, FACES DESCRIPTION, MAPPING COORDINATES
LIST
Data
None
VERTICES LIST
Identifier
0x4110
Length
varying + sub-chunks length
Chunk father
TRIANGULAR MESH
Sub chunks
None
Data
Vertices number (unsigned short)
Vertices list: x1,y1,z1,x2,y2,z2 etc. (for each
vertex: 3*float)
FACES DESCRIPTION
Identifier
0x4120
Length
varying + sub-chunks length
Chunk father
TRIANGULAR MESH
Sub chunks
FACES MATERIAL
Data
Polygons number (unsigned short)
Polygons list: a1,b1,c1,a2,b2,c2 etc. (for each
point: 3*unsigned short)
Face flag: face options, sides visibility etc.
(unsigned short)
MAPPING COORDINATES LIST
Identifier
0x4140
Length
varying + sub-chunks length
Chunk father
TRIANGULAR MESH
Sub chunks
SMOOTHING GROUP LIST
Data
Vertices number (unsigned short)
Mapping coordinates list: u1,v1,u2,v2 etc. (for each
vertex: 2*float)
Load a 3DS object
• Implement a "while" loop that continues its
execution until the end of file is reached.
• For each cycle read the chunk_id and the
chunk_length.
• Through a switch analyse the content of the
chunk_id
– If the chunk is a section of the tree not needed to
pass then jump the whole length of the chunk
moving the file pointer to the position calculated
using the length of the chunk added to the current
position. In this way jump the chunk and all the
contained sub-chunks.
– Or if the chunk enables reach of another needed
chunk, or maybe it contains data that is needed,
then read its data, then read the next chunk.