Download presentation
Presentation is loading. Please wait.
Published byGodfrey Hudson Modified over 9 years ago
1
1 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012. Geometry Sai-Keung Wong ( 黃世強 ) Computer Science National Chiao Tung University, Taiwan
2
2 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012. Objectives Introduce the elements of geometry Scalars Vectors Points Develop mathematical operations among them in a coordinate-free manner Define basic primitives Line segments Polygons
3
3 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012. Basic Elements Geometry is the study of the relationships among objects in an n-dimensional space In computer graphics, we are interested in objects that exist in three dimensions Want a minimum set of primitives from which we can build more sophisticated objects We will need three basic elements Scalars Vectors Points
4
4 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012. Coordinate-Free Geometry When we learned simple geometry, most of us started with a Cartesian approach Points were at locations in space p=(x,y,z) We derived results by algebraic manipulations involving these coordinates This approach was nonphysical Physically, points exist regardless of the location of an arbitrary coordinate system Most geometric results are independent of the coordinate system Example Euclidean geometry: two triangles are identical if two corresponding sides and the angle between them are identical
5
5 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012. Scalars Need three basic elements in geometry Scalars, Vectors, Points Scalars can be defined as members of sets which can be combined by two operations (addition and multiplication) obeying some fundamental axioms (associativity, commutivity, inverses) Examples include the real and complex number systems under the ordinary rules with which we are familiar Scalars alone have no geometric properties
6
6 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012. Vectors Physical definition: a vector is a quantity with two attributes Direction Magnitude Examples include Force Velocity Directed line segments Most important example for graphics Can map to other types v
7
7 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012. Vector Operations Every vector has an inverse Same magnitude but points in opposite direction Every vector can be multiplied by a scalar There is a zero vector Zero magnitude, undefined orientation The sum of any two vectors is a vector Use head-to-tail axiom v -v vv v u w
8
8 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012. Linear Vector Spaces Mathematical system for manipulating vectors Operations Scalar-vector multiplication u = v Vector-vector addition: w = u + v Expressions such as v=u+2w-3r Make sense in a vector space
9
9 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012. Vectors Lack Position These vectors are identical Same length and magnitude Vectors spaces insufficient for geometry Need points
10
10 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012. Points Location in space Operations allowed between points and vectors Point-point subtraction yields a vector Equivalent to point-vector addition P=v+Q v=P-Q
11
11 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012. Affine Spaces Point + a vector space Operations Vector-vector addition Scalar-vector multiplication Point-vector addition Scalar-scalar operations For any point define 1 P = P 0 P = 0 (zero vector)
12
12 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012. 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
13
13 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012. Parametric Form This form is known as the parametric form of the line More robust and general than other forms Extends to curves and surfaces Two-dimensional forms Explicit: y = mx +h Implicit: ax + by +c =0 Parametric: x( ) = x 0 + (1- )x 1 y( ) = y 0 + (1- )y 1
14
14 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012. 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 + (R-Q)=Q+ v = R + (1- )Q For 0<= <=1 we get all the points on the line segment joining R and Q
15
15 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012. Convexity An object is convex iff for any two points in the object all points on the line segment between these points are also in the object P Q Q P convex not convex
16
16 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012. Affine Sums Consider the “sum” P= 1 P 1 + 2 P 2 +…..+ n P n Can show by induction that this sum makes sense iff 1 + 2 +….. n =1 in which case we have the affine sum of the points P 1 P 2,…..P n If, in addition, i >=0, we have the convex hull of P 1 P 2,…..P n
17
17 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012. Convex Hull Smallest convex object containing P 1 P 2,…..P n Formed by “shrink wrapping” points
18
18 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012. Curves and Surfaces Curves are one parameter entities of the form P( ) where the function is nonlinear Surfaces are formed from two-parameter functions P( , ) Linear functions give planes and polygons P( ) P( , )
19
19 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012. 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-Q) u v R P R Q
20
20 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012. Triangles convex sum of P and Q convex sum of S( ) and R for 0<= , <=1, we get all points in triangle
21
Barycentric Coordinates Triangle is convex so any point inside can be represented as an affine sum P( 1, 2, 3 )= 1 P+ 2 Q+ 3 R where 1 + 2 + 3 = 1 i >=0 The representation is called the barycentric coordinate representation of P 21 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012.
22
22 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012. Normals Every plane has a vector n normal (perpendicular, orthogonal) to it From point-two vector form P( , )=R+ u+ v, we know we can use the cross product to find n = u v and the equivalent form (P( )-P) n=0 u v P
23
23 E. Angel and D. Shriener: Interactive Computer Graphics 6E © Addison-Wesley 2012 Representation Sai-Keung Wong ( 黃世強 ) Computer Science National Chiao Tung University, Taiwan
24
24 E. Angel and D. Shriener: Interactive Computer Graphics 6E © Addison-Wesley 2012 Objectives Introduce concepts such as 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
25
25 E. Angel and D. Shriener: Interactive Computer Graphics 6E © Addison-Wesley 2012 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, at least one can be written in terms of the others
26
26 E. Angel and D. Shriener: Interactive Computer Graphics 6E © Addison-Wesley 2012 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
27
27 E. Angel and D. Shriener: Interactive Computer Graphics 6E © Addison-Wesley 2012 Representation Until now we have been able to work with geometric entities without using any frame of reference, such as a coordinate system Need a frame of reference to relate points and objects to our physical world. For example, where is a point? Can’t answer without a reference system World coordinates Camera coordinates
28
28 E. Angel and D. Shriener: Interactive Computer Graphics 6E © Addison-Wesley 2012 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 We can write the representation as a row or column array of scalars a=[ 1 2 …. n ] T =
29
29 E. Angel and D. Shriener: Interactive Computer Graphics 6E © Addison-Wesley 2012 Example v=2v 1 +3v 2 -4v 3 a=[2 3 –4] T Note that this representation is with respect to a particular basis For example, in OpenGL we start by representing vectors using the object basis but later the system needs a representation in terms of the camera or eye basis
30
30 E. Angel and D. Shriener: Interactive Computer Graphics 6E © Addison-Wesley 2012 Coordinate Systems Which is correct? Both are because vectors have no fixed location v v
31
31 E. Angel and D. Shriener: Interactive Computer Graphics 6E © Addison-Wesley 2012 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
32
32 E. Angel and D. Shriener: Interactive Computer Graphics 6E © Addison-Wesley 2012 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
33
33 E. Angel and D. Shriener: Interactive Computer Graphics 6E © Addison-Wesley 2012 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 ] v=[ 1 2 3 ] which confuses the point with the vector A vector has no position v p v Vector can be placed anywhere point: fixed
34
34 E. Angel and D. Shriener: Interactive Computer Graphics 6E © Addison-Wesley 2012 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 = [ 1 2 3 0 ] [v 1 v 2 v 3 P 0 ] T P = P 0 + 1 v 1 + 2 v 2 + 3 v 3 = [ 1 2 3 1 ] [v 1 v 2 v 3 P 0 ] T Thus we obtain the four-dimensional homogeneous coordinate representation v = [ 1 2 3 0 ] T p = [ 1 2 3 1 ] T
35
35 E. Angel and D. Shriener: Interactive Computer Graphics 6E © Addison-Wesley 2012 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]
36
36 E. Angel and D. Shriener: Interactive Computer Graphics 6E © Addison-Wesley 2012 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
37
37 E. Angel and D. Shriener: Interactive Computer Graphics 6E © Addison-Wesley 2012 Change of Coordinate Systems Consider two representations of a the same vector with respect to two different bases. The representations are v= 1 v 1 + 2 v 2 + 3 v 3 = [ 1 2 3 ] [v 1 v 2 v 3 ] T = 1 u 1 + 2 u 2 + 3 u 3 = [ 1 2 3 ] [u 1 u 2 u 3 ] T a=[ 1 2 3 ] b=[ 1 2 3 ] where
38
38 E. Angel and D. Shriener: Interactive Computer Graphics 6E © Addison-Wesley 2012 Representing second basis in terms of first Each of the basis vectors, u1,u2, u3, 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
39
39 E. Angel and D. Shriener: Interactive Computer Graphics 6E © Addison-Wesley 2012 Matrix Form The coefficients define a 3 x 3 matrix and the bases can be related by see text for numerical examples a=M T b M =
40
40 E. Angel and D. Shriener: Interactive Computer Graphics 6E © Addison-Wesley 2012 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
41
41 E. Angel and D. Shriener: Interactive Computer Graphics 6E © Addison-Wesley 2012 Representing One Frame in Terms of the Other 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 =
42
42 E. Angel and D. Shriener: Interactive Computer Graphics 6E © Addison-Wesley 2012 Working with Representations Within the two frames any point or vector has a representation of the same form a=[ 1 2 3 4 ] in the first frame b=[ 1 2 3 4 ] 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
43
43 E. Angel and D. Shriener: Interactive Computer Graphics 6E © Addison-Wesley 2012 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
44
44 E. Angel and D. Shriener: Interactive Computer Graphics 6E © Addison-Wesley 2012 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 OpenGL, 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 )
45
45 E. Angel and D. Shriener: Interactive Computer Graphics 6E © Addison-Wesley 2012 Moving the Camera If objects are on both sides of z=0, we must move camera frame M =
46
46 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Transformations Sai-Keung Wong ( 黃世強 ) Computer Science National Chiao Tung University, Taiwan
47
47 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Objectives Introduce standard transformations Rotation Translation Scaling Shear Derive homogeneous coordinate transformation matrices Learn to build arbitrary transformation matrices from simple transformations
48
48 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 General Transformations A transformation maps points to other points and/or vectors to other vectors Q=T(P) v=T(u)
49
49 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Affine Transformations Line preserving Characteristic of many physically important transformations Rigid body transformations: rotation, translation Scaling, shear Importance in graphics is that we need only transform endpoints of line segments and let implementation draw line segment between the transformed endpoints
50
50 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Pipeline Implementation transformationrasterizer u v u v T T(u) T(v) T(u) T(v) vertices pixels frame buffer (from application program)
51
51 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Notation We will be working with both coordinate-free representations of transformations and representations within a particular frame P,Q, R: points in an affine space u, v, w : vectors in an affine space , , : scalars p, q, r : representations of points -array of 4 scalars in homogeneous coordinates u, v, w : representations of points -array of 4 scalars in homogeneous coordinates
52
52 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Translation Move (translate, displace) a point to a new location Displacement determined by a vector d Three degrees of freedom P’=P+d P P’ d
53
53 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 How many ways? Although we can move a point to a new location in infinite ways, when we move many points there is usually only one way objecttranslation: every point displaced by same vector
54
54 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Translation Using Representations Using the homogeneous coordinate representation in some frame p=[ x y z 1] T p’=[x’ y’ z’ 1] T d=[dx dy dz 0] T Hence p’ = p + d or x’=x+d x y’=y+d y z’=z+d z note that this expression is in four dimensions and expresses point = vector + point
55
55 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Translation Matrix We can also express translation using a 4 x 4 matrix T in homogeneous coordinates p ’= Tp where T = T (d x, d y, d z ) = This form is better for implementation because all affine transformations can be expressed this way and multiple transformations can be concatenated together
56
56 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Rotation (2D) Consider rotation about the origin by degrees radius stays the same, angle increases by x’=x cos –y sin y’ = x sin + y cos x = r cos y = r sin x = r cos ( y = r sin (
57
57 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Rotation about the z axis Rotation about z axis in three dimensions leaves all points with the same z Equivalent to rotation in two dimensions in planes of constant z or in homogeneous coordinates p ’= R z ( )p x’=x cos –y sin y’ = x sin + y cos z’ =z
58
58 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Rotation Matrix R = R z ( ) =
59
59 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Rotation about x and y axes Same argument as for rotation about z axis For rotation about x axis, x is unchanged For rotation about y axis, y is unchanged R = R x ( ) = R = R y ( ) =
60
60 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Scaling S = S(s x, s y, s z ) = x’=s x x y’=s y x z’=s z x p’=Sp Expand or contract along each axis (fixed point of origin)
61
61 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Reflection corresponds to negative scale factors original s x = -1 s y = 1 s x = -1 s y = -1s x = 1 s y = -1
62
62 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Inverses Although we could compute inverse matrices by general formulas, we can use simple geometric observations Translation: T -1 (d x, d y, d z ) = T (-d x, -d y, -d z ) Rotation: R -1 ( ) = R(- ) Holds for any rotation matrix Note that since cos(- ) = cos( ) and sin(- )=-sin( ) R -1 ( ) = R T ( ) Scaling: S -1 (s x, s y, s z ) = S(1/s x, 1/s y, 1/s z )
63
63 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Concatenation We can form arbitrary affine transformation matrices by multiplying together rotation, translation, and scaling matrices Because the same transformation is applied to many vertices, the cost of forming a matrix M=ABCD is not significant compared to the cost of computing Mp for many vertices p The difficult part is how to form a desired transformation from the specifications in the application
64
64 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Order of Transformations Note that matrix on the right is the first applied Mathematically, the following are equivalent p’ = ABCp = A(B(Cp)) Note many references use column matrices to represent points. In terms of column matrices p ’T = p T C T B T A T
65
65 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 General Rotation About the Origin x z y v A rotation by about an arbitrary axis can be decomposed into the concatenation of rotations about the x, y, and z axes R( ) = R z ( z ) R y ( y ) R x ( x ) x y z are called the Euler angles Note that rotations do not commute We can use rotations in another order but with different angles
66
66 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Rotation About a Fixed Point other than the Origin Move fixed point to origin Rotate Move fixed point back M = T(p f ) R( ) T(-p f )
67
67 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Instancing In modeling, we often start with a simple object centered at the origin, oriented with the axis, and at a standard size We apply an instance transformation to its vertices to Scale Orient Locate
68
68 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Shear Helpful to add one more basic transformation Equivalent to pulling faces in opposite directions
69
69 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Shear Matrix Consider simple shear along x axis x’ = x + y cot y’ = y z’ = z H( ) =
70
70 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 OpenGL Transformations Sai-Keung Wong ( 黃世強 ) Computer Science National Chiao Tung University, Taiwan
71
71 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Objectives Learn how to carry out transformations in OpenGL Rotation Translation Scaling Introduce mat,h and vec.h transformations Model-view Projection
72
72 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Pre 3.1OpenGL Matrices In OpenGL matrices were part of the state Multiple types Model-View ( GL_MODELVIEW ) Projection ( GL_PROJECTION ) Texture ( GL_TEXTURE ) Color( GL_COLOR ) Single set of functions for manipulation Select which to manipulated by glMatrixMode(GL_MODELVIEW); glMatrixMode(GL_PROJECTION);
73
73 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Current Transformation Matrix (CTM) Conceptually there is a 4 x 4 homogeneous coordinate matrix, the current transformation matrix (CTM) that is part of the state and is applied to all vertices that pass down the pipeline The CTM is defined in the user program and loaded into a transformation unit CTMvertices p p’=Cp C
74
74 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 CTM operations The CTM can be altered either by loading a new CTM or by postmutiplication Load an identity matrix: C I Load an arbitrary matrix: C M Load a translation matrix: C T Load a rotation matrix: C R Load a scaling matrix: C S Postmultiply by an arbitrary matrix: C CM Postmultiply by a translation matrix: C CT Postmultiply by a rotation matrix: C C R Postmultiply by a scaling matrix: C C S
75
75 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Rotation about a Fixed Point Start with identity matrix: C I Move fixed point to origin: C CT Rotate: C CR Move fixed point back: C CT -1 Result : C = TR T –1 which is backwards. This result is a consequence of doing postmultiplications. Let’s try again.
76
76 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Reversing the Order We want C = T –1 R T so we must do the operations in the following order C I C CT -1 C CR C CT Each operation corresponds to one function call in the program. Note that the last operation specified is the first executed in the program
77
77 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 CTM in OpenGL OpenGL had a model-view and a projection matrix in the pipeline which were concatenated together to form the CTM We will emulate this process
78
78 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Rotation, Translation, Scaling mat4 r = Rotate(theta, vx, vy, vz) m = m*r; mat4 s = Scale( sx, sy, sz) mat4 t = Transalate(dx, dy, dz); m = m*s*t; mat4 m = Identity(); Create an identity matrix: Multiply on right by rotation matrix of theta in degrees where ( vx, vy, vz ) define axis of rotation Do same with translation and scaling:
79
79 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Example Rotation about z axis by 30 degrees with a fixed point of (1.0, 2.0, 3.0) Remember that last matrix specified in the program is the first applied mat 4 m = Identity(); m = Translate(1.0, 2.0, 3.0)* Rotate(30.0, 0.0, 0.0, 1.0)* Translate(-1.0, -2.0, -3.0);
80
80 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Arbitrary Matrices Can load and multiply by matrices defined in the application program Matrices are stored as one dimensional array of 16 elements which are the components of the desired 4 x 4 matrix stored by columns OpenGL functions that have matrices as parameters allow the application to send the matrix or its transpose
81
81 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Matrix Stacks In many situations we want to save transformation matrices for use later Traversing hierarchical data structures (Chapter 8) Avoiding state changes when executing display lists Pre 3.1 OpenGL maintained stacks for each type of matrix Easy to create the same functionality with a simple stack class
82
82 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Reading Back State Can also access OpenGL variables (and other parts of the state) by query functions glGetIntegerv glGetFloatv glGetBooleanv glGetDoublev glIsEnabled
83
83 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Using Transformations Example: use idle function to rotate a cube and mouse function to change direction of rotation Start with a program that draws a cube in a standard way Centered at origin Sides aligned with axes Will discuss modeling in next lecture
84
84 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 main.c void main(int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(500, 500); glutCreateWindow("colorcube"); glutReshapeFunc(myReshape); glutDisplayFunc(display); glutIdleFunc(spinCube); glutMouseFunc(mouse); glEnable(GL_DEPTH_TEST); glutMainLoop(); }
85
85 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Idle and Mouse callbacks void spinCube() { theta[axis] += 2.0; if( theta[axis] > 360.0 ) theta[axis] -= 360.0; glutPostRedisplay(); } void mouse(int btn, int state, int x, int y) { if(btn==GLUT_LEFT_BUTTON && state == GLUT_DOWN) axis = 0; if(btn==GLUT_MIDDLE_BUTTON && state == GLUT_DOWN) axis = 1; if(btn==GLUT_RIGHT_BUTTON && state == GLUT_DOWN) axis = 2; }
86
86 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Display callback void display() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glUniform(…); //or glUniformMatrix glDrawArrays(…); glutSwapBuffers(); } We can form matrix in application and send to shader and let shader do the rotation or we can send the angle and axis to the shader and let the shader form the transformation matrix and then do the rotation More efficient than transforming data in application and resending the data
87
87 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Using the Model-view Matrix In OpenGL the model-view matrix is used to Position the camera Can be done by rotations and translations but is often easier to use a LookAt function Build models of objects The projection matrix is used to define the view volume and to select a camera lens Although these matrices are no longer part of the OpenGL state, it is usually a good strategy to create them in our own applications
88
88 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Smooth Rotation From a practical standpoint, we are often want to use transformations to move and reorient an object smoothly Problem: find a sequence of model-view matrices M 0,M 1,…..,M n so that when they are applied successively to one or more objects we see a smooth transition For orientating an object, we can use the fact that every rotation corresponds to part of a great circle on a sphere Find the axis of rotation and angle Virtual trackball (see text)
89
89 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Incremental Rotation Consider the two approaches For a sequence of rotation matrices R 0,R 1,…..,R n, find the Euler angles for each and use R i = R iz R iy R ix Not very efficient Use the final positions to determine the axis and angle of rotation, then increment only the angle Quaternions can be more efficient than either
90
90 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Quaternions Extension of imaginary numbers from two to three dimensions Requires one real and three imaginary components i, j, k Quaternions can express rotations on sphere smoothly and efficiently. Process: Model-view matrix quaternion Carry out operations with quaternions Quaternion Model-view matrix q=q 0 +q 1 i+q 2 j+q 3 k
91
91 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Interfaces One of the major problems in interactive computer graphics is how to use two- dimensional devices such as a mouse to interface with three dimensional obejcts Example: how to form an instance matrix? Some alternatives Virtual trackball 3D input devices such as the spaceball Use areas of the screen Distance from center controls angle, position, scale depending on mouse button depressed
92
92 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Building Models Sai-Keung Wong ( 黃世強 ) Computer Science National Chiao Tung University, Taiwan
93
93 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Objectives Introduce simple data structures for building polygonal models Vertex lists Edge lists Deprecated OpenGL vertex arrays
94
94 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Representing a Mesh 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
95
95 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Simple Representation Define each polygon by the geometric locations of its vertices Leads to OpenGL code such as Inefficient and unstructured Consider moving a vertex to a new location Must search for all occurrences vertex[i] = vec3(x1, x1, x1); vertex[i+1] = vec3(x6, x6, x6); vertex[i+2] = vec3(x7, x7, x7); i+=3;
96
96 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 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 OpenGL 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 OpenGL can treat inward and outward facing polygons differently
97
97 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 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
98
98 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 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
99
99 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 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
100
100 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Edge List v1v1 v2v2 v7v7 v6v6 v8v8 v5v5 v3v3 e1e1 e8e8 e3e3 e2e2 e 11 e6e6 e7e7 e 10 e5e5 e4e4 e9e9 e 12 e1 e2 e3 e4 e5 e6 e7 e8 e9 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 v1 v6 Note polygons are not represented
101
101 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Modeling a Cube 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}); Define global arrays for vertices and colors
102
102 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Drawing a triangle from a list of indices 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]; vcolors[i+1] = colors[d]); position[i+1] = vertices[a]; vcolors[i+2] = colors[d]; position[i+2] = vertices[a]; i+=3; }
103
103 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Draw cube from faces void colorcube( ) { quad(0,3,2,1); quad(2,3,7,6); quad(0,4,7,3); quad(1,2,6,5); quad(4,5,6,7); quad(0,1,5,4); } 0 56 2 4 7 1 3 Note that vertices are ordered so that we obtain correct outward facing normals
104
104 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 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 used to require 6 glBegin, 6 glEnd 6 glColor 24 glVertex More if we use texture and lighting
105
105 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Vertex Arrays OpenGL provided a facility called vertex arrays that allows us to store array data in the implementation Six types of arrays were supported initially Vertices Colors Color indices Normals Texture coordinates Edge flags Now vertex arrays can be used for any attributes
106
106 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Old Style 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
107
107 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Mapping indices to faces Form an array of face indices Each successive four indices describe a face of the cube Draw through glDrawElements which replaces all glVertex and glColor calls in the display callback 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};
108
108 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Drawing the cube Old Method: Problem is that although we avoid many function calls, data are still on client side Solution: no immediate mode Vertex buffer object Use glDrawArrays glDrawElements(GL_QUADS, 24, GL_UNSIGNED_BYTE, cubeIndices); Draws cube with 1 function call!!
109
Rotating Cube Full example Model Colored Cube Use 3 button mouse to change direction of rotation Use idle function to increment angle of rotation 109 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012
110
Cube Vertices 110 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 // Vertices of a unit cube centered at origin // sides aligned with axes point4 vertices[8] = { point4( -0.5, -0.5, 0.5, 1.0 ), point4( -0.5, 0.5, 0.5, 1.0 ), point4( 0.5, 0.5, 0.5, 1.0 ), point4( 0.5, -0.5, 0.5, 1.0 ), point4( -0.5, -0.5, -0.5, 1.0 ), point4( -0.5, 0.5, -0.5, 1.0 ), point4( 0.5, 0.5, -0.5, 1.0 ), point4( 0.5, -0.5, -0.5, 1.0 ) };
111
Colors 111 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 // RGBA colors color4 vertex_colors[8] = { color4( 0.0, 0.0, 0.0, 1.0 ), // black color4( 1.0, 0.0, 0.0, 1.0 ), // red color4( 1.0, 1.0, 0.0, 1.0 ), // yellow color4( 0.0, 1.0, 0.0, 1.0 ), // green color4( 0.0, 0.0, 1.0, 1.0 ), // blue color4( 1.0, 0.0, 1.0, 1.0 ), // magenta color4( 1.0, 1.0, 1.0, 1.0 ), // white color4( 0.0, 1.0, 1.0, 1.0 ) // cyan };
112
Quad Function 112 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 // quad generates two triangles for each face and assigns colors // to the vertices int Index = 0; void quad( int a, int b, int c, int d ) { colors[Index] = vertex_colors[a]; points[Index] = vertices[a]; Index++; colors[Index] = vertex_colors[b]; points[Index] = vertices[b]; Index++; colors[Index] = vertex_colors[c]; points[Index] = vertices[c]; Index++; colors[Index] = vertex_colors[a]; points[Index] = vertices[a]; Index++; colors[Index] = vertex_colors[c]; points[Index] = vertices[c]; Index++; colors[Index] = vertex_colors[d]; points[Index] = vertices[d]; Index++; }
113
Color Cube 113 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 // generate 12 triangles: 36 vertices and 36 colors void colorcube() { quad( 1, 0, 3, 2 ); quad( 2, 3, 7, 6 ); quad( 3, 0, 4, 7 ); quad( 6, 5, 1, 2 ); quad( 4, 5, 6, 7 ); quad( 5, 4, 0, 1 ); }
114
Initialization I 114 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 void init() { colorcube(); // Create a vertex array object GLuint vao; glGenVertexArrays ( 1, &vao ); glBindVertexArray ( vao );
115
Initialization II 115 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 // Create and initialize a buffer object GLuint buffer; glGenBuffers( 1, &buffer ); glBindBuffer( GL_ARRAY_BUFFER, buffer ); glBufferData( GL_ARRAY_BUFFER, sizeof(points) + sizeof(colors), NULL, GL_STATIC_DRAW ); glBufferSubData( GL_ARRAY_BUFFER, 0, sizeof(points), points ); glBufferSubData( GL_ARRAY_BUFFER, sizeof(points), sizeof(colors), colors ); // Load shaders and use the resulting shader program GLuint program = InitShader( "vshader36.glsl", "fshader36.glsl" ); glUseProgram( program );
116
Initialization III 116 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 // set up vertex arrays GLuint vPosition = glGetAttribLocation( program, "vPosition" ); glEnableVertexAttribArray( vPosition ); glVertexAttribPointer( vPosition, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0) ); GLuint vColor = glGetAttribLocation( program, "vColor" ); glEnableVertexAttribArray( vColor ); glVertexAttribPointer( vColor, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(sizeof(points)) ); theta = glGetUniformLocation( program, "theta" );
117
Display Callback 117 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 void display( void ) { glClear( GL_COLOR_BUFFER_BIT |GL_DEPTH_BUFFER_BIT ); glUniform3fv( theta, 1, theta ); glDrawArrays( GL_TRIANGLES, 0, NumVertices ); glutSwapBuffers(); }
118
Mouse Callback 118 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 void mouse( int button, int state, int x, int y ) { if ( state == GLUT_DOWN ) { switch( button ) { case GLUT_LEFT_BUTTON: Axis = Xaxis; break; case GLUT_MIDDLE_BUTTON: Axis = Yaxis; break; case GLUT_RIGHT_BUTTON: Axis = Zaxis; break; }
119
Idle Callback 119 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 void idle( void ) { theta[axis] += 0.01; if ( theta[axis] > 360.0 ) { theta[axis] -= 360.0; } glutPostRedisplay(); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.