Docsity

Polygonal Meshes - Computer Graphics - Lecture Slides, Slides of Computer Graphics

Partial preview of the text

Download Polygonal Meshes - Computer Graphics - Lecture Slides and more Slides Computer Graphics in PDF only on Docsity!

Computer Graphics3D Modeling: Polygonal Meshes

3D Modeling

n Previously n Introduced 3D modeling n Previously introduced GLUT models (wireframe/solid) and Scene Description Language (SDL): 3D file format n Previously used GLUT calls

n Cylinder: glutWireCylinder( ), glutSolidCylinder( )

n Cone: glutWireCone( ), glutSolidCone( )

n Sphere: glutWireSphere( ), glutSolidSphere( )

n Cube: glutWireCube( ), glutSolidCube( )

n Newell Teapot, torus, etc

Polygonal Meshes

n Have become standard in CG n OpenGL

n Good at drawing polygon

n Mesh = sequence of polygons

n Simple meshes exact. (e.g barn) n Complex meshes approximate (e.g. human face) n Later: use shading technique to smoothen

Non-solid Objects

n Examples: box, face n Visualize as infinitely thin skin n Meshes to approximate complex objects n Shading used later to smoothen n Non-trivial: creating mesh for complex objects (CAD)

Vertex Normal

n Use vertex normal instead of face normal n See advantages later:

n Facilitates clipping

n Shading of smoothly curved shapes

n Flat surfaces: all vertices associated with same n

n Smoothly curved surfaces: V1 , V2 with common edge share n

Defining Polygonal Mesh

n Use barn example below:

Newell Method for Normal Vectors

n Martin Newell at Utah (teapot guy) n Normal vector:

n calculation difficult by hand

n Given formulae, suitable for computer

n Compute during mesh generation

n Simple approach used previously:

n Start with any three vertices V1, V2, V

n Form two vectors, say V1-V2, V3-V

n Normal: cross product (perp) of vectors

Newell Method for Normal Vectors

n Problems with simple approach:

n If two vectors are almost parallel, cross product is small

n Numerical inaccuracy may result

n Newell method: robust

n Formulae: Normal N = (mx, my, mz)

0 (^ ) i next i

N

mx = ∑ i yi − ynext i z + z

0 (^ ) i next i

N

y (^) i i next i

m = ∑ z − z x + x

0 (^ ) i next i

N

m z = ∑ i xi − xnext i y + y

Meshes in Programs

n Class Mesh n Helper classes

n VertexID

n Face

n Mesh Object:

n Normal list

n Vertex list

n Face list

n Use arrays of pt, norm, face n Dynamic allocation at runtime n Array lengths: numVerts, numNormals, numFaces

Meshes in Programs

n Face:

n Vertex list

n Normal vector associated with each face

n Array of index pairs

n Example, vth vertex of fth face:

n Position: pt[face[f].vert[v].vertIndex]

n Normal vector: norm[face[f].vert[v].normIndex]

n Organized approach, permits random access

Meshes in Programs

n Data structure:

// ############### Vertex ID ######################class VertexID

public:int vertIndex; // index of this vertex in the vertex list

} int normIndex;^ // index of this vertex’s normal

// ############### Face ######################class Face

public:int nVerts; // number of vertices in this face

VertexIDFace( ){nVerts = 0; vert = NULL;} *vert; // the list of vertex and normal indices // constructor

}; -Face( ){delete[ ] vert; nVerts = 0;^ // destructor

Meshes in Programs

// ############### Mesh ######################class Mesh{

private:int numVerts; // number of vertices in the mesh

Point3 *pt;int numNormals; // array of 3D vertices// number of normal vertices for the mesh

Vector3 *norm;int numFaces; // number of faces in the mesh// array of normals

Face *face;//… others to be added later // array of face data

public:Mesh( ); // constructor

~Mesh( );int readFile(char *fileName); // destructor // to read in a filed mesh

} ….. other methods….

Drawing Meshes Using OpenGL

n Actual code:

Void Mesh::draw( ){ // use openGL to draw this mesh

for(int f = 0;f < numFaces;f++){

glBegin(GL_POLYGON);for(int v=0;v<face[f].nVerts;v++) // for each one

{ int in = face[f].vert[v].normIndex; // index of this normal

int iv = face[f].vert[v].vertIndex;glNormal3f(norm[in].x, norm[in].y, norm[in].z); // index of this vertex

} glVertex3f(pt[iv].x, pt[iv].y, pt[iv].z);

} glEnd( );

Drawing Meshes Using SDL

n Scene class reads SDL files n Accepts keyword Mesh n Example:

n Pawn stored in mesh file pawn.3vn

n Add line:

  • Push translate 3 5 4 scale 3 3 3 mesh pawn.3vn pop