Download - Models & Hierarchies CSE167: Computer Graphics Instructor: Steve Rotenberg UCSD, Fall 2005.

Transcript
Page 1: Models & Hierarchies CSE167: Computer Graphics Instructor: Steve Rotenberg UCSD, Fall 2005.

Models & Hierarchies

CSE167: Computer Graphics

Instructor: Steve Rotenberg

UCSD, Fall 2005

Page 2: Models & Hierarchies CSE167: Computer Graphics Instructor: Steve Rotenberg UCSD, Fall 2005.

Normals

The concept of normals is essential to lighting Intuitively, we might think of a flat triangle as having a

constant normal across the front face However, in computer graphics, it is most common to

specify normals and perform lighting at the vertices This gives us a method of modeling smooth surfaces as

a mesh of triangles with ‘shared’ normals at the vertices We will talk about lighting in the next lecture, but for

today, we will still think of our vertex as containing a normal

Page 3: Models & Hierarchies CSE167: Computer Graphics Instructor: Steve Rotenberg UCSD, Fall 2005.

Models

We will extend our concept of a Model to include normals We can do this by simply extending our vertex class:

class Vertex {Vector3 Position;Vector3 Color;Vector3 Normal;

public:void Draw() {

glColor3f(Color.x, Color.y, Color.z);glNormal3f(Normal.x, Normal.y, Normal.z);glVertex3f(Position.x, Position.y, Position.z); // This has to be last

}}

Page 4: Models & Hierarchies CSE167: Computer Graphics Instructor: Steve Rotenberg UCSD, Fall 2005.

Model Data Structures

Everybody knows that a cube has 8 vertices If we need to render a cube, however, each of

those vertices requires 3 different normals. In other words, we might really need 3*8=24 vertices

If we render it as triangles, each 4-sided face actually requires 6 vertices, meaning that we might need to store and process 36 different vertices!

Page 5: Models & Hierarchies CSE167: Computer Graphics Instructor: Steve Rotenberg UCSD, Fall 2005.

Indexed Models

So far, we have simply thought of a model as an array of triangles, each triangle storing 3 unique vertices

A more common method of storing a model is as an array of vertices, and an array of triangles

In the second method, each triangle stores an index (or pointer) to a vertex instead of storing the vertex data explicitly

This is called an indexed model or single indexed model Indexing will almost always save memory, as models often have

shared vertices that are used by several triangles Large, smooth meshes will often share a single vertex between 4-6

triangles (or more) Indexing can also save processing time as the vertex array can first

be transformed and lit, and then the triangle array can be clipped and scan converted…

Page 6: Models & Hierarchies CSE167: Computer Graphics Instructor: Steve Rotenberg UCSD, Fall 2005.

Single Indexed Model

class Vertex {Vector3 Position;Vector3 Color;Vector3 Normal;

};

class Triangle { Vertex *Vert[3]; // or int Vert[3];

};

class Model {int NumVerts,NumTris;Vertex *Vert;Triangle *Tri;

};

Page 7: Models & Hierarchies CSE167: Computer Graphics Instructor: Steve Rotenberg UCSD, Fall 2005.

Index vs. Pointer

Should we store the triangle verts as integers (indexing into the array of actual Vertex’s) ?

int Vert[3]; Or should we store them as pointers to the actual Vertex’s themselves ?

Vertex *Vert[3];

Memory: In most systems an int is 4 bytes and a pointer is 4 bytes, so there isn’t a big

difference in memory However, for smaller models, you could benefit from using short ints, which are 2

bytes each. This would cut the triangle size in half, but limit you to 65536 vertices Performance:

Storing Vertex*’s gives the triangle direct access to the data so should be faster Other Issues:

It’s definitely more convenient to store the pointers instead of integers One important reason to consider storing integers instead of pointers, however,

is if you are using some type of dynamic array for the vertices (such as an STL vector). Pointers to members of these arrays are considered dangerous, since the array may have to reallocate itself if more vertices are added

Page 8: Models & Hierarchies CSE167: Computer Graphics Instructor: Steve Rotenberg UCSD, Fall 2005.

Double Indexing

If memory is really tight, one could also consider double indexing the model

In this scheme, there are arrays of position, normal, and color vectors and vertices themselves index into those arrays

This method was useful for realtime software renderers of a few years ago, but it not too common any more

Most hardware renderers are designed to take models either un-indexed or single indexed

Page 9: Models & Hierarchies CSE167: Computer Graphics Instructor: Steve Rotenberg UCSD, Fall 2005.

Double Indexing

class Vertex {Vector3 *Position;Vector3 *Color;Vector3 *Normal;

};

class Triangle { Vertex *Vert[3]; // or int Vert[3];

};

class Model {int NumPositions,NumColors,NumNormals;Vector3 *Position,*Normal,*Color;int NumVerts,NumTris;Vertex *Vert;Triangle *Tri;

};

Page 10: Models & Hierarchies CSE167: Computer Graphics Instructor: Steve Rotenberg UCSD, Fall 2005.

Vertex Buffers

Hardware rendering API’s (like Direct3D and OpenGL) support some type of vertex buffer system as well (but everybody has a different name for it)

This is essentially an unindexed or single indexed model format You start by defining your specific vertex type. Verts usually have a position

and normal, and might have one or more colors, texture coordinates, or other properties

You then request a vertex buffer of whatever memory size you want. This memory is usually the actual video memory on the graphics board (not main memory)

The vertex buffer can then be filled up with vertex data as a single large array

One can then draw from the vertex buffer with a command like this:

DrawSomething(int type,int first vert,int numverts); // type: triangles, strips, lines…

The advantage is that a large number of triangles can be drawn with a single CPU call and all of the work then takes place entirely on the graphics board

Page 11: Models & Hierarchies CSE167: Computer Graphics Instructor: Steve Rotenberg UCSD, Fall 2005.

Index Buffers

An index buffer (or whatever name one calls it) is an array of (usually 2 byte or 4 byte) integers

It is stored in video memory like the vertex buffer The integers index into a vertex buffer array One can then draw triangles (or other primitives)

by specifying a range of these indexes

Using vertex/index buffers is most likely going to be the fastest way to render on modern graphics hardware

Page 12: Models & Hierarchies CSE167: Computer Graphics Instructor: Steve Rotenberg UCSD, Fall 2005.

Triangles, Strips, Fans

Graphics hardware usually supports slightly more elaborate primitives than single triangles

Most common extensions are strips and fans

v0

v1

v2

v4

v6v8

v7

v5v3

v0

v1

v2

v3v4

v5

v6

v7

Page 13: Models & Hierarchies CSE167: Computer Graphics Instructor: Steve Rotenberg UCSD, Fall 2005.

Materials & Grouping

Usually models are made up from several different materials

The triangles are usually grouped and drawn by material

Page 14: Models & Hierarchies CSE167: Computer Graphics Instructor: Steve Rotenberg UCSD, Fall 2005.

Model I/O

Usually, a Model class would have the ability to load data from some sort of file

There are a variety of 3D model formats out there, but unfortunately, there are no universally accepted standards

Page 15: Models & Hierarchies CSE167: Computer Graphics Instructor: Steve Rotenberg UCSD, Fall 2005.

3D Modelers

There are a variety of 3D modeling programs in use today

Popular ones include Maya (by Alias) and 3D Studio (by Autodesk). Interestingly, Autodesk bought Alias a couple weeks ago…

There are several other 3D programs out there as well, including some free ones on the web…

Page 16: Models & Hierarchies CSE167: Computer Graphics Instructor: Steve Rotenberg UCSD, Fall 2005.

Modeling Primitives

Interactive 3D modeling tools usually provide higher level primitives than triangles

Often, modelers allow the use of some type of curved surfaces

Curved surfaces are usually defined by some sort of grid of points, which is then smoothly interpolated by an automatic algorithm

Common surface types include: bicubic, Bezier, B-Spline, NURBS, subdivision surfaces, and more…

We will discuss these in more detail in a later lecture

Page 17: Models & Hierarchies CSE167: Computer Graphics Instructor: Steve Rotenberg UCSD, Fall 2005.

Editable Models

Some applications simply need to load and display 3D models (such as a video game or 3D renderer)

Other applications need to dynamically modify or construct models on the fly (like a 3D modeling tool)

One would make different choices about how these are stored and manipulated

Page 18: Models & Hierarchies CSE167: Computer Graphics Instructor: Steve Rotenberg UCSD, Fall 2005.

Modeling Operations

There are tons of different operations one might wish to perform in the interactive modeling process

We will look at some of the most common ones We will assume that we are dealing with a single

indexed model with an array of vertices and an array of triangles

Most of these operations can be extended to more complex model and primitive types as well

Page 19: Models & Hierarchies CSE167: Computer Graphics Instructor: Steve Rotenberg UCSD, Fall 2005.

Create / Delete

The most basic operations are:

Vertex *CreateVertex();void DeleteVertex(int v);

Triangle *CreateTriangle();void DeleteTriangle(int t);

Just about all higher level modeling functions can be broken down into these basic operations

All higher level functions go through these interfaces to create and remove data

These functions need to be fast and reliable The ‘delete’ operations can be done in different ways and aren’t as simple

as they might first look…

Page 20: Models & Hierarchies CSE167: Computer Graphics Instructor: Steve Rotenberg UCSD, Fall 2005.

Modeling Operations

Digitize Copy/dupe Triangulate Extrude Lathe Border Extrude Boolean Procedural modeling, scripts…

Page 21: Models & Hierarchies CSE167: Computer Graphics Instructor: Steve Rotenberg UCSD, Fall 2005.

Hierarchical Transformations

Page 22: Models & Hierarchies CSE167: Computer Graphics Instructor: Steve Rotenberg UCSD, Fall 2005.

Hierarchical Transformations We have seen how a matrix be used to place an individual object

into a virtual 3D world Sometimes, we have objects that are grouped together in some way

For example, we might have an articulated figure which contains several rigid components connected together in some fashion

Or we might have several objects sitting on a tray that is being carried around

Or we might have a bunch of moons and planets orbiting around in a solar system

Or we might have a hotel with 1000 rooms, each room containing a bed, chairs, table, etc.

In each of these cases, the placement of objects is described more easily when one considers their locations relative to each other

We will see how hierarchical transformations can be used to describe their placement

Page 23: Models & Hierarchies CSE167: Computer Graphics Instructor: Steve Rotenberg UCSD, Fall 2005.

Scene Hierarchy

If a scene contains 1000 objects, we might think of a simple organization like this:

Scene

Object 1 Object 2 Object 3 Object 1000…

Page 24: Models & Hierarchies CSE167: Computer Graphics Instructor: Steve Rotenberg UCSD, Fall 2005.

Scene Hierarchy

Or we could go for a more hierarchical grouping like:

Scene

Room 1 Room 2 Room 3

Chair 1 Chair 2 Table

Book Monitor

Bed Dresseretc…

Page 25: Models & Hierarchies CSE167: Computer Graphics Instructor: Steve Rotenberg UCSD, Fall 2005.

Scene Hierarchy

It is very common in computer graphics to define a complex scene in some sort of hierarchical fashion

The individual objects are grouped into a tree like structure (upside down tree)

Each moving part is a single node in the tree The node at the top is the root node A node directly above another is that node’s parent A node below another is a child and nodes with the

same parent are called siblings Nodes at the bottom of the tree with no children are

called leaf nodes

Page 26: Models & Hierarchies CSE167: Computer Graphics Instructor: Steve Rotenberg UCSD, Fall 2005.

Articulated Figures

An articulated figure is an example of a hierarchical object

The moving parts can be arranged into a tree data structure if we choose some particular piece as the ‘root’

For an articulated figure (like a biped character), we usually choose the root to be somewhere near the center of the torso

Each joint in the figure has specific allowable degrees of freedom (DOFs) that define the range of possible poses for the figure

Page 27: Models & Hierarchies CSE167: Computer Graphics Instructor: Steve Rotenberg UCSD, Fall 2005.

Example Articulated Figure

Root

Torso

Neck

Pelvis

HipL HipR

Head ElbowL

WristL

ElbowR

WristR

KneeL

AnkleL

KneeR

AnkleR

ShoulderL ShoulderR

Page 28: Models & Hierarchies CSE167: Computer Graphics Instructor: Steve Rotenberg UCSD, Fall 2005.

Hierarchical Transformations

We assume that each node in the tree graph represents some object that has a matrix describing its location and a model describing its geometry

When a node up in the tree moves its matrix, it takes its children with it (in other words, rotating a character’s shoulder joint will cause the elbow, wrist, and fingers to move as well)

Page 29: Models & Hierarchies CSE167: Computer Graphics Instructor: Steve Rotenberg UCSD, Fall 2005.

Local Matrices

We will assume a tree structure where child nodes inherit transformations from the parent nodes

Each node in the tree stores a local matrix which is its transformation relative to its parent

To compute a node’s world space matrix, we need to concatenate its local matrix with its parent’s world matrix:

W=Wparent·L

Page 30: Models & Hierarchies CSE167: Computer Graphics Instructor: Steve Rotenberg UCSD, Fall 2005.

Recursive Traversal

To compute all of the world matrices in the scene, we can traverse the tree in a depth-first traversal

As each node is traversed, we compute its world space matrix

By the time a node is traversed, we are guaranteed that the parent’s world matrix is available

Page 31: Models & Hierarchies CSE167: Computer Graphics Instructor: Steve Rotenberg UCSD, Fall 2005.

Forward Kinematics

In the recursive tree traversal, each joint first computes its local matrix L based on the values of its DOFs and some formula representative of the joint type:

Local matrix L = Ljoint(φ1,φ2,…,φN)

Then, world matrix W is computed by concatenating L with the world matrix of the parent joint

World matrix W = Wparent · L

Page 32: Models & Hierarchies CSE167: Computer Graphics Instructor: Steve Rotenberg UCSD, Fall 2005.

GL Matrix Stack

The GL matrix stack is set up to facilitate the rendering of hierarchical scenes

While traversing the tree, we can call glPushMatrix() when going down a level and glPopMatrix() when coming back up

Page 33: Models & Hierarchies CSE167: Computer Graphics Instructor: Steve Rotenberg UCSD, Fall 2005.

Hierarchical Culling

Scene hierarchies can also assist in the culling process

Each object has a precomputed bounding sphere

This sphere is compared against the view volume to determine if the object is visible

We can also do hierarchical culling where each sphere contains all of its children as well

Culling a sphere automatically culls an entire subtree of the scene

Page 34: Models & Hierarchies CSE167: Computer Graphics Instructor: Steve Rotenberg UCSD, Fall 2005.

Project 2

In project 2, you are must create some sort of simple articulated figure, such as a hand

It must perform some simple animation (such as opening/closing the fingers)

It must be object oriented and make use of classes for key objects such as: Camera, Light, Model, Hand…

Page 35: Models & Hierarchies CSE167: Computer Graphics Instructor: Steve Rotenberg UCSD, Fall 2005.

Cameras

Camera {float FOV, Aspect, NearClip, FarClip;Vector3 Position, Target;

public:Camera();void DrawBegin();void DrawEnd();void SetAspect(float a);

};

Page 36: Models & Hierarchies CSE167: Computer Graphics Instructor: Steve Rotenberg UCSD, Fall 2005.

Camera

void Camera::DrawBegin() {glClear();

glMatrixMode(GL_PROJECTION);glLoadIdentity();gluPerspective(FOV,Aspect,NearClip,FarClip);

glMatrixMode(GL_MODELVIEW);glLoadIdentity();gluLookAt(Position.x,Position,y,Position.z,

Target.x,Target.y,Target.z,0,1,0);glPushMatrix();

}

void Camera::DrawBegin() {glPopMatrix();glSwapBuffers();

}