0% found this document useful (0 votes)
46 views

Geometry Transformations: 1 MCA 401A - Computer Graphics, Dept of Computer SC, M U

The document discusses various geometry transformations including translation, rotation, scaling, and shear. It introduces homogeneous coordinate transformation matrices to represent these transformations and how to build arbitrary transformation matrices by concatenating simple transformation matrices. Key concepts covered include rotation matrices, translation matrices, scaling matrices, and the order and inverses of transformations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

Geometry Transformations: 1 MCA 401A - Computer Graphics, Dept of Computer SC, M U

The document discusses various geometry transformations including translation, rotation, scaling, and shear. It introduces homogeneous coordinate transformation matrices to represent these transformations and how to build arbitrary transformation matrices by concatenating simple transformation matrices. Key concepts covered include rotation matrices, translation matrices, scaling matrices, and the order and inverses of transformations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 70

Geometry Transformations

***

MCA 401A - Computer Graphics, Dept of Computer Sc, M U 1


Geometry : Transformations: Objectives
Introduce standard transformations
Rotation
Translation
Scaling
Shear
Derive homogeneous coordinate transformation matrices
Learn to build arbitrary transformation matrices from simple
transformations

MCA 401A - Computer Graphics, Dept of Computer Sc, M U 2


General Transformations
A transformation maps points to other points and/or vectors to other
vectors
v=T(u)

Q=T(P)

MCA 401A - Computer Graphics, Dept of Computer Sc, M U 3


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

MCA 401A - Computer Graphics, Dept of Computer Sc, M U 4


Pipeline Implementation
T (from application program)

frame
u T(u) buffer
transformation rasterizer
v T(v) T(v)

T(v)
v
T(u)
u T(u)
vertices vertices pixels

MCA 401A - Computer Graphics, Dept of Computer Sc, M U 5


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
a, b, g: 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

MCA 401A - Computer Graphics, Dept of Computer Sc, M U 6


Translation
Move (translate, displace) a point to a new location
P’

P
Displacement determined by a vector d
Three degrees of freedom
P’ = P + d

MCA 401A - Computer Graphics, Dept of Computer Sc, M U 7


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

object translation: every point displaced


by same vector

MCA 401A - Computer Graphics, Dept of Computer Sc, M U 8


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+dx
note that this expression is in
four dimensions and expresses
y’=y+dy point = vector + point

z’=z+dz
MCA 401A - Computer Graphics, Dept of Computer Sc, M U 9
Translation Matrix
We can also express translation using a
4 x 4 matrix T in homogeneous coordinates
p’=Tp where 1 0 0 d x 
0 1 0 dy
 
T = T(dx, dy, dz) = 0 0 1 dz
 
0 0 0 1 

This form is better for implementation because all affine transformations can be
expressed this way and multiple transformations can be concatenated together

MCA 401A - Computer Graphics, Dept of Computer Sc, M U 10


Rotation (2D)
Consider rotation about the origin by q degrees
radius stays the same, angle increases by q

x’ = r cos (f + q) = r cos f cos q - r sin f sin q


y’ = r sin (f + q) = r sin f cos q + r sin q cos f

x’ = x cos q – y sin q
y’ = x sin q + y cos q

x = r cos f
y = r sin f

MCA 401A - Computer Graphics, Dept of Computer Sc, M U 11


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
x’=x cos q –y sin q
y’ = x sin q + y cos q
z’ =z

or in homogeneous coordinates

p’=Rz(q)p

MCA 401A - Computer Graphics, Dept of Computer Sc, M U 12


Rotation Matrix
 cos q - sin q 0 0
 sin q cos q 0 0 
R = Rz(q) = 
 0 0 1 0
 
 0 0 0 1

MCA 401A - Computer Graphics, Dept of Computer Sc, M U 13


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

1 0 0 0
0 cos q - sin q 0
 
R = Rx(q) = 0 sin q cos q 0
 
0 0 0 1

 cos q 0 sin q 0
 0 1 0 0
R = Ry(q) =  
 - sin q 0 cos q 0
 
 0 0 0 1
MCA 401A - Computer Graphics, Dept of Computer Sc, M U 14
Scaling
Expand or contract along each axis (fixed point of origin)

x’ = sx x
y’ = sy x
z’ = sz x

p’ = S p

sx 0 0 0
0 sy 0 0
S = S(sx, sy, sz ) =  
0 0 sz 0
 
0 0 0 1

MCA 401A - Computer Graphics, Dept of Computer Sc, M U 15


Reflection
corresponds to negative scale factors

sx = -1 sy = 1 original

sx = -1 sy = -1 sx = 1 sy = -1

MCA 401A - Computer Graphics, Dept of Computer Sc, M U 16


Inverses
Although we could compute inverse matrices by general formulas, we
can use simple geometric observations
Translation: T-1(d , d , d ) = T(-d , -d , -d )
x y z x y z

Rotation: R -1(q) = R(-q)


 Holds for any rotation matrix
 Note that since cos(-q) = cos(q) and sin(-q)=-sin(q)

R -1(q) = R T(q)
Scaling: S-1(sx, sy, sz) = S(1/sx, 1/sy, 1/sz)

MCA 401A - Computer Graphics, Dept of Computer Sc, M U 17


Concatenation
We can form arbitrary affine transformation matrices by multiplying
together rotation, translation, and scaling matrices. Suppose we carry
out three successive transformations on a point p, creating a point q
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

MCA 401A - Computer Graphics, Dept of Computer Sc, M U 18


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 = pTCTBTAT

MCA 401A - Computer Graphics, Dept of Computer Sc, M U 19


General Rotation About the Origin
A rotation by q about an arbitrary axis
can be decomposed into the concatenation
of rotations about the x, y, and z axes

R(q) = Rz(qz) Ry(qy) Rx(qx)


y
v
q
qx qy qz are called the Euler angles
Note that rotations do not commute
x
We can use rotations in another order but
with different angles
z

MCA 401A - Computer Graphics, Dept of Computer Sc, M U 20


Rotation About a Fixed Point other than the Origin
Consider a point with its center at pf and its sides alinged with the axes
Move fixed point to origin
Rotate
Move fixed point back
M = T(pf) R(q) T(-pf)

MCA 401A - Computer Graphics, Dept of Computer Sc, M U 21


Rotation About a Fixed Point other than the Origin

M = T(pf) R(q) T(-pf)

 cos q - sin q 0 xf - xf cos q + yf sin q 


 sin q cos q 0 yf - xf sin q - yf cos q 

 0 0 1 0 
 
 0 0 0 1 

MCA 401A - Computer Graphics, Dept of Computer Sc, M U 22


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

MCA 401A - Computer Graphics, Dept of Computer Sc, M U 23


Shear
Helpful to add one more basic transformation
Equivalent to pulling faces in opposite directions

MCA 401A - Computer Graphics, Dept of Computer Sc, M U 24


Shear Matrix
Consider simple shear along x axis

x’ = x + y cot q
y’ = y
z’ = z

1 cot q 0 0
0 1 0 0
H(q) =  
0 0 1 0
 
0 0 0 1

MCA 401A - Computer Graphics, Dept of Computer Sc, M U 25


Frames in OpenGL
OpenGL is based on a pipelined model, the first of which is a sequence of
operations on vertices. Six frames embedded in the pipeline :
 Object or model coordinate
 World coordinate
 Eye or camera coordinate
 Clip coordinate
 Normalized device coordinate
 Window or Screen coordinate
Object or World coordinates : same. if not model with predefined objects or
apply transformation before glVertex function.
Eye or camera frame : a frame whose origin is the center of the camera’s lens
and axes are aligned with the sides of the camera.

MCA 401A - Computer Graphics, Dept of Computer Sc, M U 26


Frames in OpenGL
Clip coordinate: a projection transformation that brings all potentially visible
objects into a cube centered at the origin.
Perspective division yields 3-D representations in Normalized device
coordinate
taking into account the viewport creates a 3-D representation in
Window coordinates
 Window coordinate are measured in units of pixels on the display but retain
depth information. Removing depth coordinates we get 2-D screen coordinates
:

MCA 401A - Computer Graphics, Dept of Computer Sc, M U 27


Frames in OpenGL :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)

MCA 401A - Computer Graphics, Dept of Computer Sc, M U 28


Frames in OpenGL
 The camera is at the origin of the frame. The three basis vectors in eye space correspond to
 The up direction of the camera, the y direction.
 The direction of camera is pointing, the –ve z direction.
 iii third orthogonal direction , x, placed so that the x,y,z directions form a right-handed
coord.system.

MCA 401A - Computer Graphics, Dept of Computer Sc, M U 29


Moving the Camera
If objects are on both sides of z=0, we must move camera frame

MCA 401A - Computer Graphics, Dept of Computer Sc, M U 30


OpenGL Transformations:Objectives
Learn how to carry out transformations in OpenGL
Rotation
Translation
Scaling
Introduce OpenGL matrix modes
Model-view
Projection

MCA 401A - Computer Graphics, Dept of Computer Sc, M U 31


OpenGL Matrices
In OpenGL matrices are part of the state
Multiple types
Model-View (GL_MODELVIEW)
Projection (GL_PROJECTION)
Texture (GL_TEXTURE) (ignore for now)
Color(GL_COLOR) (ignore for now)
Single set of functions for manipulation
Select which to manipulated by
glMatrixMode(GL_MODELVIEW);
glMatrixMode(GL_PROJECTION);

MCA 401A - Computer Graphics, Dept of Computer Sc, M U 32


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

C
p p’=Cp

vertices CTM vertices

MCA 401A - Computer Graphics, Dept of Computer Sc, M U 33


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

MCA 401A - Computer Graphics, Dept of Computer Sc, M U 34


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.

MCA 401A - Computer Graphics, Dept of Computer Sc, M U 35


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

MCA 401A - Computer Graphics, Dept of Computer Sc, M U 36


CTM in OpenGL
OpenGL has a model-view and a projection matrix in the pipeline which
are concatenated together to form the CTM
Can manipulate each by first setting the correct matrix mode

MCA 401A - Computer Graphics, Dept of Computer Sc, M U 37


Rotation, Translation, Scaling
Load an identity matrix:
glLoadIdentity

Multiply on right:

glRotatef(theta, vx, vy, vz)


 theta in degrees,
 (vx, vy, vz) define axis of rotation

glTranslatef(dx, dy, dz)


glScalef( sx, sy, sz)

Each has a float (f) and double (d) format (glScaled)


MCA 401A - Computer Graphics, Dept of Computer Sc, M U 38
Example
Rotation about z axis by 30 degrees with a fixed point of (1.0, 2.0, 3.0)

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(1.0, 2.0, 3.0);
glRotatef(30.0, 0.0, 0.0, 1.0);
glTranslatef(-1.0, -2.0, -3.0);

Remember that last matrix specified in the program is the first applied

MCA 401A - Computer Graphics, Dept of Computer Sc, M U 39


Arbitrary Matrices
Can load and multiply by (4x4 homogeneous-coordinate) matrices
defined in the application program

glLoadMatrixf(m)

glMultMatrixf(m)

The matrix m is a one dimension array of 16 elements which are the


components of the desired 4 x 4 matrix stored by columns
In glMultMatrixf, m multiplies the existing matrix on the right

MCA 401A - Computer Graphics, Dept of Computer Sc, M U 40


Matrix Stacks
In many situations we want to save transformation matrices for use later
Traversing hierarchical data structures (Chapter 10)
Avoiding state changes when executing display lists
OpenGL maintains stacks for each type of matrix
Access present type (as set by glMatrixMode) by

glPushMatrix(); glPushMatrix();
glPopMatrix(); glTranslatef (...);
glRotatef(...);
glTranslatef
/*.. draw object .. */
glPopMatrix();
MCA 401A - Computer Graphics, Dept of Computer Sc, M U 41
Reading Back Matrices
Can also access matrices (and other parts of the state) by query
functions
glGetIntegerv
glGetFloatv
glGetBooleanv
glGetDoublev
glIsEnabled

For matrices, we use as


double m[16];
glGetFloatv(GL_MODELVIEW, m);

MCA 401A - Computer Graphics, Dept of Computer Sc, M U 42


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 (colorcube.c) in a standard
way
Centered at origin
Sides aligned with axes

MCA 401A - Computer Graphics, Dept of Computer Sc, M U 43


Spinning of the cube : main.c
void main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB |
GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutCreateWindow(“Rotating a color cube");
glutReshapeFunc(myReshape);
glutDisplayFunc(display);
glutIdleFunc(spinCube);
glutMouseFunc(mouse);
glEnable(GL_DEPTH_TEST);
glutMainLoop();
}
MCA 401A - Computer Graphics, Dept of Computer Sc, M U 44
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;
}
MCA 401A - Computer Graphics, Dept of Computer Sc, M U 45
Display callback
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glRotatef(theta[0], 1.0, 0.0, 0.0);
glRotatef(theta[1], 0.0, 1.0, 0.0);
glRotatef(theta[2], 0.0, 0.0, 1.0);
colorcube();
glutSwapBuffers();
}
Note that because of fixed from of callbacks, variables
such as theta and axis must be defined as globals

Camera information is in standard reshape callback

MCA 401A - Computer Graphics, Dept of Computer Sc, M U 46


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
gluLookAt
Build models of objects
The projection matrix is used to define the view volume and to select
a camera lens

MCA 401A - Computer Graphics, Dept of Computer Sc, M U 47


Model-view and Projection Matrices

Although both are manipulated by the same functions, we have to be


careful because incremental changes are always made by
postmultiplication
For example, rotating model-view and projection matrices by the same
matrix are not equivalent operations. Postmultiplication of the model-view
matrix is equivalent to premultiplication of the projection matrix

MCA 401A - Computer Graphics, Dept of Computer Sc, M U 48


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 M0,M1,…..,Mn 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)

MCA 401A - Computer Graphics, Dept of Computer Sc, M U 49


Incremental Rotation
Consider the two approaches
For a sequence of rotation matrices R ,R ,…..,R
0 1 n , find the Euler angles for
each and use Ri= Riz Riy Rix
 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

MCA 401A - Computer Graphics, Dept of Computer Sc, M U 50


Quaternions
Extension of imaginary numbers from two to three dimensions
Requires one real and three imaginary components i, j, k

q=q0+q1i+q2j+q3k
Quaternions can express rotations on sphere smoothly and efficiently.
Process:
 Model-view matrix  quaternion
 Carry out operations with quaternions
 Quaternion  Model-view matrix

MCA 401A - Computer Graphics, Dept of Computer Sc, M U 51


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

MCA 401A - Computer Graphics, Dept of Computer Sc, M U 52


Geometry : BuildingModels:Objectives
Introduce simple data structures for building polygonal models
Vertex lists
Edge lists
OpenGL vertex arrays

MCA 401A - Computer Graphics, Dept of Computer Sc, M U 53


Representing a Mesh
Consider a mesh e2 v5
v6 e3
e9
e8 v8 v4
e1 e11
e10
e7 v7 e4
v1
v2 e12
e6 v3
e5

There are 8 nodes and 12 edges


5 interior polygons
6 interior (shared) edges
Each vertex has a location vi = (xi yi zi)

MCA 401A - Computer Graphics, Dept of Computer Sc, M U 54


Simple Representation
Define each polygon by the geometric locations of its vertices
Leads to OpenGL code such as
glBegin(GL_POLYGON);
glVertex3f(x1, x1, x1);
glVertex3f(x6, x6, x6);
glVertex3f(x7, x7, x7);
glEnd();

Inefficient and unstructured


 Consider moving a vertex to a new location
 Must search for all occurrences

MCA 401A - Computer Graphics, Dept of Computer Sc, M U 55


Inward and Outward Facing Polygons
The order {v1, v6, v7} and {v6, v7, v1} are equivalent in that the same polygon will
be rendered by OpenGL but the order {v1, v7, v6} 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

MCA 401A - Computer Graphics, Dept of Computer Sc, M U 56


Data Structures for Object Representation : 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

MCA 401A - Computer Graphics, Dept of Computer Sc, M U 57


Data Structures for Object Representation : Vertex Lists
Put the geometry (locations of the vertices) in an array
Use pointers from the vertices into this array
Introduce a polygon list
x1 y1 z1
P1
v1 x 2 y2 z 2
P2
P3
v7 x3 y3 z3
P4 v6 x 4 y4 z 4
P5
v8 x5 y5 z5.
v5 x6 y6 z6
topology v6
geometry
x 7 y7 z 7
x8 y8 z8
MCA 401A - Computer Graphics, Dept of Computer Sc, M U 58
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

MCA 401A - Computer Graphics, Dept of Computer Sc, M U 59


Edge List
e2 v5
e1 v1 x1 y1 z1 v6
e9
e3
e2 v6 e8 v8
e3 x2 y2 z2 e1 e11
e4 e10
e5
x3 y3 z3 e7 v7 e4
v1
e6 x4 y4 z4 v2 e12
v3
e7 e6 e5
e8 x5 y5 z5.
e9
x6 y6 z6 Note polygons are
not represented
x7 y7 z7
x8 y8 z8

MCA 401A - Computer Graphics, Dept of Computer Sc, M U 60


Modeling the faces of a Cube
• Model a color cube for rotating cube program
• Define global arrays for vertices and colors

GLfloat vertices[][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[][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}};

MCA 401A - Computer Graphics, Dept of Computer Sc, M U 61


Drawing a polygon from a list of indices
Draw a quadrilateral from a list of indices into the array vertices and
use color corresponding to first index
void polygon(int a, int b, int c , int d)
{
glBegin(GL_POLYGON);
glColor3fv(colors[a]);
glVertex3fv(vertices[a]);
glVertex3fv(vertices[b]);
glVertex3fv(vertices[c]);
glVertex3fv(vertices[d]);
glEnd();
}

MCA 401A - Computer Graphics, Dept of Computer Sc, M U 62


Draw cube from faces
void colorcube( ) 5 6
{
polygon(0,3,2,1);
polygon(2,3,7,6); 2
polygon(0,4,7,3); 1
polygon(1,2,6,5);
polygon(4,5,6,7); 7
4
polygon(0,1,5,4);
}
0
3

• Note that vertices are ordered so that we obtain correct outward facing
normals
MCA 401A - Computer Graphics, Dept of Computer Sc, M U 63
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 requires
6 glBegin, 6 glEnd
6 glColor
24 glVertex
More if we use texture and lighting

MCA 401A - Computer Graphics, Dept of Computer Sc, M U 64


Vertex Arrays
OpenGL provides a facility called vertex arrays that allows us to store
array data in the implementation
Six types of arrays supported
 Vertices
 Colors
 Color indices
 Normals
 Texture coordinates
 Edge flags
We will need only colors and vertices

MCA 401A - Computer Graphics, Dept of Computer Sc, M U 65


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 arrays stored as floats data contiguous Pointer to data array

MCA 401A - Computer Graphics, Dept of Computer Sc, M U 66


Mapping indices to faces
Form an array of face indices

GLubyte cubeIndices[24] = {0,3,2,1, /* face 0 */


2,3,7,6, /* face 1 */
0,4,7,3, /* face 2 */
1,2,6,5, /* face 3 */
4,5,6,7, /* face 4 */
0,1,5,4}; /* face 5 */

Each successive four indices describe a face of the cube


Draw through glDrawElements which replaces all glVertex and
glColor calls in the display callback

MCA 401A - Computer Graphics, Dept of Computer Sc, M U 67


Drawing the cube
Method 1: number of indices
what to draw

for(i=0; i<6; i++) glDrawElements(GL_POLYGON, 4,


GL_UNSIGNED_BYTE, &cubeIndices[4*i]);

format of index data start of index data


Method 2:

glDrawElements(GL_QUADS, 24,
GL_UNSIGNED_BYTE, cubeIndices);

Draws cube with 1 function call!!

MCA 401A - Computer Graphics, Dept of Computer Sc, M U 68


REFERENCE

1. Edward Angel, Interactive Computer Graphics - A top-Down


Approach Using OpenGL,5E , Pearson 2013.

2. D Hearn and P Backer, Computer Graphics, Pearson 2004.

MCA 401A - Computer Graphics, Dept of Computer Sc, M U 69


REFERENCE

1. Edward Angel, Interactive Computer Graphics - A top-Down


Approach Using OpenGL,5E , Pearson 2013.

2. D Hearn and P Backer, Computer Graphics, Pearson 2004.

MCA 401A - Computer Graphics, Dept of Computer Sc, M U 70

You might also like