Title: Object Modeling
1Object Modeling
2Objectives
- Introduce simple data structures for building
polygonal models - Vertex lists
- Edge lists
- Non-hierarchical vs. Hierarchical Approaches
- OpenGL vertex arrays
- Polygonal mesh
3Nonhierarchical Approach
- Each object is modeled as a symbol.
- each symbol is represented at a convenient size
and orientation in its frame, object frame. -
4Nonhierarchical Approach
- glMatrixMode( GL_MODELVIEW )
- glLoadIdentity( )
- glTranslate( dx, dy, dz )
- glRotatef( angle, rx, ry, rz )
- glScale( sx, sy, sz )
- glutsolidCube( side )
Transformation needs defined before the drawing!
5Nonhierarchical Approach
- The world is composed of a collection of symbols.
- We may use a table to store the information for
each symbol
There are two 1s with different sizes and
locations.
6Example CAD
- Each picture of the object (NAND gate) in the
world area is an instance of the object. -
for (i0 iltnumGates i) //push stack
translate2D(dxi,dyi) rotate2D(Ai)
scale2D(Si,Si) drawGate(typei)
//pop stack
7Hierarchical Approach
- We represent the relationship among parts of the
model with graph, e.g. tree. - Example an automobile has a chassis and 4 wheels.
Each node stores the information about the
geometric object, such as size, size. Each edge
may store location and orientation.
8Hierarchical Approach - A Robot Arm
- The robot arm consists of three components. The
mechanism has three degrees of freedom. -
9Hierarchical Approach - A Robot Arm
- display()
- glRotatef(theta, 0.0, 1.0, 0.0)
- base()
- glTranslatef(0.0, h1, 0.0)
- glRotatef(phi, 0.0, 0.0, 1.0)
- lower_arm()
- glTranslatef(0.0, h2, 0.0)
- glRotatef(psi, 0.0, 0.0, 1.0)
- upper_arm()
- The above effort does not use tree structure.
- Each component is treated separately
- Relationship between components is not explored
at all.
10Hierarchical Approach - A Robot Arm
- To use tree structure in the modeling of this
object, we will store all necessary information
in the node -
- To do tree traversal, we start from the Base node.
11Tree Traversal
- We always traverse by depth first
- start with the left branch, follow it to the left
as deep as we can go - then go back to the first right branch, and
process recursively
12Tree Traversal A Stack Based
- figure ()
- glPushMatrix()
- torso()
- glTranslate(...)
- glRotate3(...)
- head()
- glPopMatrix()
- glPushMatrix()
- glTranslate(...)
- glRotate3(...)
- left_upper_leg()
- glTranslate(...)
- glRotate3(...)
- left_lower_leg()
- glPopMatrix()
- glPushMatrix()
- glTranslate(...)
- glRotate3(...)
- right_upper_leg()
- The glPushMatrix() duplicates the current
model-view matrix, puts the copy on the top of
the model-view-matrix stack. - The following calls to glTranslate() and
glRotate() determine Mh and concatenate it with
the initial model-view matrix. - The subsequent glPopMatrix() recovers the
original model-view matrix.
13Modeling with Polygonal Mesh
- A component itself may be an complex, could not
be described by a simple cylinder or block - Polygonal meshes are collections of polygons, or
faces. Each face has a direction (normal
vector). -
14Inward and Outward Facing
- The ordering of points to be drawn in the program
indicates the orientation of the polygon! -
- The outwardly facing polygons
-
15Simple Representation
- Define each polygon by the geometric locations of
its vertices. In OpenGL, we do - glBegin(GL_POLYGON)
- glVertex3f(x2, y2, z2)
- glVertex3f(x1, y1, z1)
- glVertex3f(x0, y0, z0)
- glVertex3f(x3, x3, x3)
- glEnd()
- Inefficient and unstructured
-
16Example Modeling a Cube
- To model a color cube for rotating cube program,
we define global arrays for vertices and colors
GLfloat vertices3 -1.0,-1.0,-1.0,1.0,-1
.0,-1.0, 1.0,1.0,-1.0, -1.0,1.0,-1.0,
-1.0,-1.0,1.0, 1.0,-1.0,1.0, 1.0,1.0,1.0,
-1.0,1.0,1.0 GLfloat colors3
0.0,0.0,0.0, 1.0,0.0,0.0, 1.0,1.0,0.0, 0.0
,1.0,0.0, 0.0,0.0,1.0, 1.0,0.0,1.0,
1.0,1.0,1.0, 0.0,1.0,1.0
17Drawing a polygon
- Draw a quadrilateral from a list of indices into
the array vertices and use color corresponding to
first index
void polygon(int a, int b, int c , int d)
glBegin(GL_POLYGON) glColor3fv(colorsa)
glVertex3fv(verticesa)
glVertex3fv(verticesb)
glVertex3fv(verticesc)
glVertex3fv(verticesd) glEnd()
18Draw cube from faces
- void colorcube( )
-
- polygon(0,3,2,1)
- polygon(2,3,7,6)
- polygon(0,4,7,3)
- polygon(1,2,6,5)
- polygon(4,5,6,7)
- polygon(0,1,5,4)
Note that vertices are ordered so that we obtain
correct outward facing normals
19Geometry vs Topology
- It is a good idea to use separate data structures
for geometry and topology - Geometry
- Topology
- Example
- Topology holds even if geometry changes
20Representing a Mesh
- The mesh consists of 8 nodes and 12 edges
- 5 interior polygons
- 6 interior (shared) edges
- Each vertex has a location vi (xi yi zi)
21Vertex List
22Edge List
- Polygons will be drawn correctly, but shared
edges are drawn twice - Can store mesh by edge list
23Normal Vector
- Normal vector is used in shading process how
much light is scattered off the face. - Associate a normal with each vertex of a face.
24Normal Vector
- If the face is flat and has 3 points V1, V2 and
V3, the normal is
25Normal Vector
- Martin Newell method assume N is the number of
vertices in the face, (xi, yi, zi) is the
position of the ith vector, next(j)(j1) mod N
is the index of the next vertex after vertex j.
26Representing Polygonal Mesh
- A polygonal mesh consists of 3 lists a vertex
list, a normal list and a face list. - Vertex list reports the locations of vertices in
the mesh. - Normal list reports the direction of norms.
- Face list indexes into vertex and normal lists.
27Polygonal Mesh
vertex x y z 0 0 0 0 1 1
0 0 2 1 1 0 3 0.5
1.5 0 4 0 1 0 5 0 0 1
6 1 0 1 7 1 1 1 8
0.5 1.5 1 9 0 1 1
face vertices normal 0 (left)
0,5,9,4 0,0,0,0 1 (roof left)
3,4,9,8 1,1,1,1 2 (roof right)
2,3,8,7 2,2,2,1 3 (right)
1,2,7,6 3,3,3,3 4 (bottom)
0,1,6,5 4,4,4,4 5 (front)
5,6,7,8,9 5,5,5,5,5 6 (back)
0,4,3,2,1 6,6,6,6,6
normal nx ny nz 0 -1
0 0 1 -.707 -.707 -.707 2
.707 .707 0 3 1
0 0 4 0 -1 0 5 0
0 1 6 0 0 -1
28Program to Handle Meshes
class VertexID public int vertIndex //
index of vertex int normIndex
// index of normal class Face public
int nVerts // number of vertices
VertexID vert //list of vert norm
indices Face()nVerts 0
vert NULL //constr.
Face()delete vert nVerts 0 //
destr. class Mesh private int
numVerts // number of vertices
Point3 pt // array of 3D vertices
int numNormals // number of norms
Vector3 norm // array of
normals int numFaces //
number of faces Face face
// array of face data . public
Mesh() // constructor
Mesh() // destructor int
readFile(char fileName) //read in mesh
29Program to Handle Meshes
void Mesh draw() for(int f
0 f lt numFaces f) // draw each face
glBegin(GL_POLYGON) for(int v 0 v
lt facef.nVerts v) int in
facef.vertv.normIndex // index of norm
int iv facef.vertv.vertIndex //
index of vertex glNormal3f(normin.
x, normin.y, normin.z)
glVertex3f(ptiv.x, ptiv.y, ptiv.z)
glEnd()