Presentation is loading. Please wait.

Presentation is loading. Please wait.

CAP 4730 Computer Graphic Methods Prof. Roy Levow Chapter 4.

Similar presentations


Presentation on theme: "CAP 4730 Computer Graphic Methods Prof. Roy Levow Chapter 4."— Presentation transcript:

1 CAP 4730 Computer Graphic Methods Prof. Roy Levow Chapter 4

2 Geometry  Basic components –Point –Vector –Line Segment –Directed Line Segment  Can be added head-to-tail  Similar to vector addition but location of start of first segment is start of sum

3 Point-Vector Operations  Inverse of a vector –reverses direction  Point + Vector –Gives new point at end of vector from original point  Interpret inverse Point – Point –to be vector from second point to first

4 Scalar Operations  Scalar times vector is standard operation  Can take linear combination of points and vectors with scalar multipliers as long as net number of points is 1 –OK: P – 2 Q + 3v –Bad: 2 P – 4 Q

5 Geometric Space  Coordinate-free system –Objects have no location  Scalar Field –add, subtract, multiply, divide –associative and commutative  Vector Space –Vectors and scalars as usual  Affine Space –Vector space plus points  point + vector; point - point

6 Computational Framework  Magnitude of a vector –|αv| = |α| |v|  Lines –parametric form –P(α) = P 0 + αv –affine addition –P = α 1 R + α 2 Q where α 1 + α 2 = 1

7 Convexity  A polygon with vertices P1, …, P n is convex precisely when all affine sums of the points P = α 1 R + … + α n P n are inside the figure when α i ‘s sum to 1

8 Dot and Cross Products  Dot (inner) Product –u∙v = sum u i v i –|u| 2 = u∙u –cos θ = u∙v / |u||v|  Cross Product –n = u x v –orthogonal to plane of u-v –|sin θ | = |u x v| / |u||v| –Right hand rule  Using right hand, u is index, v is middle, n is thumb

9 Plane  Affine sum of three non-colinear points  or point and two vectors  or solution to the equation –n ∙ (P – P 0 ) = 0

10 Three-Dimensonal Primitives  Curves  Surfaces  Volumetric Ojbects  Standard treatment –Objects are described by their surfaces and treated as hollow –Objects can be specifiedby vertices –Objects can be defined directly or approximated by flat convex polygons

11 Coordinate Systems and Frames  Any vector in 3-dimensions can be represented as a linear combination of any three lineraly independent vectors w = α 1 v 1 + α 2 v 2 + α 3 v 3 w = α 1 v 1 + α 2 v 2 + α 3 v 3  If we fix the vectors v 1, v 2, v 3 we call them a basis  we can represent a point by the coefficients a = (α 1, α 2, α 3 ) T a = (α 1, α 2, α 3 ) T

12 Coordinate Systems and Frames  Conversely w = (α 1, α 2, α 3 ) (v 1, v 2, v 3 ) T w = (α 1, α 2, α 3 ) (v 1, v 2, v 3 ) T  The basis vectors define a coordinate system  However, the basis vectors are not anchored and could be moved  Adding a point as the origin gives us a frame  Every point can then be written as P 0 plus a linear combination of the basis vectors

13 Representations  Any point can be represented as 1.a vector, from the origin 2.a 3-tuple of the coefficients for the basis

14 Changes in Coordinate System  Suppose we have two bases –{u 1, u 2, u 3 } and {v 1, v 2, v 3 }  Each vector u i is a linear combination of the v’s  If M is the 3x3 matrix of coefficients, then (u 1, u 2, u 3 ) T =M (v 1, v 2, v 3 ) T  The inverse of M transforms from u’s to v’s

15 Changing Coordinate System  If a is the coordinate vector for v’s and b is the coordinate vector for u’s then a = M T b a = M T b  Changing the basis can effect rotation and scaling  Changing the basis leaves the origin unchanged

16 Homogeneous Coordinates  We can include the origin in our calculation s by adding a 4 th dimension for the origin, P 0, and agreeing to multiply only by 1 except for the zero vector when we use 0  Basis is now {v 1, v 2, v 3, P 0 }  Last column of transform matrix is 0, 0, 0, 1 0, 0, 0, 1  Can convert to new basis {u 1, u 2, u 3, Q 0 }

17 Examples  Change of basis 4.3.3, p. 159  Change of frame 4.3.5, p. 163

18 Working with Representations  Problem: How to obtain the transformation matrix given two representations  Solution: The representation of one frame in terms of another, a = Cb, gives the inverse of the matrix, D = C -1 (see p. 165)

19 Frames in OpenGL  Camera us at origin of its frame, pointing in –z direction; y is the up direction and x completes a right- handed coordinate system (see next slide)  To view objects near origin, camera must be moved back, say a distance d (see next slide)

20

21 Camera Model-View Matrix  Model-View matrix is 1000 0100 001-d 0001 moves point (x,y,z) in world frame to (x,y,z-d)

22 Camera at (1,0,1) Pointing at Origin  Camera is centered at (1,0,1,1) T  Orthogonal to back is n=(-1,0,-1,1) T  Up is same as world coord (0,1,0,0) T  Third orthogonal is (1,0,-1,0) T  Inverse of 1 0 -1 1.5 0 -.5 0 1 0 -1 1.5 0 -.5 0 0 1 0 0 is 0 1 0 0 0 1 0 0 is 0 1 0 0 -1 0 -1 1-.5 0 -.5 0 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1

23 Modeling a Colored Cube  Steps 1.Modeling 2.Converting to camera frame 3.Clipping 4.Projecting 5.Removing Hidden Surfaces 6.Rasterizing Code is cube.c

24 Modeling Cube  Model as 6 faces defined by vertices –Vertices at 1, -1 –Each face is a square (polygon)  Polygons have inside and outside face –Face is outward if vertices occur in counterclockwise order –Use right-hand rotation through vertices, thumb points out  Outward face must be to outside of cube

25 Data Structures  Polygons or Quads  Vertex list representation

26 Cube Code  Define colors for the vertices see code

27 Coloring Faces  Faces will be filled with colors by intepolation, bilinear scan-line interpolation from modelavoids need for flat

28 Vertex Arrays  Avoid recomputation  OpenGL Arrays 1.Color 2.Vertex 3.Color Index 4.Normal 5.Texture Coordinate 6.Edge Flag

29 Application to Cube  glEnableClientState(array_type); –array types: GL_COLOR_ARRAYGL_VERTEX_ARRAY  Create global arrays of values as before

30 Application to Cube (cont)  Register data arrays with OpenGL glVertexPointer (dim, glType, gap, array_var); glColorPointe (…)  For example glVertexPointer (3, GL_FLOAT, 0, glVertexPointer (3, GL_FLOAT, 0,vertices);

31 Application to Cube (cont)  Then draw the elements with glDrawElements(type, n, fmt, ptr);  For example for (1=0; i<6; i++) for (1=0; i<6; i++) glDrawElements(GL_POLYGON, 4 GL_UNSIGNED_BYTE,&cubeIndex[4*i]);  or could do quads in one batch of 24

32 Affine Transformations  A transformation maps a point (or vector) into another point (or vector) Q = T(P)  With homogeneous coordinates we can use the same function for both points and vectors, q=f(p) or v=f(u)

33 Linear Transformations  Limit consideration to functions satisfying f(ap+bq) = af(p) + bf(q) called linear transformations  Linear transformations can always be written as matrix multiplications by a 4x4 matrix  For homogeneous coordinates, the last row is 0 0 0 1 but other 12 entries are arbitrary, but only 9 effect vectors.

34 Linear Transformations (cont)  Properites –Linear transformations can be viewed as either 1.change in representation (frame) 2.transformation on vertices within frame –Lines and planes are preserved –Transformed line segment is generated by transformed end points  This simplifies design of graphics pipeline

35 Translation, Rotation, Scaling  Translation P’ = P + d  Rotation in two coordinates about origin cos th -sin th sin th cos th

36 Rotation (cont)  Properties of rotations –one fixed point –can be composed from two-dimensional rotation in different coordinates –define positive rotation in plane if rotation is counterclockwise when viewed down normal to plane –is a rigid-body transformation  shape is not changed

37 Scaling  An affine non-rigid body transformation  Multiply coordinate by some factor  Negative multiplication produces reflection

38 Affine Transformations in Homogeneous Cooridnates  All represented by linear transformations  4x4 matrix with last row 0 0 0 1  Translation by a b c, and inverse 1 0 0 a1 0 0 -a 0 1 0 b0 1 0 –b 0 0 1 c0 0 1 -c 0 0 0 1 0 0 0 1

39 Scaling  Multiply axes by a, b, c respectively a 0 0 0 0 b 0 0 0 0 c 0 0 0 0 1  Inverse comes from 1/a, 1/b, 1/c

40 Rotation  Can compose z, x, and y rotations by thz, thx, thy  Inverse is rotation by –thz, -thx, -thy  It can be shown that R -1 = R T –Matrices satisfying this property are said to be orthogonal

41 Shear  Shear in the x axis is defined by x’ = x + y cot th  Inverse users -th

42 Concatenation of Transforms  A sequence of transformations with T 1 followed by T 2 followed by T 3 can be composed as T 3 (T 2 (T 1 (p)))  If the corrersponding matrices are A, B, C this yields M = CBA  Thus a concatenation of transformations can be represented by a single matrix

43 Composed Transformaitons  Rotation in z about a fixed point T(p)R z (th)T(-p)  General rotation R x (thx) R y (thy) R z (thz)

44 Rotation about given vector  First change coordinates so given vector is on a coordinate axis –Use unit vector to avoid scaling  Then rotate about that coordinate axis  Finally restore to original coordinate system  Can translate for fixed point as before

45 Instance Transformations  When a particular kind of figure is used many times in a scene, it is convenient to construct one prototype and draw the objects by transforming that prototype  Convention is to center prototype on center of mass and then scale, rotate, and translate: TRS

46 Open GL Transformation Matrices  Current Transformation Matrix (CTM) is part of the pipeline  It can be set or modified  After setting matrix mode glLoadMatrixf(*matrix); //load matrix glLoadIdentity();

47 OpenGL Transformations  OpenGL has functions to perform basic operations glRotatef(angle, vx, vy, vz); // rotate about vector v glTranslatef(dx, dy, dz); glScalef(sx, sy, sz);  Applied in order called

48 OpenGL Rotation about Point  Rotate 45 o about line through origin and (1, 2, 3) with fixed point (4, 5, 6) glMatrixMode(GL_MODELVIEW);glLoadIdentity(); glTranslatef(4.0, 5.0, 6.0); glRotatef(45.0, 1.0, 2.0, 3.0); glTranslatef(-4.0, -5.0, -6.0);

49 Spinning the Cube  Mouse button click sets rotation axis  Idle callback adjusts rotation theta on selected axis  Display uses glRotatef() to set cube at desired angles in 3-d

50 Loading, Pushing, Popping  Can push and pop save and restore matrix –Projection stack is often limited to 2 levels; don’t usually transform it  Can load matrix from array with glLoadMatrix(myArray);  And multiply by our own matrix with glMultMatrix(myArray);  Can’t get OpenGL matrix into array

51 Smooth Rotation  Rotation is an arbitrary direction is equivalent to rotation about a great circle connecting the start and end positions of a point  Can recompute this incrementally –but requires much computation  Altenatively can compute one incremental step, save it, and then glMultMatrixf() for each step –Saves much computation


Download ppt "CAP 4730 Computer Graphic Methods Prof. Roy Levow Chapter 4."

Similar presentations


Ads by Google