Frames Geometry Angel 4 4 4 5 Angel

  • Slides: 29
Download presentation
Frames & Geometry Angel 4. 4 -4. 5 Angel: Interactive Computer Graphics 5 E

Frames & Geometry Angel 4. 4 -4. 5 Angel: Interactive Computer Graphics 5 E © Addison-Wesley 2009 1

Objectives • Discuss frames • Introduce simple data structures for building polygonal models –

Objectives • Discuss frames • Introduce simple data structures for building polygonal models – Vertex lists – Edge lists • Open. GL vertex arrays Angel: Interactive Computer Graphics 5 E © Addison-Wesley 2009 2

Homogeneous Coordinates • The homogeneous coordinates form for a three dimensional point [x y

Homogeneous Coordinates • The homogeneous coordinates form for a three dimensional point [x y z] is given as • p =[x’ y’ z’ w] T =[wx wy wz w] T • We return to a three dimensional point (for w 0) by x x’/w y y’/w z z’/w • If w=0, the representation is that of a vector • Note that homogeneous coordinates replaces points in three dimensions by lines through the origin in four dimensions • For w=1, the representation of a point is [x y z 1] Angel: Interactive Computer Graphics 5 E © Addison-Wesley 2009 3

Homogeneous Coordinates and Computer Graphics • Homogeneous coordinates are key to all computer graphics

Homogeneous Coordinates and Computer Graphics • Homogeneous coordinates are key to all computer graphics systems – All standard transformations (rotation, translation, scaling) can be implemented with matrix multiplications using 4 x 4 matrices – Hardware pipeline works with 4 dimensional representations – For orthographic viewing, we can maintain w=0 for vectors and w=1 for points – For perspective we need a perspective division Angel: Interactive Computer Graphics 5 E © Addison-Wesley 2009 4

Change of Coordinate Systems • Consider two representations of a the same vector with

Change of Coordinate Systems • Consider two representations of a the same vector with respect to two different bases. The representations are a=[a 1 a 2 a 3 ] b=[b 1 b 2 b 3] where v=a 1 v 1+ a 2 v 2 +a 3 v 3 = [a 1 a 2 a 3] [v 1 v 2 v 3] T =b u +b u = [b b b ] [u u u ] T 1 1 2 2 3 3 1 2 3 Angel: Interactive Computer Graphics 5 E © Addison-Wesley 2009 5

Representing second basis in terms of first • Each of the basis vectors, u

Representing second basis in terms of first • Each of the basis vectors, u 1, u 2, u 3, are vectors that can be represented in terms of the first basis u 1 = g 11 v 1+g 12 v 2+g 13 v 3 u 2 = g 21 v 1+g 22 v 2+g 23 v 3 u 3 = g 31 v 1+g 32 v 2+g 33 v 3 v Angel: Interactive Computer Graphics 5 E © Addison-Wesley 2009 6

Matrix Form • The coefficients define a 3 x 3 matrix M= • and

Matrix Form • The coefficients define a 3 x 3 matrix M= • and the bases can be related by a=MTb • see text for numerical examples Angel: Interactive Computer Graphics 5 E © Addison-Wesley 2009 7

Change of Frames • We can apply a similar process in homogeneous coordinates to

Change of Frames • We can apply a similar process in homogeneous coordinates to the representations of both points and vectors u 1 v 2 Consider two frames: Q 0 (P 0, v 1, v 2, v 3) (Q 0, u 1, u 2, u 3) P 0 v 1 v 3 Angel: Interactive Computer Graphics 5 E © Addison-Wesley 2009 u 2 u 3 8

Representing One Frame in Terms of the Other Extending what we did with change

Representing One Frame in Terms of the Other Extending what we did with change of bases u 1 = g 11 v 1 + g 12 v 2 + g 13 v 3 u 2 = g 21 v 1 + g 22 v 2 + g 23 v 3 u 3 = g 31 v 1 + g 32 v 2 + g 33 v 3 Q 0 = g 41 v 1 + g 42 v 2 + g 43 v 3 + g 44 P 0 defining a 4 x 4 matrix M= Angel: Interactive Computer Graphics 5 E © Addison-Wesley 2009 9

Working with Representations • Within the two frames any point or vector has a

Working with Representations • Within the two frames any point or vector has a representation of the same form a=[a 1 a 2 a 3 a 4 ] in the first frame b=[b 1 b 2 b 3 b 4 ] in the second frame • where a 4 = b 4 = 1 for points and a 4 = b 4 = 0 for vectors and a=MTb • The matrix M is 4 x 4 and specifies an affine transformation in homogeneous coordinates Angel: Interactive Computer Graphics 5 E © Addison-Wesley 2009 10

Affine Transformations • Every linear transformation is equivalent to a change in frames •

Affine Transformations • Every linear transformation is equivalent to a change in frames • Every affine transformation preserves lines • However, an affine transformation has only 12 degrees of freedom because 4 of the elements in the matrix are fixed and are a subset of all possible 4 x 4 linear transformations Angel: Interactive Computer Graphics 5 E © Addison-Wesley 2009 11

The World and Camera Frames • When we work with representations, we work with

The World and Camera Frames • When we work with representations, we work with n-tuples or arrays of scalars • Changes in frame are then defined by 4 x 4 matrices • In Open. GL, the base frame that we start with is the world frame • Eventually we represent entities in the camera frame by changing the world representation using the model-view matrix • Initially these frames are the same (M=I) Angel: Interactive Computer Graphics 5 E © Addison-Wesley 2009 12

Moving the Camera • If objects are on both sides of z=0, we must

Moving the Camera • If objects are on both sides of z=0, we must move camera frame M= Angel: Interactive Computer Graphics 5 E © Addison-Wesley 2009 13

GEOMETRIC MODELING Angel: Interactive Computer Graphics 5 E © Addison-Wesley 2009 14

GEOMETRIC MODELING Angel: Interactive Computer Graphics 5 E © Addison-Wesley 2009 14

Representing a Mesh • Consider a mesh e 2 v 6 e 1 v

Representing a Mesh • Consider a mesh e 2 v 6 e 1 v 1 e 6 v 5 e 3 e 8 v e 9 v 4 8 e 11 e 10 e 4 e 7 v 2 e 12 e 5 v 3 • There are 8 nodes and 12 edges – 5 interior polygons – 6 interior (shared) edges • Each vertex has a location vi = (xi yi zi) Angel: Interactive Computer Graphics 5 E © Addison-Wesley 2009 15

Simple Representation • Define each polygon by the geometric locations of its vertices •

Simple Representation • Define each polygon by the geometric locations of its vertices • Leads to Open. GL code such as gl. Begin(GL_POLYGON); gl. Vertex 3 f(x 1, y 1, z 1); gl. Vertex 3 f(x 6, y 6, z 6); gl. Vertex 3 f(x 7, y 7, z 7); gl. End(); • Inefficient and unstructured – Consider moving a vertex to a new location – Must search for all occurrences Angel: Interactive Computer Graphics 5 E © Addison-Wesley 2009 16

Inward and Outward Facing Polygons • The order {v 1, v 6, v 7}

Inward and Outward Facing Polygons • 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 Open. GL but the order {v 1, v 7, v 6} is different • The first two describe outwardly facing polygons • Use the right-hand rule = counter-clockwise encirclement of outward-pointing normal • Open. GL can treat inward and outward facing polygons differently Angel: Interactive Computer Graphics 5 E © Addison-Wesley 2009 17

Geometry vs Topology • Generally it is a good idea to look for data

Geometry vs Topology • Generally it is a good idea to look for data structures that separate the geometry from the topology – Geometry: locations of the vertices – Topology: organization of the vertices and edges – 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 Angel: Interactive Computer Graphics 5 E © Addison-Wesley 2009 18

Vertex Lists • Put the geometry in an array • Use pointers from the

Vertex Lists • Put the geometry in an array • Use pointers from the vertices into this array • Introduce a polygon list P 1 P 2 P 3 P 4 P 5 topology v 1 v 7 v 6 v 8 v 5 v 6 x 1 y 1 z 1 x 2 y 2 z 2 x 3 y 3 z 3 x 4 y 4 z 4 x 5 y 5 z 5. x 6 y 6 z 6 x 7 y 7 z 7 geometry x 8 y 8 z 8 Angel: Interactive Computer Graphics 5 E © Addison-Wesley 2009 19

Shared Edges • Vertex lists will draw filled polygons correctly but if we draw

Shared Edges • Vertex lists will draw filled polygons correctly but if we draw the polygon by its edges, shared edges are drawn twice • Can store mesh by edge list Angel: Interactive Computer Graphics 5 E © Addison-Wesley 2009 20

Edge List e 2 e 1 e 2 e 3 e 4 e 5

Edge List e 2 e 1 e 2 e 3 e 4 e 5 e 6 e 7 e 8 e 9 v 1 v 6 x 1 y 1 z 1 x 2 y 2 z 2 x 3 y 3 z 3 x 4 y 4 z 4 x 5 y 5 z 5. x 6 y 6 z 6 x 7 y 7 z 7 x 8 y 8 z 8 v 6 e 1 v 1 e 6 v 5 e 3 e 8 v e 9 8 e 11 e 10 e 4 e 7 v 2 e 12 e 5 v 3 Note polygons are not represented Angel: Interactive Computer Graphics 5 E © Addison-Wesley 2009 21

Modeling a Cube Model a color cube for rotating cube program Define global arrays

Modeling a Cube Model a color cube for rotating cube program Define global arrays for vertices and colors GLfloat vertices[][3] = {{-1. 0, -1. 0}, {1. 0, -1. 0}, {-1. 0, 1. 0}, {1. 0, 1. 0}, {-1. 0, 1. 0}}; GLfloat colors[][3] = {{0. 0, 0. 0}, {1. 0, 0. 0}, {0. 0, 1. 0}, {1. 0, 1. 0}, {0. 0, 1. 0}}; Angel: Interactive Computer Graphics 5 E © Addison-Wesley 2009 22

Drawing a polygon from a list of indices • Draw a quadrilateral from a

Drawing a polygon from a list of indices • 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) { gl. Begin(GL_POLYGON); gl. Color 3 fv(colors[a]); gl. Vertex 3 fv(vertices[b]); gl. Vertex 3 fv(vertices[c]); gl. Vertex 3 fv(vertices[d]); gl. End(); } Angel: Interactive Computer Graphics 5 E © Addison-Wesley 2009 23

Draw cube from faces void colorcube( ) { polygon(0, 3, 2, 1); polygon(2, 3,

Draw 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); } 5 6 2 1 7 4 0 3 Note that vertices are ordered so that we obtain correct outward facing normals Angel: Interactive Computer Graphics 5 E © Addison-Wesley 2009 24

Efficiency • The weakness of our approach is that we are building the model

Efficiency • The weakness of our approach is that we are building the model in the application and must do many function calls to draw the cube • Drawing a cube by its faces in the most straight forward way requires – 6 gl. Begin, 6 gl. End – 6 gl. Color – 24 gl. Vertex – More if we use texture and lighting Angel: Interactive Computer Graphics 5 E © Addison-Wesley 2009 25

Vertex Arrays • Open. GL provides a facility called vertex arrays that allows us

Vertex Arrays • Open. GL provides a facility called vertex arrays that allows us to store array data in the implementation • Six types of arrays supported – – – Vertices Color indices Normals Texture coordinates Edge flags • We will need only colors and vertices Angel: Interactive Computer Graphics 5 E © Addison-Wesley 2009 26

Initialization • Using the same color and vertex data, first we enable gl. Enable.

Initialization • Using the same color and vertex data, first we enable gl. Enable. Client. State(GL_COLOR_ARRAY); gl. Enable. Client. State(GL_VERTEX_ARRAY); • Identify location of arrays gl. Vertex. Pointer(3, GL_FLOAT, 0, vertices); 3 d arrays stored as floats data array data contiguous gl. Color. Pointer(3, GL_FLOAT, 0, colors); Angel: Interactive Computer Graphics 5 E © Addison-Wesley 2009 27

Mapping indices to faces • Form an array of face indices GLubyte cube. Indices[24]

Mapping indices to faces • Form an array of face indices GLubyte cube. Indices[24] = {0, 3, 2, 1, 2, 3, 7, 6 0, 4, 7, 3, 1, 2, 6, 5, 4, 5, 6, 7, 0, 1, 5, 4}; • Each successive four indices describe a face of the cube • Draw through gl. Draw. Elements which replaces all gl. Vertex and gl. Color calls in the display callback Angel: Interactive Computer Graphics 5 E © Addison-Wesley 2009 28

Drawing the cube • Method 1: what to draw number of indices for(i=0; i<6;

Drawing the cube • Method 1: what to draw number of indices for(i=0; i<6; i++) gl. Draw. Elements(GL_POLYGON, 4, GL_UNSIGNED_BYTE, &cube. Indices[4*i]); format of index data start of index data • Method 2: gl. Draw. Elements(GL_QUADS, 24, GL_UNSIGNED_BYTE, cube. Indices); Draws cube with 1 function call!! Angel: Interactive Computer Graphics 5 E © Addison-Wesley 2009 29