Computer Graphics CS 543 – Lecture 4 (Part 1) Building 3D Models (Part 1) Prof Emmanuel Agu Computer Science Dept. Worcester Polytechnic Institute (WPI)
Objectives Introduce simple data structures for building polygonal models Vertex lists Edge lists Deprecated OpenGL vertex arrays Drawing 3D objects
3D Applications 2D: points have (x,y) coordinates 3D: points have (x,y,z) coordinates In OpenGL, 2D graphics are special case of 3D graphics
Setting up 3D Applications Programming 3D, not many changes from 2D Load representation of 3D object into data structure 1. Note: Vertices stored as 3D points (x, y, z) Use vec3, glUniform3f instead of vec2 Draw 3D object 2. Hidden surface removal: Correctly determine order 3. in which primitives (triangles, faces) are rendered (Blocked faces NOT drawn)
3D Coordinate Systems Tip: sweep fingers x ‐ y: thumb is z Y Y + z x x + z Left hand coordinate system •Not used in this class and Right hand coordinate system •Not in OpenGL
Generating 3D Models: GLUT Models One way of generating 3D shapes is by using GLUT 3D models (Restrictive?) Note: Simply make GLUT 3D calls in application program (Not shaders) Two main categories of GLUT models: Wireframe Models Solid Models Solid m odels W irefram e m odels
3D Modeling: GLUT Models Basic Shapes Cone: glutWireCone( ), glutSolidCone( ) Sphere: glutWireSphere( ), glutSolidSphere( ) Cube: glutWireCube( ), glutSolidCube( ) Torus More advanced shapes: Cone Newell Teapot: (symbolic) Dodecahedron, Torus Sphere
GLUT Models: glutwireTeapot( ) Famous Utah Teapot has become an unofficial computer graphics mascot glutWireTeapot(0.5) - Create teapot of size 0.5, center positioned at (0,0,0) Also glutSolidTeapot( ) You need to apply transformations to position, scale and rotate it
3D Modeling: GLUT Models Glut functions under the hood generate sequence of points that define a shape centered at 0.0 Example: glutWireCone generates sequence of vertices, and faces defining cone and connectivity Generated vertices and faces passed to OpenGL for rendering glutWireCone generates OpenGL program sequence of vertices, and receives vertices, faces defining cone Faces and renders them
Polygonal Meshes Modeling with GLUT shapes (cube, sphere, etc) too restrictive Difficult to approach realism Other (preferred) way is using polygonal meshes: Collection of polygons, or faces, that form “skin” of object More flexible Represents complex surfaces better Examples: Human face Animal structures Furniture, etc
Polygonal Mesh Example Sm oothed Mesh Out w ith ( w irefram e) Shading ( later)
Polygonal Meshes Meshes now standard in graphics OpenGL Good at drawing polygons, triangles Mesh = sequence of polygons forming thin skin around object Simple meshes exact. (e.g barn) Complex meshes approximate (e.g. human face) Use shading technique later to smoothen
Meshes at Different Resolutions Original: 424,000 60,000 triangles 1000 triangles triangles (14%). (0.2%) (courtesy of Michael Garland and Data courtesy of Iris Development.)
Representing a Mesh v 5 e 2 v 6 e 3 Consider a mesh e 9 e 8 v 8 v 4 e 1 e 11 e 10 v 7 e 4 e 7 v 1 e 12 e 6 v 3 e 5 v 2 There are 8 vertices and 12 edges 5 interior polygons 6 interior (shared) edges (shown in orange) Each vertex has a location v i = (x i y i z i )
Simple Representation Define each polygon by (x,y,z) locations of its vertices Leads to OpenGL code such as vertex[i] = vec3(x1, y1, z1); vertex[i+1] = vec3(x6, y6, z6); vertex[i+2] = vec3(x7, y7, z7); i+=3; Inefficient and unstructured Vertices shared by many polygons are declared multiple times Consider deleting vertex, moving vertex to new location Must search for all occurrences
Geometry vs Topology Better data structures should separate geometry from topology Geometry: (x,y,z) locations of the vertices Topology: How vertices and edges are connected Example: a polygon is an ordered list of vertices with an edge connecting successive pairs of vertices and the last to the first Topology holds even if geometry changes (vertex moves)
Polygon Traversal Convention Use the right ‐ hand rule = counter ‐ clockwise encirclement of outward ‐ pointing normal OpenGL can treat inward and outward facing polygons differently The order {v 1 , v 6 , v 7 } and {v 6 , v 7 , v 1 } are equivalent in that the same polygon will be rendered by OpenGL but the order {v 1 , v 7 , v 6 } is different 4 The first two describe outwardly facing 3 5 polygons 2 6 1
Vertex Lists Vertex list: (x,y,z) of vertices (its geometry) are put in array Use pointers from vertices into vertex list Polygon list: vertices connected to each polygon (face) Example: x 1 y 1 z 1 v 1 ‐ Polygon P1 of x 2 y 2 z 2 mesh is connected P1 v 7 x 3 y 3 z 3 to vertices (v1,v7,v6) P2 v 6 x 4 y 4 z 4 P3 ‐ Vertex v7 coordinates x 5 y 5 z 5. P4 v 8 are (x7,y7,z7) x 6 y 6 z 6 P5 v 5 v 6 x 7 y 7 z 7 topology x 8 y 8 z 8 geometry
Shared Edges Vertex lists draw filled polygons correctly If each polygon is drawn by its edges, shared edges are drawn twice Alternatively: Can store mesh by edge list
Edge List Simply draw each edges once E.g e1 connects v1 and v6 v 5 e 2 v 6 e 3 x 1 y 1 z 1 e 9 e1 v1 e 8 v 8 x 2 y 2 z 2 e2 v6 e 11 e 1 e 10 e3 x 3 y 3 z 3 v 7 e 4 e 7 e4 v 1 x 4 y 4 z 4 e 12 v 3 e5 e 6 x 5 y 5 z 5. e 5 e6 v 2 x 6 y 6 z 6 e7 x 7 y 7 z 7 e8 Note polygons are x 8 y 8 z 8 e9 not represented
Modeling a Cube • In 3D, declare vertices as (x,y,z) using point3 v[3] • Define global arrays for vertices and colors typedef vex3 point3 ; point3 vertices[] = {point3(-1.0,-1.0,-1.0), point3(1.0,-1.0,-1.0), point3(1.0,1.0,-1.0), point3(-1.0,1.0,-1.0), point3(-1.0,-1.0,1.0), point3(1.0,-1.0,1.0), point3(1.0,1.0,1.0), point3(-1.0,1.0,1.0)}; typedef vec3 color3; color3 colors[] = {color3(0.0,0.0,0.0), color3(1.0,0.0,0.0), color3(1.0,1.0,0.0), color(0.0,1.0,0.0), color3(0.0,0.0,1.0), color3(1.0,0.0,1.0), color3(1.0,1.0,1.0), color3(0.0,1.0,1.0});
References Angel and Shreiner Hill and Kelley, appendix 4
Recommend
More recommend