€¦  · Web viewThe mathematical underpinnings of computer graphics. To distinguish among a...

22
www.Bookspar.com | Website for Students | VTU - Notes - Question Papers Page 1 of 22 UNIT -4 Geometric Objects and Transformations Related to three-dimensional graphics: Representations of basic geometric types Conversion between various representations Representation independent statements on geometric objects Flow of Discussion: The mathematical underpinnings of computer graphics. To distinguish among a geometric entity, its representation in a particular reference system, and a mathematical abstraction of it. Notions of affine and Euclidean vector spaces to create the necessary mathematical foundation for later work. Method which is independent of (free from) coordinate systems, to deal with geometric problems. This coordinate-free approach is more robust than one based on representing the objects in a particular coordinate system or frame. A system called “homogeneous coordinates” is used to explain this coordinate-free approach. It also leads to the efficient implementation techniques. To differentiate between and object and its representation, the terminology of abstract data types is used. Application of vector spaces, geometry, and linear algebra are presented much. A simple application program is developed to illustrate the basic principles and to realize the concepts within an API. In this chapter, the example is focused on the representation and transformations of a cube. The specification of transformations interactively is also considered. Scalars, Points, and Vectors In computer graphics, most of geometric objects can be defined using a limited set of simple entities (geometric objects) like: scalars, points, and vectors. These basic entities can be defined in at least three different ways: from a geometric perspective. mathematical perspective , or programming (computer-science) perspective To understand the difference between the mathematical definition of each type and the particular implementation of that type, the study of underlying mathematics is required. The Geometric View POINT In a three-dimensional geometric system, a point is a location in space. The only attribute that a point possesses is that point's location; a mathematical point does not even have a size. Points exist in space regardless of any reference or coordinate system. Though coordinate systems and frames solve the reference problem, point need to be obtained without introducing an arbitrary reference system. SCALARS Scalars are always the real numbers. Although scalars lack geometric properties, scalars are used as units of measurement. For example, the length of a line segment is scalar. VECTORS The directed line segments (vectors) connecting points are called vectors. The term vector is used for any quantity with direction and magnitude.

Transcript of €¦  · Web viewThe mathematical underpinnings of computer graphics. To distinguish among a...

www.Bookspar.com | Website for Students | VTU - Notes - Question Papers Page 1 of 15

U N I T - 4 G e o m e t r i c O b j e c t s a n d T r a n s f o r m a t i o n s

Related to three-dimensional graphics: Representations of basic geometric types Conversion between various representations Representation independent statements on geometric objects

Flow of Discussion: The mathematical underpinnings of computer graphics. To distinguish among a geometric entity, its

representation in a particular reference system, and a mathematical abstraction of it. Notions of affine and Euclidean vector spaces to create the necessary mathematical foundation for later

work. Method which is independent of (free from) coordinate systems, to deal with geometric problems. This

coordinate-free approach is more robust than one based on representing the objects in a particular coordinate system or frame. A system called “homogeneous coordinates” is used to explain this coordinate-free approach. It also leads to the efficient implementation techniques.

To differentiate between and object and its representation, the terminology of abstract data types is used. Application of vector spaces, geometry, and linear algebra are presented much. A simple application program is developed to illustrate the basic principles and to realize the concepts within an API. In this chapter, the example is focused on the representation and transformations of a cube. The specification of transformations interactively is also considered.

Scalars, Points, and VectorsIn computer graphics, most of geometric objects can be defined using a limited set of simple entities (geometric objects) like:

scalars, points, and vectors.

These basic entities can be defined in at least three different ways: from a geometric perspective. mathematical perspective , or programming (computer-science) perspective

To understand the difference between the mathematical definition of each type and the particular implementation of that type, the study of underlying mathematics is required.The Geometric View POINTIn a three-dimensional geometric system, a point is a location in space. The only attribute that a point possesses is that point's location; a mathematical point does not even have a size. Points exist in space regardless of any reference or coordinate system. Though coordinate systems and frames solve the reference problem, point need to be obtained without introducing an arbitrary reference system.SCALARSScalars are always the real numbers. Although scalars lack geometric properties, scalars are used as units of measurement. For example, the length of a line segment is scalar.

VECTORSThe directed line segments (vectors) connecting points are called vectors. The term vector is used for any quantity with direction and magnitude.Physical quantities, such as velocity and force, are vectors. A vector does not, however, have a fixed position.

Hence, the directed line segments shown inThree vectors are identical because they have the same direction (orientation) and magnitude (length). They are identical, although their positions differ.

The length and the direction of a vector can be changed by real numbers. In the figure, vector A has the same direction as line segment B, but B has twice the length that A has i.e., B = 2A.Likewise, C has the same length as A but points in the opposite direction, so C = -A.

www.bookspar.com

www.Bookspar.com | Website for Students | VTU - Notes - Question Papers Page 2 of 15

Combining vectors: Vectors can be combined by the head-to-tail rule.

In the figure, head of vector A is connected to the tail of vector B to form a new vector C whose magnitude and direction are determined by the line segment from the tail of A to the head of B so, C = A + B. Note that, because vectors have no fixed positions, any two vectors can be moved as necessary to form their sum graphically.

Operation between points and vectors:

Points and vectors are two distinct geometric types. It is possible to move from one point to another using a vector. Equivalently, two points define a vector between them and a point and a vector define a second point.

Figure shows a dangerous representation of a vector. The correct interpretation is that the given vector can be defined as going from a fixed reference point (the origin) to a particular point in space. Both the point and the vector exists regardless of the reference system.But in computer graphics both points and vectors are represented with a particular reference system and are operated.

The Mathematical View: Vector, Affine and Euclidean Spaces

Points, Scalars, and Vectors can be considered as members of mathematical sets. Mathematics defines a variety of spaces to represent and manipulate theses members.

These spaces are Vector (Linear) Space

A vector space contains two distinct entities: vectors and scalars and rules to perform operations on them.Operation between Scalars:Scalars can be combined with the rules (operations) called addition and multiplication, to form a Operation between a scalar and a vector: scalar field. Examples of scalars are the real numbers, complex numbers, and rational functions. A scalar and a vector can be combined with in a vector space, to form new vector through scalar-vector multiplication.Operation between vectors: Vector can be combined with another vector through vector-vector addition. Examples of mathematical vector spaces include n-tuples of real numbers, the solution of homogeneous linear differential equations, and the geometric operations on vectors.

Affine Space An affine space is an extension of the vector space that includes an additional type of object: the point and rules to perform operations on them.Operation between points: There are no operations between two points.Operation between point and scalar: There are no operations between a point and a scalar. Operation between point and vector: vector-point addition: is an operation in affine space that produces a new point.

Alternately, point-point subtraction is an operation in affine space that produces a vector. Examples of affine spaces include the geometric operations on points and vectors and the solution of linear inhomogeneous ordinary differential equations.

Euclidean space In a linear vector space, there is no way of measuring a scalar quantity. A Euclidean space is an extension that adds a measure of size or distance.In these abstract spaces, objects can be defined independently of any particular representation. But representations provide the tie between abstract objects and their implementation, and conversion between representations leads to geometric transformations.The Computer-Science ViewThe computer-Science view points, scalars and vectors as abstract data types (ADTs). An ADT is a set of operations on data; the operations are defined independently of how the data are represented internally or of how the operations are implemented. From a computational point of view, the geometric objects can be declared through code such as

vector u, v;point p, q;scalar a, b;

www.Bookspar.com | Website for Students | VTU - Notes - Question Papers Page 3 of 15

regardless of the internal representation or implementation of the objects on a particular system. Geometric ADTsPoints, Scalars and Vectors with their geometric, Mathematical and Computer Science Views can be used to perform geometrical operations and to form new objects. Let P, Q, R, . . .define points; Let α, β, γ, . . . denote scalars; and Let u, v, w,. . . denote vectors. For vectors and points, this notation refers to the abstract objects and this notation does not refer to the representations of these objects in a particular reference system. The magnitude of a vector v is a real number denoted by |v|.

Vector-Scalar Multiplication: The operation of vector-scalar multiplication has the property that |αv| = |α||v|, and the direction of αv is the same as the direction of v if α is positive. There is a special vector, the zero vector, 0, with zero magnitude.

Two equivalent operations that relate points and vectors: Subtraction of two points, P and Q-an operation that yields a vector v

denoted by v = P - Q. A consequence of this operation, is that, given any point Q and vector v,

there is a unique point, P, that satisfies the preceding relationship. This statement can be expressed as: Given a point Q and a vector v, there is a point P such that P = v + Q. Thus, P is formed by a point-vector addition operation. Figure shows a visual interpretation of this operation.

Vector-vector addition: The head-to-tail rule gives way of visualizing vector-vector addition. The sum u + v as shown below:

Figure by drawing the sum vector as connecting the tail of u to the head of v.

However, this can also be visualized [to show that, for any three points P, Q, and R, (P - Q) + (Q - R) = P – R] as below:

LinesThe sum of a point and a vector (or the subtraction of two points) leads to the notion of a line in an affine space. Consider all points of the form P(α) = Po+ αd, where Po is an arbitrary point, d is an arbitrary vector, and α is a scalar. Given the rules for combining points, vectors, and scalars in an affine space, P(α) is a point for any value of α. For geometric vectors, these points lie on a line, as shown below:

This form is sometimes called the parametric form of the line, because we generate points on the line by varying the parameter a. For α=0, the line passes through the point Po and, as a is increased, all the points generated lie in the direction of the vector d. If α is restricted to nonnegative values, ray emanating from Po and going in the direction of d is obtained.

Affine SumsIn an affine space, the addition of two arbitrary points and the multiplication of a point by a scalar are not defined. However, there is an operation called affine addition that has certain elements of these latter two operations. For any point Q, vector v, and positive scalar α,

www.Bookspar.com | Website for Students | VTU - Notes - Question Papers Page 4 of 15

P =Q + αvdescribes all points on the line from Q in the direction of v, as shown below:

A point R such that v = R – Q can always be found. ThusP = Q + α(R - Q) =αR + (1 – α)Q.This operation looks like the addition of two points and leads to the equivalent form P =αlR +α2Q, where αl + α2 = 1.

ConvexityA convex object is one for which any point lying on the line segment connecting any two points in the object is also in the object. The affine sums help in deep understanding of convexity.For 0 ≥ α ≤ 1, the affine sum defines the line segment connecting R and Q, as shown in figure below.

Thus, this line segment is a convex object. The affine sum can be extended to include objects defined by n points P1, P2,. . ., Pn. Consider the formP =α1P1+ α2P2+ α3P3+ … αnPn;This sum is defined if and only if αl + α2 + α3 …+ αn= 1.(By Induction it can be proved)

Convex Hull : The set of points formed by the affine sum of n points, under the additional restriction α i ≥ 0, i = 1,2,3…n, is called the convex hull of the set of points.

It is easy to verify that the convex hull includes all line segments connecting pairs of points in {P1, P2, . . . , Pn} Geometrically, the convex hull is the set of points that form by stretching a tight-fitting surface over the given set of points - shrink wrapping the points. It is the smallest convex object that includes the set of points. The notion of convexity is extremely important in the design of curves and surfaces.

Dot and Cross ProductsThey give many of the geometric concepts related to the orientation (direction) between two vectors. DOT PRODUCT:

The dot product of u and v is written u.v. If u.v = 0, u and v are orthogonal. In a Euclidean space, the square of

the magnitude of a vector is |u|2=u.u The angle between two vectors u and v is given by

cosѲ= u . v|u|∨v∨¿¿

|v|cosѲ=u . v|u|

is the orthogonal projection of v onto u.

Given three linearly independent vectors in a three-dimensional space, dot product can be used to construct three vectors, each of which is orthogonal to the other two.

CROSS PRODUCT:Cross product of two non-parallel vectors, say u & v, gives a third vector, say n, that is orthogonal to both u and v. i.e., n = u x v

sin Ѳ=|u|X∨v∨ ¿|u|∨v∨¿¿

¿

Note that the vectors u, v, and n form a right-handed coordinate system; that is, if u points in the direction of the thumb of the right hand, and v points in the direction of the index finger, then n points in the direction of the middle finger.

www.Bookspar.com | Website for Students | VTU - Notes - Question Papers Page 5 of 15

PlanesA plane in an affine space can be defined as a direct extension of the parametric line. From simple geometry, the three points, not in a line, determine a unique plane.

Suppose that P, Q, and R are three such points in an affine space.The line segment that joins P and Q is the set of points of the form

S(α) = α P + (1 - α) Q, 0 ≥ α ≤ 1.Suppose an arbitrary point on this line segment is taken and form the line segment from this point to R, as shown in figure. Using a second parameter β, points along this line segment can be described as

T(β) = β S + (1 - β) R, 0 ≥ β ≤ 1Such points are determined by both α and β, and form the plane determined by P, Q, and R. Combining the preceding two equations, the equation of a

plane is:T(α, β) = β[α P + (1 - α) Q] + (1 - β) R

By rearranging, T(α, β) = β[α P + (1 - α) Q] + (1 - β) RT(α, β) = P + β(1 - α) (Q-P)] + (1 - β) (R-P)Noting that (Q – P) and (R – P) are arbitrary vectors, it is shown that a plane can also be determined from a point and two nonparallel vectors.A plane defined by Po, u, and v can be written as,T(α, β) = Po + α u + β vFor 0 ≥ α, β ≤ 1, all the points T(α, β) lie in the triangle formed by P, Q, and R.If a point P lies in the plane, then

P - Po = α u + β vA vector n that is orthogonal to both u and v is given by the cross product n =u x v.Then the equation of the plane becomes n . (P - Po)= 0.The vector n is perpendicular, or orthogonal, to the plane; it is called the normal to the plane. The forms P(α) for the line and T(α, β) for the plane, are known as parametric forms, because they give the value of a point in space for each value of the parameters α and β.

Three-Dimensional Primitives3-D world provides more variety of geometric objects. It retains even the objects available in 2-D world but they are no longer restricted to lie in the same plane. E.g. 1. 2-D curves become 2. Objects with interiors can 3.In addition, 3-D provides objects with volumes,curves in space become surfaces in space such as parallelepipeds and ellipsoids

Two problems to incorporate all these possibilities in a graphics system:1. The mathematical definitions of these objects can become complex. 2. All objects can not be implemented efficiently in graphics system. But, full range of 3-D objects can be

implemented by approximate methods.Three features characterize three-dimensional objects that fit well with existing graphics hardware and software:

1. The objects are described by their surfaces and can be thought of as being hollow:Reason for this condition: This condition implies that only 2-D primitives are needed to model 3-D objects, because a surface is a two- rather than a 3-D entity. This is because the modern graphics systems render triangular or other flat, polygons. High-end systems can render over 5 million small, flat polygons per second; boards in personal computers have rendering speeds of over 1 million polygons per second.

2. The objects can be specified through a set of vertices in three dimensions.Reason for this condition: If an object is defined by vertices, pipeline architecture can be used to process these vertices at high rates, and hardware can be used to generate the images of the objects only during rasterization.

3. The objects either are composed of or can be approximated by flat convex polygons.

www.Bookspar.com | Website for Students | VTU - Notes - Question Papers Page 6 of 15

Reason for this condition: Most graphics systems are optimized for the processing of points and polygons. In 3-Ds, a polygon can be defined by an ordered list of vertices. However, if there are more than three vertices, they do not have to be in the same plane; if they are not, there is no simple way to define the interior of the object. Consequently, most graphics systems require that the user specify simple planar polygons, or the results of rasterizing the polygon are not guaranteed to be what the programmer might desire. Because triangular polygons are always flat, either the modeling system is designed always to produce triangles, or the graphics system provides a method to divide, or tessellate, an arbitrary polygon into triangular polygons. If the same argument is applied to a curved object, such as to a sphere, small flat polygons can be approximated to form the sphere. Hence, if the modeling system provides curved objects, it can be assumed that a polygonal approximation is used for implementation.Constructive Solid Geometry (CSG): It is an approach of building objects from a small set of volumetric objects through set operations such as union and intersection. Although this approach is an excellent one for modeling, rendering CSG models is more difficult than is rendering surface-based polygonal models. (Chapter 8)Coordinate Systems and Frames

In a 3-D vector space, any vector w can be uniquely represented in terms of any three linearly independent vectors, v1, v2, and v3 as

w =αlv1 + α2v2 + α3v3.The scalars α1, α2, and α3 are the components of w with respect to the basis v1, v2, and v3. These relationships are shown in the figure.

The same w can be represented with the column matrix: [α 1α 2α 3]

This relationship can also be written as w = aT [v 1v 2v 3]

These basis vectors define a coordinate system.However, for dealing with problems using points, vectors, and scalars, a more general method is needed.

Figure shows the problem. The three vectors form a coordinate system that is shown in figure (a) with the three vectors emerging from a single point.These three basis vectors can be used as a basis to represent any vector in three dimensions. Vectors, however, have direction and magnitude, but lack a position attribute. Hence, figure (b) is equivalent, because the basis vectors, are moved, leaving their magnitudes and directions unchanged. Most people find this second figure confusing, even though

mathematically it expresses the same the information as the first figure. To represent points: Because an affine space contains points, in such a space, once a particular reference point say -the origin- is fixed, all other points can be represented unambiguously. The usual convention for drawing coordinate axes is emerging from the origin, as shown in above figure (a). It makes sense in the affine space where both points and vectors have representations. However, this representation requires both the reference point and the basis vectors; it is called a frame. Loosely, this extension fixes the origin of the vector coordinate system at some point Po. Within a given frame, every vector can be written uniquely as

w =αlv1 + α2v2 + α3v3.just as in a vector space. In addition, every point can be written uniquely as

P = Po + αlv1 + α2v2 + α3v3.Thus,

the representation of a particular vector in a frame requires three scalars; the representation of a point requires three scalars and the knowledge of where the origin is located.

Changes of Coordinate SystemsThe representation of a vector changes when the basis vectors are changed. Suppose that {v1, v2, v3}and {u1, u2, u3} are two bases. Each basis vector in the second set can be represented in terms of the first basis (and vice versa). Hence, there exist nine scalar components, {γij }, such that

The 3 x 3 matrix

is defined by these scalars, and

www.Bookspar.com | Website for Students | VTU - Notes - Question Papers Page 7 of 15

M contains the information to go from a representation of a vector in one basis to its representation in the second basis. The inverse of M gives the matrix representation of the change from {u1, u2, u3} to {v1, v2, v3}. Consider a vector w that has the representation {α1, α2, α3} with respect to {v1, v2, v3} i.e. w =αlv1 + α2v2 + α3v3.

Equivalently, w = aT [v 1v 2v 3] where a = [α 1

α 2α 3]

Assume that b is the representation of w with respect to {u1, u2, u3} i.e.w =βlu1 + β2u2 + β3u3.

or

w = bT [u 1u 2u 3]where b = [β 1

β 2β 3]

Then, using the representation of the second basis in terms of the first,

Thus, a = MTb.The matrix (MT)-1 moves from a to b:

b = Aa = (MT)-1 aThis result indicates that abstract vectors can be worked with column matrices of scalars - the vectors' representations. The important point to remember is that, during working with columns of real numbers as "vectors," care should be taken to prevent working in the wrong coordinate system.

These changes in basis leave the origin unchanged. These changes can be used represent rotation and scaling of a set of basis vectors to derive another basis set, as shown in figure.

However, a simple translation of the origin, or change of frame, as shown in figure, cannot be represented in this way.

Homogeneous Coordinates:A point P located at (x, y, z) can be represented using a 3-D frame determined by Po, v1, v2, and v3, with the column matrix:

[ xyz ] where x, y, and z are the components of the basis vectors for this point, so that,

P = Po + xv1 + yv2 + zv3.If the point is represented this way, then its representation is of the same form as the vector

w =αlv1 + α2v2 + α3v3. Then w can be represented as w = [α 1α 2α 3]

Many references associate the point (x, y, z) with the vector from the origin to this point. This association can cause confusion. E.g. the vector from the point (1,1,1) to (2,3,4) is the same as the vector from (0,0,0) to (1,2,3), because they have the same magnitude and direction, but the first vector would not be associated with point (1,2,3) unless one end of it is fixed to (0,0,0). Here points and vectors are maintained preferably as distinct geometric types.

In addition, failure to distinguish between points and vectors can make implementation more difficult because matrix multiplication in 3-Ds cannot represent a change in frames.

www.Bookspar.com | Website for Students | VTU - Notes - Question Papers Page 8 of 15

Hence another approach is used which uses homogeneous-coordinates to avoid these difficulties by using 4-D column matrices to represent both points and vectors in 3-Ds. In the frame specified by ( v1, v2, v3, Po), any point P can be written uniquely as

P = αlv1 + α2v2 + α3v3 + PoIf the "multiplication" of a point by the scalars 0 and 1 is defined as 0.P = 0 and 1.P= P, then, the aforesaid relation can be represented formally, using a matrix product, as

Strictly speaking, this expression is not a dot or inner product, because the elements of the matrices are dissimilar; nonetheless, the expression is computed as though it were an inner product. The 4-D row matrix on the right side of the equation is the homogeneous-coordinate representation of the point P in the frame determined by (v1, v2, v3, Po).

Equivalently, it can be said that P is represented by the column matrix, P = [α 1α 2α 31 ]

In the same frame, any vector w can be written

Thus, w can be is represented by the column matrix, w =

There are numerous ways to interpret this formulation geometrically. But here simply note that operations on points and vectors can be carried out using their homogeneous-coordinate representations and ordinary matrix algebra. E.g Consider a change of frames - a problem that caused difficulties when in 3-D representations. If (v1, v2, v3, Po) and (u1, u2, u3, Q0) are two frames, then the basis vectors and reference point of the second frame can be expressed in terms of the first as:

These equations can be written in the form:

where now M is the 4 x 4 matrix:

M is called the matrix representation of the change of frames.M can also be used to compute the changes in the representations directly. Suppose that a and b are the homogeneous-coordinate representations either of two points or of two vectors in the two frames. Then,

Hence a = MTb Advantages of using homogeneous coordinates:

All affine (line-preserving) transformations can be represented as matrix multiplications in homogeneous coordinates. Although 4-D work is involved in solving 3-D problems, less arithmetic work is involved when homogeneous coordinates are used.

The uniform representation of all affine transformations makes carrying out successive transformations (concatenation) far easier than in 3-D space.

In addition, modern hardware implements homogeneous-coordinate operations directly, using parallelism to achieve high-speed calculations.

Example of Change in Frames:

Assume a vector w whose representation in some basis is [123]

www.Bookspar.com | Website for Students | VTU - Notes - Question Papers Page 9 of 15

These three basis vectors can be denoted as v1, v2, v3. Hence, w =v1 + 2v2 + 3v3.Assume that a new basis is to be made from the three vectors v1, v2, and v3.

u1 = v1u2 = v1+v2u3 = v1+v2+v3

Since the reference point is not changed following equation is added: Q0 = P0Thus, the matrices of interest are the following matrix, its transpose and inverse:

[1 0 0 01 1 0 01 1 1 00 0 0 1]

Suppose that in addition to changing the basis vectors, it is needed to move the reference point to the point that has the representation (1,2,3,1 ) in the original system. The displacement vector v =v1 + 2v2 + 3v3 moves Po to Qo. The

fourth component identifies this entity as a point. Thus, the three equations are added from the previous example, the equation Q0 =P0 + v1 + 2v2 + 3v3 and the matrix MT becomes

[1 1 1 11 1 1 20 0 1 30 0 0 1]

Its inverse is: A = (MT)-1 = [1 −1 0 10 1 −1 10 0 1 −30 0 0 1 ]

This pair of matrices allows moving back and forth between representations in the two frames. Note that A takes the point

(1,2,3) in the original frame whose representation is P = [1231] to P' = [0001] the origin in the new system. However, the

vector (1,2,3) which is represented as a = [1230]in the original system is transformed to b =[−1−130 ], a transformation

that is consistent with the results from the example of change in coordinate systems, and that also demonstrates the importance of distinguishing between points and vectors.

Frames in OpenGLTwo frames in OpenGLare:

The camera frame The world frame.

It is fixed. The model-view matrix positions the world frame relative to the camera frame. Thus, the model-view matrix converts the homogeneous-coordinate representations of points and vectors to their representations in the camera frame. Because the model-view matrix is part of the state of the system, there is always a camera frame and a present-world frame. OpenGL provides matrix stacks, so model-view matrices or, equivalently, the frames, can be stored. The camera is at the origin of its frame. The three basis vectors correspond to the up direction of the camera, the y direction; the direction the camera is pointing, the negative z direction; and a third orthogonal direction, x, that is placed so that the x, y, z directions form a right-handed coordinate system.Other frames can be obtained in which objects can be placed by performing homogeneous coordinate transformations that define new frames relative to the camera frame. These transformations are defined and also can be used to position the camera relative to the objects. Because changes of frame are represented by model-view matrices and also these matrices can be stored, frames can be saved and moved between frames by changing the present model-view matrix.E.g. In the default settings below, the camera and world frames coincide with the camera pointing in the negative z direction.

In many applications, it is natural to define objects near the origin, such as a square centered at the origin or, perhaps, a group of objects whose center of mass is at the origin. It is also natural to set up the viewing conditions so that the camera sees only those objects that are in front of it. Consequently, to form images that contain all these objects, either the camera must be moved away from the objects or the objects must be moved away from the camera. Equivalently, the camera frame is moved relative to the world frame.

www.Bookspar.com | Website for Students | VTU - Notes - Question Papers Page 10 of 15

If the camera frame is regarded as fixed and the model-view matrix as positioning the world frame relative to the camera frame, then the model-view matrix, moves a point (x, y, z) in the world frame to the point (x, y, z, -d) in the camera frame. Thus, by making d a suitably large positive number, the objects are “moved” in front of the camera by moving the world frame relative to the camera frame (figure (b)). Note that, as far as the user - who is working in world coordinates - is concerned, user is positioning objects as before. The model-view matrix takes care of the relative positioning of the frames. Using this strategy is almost always better than attempting to alter the positions of the object by changing their vertices to place them in front of the camera.

In OpenGL, a model-view matrix can be set by sending an array of 16 elements to glLoadMatrix. However, the difficult question is how to find these coefficients. For geometric problems, one frame to another frame change can be obtained by a sequence of geometric transformations such as rotations, translations, and scales. Modeling a Colored CubeTo model, the pipeline approach used in 2-D is used again here with which the objects are defined in terms of sets of vertices. These vertices will pass through a number of transformations before the primitives that they define are rasterized in the frame buffer. The use of homogeneous coordinates not only will enable explaining this process, but also will lead to efficient implementation techniques.Consider the problem of drawing a rotating cube on the screen of our CRT. One frame of an animation is shown in figure.To generate the image following tasks are performed:

Modeling Converting to the camera frame Clipping Projecting Removing hidden surfaces Rasterizing

Modeling of a CubeThere are a number of ways to model a cube. A CSG system regards it as a single primitive. On the other extreme, the hardware processes the cube as an object consisting of eight vertices. But here surface-based model is used. It implies that a cube is regarded either as the intersection of six planes, or as the six polygons that define its faces, called its facets. Assume that the vertices of the cube are available through an array of vertices; for example, as follows:GLfloat vertices[8][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}};A more object-oriented form is adopted if a 3-D point type is defined: typedef GLfloat point3[3];

The vertices of the cube can then be defined aspoint3 vertices[8] ={{-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}};OpenGL implements all vertices in 4-D homogeneous coordinates. Function calls using a 3-D type, such as glVertex3fv, have the values placed into to 4-D form within the graphics system.List of points can then be used to define the faces of the cube.E.g. One face isglBegin(GL_POLYGON);

glVertex3fv(vertices[0]);glVertex3fv(vertices[3]);glVertex3fv(vertices[2]);glVertex3fv(vertices[1]);

glEnd( );and similarly other five faces can be defined.

www.Bookspar.com | Website for Students | VTU - Notes - Question Papers Page 11 of 15

Inward- and Outward-Pointing FacesWhen defining a 3-D polygon vertices must be specified in the order. E.g. The orders of the vertices (0, 3, 2, 1) and (1, 0, 3, 2) are same for the first face because the final vertex in a polygon definition is always linked back to the first. However, the order (0, 1, 2, 3) is different. Although it describes the same boundary, the edges of the polygon are traversed in the reverse order – (0, 3, 2, 1) - as shown in figure.

In addition, each polygon has two sides. Either or both of them can be displayed. But, a consistent way of identifying the faces is needed. A face is outward facing if the vertices are traversed in a counterclockwise order when the face is viewed from the outside. This method is also known as the right-hand rule because, if the fingers of right hand are oriented in the direction the vertices are traversed, the thumb points outward.In the example with the order (0,3,2, 1) rather than as (0, 1,2,3), the outer side of the back of the

cube could be defined correctly.Data Structures for Object RepresentationCube is now described through a set of vertex specifications. E.g.glBegin(GL_POLYGON) six times, each time followed by four vertices (via glVertex) and a glEnd

or via glBegin(GL_QUADS) followed by 24 vertices and a glEnd.Need for data structure: Both of the above methods work, but both fail to capture the essence of the cube's topology, as opposed to the cube's geometry.Topology of a six-sided polyhedron can be described by following statements:

Considering the cube as a polyhedron, the object i.e. the cube composed of six faces. The faces are each quadrilaterals that meet at vertices; each vertex is shared by three faces. In addition, pairs of vertices define edges of the quadrilaterals; each edge is shared by two faces.

All are true, regardless of the location of the vertices – i.e. regardless of the geometry of the object.

Data structures separate the topology of the object from its geometry, if used in building the objects. E.g. The data specifying the location of the vertices specify the geometry and can be stored as a simple list or array, such as in vertices[8] - the vertex list. The top-level entity is a cube; considered being composed of six faces. Each face consists of four ordered vertices. Each vertex can be specified indirectly through its index. This data structure is shown in figure.Advantages of this structure:

Each geometric location appears only once, instead of being repeated each time it is used for a facet. If, in an interactive application, the location of a vertex is

changed, the application needs to change that location only once, rather than searching for multiple occurrences of the vertex.

The vertex list structure is used in this example which can be expanded later if necessary.The Color Cube

Vertex list is used to define a color cube. Colors of the color solid (black, white, red, green, blue, cyan, magenta, yellow) are assigned to the

vertices. quad function is used to draw quadrilateral polygons specified by pointers into the vertex list. Finally, the color cube specifies the six faces, taking care to make them all outward-facing.

typedef GLfloat point3[3];point3 vertices[8] -{{-1.0,-1.0,1.0}.{-1.0,1.0,1.0}, {1.0,1.0,10}, {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[8][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}};void quad(int a. int b. int c . int d){

glBegin(GL_QUADS);glColor3fv(colors[a]);glVertex3fv(vertices[a]);glColor3fv(colors[b]);glVertex3fv(vertices[b]);glColor3fv(colors[c]);glVertex3fv(vertices[c]);

www.Bookspar.com | Website for Students | VTU - Notes - Question Papers Page 12 of 15

glColor3fv(colors[d]);glVertex3fv(vertices[d]);

glEnd( );}void colorcube( ){

quad(0,3,2,1);quad(2.3.7,6);quad(0.4.7,3);quad(l.2.6.5);quad(4.5.6.7);quad(0,1.5.4);

}Bilinear InterpolationIt is one of the methods the graphics system adopts to assign colors to points inside the polygon (i.e. interpolate), using the specified color information.

Consider the polygon in figure. The colors C0, Cl, C2, and C3 are the ones assigned to the vertices in the application program. Linear interpolation can be used to interpolate colors along the edges between vertices 0 and 1, and between 2 and 3, by using

C01(α) = (1 - α )C0 + α C1,C23(α )= (l - α )C2 + α C3.

As α goes from 0 to 1, colors, C0l(α) and C23(α) are generated along these two edges. For a given value of α, two colors, C4 and C5 are obtained on these edges.Colors along the line connecting the two points on the edges corresponding to C4 and C5 can be interpolated as below:

C45(β) =(1 - β)C4+ βC5.For a flat quadrilateral, each color generated by this method corresponds to a point on the polygon. If the four vertices are not all in the same plane, then, although a color is generated, its location on a surface is not dearly defined.A related algorithm, scan-line interpolation, avoids the flatness issue and can be made part of the scan-conversion process. A polygon is filled only when it is displayed.

If waited until rasterization, the polygon is first projected onto the 2-D plane as in figure. If a quadrilateral is filled scan line by scan line, as shown below, then colors are assigned scan line by scan line on the basis of only two edges.

OpenGL provides this method, not only for colors, but also for other values that can be assigned on a vertex-by-vertex basis (Chapter 6).

Vertex ArraysVertex arrays provide a method for encapsulating the information in the data structure such that polyhedral objects can be drawn with only a few function calls.

OpenGL provides vertex arrays, a facility that extends the use of arrays in a way that avoids most of the function calls to draw the cube. The main idea is that information stored in arrays can be stored on the clients, or application programs, and accessed by a single function call. The information can be stored in a way that retains the structuring earlier defined earlier, such as the order in which vertices are called to draw the cube.

Although the facility is called vertex arrays, OpenGL provides support for six types of arrays: vertex, color, color index, normal, texture coordinate, and edge flag. Some of the other types are discussed later.

Using vertex arrays requires following three steps. The first two steps are usually part of the initialization phase of the program.

1. Enabling their functionality.In this example, only color and vertex arrays are used and they are enabled by

glEnableClientState(GL_COLOR_ARRAY); glEnableClientState(GL_VERTEX_ARRAY);Syntax Description: void glEnableClientState(GLenum array) void glDisableClientState(GLenum array)Enables and disables arrays of types GL_VERTEX_ARRAY, GL_COLOR_ARRAY, GL_INDEX_ARRAY, GL_NORMAL_ARRAY, GL_TEXTURE_COORD_ARRAY, or GL_EDGE_FLAG_ARRAY.

2. Specifying the format of the arrays. The form of the arrays is given by

glVertexPointer(3, GL_FLOAT, 0, vertices); glColorPointer(3, GL_FLOAT, 0, colors);

The first value (3) denotes three-dimensional data. The second and third parameters indicate that the data are floats packed

www.Bookspar.com | Website for Students | VTU - Notes - Question Papers Page 13 of 15

in the array given by the fourth parameter.Syntax Description: void glVertexPointer(GLint dim, GLenum type, GLsizei stride, GLvoid *array)void glColorPointer(GLint dim, GLenum type, GLsizei stride, GLvoid *array)Provides the information on arrays. The data are in array, dim is the dimension of the data (2,3, or 4), type denotes how the data are stored (GL_SHORT, GL_INT, GL_FLOAT, or GL_DOUBLE), and stride is the number of bytes between consecutive data values (0 means that the data are packed in the array).

3. Using the arrays to render the scene.A new array is needed that stores the indices in the order in which they are used. The following array contains the necessary information: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};

The cube can be drawn through the function glDrawElements( ).Syntax Description:

void glDrawElements(GLenum mode, GLsizei n, GLenum type, void *indices)It draws elements of type mode using n indices from the array indices for each.The array is of type (GL_UNSIGNED_BYTE, GL_UNSIGNED_SHORT, or GL_UNSIGNED_INT).

If each face need to be rendered individually, following loop can be used:

for(i = 0; i < 6; i++)glDrawElements(GL_POLYGON, 4, GL_UNSIGNED_BYTE, &cubeIndices[4*i]);

However, If GL_QUADS is used, each successive group of four vertices determines a new quad. Thus, a single function call suffices:

glDrawElements(GL_QUADS, 24, GL_UNSIGNED_BYTE, cubeIndices);AffineTransformations

A transformation is a function that takes a point (or vector) and maps that point (or vector) into another point (or vector).

Such a function is shown in the figure.In the functional form, Q = T(P) for points, or v =R(u) for vectors. Using homogeneous coordinates, both vectors and points can be represented as 4-D column matrices, and the transformation can be defined with a single functionq = f(p), v = f(u),that transforms the representations of both points and vectors in a given frame.

Advantages/ Characteristics of Affine Transformations: When working with homogeneous coordinates, A is a 4 x 4 matrix that leaves unchanged the fourth (w)

component of a representation. A is of the form

The 12 values can be set arbitrarily, and hence this transformation can be said to have has 12 degrees of freedom. However, points and vectors have slightly different representations in the affine space. Vector is representation Point representation

If an arbitrary A is applied to a vector, then v=Au, and only nine of the elements of A affect u, and, thus, there are only 9 degrees of freedom in the transformation of vectors. Affine transformations of points have the full 12 degrees of freedom.

Affine transformations preserve lines. Consider the line P(α) = Po+ α d, where Po is a point and d is a vector. In any frame, the line can be expressed as p(α) =Po + α d, where Po and d are the representations of Po and d in that frame.

For any affine transformation matrix A, A P(α) =A Po + α A d.Thus, transformed line can be constructed by first transforming Po and d, and then using whatever line-generation algorithm for the display.

Consider 2-point form of the line, p(α ) = α Po + (l - α )Pl, a similar result holds. The representations of Po and P1 are transformed, and then the transformed line is constructed. Because there are only 12

www.Bookspar.com | Website for Students | VTU - Notes - Question Papers Page 14 of 15

elements in M that can be selected arbitrarily, there are 12 degrees of freedom in the affine transformation of a line or line segment.

Types of Affine Transformations in Computer Graphics: 1) Translation 2) Rotation, and 3) Scaling.

With slight modifications, these results can also be used to describe the standard parallel and perspective projections.

TranslationTranslation is an operation that displaces points by a fixed distance in a given direction.

To specify a translation, only a displacement vector d is specified, because the transformed points are given by P'=P+d for all points P on the object. Note that this definition of translation makes no reference to a frame or representation. Translation has 3 degrees of freedom, because the three components of the displacement vector can be specified arbitrarily.

RotationRotation is more difficult to specify than translation, because more parameters are involvedE.g. Rotating a point about the origin in a 2-D plane, as shown below.

Having specified a particular point - the origin – there is a particular frame. A 2-D point at (x, y) in this frame is rotated about the origin by an angle Ѳ to the position (x’, y’). Standard equations describing this rotation can be obtained by representing (x, y) and (x', y') in polar form:x = ρ cosϕy = ρ sinϕ

x’ = ρ cos(Ѳ+ϕ)y’ = ρ sin(Ѳ+ϕ)Expanding these terms using the trigonometric identities for the sine and cosine of the sum of two angles,x’ = ρ cos(ϕ) cos(Ѳ) - ρ sin(ϕ) sin(Ѳ) = x cos(Ѳ) - y sin(Ѳ)y’ = ρ cos(ϕ) sin(Ѳ) + ρ sin(ϕ) cos(Ѳ) = x sin(Ѳ) + y cos(Ѳ)These equations can be written in matrix form as

This form can be expanded to 3-Ds later.Note three features of this transformation that extend to other rotations:

1. There is one point- the origin, in this case-that is unchanged by the rotation. This point is called the fixed point of the transformation.

2. Knowing that the 2-D plane is part of 3-D space, this rotation can be re-interpreted in 3-Ds. In a right handed system, when the x and y axes are drawn in the standard way, the positive z axis comes out of the page. Positive direction of rotation is defined counterclockwise when looking down the positive z axis toward the origin. This definition is used to define positive rotations about other axes.

3. 2-D rotation in the plane is equivalent to 3-D rotation about the z-axis. Points in planes of constant z all rotate in a similar manner, leaving their z values unchanged.

These observations can be used to define a general 3-D rotation that is independent of the frame. Three entities shown in figure, to be specified are:

a. A fixed point (Pf), b. A rotation angle (Ѳ), and c. A line or vector about which to rotate.

For a given fixed point, there are 3 degrees of freedom: the two angles necessary to specify the orientation of the vector, and the angle that specifies the amount of rotation about the vector.Rigid-body transformations: Rotation and translation are known as rigid-body transformations. No combination of rotations and translations can alter the shape of an object; they can alter only the object's location and orientation. Consequently, rotation and translation alone cannot give all possible affine transformations.

ScalingScaling is an affine non-rigid-body transformation. Scaling can make an object bigger or smaller, as shown in figure below.

www.Bookspar.com | Website for Students | VTU - Notes - Question Papers Page 15 of 15

This figure illustrates both uniform scaling in all directions and scaling in a single direction. Non-uniform scaling is needed to build up the full set of affine transformations that are used in modeling and viewing.

Scaling transformations have a fixed point, as shown below.Hence, to specify a scaling, the fixed point, a direction in which scaling is needed, and a scale factor (α) can be specified. For α > 1, the object gets longer in the specified direction; for 0 ≥ α < 1, the object gets smaller in that direction.

Negative values of α give reflection (figure below) about the fixed point, in the scaling direction.

A properly chosen sequence of scalings, translations, and rotations can be combined to form any affine transformation.