Computer Graphics Laboratory With Mini Project
Computer Graphics Laboratory With Mini Project
7omputer Graphics
C
Laboratory with mini project
Modelling a color cube
► Aim
► Draw a colour cube and spin it using OpenGL transformation matrices.
► 3D and animation
► For 3D
► DEPTH buffer, enable dept test, clear the DEPTH buffer
► Use glOrtho to specify viewing volume
► For animation
► Use GLUT_DOUBLE
► glutSwapBuffers()
void display() {
glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);
z Vertices at z=-
1.0
y
z -1.0,1.0)
(-1.0, (1.0, -1.0,1.0)
z Vertices at
z=1.0
y
3 2
7
6
x
0 1
4 z 5 Setting
Indices
Order of specifying quads
cube
using
indices
► Each polygon has 2 sides
► Use the right hand rule
► Outward facing : if vertices are traversed counter
clockwise
► Inward facing : if vertices are traversed clockwise.
► Purpose : eliminate faces that are not visible.
► By default the back face is culled. Enable and see what
happens. glColor3f(1,0,0); 0321
► glEnable(GL_CULL_FACE); quad(0,3,2,1); // front facing
glColor3f(1,1,0);
quad(0,1,2,3); // back facing
y
3 2
Faces
A0321
7
B1265
6
C4567
x D0473
0 E2376
1
F0154
4 z 5 Setting
Indices
Spin the cube on idle
► General Rotation
► First rotate about z axis, then about y axis then about x
axis
► R = RxRyRz
void display() {
glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);
glutPostRedisplay();
}
COLORS Assign 7 c olors to each
vertex
GREEN YELLOW
CYAN WHITE
BLACK RED
BLUE
MAGENT
Variations