You are on page 1of 67

UNIT - 4

GEOMETRIC OBJECTS AND


TRANSFORMATIONS –I
• Scalars, Vectors and Points
• Three-dimensional primitives
• Coordinate systems and frames
• Modelling a colored cube
• Affine transformations
• Rotation, translation and scaling
Scalars, vectors and Points
• Scalars:
Scalars can be defined as members of sets.
ex1: Real and complex numbers
(or)
Scalars are quantities which have magnitude.
ex2: Distance between two points,
length of line segment.
• Scalars are denoted by the
Greek letters a, b, .........
ex: a + b = b + a
a.b =b.a
a + (b + ) = (a + b) + 
where a, b,  are real or complex
• Scalars alone have no geometric properties
• There are two special scalars
(i) Additive Identity (0)
ex: a + 0 = 0 + a = a
(ii) Multiplicative Identity (1)
ex: a . 1 = 1 . a = a
Vectors:
• A vector is a quantity with two attributes
– Direction (orientation)
– Magnitude (length)
ex: Directed line segments
• Vectors are denoted by lower case letters u,v,w...
• Vector Operations:
(a) Every vector has an inverse
– Same magnitude but points in opposite direction
ex:
v
-v
(b) Every vector can be multiplied by a scalar
ex:
v av

(c) The sum of any two vectors is a vector


– Using head-to-tail axiom ex:
u+v v

(d) There is a zero vector u


– Zero magnitude, undefined orientation
Zero vector is formed by the sum v
of one vector and its inverse ex:
-v
Vector Space :
• A Vector space contains two distinct types of
entities: Vectors and Scalars
• It is used for manipulating vectors.
Operations allowed in vector space:
(a) Scalar-Vector multiplication
ex: u = a v
(b) Vector – Vector addition
ex: w = u + v
v = u + 2w - 3r is a valid statement
Note:
• Vectors have no fixed positions, they exist any
where in space
ex: Following vectors are identical:
Same length and orientation
• The magnitude of a vector v is a real number denoted by
|v|. The operation of vector-scalar multiplication has the
property that |av| = |a| |v|, and the direction of av is the
same as the direction of v if a is positive and the opposite
direction if a is negative.

• Vectors in vector spaces are represented in two ways


1. Geometric Vector
ex: directed line segments
2. n-tuples of real numbers
ex: matrix form

• Vector spaces are not sufficient for defining geometric


objects, so we need points.
Points:
• A point is a location in space.
• Points are denoted using upper case letters
P,Q,R……
• Operations allowed between points and vectors
are:
(a) Point – Point subtraction
that yields a vector
(b) Point – Vector addition
that yields a new point
• For any point P, we can define 1 * P = P
0*P=0
v=P-Q

P=v+Q
Affine Space:
• Affine space = Vector space + point
• Operations allowed:
– Vector-vector addition
– Scalar-vector multiplication
– Point-vector addition
– Scalar-scalar operations
– Point-point subtraction
Example:
.
P

(P-Q)

R
. .Q
(Q-R)

(Q-R) + (P-Q) = (P-R)


Lines:
• Consider all points of the form
P(a) = P0 + a d (parametric form of the line)
• If a >= 0, then P(a) is the ray leaving P0 in the
direction d.
• If a takes both positive and negative values then
P(a) is a line that is infinitely long in both
directions.
• If 0<=a<=1 then P(a) is a line segment between
two points P(0) and P(1).
Affine sums:
For any point Q, vector v, and positive scalar a
P = Q + av describe all points on the line from
Q in the direction of v.

However, we can always find a


point R such that
v=R-Q
• Thus, P = Q + a(R - Q)
P = Q + aR - aQ
P = aR + (1- a)Q
This operation looks like the addition of two
points and multiplication of a point by a scalar and
leads to the equivalent form
P = a1 R + a2 Q where a1 + a2 = 1
This is called affine sum of points Q and R.
• Similarly, affine sum of points P1, P2…….Pn is
P = a1P1 + a2P2 + ……..+anPn
where a1 + a2 + ……..+an = 1

• If ai >=0, i=1,2,……n then it is called convex


hull of the set of points
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
P

Q Q

not convex
convex
Dot Product:

• Cos = u.v / |u||v|


• |u|2 = u.u


|u|cos v
Cross Product:
• |Sin| = |u x v| / |u||v|

n=uxv

v
Planes :
• A plane can be defined by a point and two
vectors or by three points P

Q
R u R

P(a,b)=R+au+bv P(a,b)=R+a(Q-R)+b(P-Q)
Formation of triangular plane

convex sum of S(a) and R


convex sum of P and Q

for 0<=a,b<=1, we get all points in triangle


S(a) = a P + (1- a) Q , 0 <= a <=1
(according to Affine sum)
T(b) = b S + (1- b) R , 0 <= b <=1

T(a,b) = b [a P + (1- a) Q] + (1- b) R


By rearranging the above equation, we get a
general form
T(a,b) = P + a u + b v
(or)
T(a,b) = P + a (Q-P) + b (R-Q)
Normals
• Every plane has a vector n normal (perpendicular,
orthogonal) to it
• From point & two vector form T(a,b)=P0+au+bv, we
know we can use the cross product to find n = u  v
• If a point P lies in the plane, then
(P- P0) becomes vector in the plane.
• So the equation of the plane becomes
n . (P- P0) = 0
v

u
P0
Three-dimensional primitives
1. Objects are described by their surfaces and can
be thought of as being hallow
2. Objects can be specified through a set of
vertices in 3D.
3. Objects are either composed of or can be
approximated by flat, convex polygons.
Coordinate systems and frames:
• Linear independent: A set of vectors v1, v2, …, vn
is linearly independent if
a1v1+a2v2+.. anvn=0 and iff a1=a2=…an=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
Dimension: In a vector space, the maximum number
of linearly independent vectors is fixed and is called
the dimension of the space

Basis: In an n-dimensional space, any set of n


linearly independent vectors form a basis for the
space.
These basis vectors(such as v1, v2, v3) define a
coordinate system
• In a 3D vector space, we can represent any vector
w uniquely in terms of any three linearly
independent vectors v1, v2, v3 as
w=a1v1+ a2v2 + a3v3
v2
w=a1v1+ a2v2 + a3v3

v1

v3
Here the scalars a1, a2 and a3 are the components of w
with respect to the basis v1, v2, v3.
• We can also represent the vector w as the column
matrix with respect to basis as shown below
w = aT v
a1 v1
where a = a2 and v= v2
a3 v3

v1
w = aT v = [a1 a2 a3] v2 = a1v1+ a2v2 + a3v3
v3
Example
w = 2v1 + 3v2 - 4v3 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 basis.
Note: We usually think of the basis vectors v1, v2, v3
as defining a coordinate system. But actually vectors
have no fixed position, so the following two figures are
equivalent.

v
v
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.
• A frame consists of 2 elements
(i) Basis vectors (B) (ii) a point P0

v2

P0 v1

v3
Representation in a Frame:

• Frame determined by (P0, v1, v2, v3)


• Within this frame, every vector can be written
as
v=a1v1+ a2v2 +….+anvn
• Every point can be written as
P = P0 + b1v1+ b2v2 +….+bnvn
Change of coordinate system
• Consider that { v1, v2, v3 } and { u1, u2, u3 } are
two bases

u1 = 11v1+12v2+13v3
u2 = 21v1+22v2+23v3 w

u3 = 31v1+32v2+33v3
The above 3 equations can also be expressed as
u1 v1
u2 = M v2
u3 v3
i.e u = M v------------------(1)

Where M is a 3x3 matrix defined by these scalars

 11 1 1 


     
M =  1
  1     
• Any vector w with respect to the first frame is represented by
w=a1v1+ a2v2 + a3v3

i.e w = aT v --------------------(2)
a1 v1
where a = a2 and v= v2
a3 v3

• Same vector w with respect to the second frame is represented by

w = bT u --------------------- (3)
b1 u1
where b = b2 and u = u2
b3 u3

Substituting equation (1) in (3)


w = bT M v ---------------------(4)

equating (2) with (4)


aT v = bT M v
aT = bT M
a = b MT
b = (MT ) -1 a
b = T a where T = (MT ) -1
Homogeneous Coordinates:
• There is a confusion between a vector and a point in 3D
representation because we can represent points and
vectors in a similar manner.
• For example, start with a frame defined by the point P0
and the vectors v1, v2, v3 as shown below
v2
.P = (x,y,z)
w
P0 v1

v3
• A point P located at (x,y,z) is represented by the
column matrix
𝑥
P = 𝑦 w.r.t basis vectors {v1, v2, v3}
𝑧
where x,y,z are the components of basis vectors
• Vector w is represented as
𝑥
w = 𝑦 w.r.t basis vectors {v1, v2, v3}
𝑧
• Homogeneous coordinates avoid this confusion by using a
4D representation for both points and vectors.
• In homogeneous coordinate representation “0” is
appended for vectors and “1” is appended for points.
• In the frame specified by (P0, v1, v2, v3), any point P can be
written uniquely as
P = a1v1+ a2v2 + a3v3 + P0

v1
v2
P = a1 a2 a3 1 ( i.e P = aT v )
v3
P0

This is called homogeneous coordinate representation of a


point.
• Any vector w can be written as
w = 1v1+ 2v2 + 3v3
v1
v2
w = [ 1 2 3 0 ] ( i.e w = bT v )
v3
P0
This is called homogeneous coordinate representation of a
vector.
Note:
(1) All standard transformations (rotation, translation,
scaling) can be implemented with matrix multiplications
using 4x4 matrices.
(2) The basis vectors must themselves have representations,
3-tuple is the representation of the basis vectors.
v1 = (1,0,0)T v2 = (0,1,0)T v3 = (0,0,1)T
Change of Frames:
Consider two frames (P0, v1, v2, v3) & (Q0, u1, u2, u3)
v2 u1
u2
v1 Q0
P0

v3 u3
• If (P0, v1, v2, v3 ) & (Q0, u1, u2, u3 ) are two frames,
then we can express the basis vectors & reference
point of the second frame in terms of the first as

u1 = 11v1+12v2+13v3
u2 = 21v1+22v2+23v3
u3 = 31v1+32v2+33v3
Q0 = 41v1+42v2+43v3 + P0

• The above equations can be written in the form of


u = M v ----------------------(1)
u1 v1
u2 v2
where u = u , v = and
3 v3
Q0 P0

 11 1 1 0


   
  0
M =  1
  1     0
 
  1     1

M is called the matrix representation of the


change of frames.
• Any vector w (or any point) with respect to the first frame is
represented by
w = aT v --------------------(2)
• Same vector w with respect to the second frame is
represented by
w = bT u --------------------- (3)

Substituting equation (1) in (3)


w = bT M v ---------------------(4)

equating (2) with (4)


aT v = bT M v
aT = bT M
a = b MT
b = (MT ) -1 a
b = T a where T = (MT ) -1
Example:
Consider two frames (P0, v1, v2, v3) & (Q0, u1, u2, u3)
having the following relationship
u1 = v1
u2 = v1+v2
u3 = v1+v2+v3
Q0 = v1+2v2+3v3 + P0

If P(1,2,3) is a point w.r.t first frame then the same


point w.r.t second frame is calculated using the
formula b = T a where T = (MT ) -1
1 0 0 0
1 1 0 0
M= 
1 1 1 0
 
1   1

1 1 1 1 1  1 0 1
0 1 1 
 0 1  1 1 
  
T
M = 0 0 1   (MT ) -1 =
0 0 1  
   
0 0 0 1
0 0 0 1
b=Ta
1  1 0 1 1  0 
0 1  1 1    0 
=    =  
0 0 1     0 
     
0 0 0 1 1  1

 the point P(1,2,3) in the first frame is


represented as P’(0,0,0) in the second frame.
Frames in OpenGL:
1. Object/Model Coordinates
2. World Coordinates
3. Eye/Camera Coordinates
4. Clip Coordinates
5. Normalized device coordinates
6. Window/Screen Coordinates
• Initially, the model view matrix is an identity
matrix, so the object frame and eye frame are
identical.

1 0 0 0
0 1 0 
0
M= 
0 0 1 0
 
0 0 0 1
Moving the Camera
1 0 0 0
0 1 0 0 
M= 
0 0 1  d
 
0 0 0 1

50
Modeling a colored cube:
• A cube is a simple 3D object.
• A cube is composed of 8 vertices and 6 faces. Each
face is a quadrilateral. Each vertex is shared by 3 faces.
Each edge is shared by 2 faces.
• We start modeling the cube by assuming that the vertices of the
cube are available through an array of vertices
i.e 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} } ;

5 6

1 2

4 7

0 3
• We can use these list of vertices to specify the faces of the cube.
• For example one face is
glBegin (GL_POLYGON);
glVertex3fv (vertices [0]);
glVertex3fv (vertices [3]);
glVertex3fv (vertices [2]);
glVertex3fv (vertices [1]);
glEnd ();
• Similarly the other five faces of the cube can be define as
(2,3,7,6), (0,4,7,3), (1,2,6,5), (4,5,6,7), (0,1,5,4).
• When we are defining the 3D polygon, we have to be careful
about the order in which we specify the vertices, because each
polygon has two faces(outer & inner).
• Note that vertices are ordered so that we obtain correct outward
facing.
• We call a face outward facing if the vertices are traversed in a
counter-clockwise direction when the face is viewed from the
outside.
• This method is known as the right-hand rule because if you orient
the fingers of your right hand in the direction the vertices are
traversed, the thumb points outward.
• We use the following functions to draw the faces of the cube and to use colors.

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} } ;

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 polygon(int a,int b, int c,int d) void colorcube(void)
{ {
glBegin(GL_POLYGON); polygon(0,3,2,1);
glColor3fv(colors[a]); polygon(2,3,7,6);
glVertex3fv(vertices[a]); polygon(0,4,7,3);
glColor3fv(colors[b]); polygon(1,2,6,5);
glVertex3fv(vertices[b]); polygon(4,5,6,7);
glColor3fv(colors[c]); polygon(0,1,5,4);
glVertex3fv(vertices[c]); }
glColor3fv(colors[d]);
glVertex3fv(vertices[d]);
glEnd();
}
• The above approach is inefficient because we use
many function calls to draw the cube.
• Drawing a cube by its faces in the most straight
forward way requires
– 6 glBegin, 6 glEnd
– 24 glColor
– 24 glVertex
– More if we use texture and lighting
Vertex Arrays:
• OpenGL provides a facility called vertex arrays, that
allows to incorporate the relationships among the
vertices, edges, and faces of geometric objects.
• The advantage of this method is to reduce the number
of function calls.
• Vertex arrays provide a method for storing array data
and this data is provided during the implementation.
• Six types of arrays supported
1. Vertices 2. Colors 3. Color indices
4. Normals 5. Texture coordinates 6. Edge flags
There are 3 steps in using vertex arrays
1. Enable the functionality of vertex arrays using the
following function calls
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
2. Tell OpenGL where and in what format the arrays are
glVertexPointer(3, GL_FLOAT, 0, vertices);
data array
3D points stored as floats data contiguous

glColorPointer(3, GL_FLOAT, 0, colors);

3. Render the object


• To provide the information in our data structure
about the relationship between the vertices and the
faces of the cube form the following array
GLubyte cubeIndices[24] = {0,3,2,1, /* face 1*/
2,3,7,6 /* face 2*/
0,4,7,3, /* face 3*/
1,2,6,5, /* face 4*/
4,5,6,7, /* face 5*/
0,1,5,4}; /* face 6*/
• Each successive four indices describe a face of the
cube
• Draw through glDrawElements which replaces all
glVertex and glColor calls in the display callback
Drawing the cube:
• Method 1: what to draw
for(i=0; i<6; i++)
number of indices
glDrawElements(GL_POLYGON, 4,
GL_UNSIGNED_BYTE, &cubeIndices[4*i]);

format of index data start of index data


Draws cube with 6 function calls!

• Method 2:
glDrawElements(GL_QUADS, 24,
GL_UNSIGNED_BYTE, cubeIndices);

Draws cube with 1 function call!!


Bilinear Interpolation: It is a method of assigning
colors to points inside the polygon.
C1
C2
C01(a)
C45(b)
C4 C5
C23(a)

C0 C3

C01(a) = (1- a) C0 + a C1
C23(a) = (1- a) C2 + a C3

C45(b) = (1- b) C4 + b C5
Affine Transformations
A transformation maps points to other points
and/or vectors to other vectors

v=R(u)

Q=T(P)
• Using homogeneous coordinates, a linear
transformation, v = Cu, can transform the
representation of a given point(or vector) into
another representation
a11 a12 a13 a14
a21 a22 a23 a24
where C =
a31 a32 a33 a34
0 0 0 1
• Any point in affine space is represented by
b1 a1
b2 a2
P= and vector by u =
b3 a3
1 0
Translation, Rotation and scaling
Translation:
• Move (translate, displace) a point to a new
location
• Displacement determined by a vector d
– Three degrees of freedom
– P’=P+d
Rotation:
Consider rotation about the origin by  degrees
– radius stays the same, angle increases by 
x = r cos (f + )
y = r sin (f + )

x’=x cos  –y sin 


y’ = x sin  + y cos 

x = r cos f
y = r sin f
Scaling
Expand or contract along each axis (fixed point of origin)

x’= bx x
y’= by y
Non-Uniform

Uniform
Euclidean Space:
• A Euclidean space is an extension of a vector
space that adds a measure of size or distance
and allows us to define such things as the
length of a line segment.

You might also like