//////////////////////////////////////////////////// // robot.cpp // Template code for drawing an articulated figure. //////////////////////////////////////////////////// #ifdef WIN32 #include #endif #ifndef WIN32 #include #include #include #include #include #include #endif #ifdef WIN32 #include #include #include #include #include "GL/glut.h" #endif void save_image(); void instructions(); const int STRLEN = 100; typedef char STR[STRLEN]; int Win[2]; // window (x,y) size #define PI 3.1415926535897 #define X 0 #define Y 1 #define Z 2 // // The eye point and look-at point. // double eye[3] = {1.5, 2.0, 10.0}; double ref[3] = {0.0, 0.0, 0.0}; // // The four degrees of freedom. // The angles are given in degrees, not radians. // double theta1=45; double theta2=45; double L = 2; double D = 0.6; ////////////////////////////////////////////////////// // PROC: drawCube() // DOES: this function draws a cube 1x1x1 // center around the origin. // // Don't change. ////////////////////////////////////////////////////// void drawCube(void) { glutSolidCube(1.0) ; } ////////////////////////////////////////////////////// // PROC: drawSphere() // DOES: this function draws a sphere with radius 1 // centered around the origin. // Don't change. ////////////////////////////////////////////////////// void drawSphere(void) { glutSolidSphere(1.0, 10, 10) ; } ////////////////////////////////////////////////////// // PROC: glut_key_action() // DOES: this function gets caled for any keypresses // // Don't change. ////////////////////////////////////////////////////// void glut_key_action(unsigned char key, int x, int y) { switch (key) { case 'q': case 27: exit(0); case 's': save_image(); break; case 'h': case '?': instructions(); break; } } ///////////////////////////////////////// // PROC: save_image // DOES: saves the current image to a ppm file // // Don't change ///////////////////////////////////////// void save_image() { FILE *fp; STR fname; const int maxVal=255; register int y; unsigned char *pixels; strcpy(fname,"scene.ppm"); printf("Saving image %s: %d x %d\n", fname,Win[0],Win[1]); fp = fopen(fname,"wb"); if (!fp) { printf("Unable to open file '%s'\n",fname); return; } fprintf(fp, "P6\n"); fprintf(fp, "%d %d\n", Win[0], Win[1]); fprintf(fp, "%d\n", maxVal); pixels = new unsigned char [3*Win[0]]; for ( y = Win[1]-1; y>=0; y-- ) { glReadPixels(0,y,Win[0],1,GL_RGB,GL_UNSIGNED_BYTE, (GLvoid *) pixels); fwrite(pixels, 3, Win[0], fp); } fclose(fp); } /********************************************************* PROC: myinit() DOES: performs most of the OpenGL intialization -- change these with care, if you must. **********************************************************/ void myinit(void) { GLfloat ambient[] = { 0.0, 0.0, 0.0, 1.0 }; GLfloat diffuse[] = { 1.0, 1.0, 1.0, 1.0 }; GLfloat specular[] = { 1.0, 1.0, 1.0, 1.0 }; GLfloat position[] = { 0.0, 3.0, 3.0, 0.0 }; GLfloat lmodel_ambient[] = { 0.2f, 0.2f, 0.2f, 1.0f }; GLfloat local_view[] = { 0.0 }; /**** set lighting parameters ****/ glLightfv(GL_LIGHT0, GL_AMBIENT, ambient); glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse); glLightfv(GL_LIGHT0, GL_POSITION, position); glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient); glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, local_view); /* glFrontFace (GL_CW); */ glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glEnable(GL_AUTO_NORMAL); glEnable(GL_NORMALIZE); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LESS); } /********************************************************* PROC: set_colour(); DOES: sets all material properties to the given colour -- don't change **********************************************************/ void set_colour(float r, float g, float b) { float ambient = 0.2f; float diffuse = 0.7f; float specular = 0.4f; GLfloat mat[4]; /**** set ambient lighting parameters ****/ mat[0] = ambient*r; mat[1] = ambient*g; mat[2] = ambient*b; mat[3] = 1.0; glMaterialfv (GL_FRONT, GL_AMBIENT, mat); /**** set diffuse lighting parameters ******/ mat[0] = diffuse*r; mat[1] = diffuse*g; mat[2] = diffuse*b; mat[3] = 1.0; glMaterialfv (GL_FRONT, GL_DIFFUSE, mat); /**** set specular lighting parameters *****/ mat[0] = specular*r; mat[1] = specular*g; mat[2] = specular*b; mat[3] = 1.0; glMaterialfv (GL_FRONT, GL_SPECULAR, mat); glMaterialf (GL_FRONT, GL_SHININESS, 0.5); } /********************************************************* ********************************************************** ********************************************************** PROC: display() DOES: this gets called by the event handler to draw the scene, so this is where you need to build your ROBOT -- MAKE YOUR CHANGES AND ADDITIONS HERE Add other procedures if you like. ********************************************************** ********************************************************** **********************************************************/ void display(void) { glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt (eye[X], eye[Y], eye[Z], ref[X], ref[Y], ref[Z], 0.0,1.0,0.0); /* glClearColor (red, green, blue, alpha) */ /* Ignore the meaning of the 'alpha' value for now */ glClearColor(0.7f,0.7f,0.9f,1.0f); /* set the background colour */ /* OK, now clear the screen with the background colour */ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glFlush(); glutSwapBuffers(); } /********************************************** PROC: myReshape() DOES: handles the window being resized -- don't change **********************************************************/ void myReshape(int w, int h) { Win[0] = w; Win[1] = h; glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); /*** this defines the field of view of the camera ***/ /*** Making the first 4 parameters larger will give ***/ /*** a larger field of view, therefore making the ***/ /*** objects in the scene appear smaller ***/ glFrustum(-1,1,-1,1,4,100); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); /*** this sets the virtual camera ***/ /*** gluLookAt( x,y,z, x,y,z x,y,z ); ***/ /*** camera look-at camera-up ***/ /*** pos'n point vector ***/ gluLookAt(eye[X],eye[Y],eye[Z], ref[X],ref[Y],ref[Z], 0,1,0); } void instructions() { printf("Press:\n"); printf(" s to save the image\n"); printf(" q to quit.\n"); } /********************************************************* PROC: main() DOES: calls initialization, then hands over control to the event handler, which calls display() whenever the screen needs to be redrawn **********************************************************/ int main(int argc, char** argv) { char *filename = argv[1]; FILE *fp; if( argc == 1) { fp = stdin ; fprintf(stdout, "Give theta 1 theta 2 L and D\n") ; } else if ((fp=fopen(filename,"r")) == NULL) { fprintf(stdout,"Can't read %s\n",filename); exit(1); } // // Read the degrees of freedom from standard input. // fscanf(fp,"%lf %lf %lf %lf",&theta1,&theta2,&L,&D); fclose(fp); glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowPosition (0, 0); glutInitWindowSize(196,196); glutCreateWindow(argv[0]); myinit(); glutReshapeFunc (myReshape); glutKeyboardFunc( glut_key_action ); instructions(); glutDisplayFunc(display); glutMainLoop(); return 0; // never reached }