Download presentation
Presentation is loading. Please wait.
Published byAmberly Peters Modified over 9 years ago
1
Modeling
2
Geometric Models 2D and 3D objects Triangles, quadrilaterals, polygons Spheres, cones, boxes Surface characteristics Color Texture Composite objects Other objects and their relationships to each other 2
3
The basic idea Describe objects using surfaces, which are polygons Triangles, quadrilaterals, etc. Important thing is that they are flat and convex Convex : A line segment connecting any two points on the polygon is contained in the polygon. Provide points in counterclock-wise order from the visible side 3 P Q P convex concave
4
Outline 4 OpenGL Elementary Rendering (codes) Representation Appendix: Vector Operations *
5
OpenGL Elementary Rendering Geometric Primitives Building Models Managing OpenGL State 5
6
OpenGL Geometric Primitives All geometric primitives are specified by vertices GL_QUAD_STRIP GL_POLYGON GL_TRIANGLE_STRIP GL_TRIANGLE_FAN GL_POINTS GL_LINES GL_LINE_LOOPGL_LINE_STRIP GL_TRIANGLES GL_QUADS 6
7
Simple Example void drawRhombus( GLfloat color[] ) { glBegin( GL_QUADS ); glColor3fv( color ); glVertex2f( 0.0, 0.0 ); glVertex2f( 1.0, 0.0 ); glVertex2f( 1.5, 1.118 ); glVertex2f( 0.5, 1.118 ); glEnd(); } 7
8
OpenGL Command Formats glVertex3fv( v ) Number of components 2 - (x,y) 3 - (x,y,z) 4 - (x,y,z,w) Data Type b - byte ub - unsigned byte s - short us - unsigned short i - int ui - unsigned int f - float d - double Vector omit “ v ” for scalar form glVertex2f( x, y ) 8
9
Specifying Geometric Primitives Primitives are specified using glBegin( primType ); glEnd(); primType determines how vertices are combined GLfloat red, greed, blue; Glfloat coords[3]; glBegin( primType ); for ( i = 0; i < nVerts; ++i ) { glColor3f( red, green, blue ); glColor3f( red, green, blue ); glVertex3fv( coords ); glVertex3fv( coords );}glEnd(); 9
10
Shapes Tutorial 10
11
OpenGL Color Models * RGBA or Color Index color index mode Display 1 2 4 8 16 RedGreenBlue 0 1 2 3 24 25 26 12321974 RGBA mode 11
12
Building models Consider a mesh There are 8 nodes and 12 edges 5 interior polygons 6 interior (shared) edges Each vertex has a location v i = (x i y i z i ) v1v1 v2v2 v7v7 v6v6 v8v8 v5v5 v4v4 v3v3 e1e1 e8e8 e3e3 e2e2 e 11 e6e6 e7e7 e 10 e5e5 e4e4 e9e9 e 12
13
Simple Representation Each polygon is defined by the geometric locations of its vertices, which leads to OpenGL code such as Inefficient and unstructured glBegin(GL_POLYGON); glVertex3f(x1, y1, z1); glVertex3f(x7, y7, z7); glVertex3f(x8, y8, z8); glVertex3f(x6, y6, z6); glEnd();
14
Vertex Lists Put the geometry in an array Use pointers from the vertices into this array Introduce a polygon list 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 P1 P2 P3 P4 P5 v1v7v6v1v7v6 v8v5v6v8v5v6 topology geometry
15
Example: Modeling a Cube 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,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 colors[][3] = {{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}};
16
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(colors[a]); glVertex3fv(vertices[a]); glVertex3fv(vertices[b]); glVertex3fv(vertices[c]); glVertex3fv(vertices[d]); glEnd(); }
17
Draw cube from faces Note that vertices are ordered so that we obtain correct outward facing normals 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); } 0 56 2 4 7 1 3
18
Efficiency The problem is that we must do many function calls to draw the cube 6 glBegin, 6 glEnd 6 glColor 24 glVertex More if we use texture and lighting
19
* OpenGL provides vertex arrays to store array data Six types of arrays supported Vertices Colors Color indices Normals Texture coordinates Edge flags Here we will need only colors and vertices
20
* Initialization Using the same color and vertex data, first we enable glEnableClientState(GL_COLOR_ARRAY); glEnableClientState(GL_VERTEX_ARRAY); Identify location of arrays glVertexPointer(3, GL_FLOAT, 0, vertices); glColorPointer(3, GL_FLOAT, 0, colors); 3d arraysstored as floats data contiguous data array
21
* Form an array of face indices Each successive four indices describe a face of the cube GLubyte cubeIndices[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};
22
* Drawing the cube Method 1: Method 2: for(i=0; i<6; i++) glDrawElements(GL_POLYGON, 4, GL_UNSIGNED_BYTE, &cubeIndices[4*i]); format of index data start of index data what to draw number of indices glDrawElements(GL_QUADS, 24, GL_UNSIGNED_BYTE, cubeIndices); Draws cube with 1 function call!!
23
OpenGL’s State Machine All rendering attributes are encapsulated in the OpenGL State rendering styles shading lighting texture mapping 23
24
Manipulating OpenGL State Appearance is controlled by current state for each ( primitive to render ) { update OpenGL state render primitive } Manipulating vertex attributes is most common way to manipulate state glColor*() / glIndex*() glNormal*()glTexCoord*() 24
25
Controlling current state Setting State glPointSize( size ); glShadeModel( GL_SMOOTH ); Enabling Features glEnable( GL_LIGHTING ); glDisable( GL_TEXTURE_2D ); 25
26
Outline 26 OpenGL Elementary Rendering (codes) Representation Appendix: Vector Operations
27
Objectives Introduce dimension and basis Introduce coordinate systems for representing vectors spaces and frames for representing affine spaces Discuss change of frames and bases Introduce homogeneous coordinates
28
Vectors Objects with length and direction Examples include Force Velocity Directed line segments We will think of a vector as a displacement from one point to another We use bold typeface to indicate vectors v 28
29
Vectors as Displacements 29 As a displacement, a vector has length and direction, but no inherent location You can drive one mile East in Zhuhai To represent vectors, we need firstly learn…
30
Linear Independence A set of vectors v 1, v 2, …, v n is linearly independent if 1 v 1 + 2 v 2 +.. n v n =0 iff 1 = 2 =…=0 If a set of vectors is linearly independent, we cannot represent one in terms of the others If a set of vectors is linearly dependent, as least one can be written in terms of the others
31
Dimension In a vector space, the maximum number of linearly independent vectors is fixed and is called the dimension of the space In an n-dimensional space, any set of n linearly independent vectors form a basis for the space Given a basis v 1, v 2,…., v n, any vector v can be written as v= 1 v 1 + 2 v 2 +….+ n v n where the { i } are unique
32
Coordinate Systems Consider a basis v 1, v 2,…., v n A vector is written v= 1 v 1 + 2 v 2 +….+ n v n The list of scalars { 1, 2, …. n } is the representation of v with respect to the given basis Scalar: here an ordinary real (e.g., float) number. Scalars alone have no geometric properties We can write the representation as a row or column array of scalars a=[ 1 2 …. n ] T =
33
Examples v=2v 1 +3v 2 -4v 3 a=[2 3 –4] T Note that this representation is with respect to a particular basis v = 5 v 1 -2v 2 in a plane
34
Coordinate Systems Which is correct? Both are because vectors have no fixed location v v
35
Frames A coordinate system is insufficient to represent points If we work in an affine space we can add a single point, the origin, to the basis vectors to form a frame P0P0 v1v1 v2v2 v3v3
36
Representation in a Frame Frame determined by (P 0, v 1, v 2, v 3 ) Within this frame, every vector can be written as v= 1 v 1 + 2 v 2 +….+ n v n Every point can be written as P = P 0 + 1 v 1 + 2 v 2 +….+ n v n
37
Confusing Points and Vectors Consider the point and the vector P = P 0 + 1 v 1 + 2 v 2 +….+ n v n v= 1 v 1 + 2 v 2 +….+ n v n They appear to have the similar representations p=[ 1 2 3 ] T v=[ 1 2 3 ] T which confuses the point with the vector v p v Vector can be placed anywhere point: fixed
38
A Single Representation If we define 0P = 0 and 1P =P then we can write v= 1 v 1 + 2 v 2 + 3 v 3 = [v 1 v 2 v 3 P 0 ] [ 1 2 3 0 ] T P = P 0 + 1 v 1 + 2 v 2 + 3 v 3 = [v 1 v 2 v 3 P 0 ] [ 1 2 3 1 ] T Thus we obtain the four-dimensional homogeneous coordinate representation v = [ 1 2 3 0 ] T p = [ 1 2 3 1 ] T
39
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 For w=1, the representation of a point is [x y z 1] Note that homogeneous coordinates replaces points in three dimensions by lines through the origin in four dimensions
40
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
41
Change of Coordinate Systems Consider two representations of a same vector with respect to two different bases. The representations are a=[ 1 2 3 ] T b=[ 1 2 3 ] T where v= 1 v 1 + 2 v 2 + 3 v 3 = [v 1 v 2 v 3 ] [ 1 2 3 ] T = 1 u 1 + 2 u 2 + 3 u 3 = [u 1 u 2 u 3 ] [ 1 2 3 ] T
42
Representing 2 nd basis in terms of 1 st 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 = 11 v 1 + 12 v 2 + 13 v 3 u 2 = 21 v 1 + 22 v 2 + 23 v 3 u 3 = 31 v 1 + 32 v 2 + 33 v 3 v [u 1 u 2 u 3 ] = [v 1 v 2 v 3 ]M T
43
Simple example 43
44
Matrix Form The coefficients define a 3 x 3 matrix and the representations can be related by a=M T b M =
45
Change of Frames We can apply a similar process in homogeneous coordinates to the representations of both points and vectors Any point or vector can be represented in either frame We can represent Q 0, u 1, u 2, u 3 in terms of P 0, v 1, v 2, v 3 Consider two frames: (P 0, v 1, v 2, v 3 ) (Q 0, u 1, u 2, u 3 ) P0P0 v1v1 v2v2 v3v3 Q0Q0 u1u1 u2u2 u3u3
46
Representing 2 nd Frame in the 1 st u 1 = 11 v 1 + 12 v 2 + 13 v 3 u 2 = 21 v 1 + 22 v 2 + 23 v 3 u 3 = 31 v 1 + 32 v 2 + 33 v 3 Q 0 = 41 v 1 + 42 v 2 + 43 v 3 + 44 P 0 Extending what we did with change of bases defining a 4 x 4 matrix M =
47
Working with Representations Within the two frames any point or vector has a representation of the same form a=[ 1 2 3 4 ] T in the first frame b=[ 1 2 3 4 ] T in the second frame where 4 4 for points and 4 4 for vectors and The matrix M is 4 x 4 and specifies an affine transformation in homogeneous coordinates a=M T b
48
Representing geometry elements Points Location in space Operations allowed between points and vectors Point-point subtraction yields a vector Equivalent to point-vector addition P=v+QP=v+Q v=P-Qv=P-Q 48
49
Lines Consider all points of the form P( )=P 0 + d Set of all points that pass through P 0 in the direction of the vector d 49
50
Rays and Line Segments If >= 0, then P( ) is the ray leaving P 0 in the direction d If we use two points to define v, then P( ) = Q+ v = Q + (R-Q) = R + (1- )Q For 0<= <=1 we get all the points on the line segment joining R and Q 50
51
Planes A plane can be defined by a point and two vectors or by three points P( , )=R+ u+ v P( , )=R+ (Q-R)+ (P-R) u v R P R Q 51
52
Barycentric Coordinates 52 We can reorder the terms of the equation: with: This coordinate system is called barycentric coordinates For any point P inside the triangle (a, b, c): Point on an edge: one coefficient is 0 Vertex: two coefficients are 0, remaining one is 1
53
Outline 53 OpenGL Elementary Rendering Representation Appendix: Vector Operations *
54
Vector Operations 54 Addition and multiplication by a scalar: a + b s*a Examples:
55
Vector Addition 55 Head to tail: At the end of a, place the start of b. Parallelogram rule: Components: [a 1 + b 1, a 2 + b 2 ]
56
Vector Subtraction 56 To draw a - b, go forward along a and then backward along b. Components: [a 1 - b 1, a 2 - b 2 ]
57
The Dot Product 57 Also called the inner product of two vectors Produces a number, not a vector In 2D: In 3D: Symmetry: Linearity: Homogeneity:
58
Length of a Vector 58 The length (or norm) of a vector a is: The dot product of a vector with itself yields the length squared:
59
Unit Vectors and Normalization 59 A unit vector is a vector whose length equals 1 Example: (1, 0, 0), (0, 1, 0), (0, 0, 1) Divide any nonzero vector v by its length |v|. The result is a unit vector in the same direction as v. This is called normalizing a vector: Any vector can be written as its magnitude times its direction:
60
The Angle Between Vectors 60 The angle between vectors can be computed by: Using normalized vectors:
61
The Sign of ab 61 Imagine the cosine function: Two vectors a and b are perpendicular, orthogonal, or normal if ab= 0
62
The end 62 Questions and answers
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.