source code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <glut.h>
#define TEXTID 5
int form=0;
static int useLighting = 1;
static int useFog = 0;
float cx = 3.2,cy = 0.37,cz = .37;
static int tick = -1;
static int moving = 1;
#define GREY 0
#define RED 1
#define ORANGE 2
#define BLUE 3
#define CYAN 4
#define MAGENTA 5
#define YELLOW 6
#define BLACK 7
int obj_col = BLUE;
static float materialColor[9][4] =
{
{0.8, 0.8, 0.8, 1.0},
{0.8, 0.0, 0.0, 1.0},
{1.0, 0.3, 0.0, 1.0},
{0.0, 0.0, 0.8, 1.0},
{0.0, 0.8, 0.8, 1.0},
{0.8, 0.0, 0.8, 1.0},
{0.8, 0.8, 0.0, 1.0},
{0.0, 0.0, 0.0, 0.6},
};
static float lightPos[4] = {2.0, 4.0,2.0, 0.0};
static float lightAmb[4] = {0.2, 0.2, 0.2, 1.0};
static float lightDiff[4] = {0.8, 0.8, 0.8, 1.0};
static float lightSpec[4] = {0.4, 0.4, 0.4, 1.0};
static float groundPlane[4] = {0.0, 1.0, 0.0, 1.499};
static float firePlane[4] = {1.0, 0.0, 1.0, 0.899};
static float backPlane[4] = {0.0, 0.0, 1.0, 0.899};
static float fogColor[4] = {0.0, 0.0, 0.0, 0.0};
static float fogIndex[1] = {0.0};
static float cube_vertexes[6][4][4] =
{
{
{-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},
{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}
},
{
{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},
{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}
}
};
static float cube_normals[6][4] =
{
{-1.0, 0.0, 0.0, 0.0},
{1.0, 0.0, 0.0, 0.0},
{0.0, -1.0, 0.0, 0.0},
{0.0, 1.0, 0.0, 0.0},
{0.0, 0.0, -1.0, 0.0},
{0.0, 0.0, 1.0, 0.0}
};
void DrawTextXY(double x,double y,double z,double scale,char *s)
{
int i;
glPushMatrix();
glTranslatef(x,y,z);
glScalef(scale,scale,scale);
for (i=0;i<strlen(s);i++)
{
glColor3f(0.0,0.0,0.0);
glutStrokeCharacter(GLUT_STROKE_ROMAN,s[i]);
}
glPopMatrix();
}
static void setColor(int c)
{
if (useLighting)
glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,&materialColor[c][0]);
else
glColor4fv(&materialColor[c][0]);
}
static void drawCube(int color)
{
int i;
setColor(color);
for (i = 0; i < 6; ++i)
{
glNormal3fv(&cube_normals[i][0]);
glBegin(GL_QUADS);
glVertex4fv(&cube_vertexes[i][0][0]);
glVertex4fv(&cube_vertexes[i][1][0]);
glVertex4fv(&cube_vertexes[i][2][0]);
glVertex4fv(&cube_vertexes[i][3][0]);
glEnd();
}
}
static void drawCheck(int w, int h, int evenColor, int oddColor)
{
static int initialized = 0;
static int usedLighting = 0;
static GLuint checklist = 0;
if(!initialized || (usedLighting != useLighting))
{
static float square_normal[4] = {0.0, 0.0, 1.0, 0.0};
static float square[4][4];
int i, j;
if (!checklist)
checklist = glGenLists(1);
glNewList(checklist,GL_COMPILE_AND_EXECUTE);
glNormal3fv(square_normal);
glBegin(GL_QUADS);
for (j = 0; j < h; ++j)
{
for (i = 0; i < w; ++i)
{
square[0][0] = -1.0 + 2.0 / w * i;
square[0][1] = -1.0 + 2.0 / h * (j + 1);
square[0][2] = 0.0;
square[0][3] = 1.0;
square[1][0] = -1.0 + 2.0 / w * i;
square[1][1] = -1.0 + 2.0 / h * j;
square[1][2] = 0.0;
square[1][3] = 1.0;
square[2][0] = -1.0 + 2.0 / w * (i + 1);
square[2][1] = -1.0 + 2.0 / h * j;
square[2][2] = 0.0;
square[2][3] = 1.0;
square[3][0] = -1.0 + 2.0 / w * (i + 1);
square[3][1] = -1.0 + 2.0 / h * (j + 1);
square[3][2] = 0.0;
square[3][3] = 1.0;
if (i & 1 ^ j & 1)
setColor(oddColor);
else
setColor(evenColor);
glVertex4fv(&square[0][0]);
glVertex4fv(&square[1][0]);
glVertex4fv(&square[2][0]);
glVertex4fv(&square[3][0]);
}
}
glEnd();
glEndList();
initialized = 1;
usedLighting = useLighting;
}
else
glCallList(checklist);
}
static void myShadowMatrix(float plane[4], float light[4])
{
float dot;
float shadowMat[4][4];
dot = plane[0] * light[0] +
plane[1] * light[1] +
plane[2] * light[2] +
plane[3] * light[3];
/* for ground plane dot = 5.499 & for back dot = 2.899 */
/* Diagonals to maintain symmetry */
shadowMat[0][0] = dot - light[0] * plane[0];
shadowMat[1][0] = 0.0 - light[0] * plane[1]; /* light[0]=2 */
shadowMat[2][0] = 0.0 - light[0] * plane[2];
shadowMat[3][0] = 0.0 - light[0] * plane[3];
shadowMat[0][1] = 0.0 - light[1] * plane[0];
shadowMat[1][1] = dot - light[1] * plane[1]; /* light[1]=4 */
shadowMat[2][1] = 0.0 - light[1] * plane[2];
shadowMat[3][1] = 0.0 - light[1] * plane[3];
shadowMat[0][2] = 0.0 - light[2] * plane[0];
shadowMat[1][2] = 0.0 - light[2] * plane[1]; /* light[2]=2 */
shadowMat[2][2] = dot - light[2] * plane[2];
shadowMat[3][2] = 0.0 - light[2] * plane[3];
shadowMat[0][3] = 0.0 - light[3] * plane[0];
shadowMat[1][3] = 0.0 - light[3] * plane[1]; /* light[3]=1 */
shadowMat[2][3] = 0.0 - light[3] * plane[2];
shadowMat[3][3] = dot - light[3] * plane[3];
glMultMatrixf((const GLfloat *) shadowMat);
}
void idle(void)
{
tick++;
if (tick >= 520)
tick = 0;
glutPostRedisplay();
}
void second()
{
glColor3f(1.0,0.0,0.0);
glLoadName(TEXTID);
DrawTextXY(-2.20,1.60,0.0,0.002,"OPTIONS: ");
glColor3f(1.0,0.0,0.0);
glLoadName(TEXTID);
DrawTextXY(-2.20,1.20,0.0,0.001,"press 1 to execute ");
glColor3f(1.0,0.0,0.0);
glLoadName(TEXTID);
DrawTextXY(-2.20,1.0,0.0,0.001,"press any key to exit ");
}
void first()
{
glColor3f(1.0,0.0,0.0);
glLoadName(TEXTID);
DrawTextXY(-2.20,1.50,0.0,0.0025,"Graphical Implementation Of ");
glColor3f(1.0,0.0,0.0);
glLoadName(TEXTID);
DrawTextXY(-2.20,1.20,0.0,0.0025,"''SHADOW OF AN OBJECT'' ");
glColor3f(1.0,1.0,1.0);
glLoadName(TEXTID);
DrawTextXY(-1.50,0.80,0.0,0.002," USING OpenGL");
glColor3f(1.0,1.0,0.0);
glLoadName(TEXTID);
DrawTextXY(-2.5,-0.0,0.0,0.001," SUBMITTED BY:");
glColor3f(1.0,1.0,0.0);
glLoadName(TEXTID);
DrawTextXY(-2.5,-0.25,0.0,0.001,"KAVYASHREE M.D. VALENTINA MONDAL");
glColor3f( 1.0,1.0,0.0);
glLoadName(TEXTID);
DrawTextXY(-2.5,-0.50,0.0,0.001,"(1BY08CS019) (1BY08CS057)");
glColor3f(0.0,1.0,0.0);
glLoadName(TEXTID);
DrawTextXY(-2.5,-0.75,0.0,0.001," UNDER THE GUIDANCE OF:");
glColor3f(0.0,0.0,1.0);
glLoadName(TEXTID);
DrawTextXY(-2.5,-1.00,0.0,0.001,"Mr. Muneshwara. M. S Mrs. Bharthi.R.");
glColor3f(0.0,0.0,1.0);
glLoadName(TEXTID);
DrawTextXY(-2.5,-1.25,0.0,0.001,"Lecturer,Dept. of CS&E Asst. Prof. , Dept. of CS&E");
glColor3f(1.0,0.0,0.0);
glLoadName(TEXTID);
DrawTextXY(-4.9,-1.60,0.0,0.002," BMS INSTITUTE OF TECHNOLOGY");
glColor3f(1.0,0.0,1.0);
glLoadName(TEXTID);
DrawTextXY(0.8,-1.855,0.0,0.0005," PRESS ANY KEY TO CONTINUE");
}
void play(void)
{
GLfloat cubeXform[4][4];
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(0.0, -1.9, 0.0);
glRotatef(-90.0, 1, 0, 0); /* Ground Plane */
glScalef(2.0, 2.0, 2.0);
drawCheck(6, 6, RED, GREY); /* draw ground */
glPopMatrix();
glPushMatrix();
glTranslatef(0.0, -0.4, -1);
glRotatef(23.0, 0, 1, 0); /* left Plane */
glScalef(2.2, 2.5, 2);
drawCheck(6, 6, RED, GREY); /* draw back */
glPopMatrix();
glPushMatrix();
glTranslatef(0.0, -0.4, -1);
glRotatef(-23.0, 0, 1, 0); /* Right Plane */
glScalef(2.2, 2.5, 2);
drawCheck(6, 6, RED,GREY); /* draw back */
glPopMatrix();
glPushMatrix();
glTranslatef(0.0, 0.15, 0.0);
glScalef(0.25, 0.25, 0.25);
glRotatef((360.0 / (50)) * tick, 1, 0,0);
glRotatef((360.0 / (50)) * tick, 0, 1,0);
glRotatef((360.0 / (50*4)) * tick, 0, 0,1);
glScalef(cx, cy, cz);
glGetFloatv(GL_MODELVIEW_MATRIX, (GLfloat *) cubeXform);
drawCube(obj_col); /* draw cube */
glPopMatrix();
glDepthMask(GL_FALSE);
glEnable(GL_BLEND);
glPushMatrix();
myShadowMatrix(backPlane, lightPos);
glTranslatef(0.0, 0.0, 3.0);
glMultMatrixf((const GLfloat *) cubeXform);
drawCube(BLACK); /* draw black shadow */
glPopMatrix();
glPushMatrix();
myShadowMatrix(firePlane, lightPos);
glTranslatef(0.0, 0.0, 2.0);
glMultMatrixf((const GLfloat *) cubeXform);
drawCube(BLACK); /* draw black shadow */
glPopMatrix();
glPushMatrix();
myShadowMatrix(groundPlane, lightPos);
glTranslatef(5.0, 0.0, 0.0);
glMultMatrixf((const GLfloat *) cubeXform);
drawCube(BLACK); /* draw ground shadow */
glPopMatrix();
glDepthMask(GL_TRUE);
glutSwapBuffers();
if(useFog)
glEnable(GL_FOG);
glFlush();
glutSwapBuffers();
glutPostRedisplay();
}
void display()
{
glClearColor(0.0,0.0,0.0,1.0);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
if(form==0)
first();
else if(form==1)
second();
else if(form==2)
play();
glFlush();
glutSwapBuffers();
glutPostRedisplay();
}
void myreshape(int w,int h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if(w<=h) glOrtho(-2.0,2.0,-2.0*(GLfloat)h/(GLfloat)w,2.0*(GLfloat)h/(GLfloat)w,-10.0,10.0);
else glOrtho(-2.0*(GLfloat)w/(GLfloat)h,2.0*(GLfloat)w/(GLfloat)h,-2.0,2.0,-10.0,10.0);
glMatrixMode(GL_MODELVIEW);
}
void HandleKeyboard(unsigned char key, int x, int y)
{
if(form==0)
{
form=1;
}
else if(form==1)
{
if(key=='1')
form=2;
else
exit(1);
}
display();
}
void cube_select(int col)
{
switch(col){
case 0: obj_col=0;break;
case 1: obj_col=1;break;
case 2: obj_col=2;break;
case 3: obj_col=3;break;
case 4: obj_col=4;break;
case 5: obj_col=5;break;
case 6: obj_col=6;break;
case 7: obj_col=7;break;}
}
void scale_select( int s)
{
switch(s){
case 0: cx=1.0,cy=2.0,cz=1.0;break;
case 1: cx=1.0,cy=1.0,cz=1.0;break;
case 2: cx=2.0,cy=2.0,cz=2.0;break;
case 3: cx=3.2,cy=.35,cz=.35;break;
case 4: cx=2.0,cy=2.0,cz=0.0;break;
case 5: break;}
}
void fog_select(int fog)
{
glFogf(GL_FOG_MODE, fog);
glutPostRedisplay();
}
void menu_select(int mode)
{
switch (mode)
{
case 1:
moving = 0;
glutIdleFunc(NULL);
break;
case 2:
moving = 1;
glutIdleFunc(idle);
break;
case 3:
useFog = !useFog;
useFog ? glEnable(GL_FOG) : glDisable(GL_FOG);
glutPostRedisplay();
break;
case 4:
useLighting = !useLighting;
useLighting ? glEnable(GL_LIGHTING) :
glDisable(GL_LIGHTING);
glutPostRedisplay();
break;
case 5:
exit(0);
break;
}
}
void main(int argc, char **argv)
{
int width = 1000, height = 1000;
int i,fog_menu,cube_menu,scale_menu;
glutInitWindowSize(width, height);
glutInitWindowPosition(360,100);
glutInit(&argc, argv);
/* choose visual */
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutCreateWindow("Cube Simulation(OpenGL Project)");
glutKeyboardFunc(HandleKeyboard);
glutReshapeFunc(myreshape);
glutDisplayFunc(display);
if (moving)
glutIdleFunc(idle);
cube_menu = glutCreateMenu(cube_select);
glutAddMenuEntry("GREY", 0);
glutAddMenuEntry("RED", 1);
glutAddMenuEntry("ORANGE", 2);
glutAddMenuEntry("BLUE", 3);
glutAddMenuEntry("CYAN", 4);
glutAddMenuEntry("MAGENTA", 5);
glutAddMenuEntry("YELLOW", 6);
glutAddMenuEntry("BLACK", 7);
glutAddMenuEntry("WHITE",8);
scale_menu = glutCreateMenu(scale_select);
glutAddMenuEntry("Default(Cuboid)", 0);
glutAddMenuEntry("CUBE (small)", 1);
glutAddMenuEntry("CUBE (large)", 2);
glutAddMenuEntry("ROD", 3);
glutAddMenuEntry("FLAT SHEET", 4);
fog_menu = glutCreateMenu(fog_select);
glutAddMenuEntry("Linear fog", GL_LINEAR);
glutAddMenuEntry("Exp fog", GL_EXP);
glutAddMenuEntry("Exp^2 fog", GL_EXP2);
glutCreateMenu(menu_select);
glutAddMenuEntry("Start motion", 2);
glutAddMenuEntry("Stop motion", 1);
glutAddMenuEntry("", 2);
glutAddMenuEntry("OPTIONS", 2);
glutAddMenuEntry("", 1);
glutAddSubMenu("Cube Colors", cube_menu);
glutAddSubMenu("Scaling", scale_menu);
glutAddMenuEntry("Toggle lighting",4 );
glutAddMenuEntry("Toggle Fog", 3);
glutAddSubMenu("Fog type", fog_menu);
glutAddMenuEntry("Quit", 5);
glutAttachMenu(GLUT_RIGHT_BUTTON);
/* setup context */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1.0, 1.0, -1.0, 1.0, 1.0, 3.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0, 0.0, -2.0);
glEnable(GL_NORMALIZE);
glEnable(GL_DEPTH_TEST);
if (useLighting)
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmb);
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightDiff);
glLightfv(GL_LIGHT0, GL_SPECULAR, lightSpec);
#if 0
glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, lightDir);
glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, 50);
glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 25);
#endif
if (useFog) {
glEnable(GL_FOG);}
glFogfv(GL_FOG_COLOR, fogColor);
glFogfv(GL_FOG_INDEX, fogIndex);
glFogf(GL_FOG_MODE, GL_EXP);
glFogf(GL_FOG_DENSITY, 0.5);
glFogf(GL_FOG_START, 1.0);
glFogf(GL_FOG_END, 3.0);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glShadeModel(GL_SMOOTH);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glutMainLoop();
}