Computer Graphics (CS 4731) Computer Graphics (CS 4731) Lecture 7: - - PowerPoint PPT Presentation
Computer Graphics (CS 4731) Computer Graphics (CS 4731) Lecture 7: - - PowerPoint PPT Presentation
Computer Graphics (CS 4731) Computer Graphics (CS 4731) Lecture 7: Building 3D Models (Part 1) Prof Emmanuel Agu Computer Science Dept. Worcester Polytechnic Institute (WPI) St Standard Unit Vectors d d U it V t y Define
St d d U it V t Standard Unit Vectors
, , 1 i
Define y i k
, 1 , j
i k
1 , , k
j So that any vector,
k j i b b
z x
k j i v c b a c b a , ,
C P d t (V t d t) Cross Product (Vector product)
z y x
a a a , , a
z y x
b b b , , b
If Then
k j i b a ) ( ) ( ) (
x y y x x z z x y z z y
b a b a b a b a b a b a
Remember using determinant
a a a k j i
z y x z y x
b b b a a a
Note: a x b is perpendicular to a and b
C P d t Cross Product
Note: a x b is perpendicular to both a and b a x b a b a b
C P d t Cross Product
Calculate a x b if a = (3,0,2) and b = (4,1,8)
C P d t Cross Product
Calculate a x b if a = (3,0,2) and b = (4,1,8) a x b = -2i – 16j + 3k
Fi di V R fl d F S f Finding Vector Reflected From a Surface
a = original vector
a = original vector
n = normal vector
r = reflected vector j i f l
m = projection of a along n
e = projection of a orthogonal to n
n a Note: Θ1 = Θ2 m
- m
r
m a r m a e m e r 2
Θ1 Θ2 e e
m a r 2
Li
C id ll i t f th f
Lines
Consider all points of the form
P()=P0 + d
Li S t f ll i t th t th h P i di ti
Line: Set of all points that pass through P0 in direction
- f vector d
P t i F Parametric Form
T di i l f f li
Two‐dimensional forms of a line
Explicit: y = mx +h Implicit: ax + by +c =0 Implicit: ax + by +c =0 Parametric:
x() = x0 + (1-)x1
P1 α P 1 - α
x() x0 (1 )x1 y() = y0 + (1-)y1
Parametric form of line
Po Pα More robust and general than other forms Extends to curves and surfaces
C it Convexity
An object is convex iff for any two points in the An object is convex iff for any two points in the
- bject all points on the line segment between
these points are also in the object these points are also in the object
P P Q Q t convex not convex
Obj ti Objectives
Introduce 3D set up Introduce simple data structures for 3D models
Vertex lists Edge lists
Deprecated OpenGL vertex arrays Drawing 3D objects
3D A li ti 3D Applications
2D points: (x y) coordinates 2D points: (x,y) coordinates 3D points: have (x,y,z) coordinates In OpenGL 2D graphics are special case of 3D graphics In OpenGL, 2D graphics are special case of 3D graphics
Setting up 3D Applications
Programming 3D, not many changes from 2D
1.
Load representation of 3D object into data structure
Each vertex has (x,y,z) coordinates. Store as vec3, glUniform3f NOT vec2
2
Draw 3D object
2.
Draw 3D object
3.
Set up Hidden surface removal: Correctly determine
- rder in which primitives (triangles, faces) are
rendered (e.g Blocked faces NOT drawn)
3D C di t S t
All vertex (x,y,z) positions are with respect to a
3D Coordinate Systems
All vertex (x,y,z) positions are with respect to a
coordinate system
OpenGL uses right hand coordinate system
p g y
Y Y x + z + z x
Left hand coordinate system Right hand coordinate system Tip: sweep fingers x‐y: thumb is z Left hand coordinate system
- Not used in OpenGL
G i 3D M d l GLUT M d l 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 OpenGL program to
generate vertices describing different shapes generate vertices describing different shapes
Two types of GLUT models:
Wireframe Models
Solid Models
Solid models Wireframe models Wireframe models
3D M d li GLUT M d l 3D Modeling: GLUT Models
Basic Shapes Basic Shapes
Cone: glutWireCone( ), glutSolidCone( )
Sphere: glutWireSphere( ), glutSolidSphere( )
Cube: glutWireCube( ), glutSolidCube( )
More advanced shapes:
Newell Teapot: (symbolic)
Torus Cone
Newell Teapot: (symbolic)
Dodecahedron, Torus
Sphere Sp e e
GLUT M d l l t i T t( ) GLUT Models: glutwireTeapot( )
Famous Utah Teapot: unofficial computer graphics mascot Famous Utah Teapot: unofficial computer graphics mascot glutWireTeapot(0 5) Create teapot of size 0 5 center positioned at (0 0 0) 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 M d li GLUT M d l 3D Modeling: GLUT Models
Glut functions under the hood Glut functions under the hood
generate sequence of points that define a shape
E l l tWi C t f
Example: glutWireCone generates sequence of
vertices, and faces defining cone and connectivity
Generated vertices and faces passed to OpenGL for Generated vertices and faces passed to OpenGL for
rendering
glutWireCone generates sequence of vertices, and faces defining cone OpenGL program receives vertices and Faces, renders them
P l l M h Polygonal Meshes
Modeling with GLUT shapes (cube sphere etc) too restrictive Modeling with GLUT shapes (cube, sphere, etc) too restrictive Difficult to approach realism Other (preferred) way is using polygonal meshes:
(p ) y g p yg
Collection of polygons, or faces, that form “skin” of object More flexible, represents complex surfaces better Examples: Human face Animal structures Furniture, etc
Each face of mesh is a polygon
P l l M h E l Polygonal Mesh Example
h d Mesh (wireframe) Smoothed Out with Shading (later)
P l l M h Polygonal Meshes
Meshes now standard in graphics 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)
C l h i ( h f )
Complex meshes approximate (e.g. human face)
M h t Diff t R l ti Meshes at Different Resolutions
Original: 424,000 triangles 60,000 triangles (14%). 1000 triangles (0.2%) (courtesy of Michael Garland and Data courtesy of Iris Development.)
R ti M h Representing a Mesh
Consider a mesh
v6 v5 e3 e2 e
Consider a mesh
v v8 v4 e1 e8
3
e11 e10 e e9 v1 v7 v3 e6 e7 e5 e4 e12
There are 8 vertices and 12 edges
v2
5 interior polygons 6 interior (shared) edges (shown in orange)
( )
Each vertex has a location vi = (xi yi zi)
Si l R t ti Simple Representation
Define each polygon by (x y z) locations of its vertices Define each polygon by (x,y,z) locations of its vertices OpenGL code
t [i] 3( 1 1 1) 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 Vertices shared by many polygons are declared multiple times Consider deleting vertex, moving vertex to new location Must search for all occurrences
I i h Si l R i Issues with Simple Representation
Declaring face f1
v5
Declaring face f1
vertex[i] = vec3(x1, y1, z1); vertex[i+1] = vec3(x7, y7, z7); vertex[i+2] = vec3(x8, y8, z8);
v6 v8 v4 f1
Declaring face f2
vertex[i+3] = vec3(x6, y6, z6);
v1 v7 v
vertex[i] = vec3(x1 y1 z1);
f2 f1
Inefficient and unstructured
v2 v3
vertex[i] = vec3(x1, y1, z1); vertex[i+1] = vec3(x2, y2, z2); vertex[i+2] = vec3(x7, y7, z7);
Inefficient and unstructured
In example, vertices v1 and v7 repeated while declaring f1 and f2 Vertices shared by many polygons are declared multiple times
y y p yg p
Consider deleting vertex, moving vertex to new location Must search for all faces in which vertex occurs
G t T l Geometry vs Topology
Better data structures separate geometry from topology
Better data structures 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 T l h ld if t h ( t )
Topology holds even if geometry changes (vertex moves)
v6 v v5 v v v7 v8 v4 f f1
Example: even if we move (x,y,z) location of v1, v1 still connected to v6, v7 and v2
v1 v2 v3 f2 v1
P l T l C i Polygon Traversal Convention
Use the right hand rule = counter clockwise encirclement Use the right‐hand rule = counter‐clockwise encirclement
- f outward‐pointing normal
OpenGL can treat inward and outward
p facing polygons differently
The order {v1, v0, v3} and {v3, v2, v1} are
equivalent in same polygon, rendered same way rendered by OpenGL
But order of {v1, v2, v3} is different The first two describe outwardly facing
polygons
5 4 3
polygons
1 6 2
V t Li t 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)
x1 y1 z1
Topology example: Polygon P1 of mesh is connected to vertices (v1,v7,v6)
x2 y2 z2 x3 y3 z3 x y z
P1 P2 v1 v7 v6
x4 y4 z4 x5 y5 z5. x6 y6 z6
P3 P4 P5 v8 v5
Geometry example: Vertex v7 coordinates ( 7 7 7)
6 y6 6
x7 y7 z7 x8 y8 z8
P5 v5 v6
are (x7,y7,z7). Note: If v7 moves, changed once in vertex list
Sh d Ed Shared Edges
Vertex lists draw filled polygons correctly Vertex lists draw filled polygons correctly If each polygon is drawn by its edges, shared edges are
drawn twice drawn twice
Alternatively: Can store mesh by edge list Alternatively: Can store mesh by edge list
Ed Li t Edge List
Simply draw each edges once
v6 v5 e3 e2
Simply draw each edges once E.g e1 connects v1 and v6
v7 v8 e1 e8
3
e11 e10 e e9 e1 e2 e3
x1 y1 z1 x2 y2 z2 x3 y3 z3
v1 v6 v1 v7 v3 e6 e7 e5 e4 e12 e4 e5 e6
x3 y3 z3 x4 y4 z4 x5 y5 z5.
v2 e6 e7 e8 9
x6 y6 z6 x7 y7 z7 x y z
Note polygons are not represented
e9
x8 y8 z8
not represented
M d li C b Modeling a Cube
- In 3D, declare vertices as (x,y,z) using point3 v[3]
- Define global arrays for vertices and colors
x y z
typedef vec3 point3; point3 vertices[] = {point3(-1.0,-1.0,-1.0), point3(1.0,-1.0,-1.0), point3(1.0,1.0,-1.0),
y z
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)};
r g b
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),
g b
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});
D i i l f li f i di Drawing a triangle from list of indices
Draw a triangle from a list of indices into the array Draw a triangle from a list of indices into the array
vertices and assign a color to each index
void triangle(int a, int b, int c, int d) { vcolors[i] = colors[d]; position[i] = vertices[a];
a
vcolors[i+1] = colors[d]); position[i+1] = vertices[b]; vcolors[i+2] = colors[d]; position[i+2] = vertices[c]; i+=3; }
b c
Variables a, b, c are indices into vertex array Variable d is index into color array Note: Same face, so all three vertices have same color
N l V Normal Vector
Normal vector: Direction each polygon is facing Normal vector: Direction each polygon is facing Each mesh polygon has a normal vector Normal vector used in shading Normal vector used in shading Normal vector • light vector determines shading (Later)
D b f f Draw cube from faces
void colorcube( ) { quad(0,3,2,1); d(2 3 7 6)
5 6
quad(2,3,7,6); quad(0,4,7,3); quad(1,2,6,5); quad(4,5,6,7);
2 4 7 1
Normal vector q quad(0,1,5,4); }