P. 1
cg project

cg project

|Views: 72|Likes:
Published by Ramkrishna Pal

More info:

Published by: Ramkrishna Pal on Feb 17, 2011
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as TXT, PDF, TXT or read online from Scribd
See more
See less

02/17/2011

pdf

text

original

#include #include #include #include #include

<stdio.h> <stdlib.h> <string.h> <math.h> <GL/glut.h>

void Display(void); void CreateEnvironment(void); void MakeGeometry(void); void MakeLighting(void); void MakeCamera(int,int,int); void HandleKeyboard(unsigned char key,int x, int y); void HandleSpecialKeyboard(int key,int x, int y); void HandleMouse(int,int,int,int); void HandleMainMenu(int); void HandleSpeedMenu(int); void HandleVisibility(int vis); void HandleIdle(void); void DrawTextXY(double,double,double,double,char *); void GiveUsage(char *); static GLint fogMode; #define TRUE 1 #define FALSE 0 #define PI 3.141592653589793238462643 #define DRAFT 0 #define MEDIUM 1 #define BEST 2 int drawquality = DRAFT; int spincamera = TRUE; int cameradirection = 1; double updownrotate = 60; int ballbounce = TRUE; double ballspeed = 2; #define #define #define #define #define OVALID SPHEREID BOXID PLANEID TEXTID 1 2 3 4 5

static void init(void) { GLfloat position[] = { 0.5, 0.5, 3.0, 0.0 }; glEnable(GL_DEPTH_TEST); glLightfv(GL_LIGHT0, GL_POSITION, position); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); { GLfloat mat[3] = {0.1745, 0.01175, 0.01175}; glMaterialfv (GL_FRONT, GL_AMBIENT, mat); mat[0] = 0.61424; mat[1] = 0.04136; mat[2] = 0.04136; glMaterialfv (GL_FRONT, GL_DIFFUSE, mat); mat[0] = 0.727811; mat[1] = 0.626959; mat[2] = 0.626959; glMaterialfv (GL_FRONT, GL_SPECULAR, mat);

0). GLUT_DEPTH).5.j. if (drawquality < DRAFT) drawquality = DRAFT. 0.1). drawquality = atoi(argv[i+1]). glutKeyboardFunc(HandleKeyboard). CreateEnvironment(). glFogf (GL_FOG_END.2). int mainmenu. 0. glutSpecialFunc(HandleSpecialKeyboard).6*128.0}. for (i=1.i<argc.depth."-h") != NULL) GiveUsage(argv[0]). . // glutKeyboardFunc (keyboard). glFogfv (GL_FOG_COLOR. } glClearColor(0. glutAddMenuEntry("Slow"."-q") != NULL) { if (i+1 >= argc) GiveUsage(argv[0]). 0.35). fogMode = GL_EXP.5. 1. 1. if (drawquality > BEST) drawquality = BEST. glutAddMenuEntry("fast". 0. GL_DONT_CARE). 0.argv). if (strstr(argv[i]. glFogi (GL_FOG_MODE. glutVisibilityFunc(HandleVisibility).5.3). i++. /* fog color */ } int main(int argc.0). 5. glutDisplayFunc(Display). } glEnable(GL_FOG). } } /* Set things up and go */ glutInit(&argc.char **argv) { int i. fogColor). glHint (GL_FOG_HINT. glutInitDisplayMode(GLUT_DOUBLE GLUT_RGB glutCreateWindow("OpenGL Demo").0). /* Set up some menus */ speedmenu = glutCreateMenu(HandleSpeedMenu). glutMouseFunc(HandleMouse).speedmenu. glFogf (GL_FOG_START. 0. GL_SHININESS.glMaterialf (GL_FRONT. { GLfloat fogColor[4] = {0.5. fogMode).5.5. glFogf (GL_FOG_DENSITY.i++) { if (strstr(argv[i]. glutAddMenuEntry("Medium".0). 1.

} if (drawquality == BEST) { glEnable(GL_LINE_SMOOTH). MakeLighting(). if (drawquality == DRAFT) { glShadeModel(GL_FLAT).GL_FILL).1). glFrontFace(GL_CW). glShadeModel(GL_SMOOTH).0.0). glutAddSubMenu("Ball speed". glClearColor(0. . glEnable(GL_POINT_SMOOTH). } /* This is the basic display callback routine It creates the geometry.0. and viewing position In this case it rotates the camera around the scene */ void Display(void) { glClear(GL_COLOR_BUFFER_BIT glPushMatrix(). } if (drawquality == MEDIUM) { glShadeModel(GL_SMOOTH).0.0). MakeCamera(FALSE. glPopMatrix().0). This isn't necessary for double buffers */ GL_DEPTH_BUFFER_BIT).0.2). /* glFlush(). things that will not change in time */ void CreateEnvironment(void) { glEnable(GL_DEPTH_TEST). glutAddMenuEntry("Quit".0.0. glEnable(GL_POLYGON_SMOOTH).100). lighting. glutAddMenuEntry("Toggle camera spin". that is. MakeGeometry().mainmenu = glutCreateMenu(HandleMainMenu). /* Assume RGBA capabilities */ } glLineWidth(1. glPolygonMode(GL_FRONT_AND_BACK. glDisable(GL_CULL_FACE). glDisable(GL_DITHER). glutMainLoop(). } /* This is where global settings are made.speedmenu). return(0). glutAttachMenu(GLUT_RIGHT_BUTTON). /* Background colour */ glEnable(GL_COLOR_MATERIAL). glutAddMenuEntry("Toggle ball bounce".0. glPointSize(1.0).

1.5.0. glEnd().0).0}.4).0.mdiff3). 0.0}.0. glMaterialfv(GL_FRONT_AND_BACK.0. /* Place a few grey boxes around the place */ glLoadName(BOXID).0.0.0}. GLfloat mshin1[] = {5. } glNormal3f( 0.0.0.5. } glPushMatrix().0.1.0}. /* For the sphere */ GLfloat mspec1[] = {0.0}.6.0. glColor3f(0.mamb2).1.0).0.0. else glutSolidCube(0.5. } /* Create the geometry */ void MakeGeometry(void) { int i.0.GL_AMBIENT.0). glPopMatrix().0.2.8).1.0. GLfloat mdiff1[] = {0. if (drawquality > DRAFT) glutSolidCube(0. 0. glMaterialfv(GL_FRONT_AND_BACK.-2.0.1.4). GLfloat mdiff3[] = {0. else glutSolidCube(0. /* Grey boxes */ GLfloat mamb3[] = {0. glVertex3f(-2.0.6.1. if (drawquality > DRAFT) glutWireCube(0.0. double radius = 0.1.0. 0.0.5.2.1.1. glVertex3f( 2.0}. GLfloat mdiff2[] = {0.0. else glBegin(GL_LINE_LOOP). if (drawquality > DRAFT) glBegin(GL_POLYGON). /* Create a green ground plane */ glLoadName(PLANEID).0.GL_DIFFUSE.0.0).0}.0.0). if (drawquality > DRAFT) { glMaterialfv(GL_FRONT_AND_BACK. static double theta = 0. glVertex3f( 2. GLfloat mamb1[] = {0.0.0}. glColor3f(0.0. /* Create a magenta bouncing ball */ glLoadName(SPHEREID).5.mamb3).GL_DIFFUSE.6.0. 0.0.8.2. if (drawquality > DRAFT) { glMaterialfv(GL_FRONT_AND_BACK.0. .5.0).2.0.0.0.0.2.0.5.0). 2.1.5.mdiff2).1.5.5).0.glutSwapBuffers().GL_AMBIENT. /* Green plane */ GLfloat mamb2[] = {0. glTranslatef(1.0.4).0.-2. glTranslatef(-3.4). 0. 1.1. 2. glVertex3f(-2.0.0.

0.mshin1). for (i=0.0.0).mspec1).0.8.8.GL_SHININESS.002.1.0.dif0).0. if (drawquality > DRAFT) { glMaterialfv(GL_FRONT.0). glLightfv(GL_LIGHT1.4. glScalef(radius.1. GLfloat dif1[] = {0. glColor3f(1. glBegin(GL_LINE_STRIP).16.GL_SPECULAR.0.0.radius).GL_DIFFUSE. glEnable(GL_LIGHT1).0. glEnable(GL_LIGHT0).0.1. glPopMatrix().1.32).0.-5.radius+0.0.4.0}.0.0}. GLfloat pos1[] = {5.0}.5*sin(i*PI/180).0). /* The specifications for 3 light sources */ GLfloat pos0[] = {1.0. /* Write some text */ glLoadName(TEXTID).i<=180.0.0. /* Create a white half-oval loop at one end */ glLoadName(OVALID).0.GL_AMBIENT_AND_DIFFUSE). if (drawquality > DRAFT) glutSolidSphere(1.GL_DIFFUSE.1.dif1).pos0). glLightfv(GL_LIGHT0.3.0.glColor3f(0. if (drawquality > DRAFT) { /* Set ambient globally."BOUNCING SPHERE"). /* w = 0 == infinite distance */ GLfloat dif0[] = {0.0. glTranslatef(0.3.4.6).0.2.16).0}.0. default ambient for light sources is 0 */ glLightModelfv(GL_LIGHT_MODEL_AMBIENT.3. glColorMaterial(GL_FRONT_AND_BACK.0.0.0. glLightfv(GL_LIGHT0.5*(1+sin(PI*theta/180)).GL_POSITION.-0.-2.0. else glutWireSphere(1.0.globalambient). DrawTextXY(-2.radius.25.0.6.1.0. glMaterialfv(GL_FRONT.pos1). if (ballbounce) theta += ballspeed. } glPushMatrix().1.GL_POSITION.32.0}. glEnable(GL_LIGHTING). } /* Light from below */ /* Fainter */ . glEnd(). } /* Set up the lighing environment */ void MakeLighting(void) { GLfloat globalambient[] = {0.0.8.i++) glVertex3f(2*cos(i*PI/180). glLightfv(GL_LIGHT1.

if (pickmode == TRUE) { glGetIntegerv(GL_VIEWPORT. } glFogi (GL_FOG_MODE. glLoadIdentity().0. int y) { switch (key) { case 'f': case 'F': if (fogMode == GL_EXP) { fogMode = GL_EXP2.} /* Set up the camera Optionally creating a small viewport about the mouse click point for object selection */ void MakeCamera(int pickmode.0. } /* Deal with plain key strokes */ void HandleKeyboard(unsigned char key. //glutPostRedisplay().0. glLoadIdentity().0.0.2). */ */ .3. gluLookAt(5*cos(theta*PI/180)*sin(updownrotate*PI/180).0.0. /* Focus 0. printf ("Fog mode is GL_EXP\n"). /* Field of view */ 1.1. } else if (fogMode == GL_EXP2) { fogMode = GL_LINEAR.0). /* Get the viewport bounds */ gluPickMatrix(x. 5*sin(theta*PI/180)*sin(updownrotate*PI/180). 0.0.0. } else if (fogMode == GL_LINEAR) { fogMode = GL_EXP.0.viewport).viewport[3]-y. /* Camera setup */ glMatrixMode(GL_PROJECTION). printf ("Fog mode is GL_LINEAR\n"). 5*cos(updownrotate*PI/180).1.0). /* aspect ratio */ 0.3.0. fogMode).int x.viewport). printf ("Fog mode is GL_EXP2\n").int x. /* near and far */ glMatrixMode(GL_MODELVIEW).0. GLint viewport[4].1000.int y) { static double theta = 0. /* Up if (spincamera) theta += (cameradirection * 0. } gluPerspective(70. break.

glPushMatrix().maxselect = 100."Select buffer overflow\n"). MakeCamera(TRUE.int x. nhits = glRenderMode(GL_RENDER). break. break.nhits). break.int state. MakeGeometry(). } } /* Deal with special key strokes */ void HandleSpecialKeyboard(int key. break. for (i=0. glInitNames(). if (button == GLUT_LEFT_BUTTON) { } else if (button == GLUT_MIDDLE_BUTTON) { } /* Right button events are passed to menu handlers */ if (nhits == -1) fprintf(stderr. break. case 's': case 'S': spincamera = !spincamera.int y) { int i.int x.i<nhits. if (state == GLUT_DOWN) { glSelectBuffer(maxselect.nhits = 0. case 'b': case 'B': ballbounce = !ballbounce. case GLUT_KEY_DOWN: updownrotate += 2. int y) { switch (key) { case GLUT_KEY_LEFT: cameradirection = -1.selectlist).i++) ."\tPicked %d objects: ".case 27: /* ESC */ case 'Q': case 'q': exit(0). glPushName(-1). default: break. } } /* Handle mouse events */ void HandleMouse(int button. if (nhits > 0) { fprintf(stderr. case GLUT_KEY_UP: updownrotate -= 2. glPopMatrix(). break.x. glRenderMode(GL_SELECT).y). case GLUT_KEY_RIGHT: cameradirection = 1. break. // glutPostRedisplay(). GLuint selectlist[100].

fprintf(stderr. break.5. . else glutIdleFunc(NULL). } } } /* Handle the main menu */ void HandleMainMenu(int whichone) { switch (whichone) { case 1: spincamera = !spincamera. case 3: ballspeed = 10.z coordinate is the bottom left corner (looking down -ve z axis) */ void DrawTextXY(double x.double z. case 2: ballspeed = 2. break.fprintf(stderr. case 100: exit(0).char *s) { int i. } /* Draw text in the x-y plane The x. break.double scale.selectlist[4*i+3]). case 2: ballbounce = !ballbounce."%d ". break. glPushMatrix(). } } /* Handle the ball speed sub menu */ void HandleSpeedMenu(int whichone) { switch (whichone) { case 1: ballspeed = 0.double y."\n"). break. break.y. } /* What to do on an idle event */ void HandleIdle(void) { glutPostRedisplay(). } } /* How to handle visibility */ void HandleVisibility(int visible) { if (visible == GLUT_VISIBLE) glutIdleFunc(HandleIdle).

2\n"). fprintf(stderr. } /* Display the program usage information */ void GiveUsage(char *cmd) { fprintf(stderr. glScalef(scale.toggle camera spin\n").change rotation direction\n"). fprintf(stderr. fprintf(stderr.i<strlen(s).scale)."Key Strokes and Menus:\n").1." -h this text\n"). fprintf(stderr. 0.rotate camera down\n")." down arrow .glTranslatef(x.cmd).y.rotate camera up\n").z).s[i])." b ."Usage: %s [-h] [-q n]\n".scale. fprintf(stderr." left arrow . fprintf(stderr." q . glPopMatrix(). fprintf(stderr. for (i=0. } .quit\n"). exit(-1)." s . fprintf(stderr." -q n quality. fprintf(stderr.i++) glutStrokeCharacter(GLUT_STROKE_ROMAN." right arrow .change rotation direction\n").toggle ball bounce\n"). fprintf(stderr." up arrow .

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->