0% found this document useful (0 votes)
100 views16 pages

Computer Graphics Laboratory With Mini Project

The document provides instructions for modeling and animating a color cube in OpenGL by defining 8 vertices and 6 polygons to represent the cube, assigning colors to each vertex, and using transformation matrices to spin the cube by changing the theta values for rotation around the x, y, and z axes using keyboard input to select the axis of rotation. It also describes setting up the viewing environment in OpenGL and using display and idle functions along with buffers to continuously redraw and spin the cube.
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)
100 views16 pages

Computer Graphics Laboratory With Mini Project

The document provides instructions for modeling and animating a color cube in OpenGL by defining 8 vertices and 6 polygons to represent the cube, assigning colors to each vertex, and using transformation matrices to spin the cube by changing the theta values for rotation around the x, y, and z axes using keyboard input to select the axis of rotation. It also describes setting up the viewing environment in OpenGL and using display and idle functions along with buffers to continuously redraw and spin the cube.
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/ 16

18CSL6

7omputer Graphics
C
Laboratory with mini project
Modelling a color cube

► Aim
► Draw a colour cube and spin it using OpenGL transformation matrices.

► Refer:Text-2: Modelling a Coloured Cube


Setting up environment

► 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);

glutSwapBuffers(); int main(int argc, char **argv){


}
void myinit(){
glutInit(&argc, argv);
glMatrixMode(GL_PROJECTION);
glLoadIdentity(); glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);
glOrtho(-2,2,-2,2,-2,2); glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_MODELVIEW); glutInitWindowSize(500,500);
}
glutCreateWindow("Color Cube - Your name");
glutDisplayFunc(display);
myinit();
glutMainLoop();
return 0;
}
Modelling a Color
cube

► use the vertex list to define a color


cube
► Surfac e based model
► 6 faces / 6 polygons / facets

► h/w process : cube with 8 vertices.

► Use indices for each point


► Let the vertic es be numbered 0,…7
Data structures used

► Use 8 vertices – A vertex list


► - The top level entity of cube.
► Each vertex can be specified indirectly through index.
► Advantage : each geometric location appears only once
► Does not have to repeat.
y

(-1.0, 1.0,-1.0) (1.0, 1.0,-1.0)

(-1.0, -1.0,-1.0) (1.0, -1.0,-1.0)


z

z Vertices at z=-
1.0
y

(-1.0, 1.0,1.0) (1.0, 1.0,1.0)


x

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

► 360 degree spin on idle


► On mouse click, change the axis of rotation.

► Use OpenGL functions.


► glRotatef(theta, x,y,z) // x,y,z specifies the axis.

► 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);

glLoadIdentity(); void spinCube() {


glRotatef(theta[0],1,0,0); theta[axis]+= 0.05;
glRotatef(theta[1],0,1,0); if(theta[axis]>=360) theta[axis] -= 360;
glRotatef(theta[2],0,0,1);
cube(); glutPostRedisplay();
}
glFlush(); void mouse(int btn, int state, int x, int y){
glutSwapBuffers(); if(btn == GLUT_LEFT_BUTTON &&
} state==GLUT_DOWN) axis = 0;
if(btn == GLUT_RIGHT_BUTTON &&
state==GLUT_DOWN) axis = 1;
if(btn == GLUT_MIDDLE_BUTTON &&
state==GLUT_DOWN) axis = 2;

glutPostRedisplay();
}
COLORS Assign 7 c olors to each
vertex

GREEN YELLOW

CYAN WHITE

BLACK RED

BLUE
MAGENT
Variations

► Try rotating about an arbitrary axis, eg. 1,1,0


► Increase or decrease the speed of rotation.
► Use the cube as a building block to design a 3 D object of your choice. A tree, a symbol,
a building. TRS
► Set glOrtho to -5,5,-5,5,-10,10
► Set vertices such that the cube is not at the center (0,0). Observe what happens. Why?
► float vertices[][3]={ {1,1,-1},{2,1,-1},{2,2,-1},{1,2,-1}, // z=-1
► {1,1,0},{2,1,0},{2,2,0},{1,2,0}, // z=0
► };

You might also like