510 likes | 591 Views
CAP 4730 Computer Graphic Methods. Prof. Roy Levow Chapter 4. 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. Point-Vector Operations.
E N D
CAP 4730Computer Graphic Methods Prof. Roy Levow Chapter 4
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
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
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
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
Computational Framework • Magnitude of a vector • |αv| = |α| |v| • Lines • parametric form • P(α) = P0 + αv • affine addition • P = α1 R + α2 Q where α1 + α2 = 1
Convexity • A polygon with vertices P1, …, Pn is convex precisely when all affine sums of the points P = α1 R + … + αn Pn are inside the figure when αi‘s sum to 1
Dot and Cross Products • Dot (inner) Product • u∙v = sum ui vi • |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
Plane • Affine sum of three non-colinear points • or point and two vectors • or solution to the equation • n ∙ (P – P0) = 0
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
Coordinate Systemsand Frames • Any vector in 3-dimensions can be represented as a linear combination of any three lineraly independent vectors w = α1 v1 + α2 v2 + α3 v3 • If we fix the vectors v1, v2, v3 we call them a basis • we can represent a point by the coefficients a = (α1,α2,α3)T
Coordinate Systemsand Frames • Conversely w = (α1,α2,α3) (v1, v2, v3)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 P0 plus a linear combination of the basis vectors
Representations • Any point can be represented as • a vector, from the origin • a 3-tuple of the coefficients for the basis
Changes in Coordinate System • Suppose we have two bases • {u1, u2, u3} and {v1, v2, v3} • Each vector ui is a linear combination of the v’s • If M is the 3x3 matrix of coefficients, then (u1, u2, u3)T =M (v1, v2, v3)T • The inverse of M transforms from u’s to v’s
Changing Coordinate System • If a is the coordinate vector for v’s and b is the coordinate vector for u’s then a = MT b • Changing the basis can effect rotation and scaling • Changing the basis leaves the origin unchanged
Homogeneous Coordinates • We can include the origin in our calculation s by adding a 4th dimension for the origin, P0, and agreeing to multiply only by 1 except for the zero vector when we use 0 • Basis is now {v1, v2, v3 , P0} • Last column of transform matrix is 0, 0, 0, 1 • Can convert to new basis {u1, u2, u3, Q0}
Examples • Change of basis 4.3.3, p. 159 • Change of frame 4.3.5, p. 163
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)
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)
Camera Model-View Matrix • Model-View matrix is 1 0 0 0 0 1 0 0 0 0 1 -d 0 0 0 1 moves point (x,y,z) in world frame to (x,y,z-d)
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 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
Modeling a Colored Cube • Steps • Modeling • Converting to camera frame • Clipping • Projecting • Removing Hidden Surfaces • Rasterizing Code is cube.c
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
Data Structures • Polygons or Quads • Vertex list representation
Cube Code • Define colors for the vertices see code
Coloring Faces • Faces will be filled with colors by intepolation, bilinear scan-line interpolation from model avoids need for flat
Vertex Arrays • Avoid recomputation • OpenGL Arrays • Color • Vertex • Color Index • Normal • Texture Coordinate • Edge Flag
Application to Cube • glEnableClientState(array_type); • array types: GL_COLOR_ARRAY GL_VERTEX_ARRAY • Create global arrays of values as before
Application to Cube (cont) • Register data arrays with OpenGL glVertexPointer (dim, glType, gap, array_var); glColorPointe (…) • For example glVertexPointer (3, GL_FLOAT, 0, vertices);
Application to Cube (cont) • Then draw the elements with glDrawElements(type, n, fmt, ptr); • For example 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
Affine Transformations • A transformation maps a point (or vector) into another point (or vector) Q = T(P) • With homogeneouscoordinates we can use the same function for both points and vectors, q=f(p) or v=f(u)
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.
Linear Transformations (cont) • Properites • Linear transformations can be viewed as either • change in representation (frame) • 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
Translation, Rotation, Scaling • Translation P’ = P + d • Rotation in two coordinates about origin cos th -sin th sin th cos th
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
Scaling • An affine non-rigid body transformation • Multiply coordinate by some factor • Negative multiplication produces reflection
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 a 1 0 0 -a 0 1 0 b 0 1 0 –b 0 0 1 c 0 0 1 -c 0 0 0 1 0 0 0 1
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
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 = RT • Matrices satisfying this property are said to be orthogonal
Shear • Shear in the x axis is defined by x’ = x + y cot th • Inverse users -th
Concatenation of Transforms • A sequence of transformations with T1 followed by T2 followed by T3 can be composed as T3(T2(T1(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
Composed Transformaitons • Rotation in z about a fixed point T(p)Rz(th)T(-p) • General rotation Rx(thx) Ry(thy) Rz(thz)
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
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
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();
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
OpenGL Rotation about Point • Rotate 45o 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);
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
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