/* simple open gl 3D window-based program */ // ================================================================================= // 3D TEST // simplified from glut example in OpenGL SDK // ================================================================================= // INCLUDES #include "glut.h" #include #include #include #include "materials.h" #include "geometry.h" #include "matrix.h" // ================================================================================= // DEFINES #define PI 3.14159265 // ================================================================================= // PROTOTYPE FUNCTIONS void display(void); void initRenderingEnvironment (void); void display_list_1 (void); void parse_key(unsigned char key, int x, int y); void parse_special_key(int key, int x, int y); void SetCamera(void); void Animate(void); void myReshape(int w, int h); void setupLights(); // ================================================================================= // GLOBALS int frame = 0; int angle = 0; float eyeDistance=30.0; float eyeAround=0.0,eyeTilt=0.0; float eyex=1.0,eyey=3.0,eyez=4.0,coix=0.0,coiy=5.0,coiz=0.0; float goalx=-5,goaly=5,goalz=0; float initAngle0=-40.0,initAngle1=20.0,initAngle2=10.0; // ================================================================================= // MAIN // ================================================================================= int main(int argc, char *argv[]) { int sz; if (argc > 1) sz = atoi(argv[1]); else sz = 400; // instructions printf("\n"); printf(" key assignment\ns"); printf(" arrow keys rotate the eye around the origin\n"); printf(" 'i' zooms in; 'o' zooms out\n"); printf(" a adds one, z subtracts one, to goal x-coordinate\n"); printf(" s adds one, x subtracts one, to goal x-coordinate\n"); printf(" d adds one, c subtracts one, to goal x-coordinate\n"); printf("\n"); // set up display window // glutInitDisplayMode(GLUT_DEPTH | GLUT_RGBA | GLUT_DOUBLE | GLUT_MULTISAMPLE); glutInitDisplayMode(GLUT_DEPTH | GLUT_RGBA | GLUT_DOUBLE); glutInitWindowPosition(40, 40); glutInitWindowSize(sz, sz); glutCreateWindow("Numeric Inverse Kinematics"); // setup callbacks glutDisplayFunc(display); glutKeyboardFunc(parse_key); glutSpecialFunc(parse_special_key); glutReshapeFunc(myReshape); glutIdleFunc(Animate); initRenderingEnvironment(); initialize_numericinvkin(goalx,goaly,goalz,initAngle0,initAngle1,initAngle2); glutMainLoop(); return 0; } // ================================================================================= // DISPLAY // ================================================================================= void display(void) { // clear buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // display particles glPushMatrix(); display_numericinvkin(); glPopMatrix(); // force out and swap buffers glFlush(); glutSwapBuffers(); } // ================================================================================= // INIT RENDERING ENVIRONMENT // ================================================================================= void initRenderingEnvironment (void) { glShadeModel (GL_SMOOTH); glFrontFace(GL_CCW); glEnable(GL_DEPTH_TEST); setupLights(); glClearColor(0.8,0.8,0.8,1.0); glColor3f(1.0, 1.0, 1.0); glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); } // ================================================================================= // PARSE KEY // ================================================================================= void parse_key(unsigned char key, int x, int y) { switch (key) { case 27: exit(0); case 13: angle = 0; break; case 105: eyeDistance /= 2.0; break; // i: zoom In case 111: eyeDistance *= 2.0; break; // o: zoom Out case 97: goalx++; set_goal(goalx,goaly,goalz);break; // a: move right case 122: goalx--; set_goal(goalx,goaly,goalz);break; // z: move left case 115: goaly++; set_goal(goalx,goaly,goalz);break; // s: move up case 120: goaly--; set_goal(goalx,goaly,goalz);break; // x: move down case 100: goalz++; set_goal(goalx,goaly,goalz);break; // d: move in case 99: goalz--; set_goal(goalx,goaly,goalz);break; // c: move out case 32: set_goal(goalx,goaly,goalz);break; // sp: don't move, just iterate default: printf("%d",key); } } // ================================================================================= // PARSE SPECIAL KEY // ================================================================================= void parse_special_key(int key, int x, int y) { switch (key) { case GLUT_KEY_UP: eyeTilt += 0.25; break; case GLUT_KEY_DOWN: eyeTilt -= 0.25; break; case GLUT_KEY_RIGHT: eyeAround += 0.25; break; case GLUT_KEY_LEFT: eyeAround -= 0.25; break; } } // ================================================================================= // SET CAMERA // ================================================================================= void SetCamera(void) { glMatrixMode(GL_PROJECTION); glLoadIdentity(); glFrustum (-0.1, 0.1, -0.1, 0.1, 0.2, 550.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); eyez = eyeDistance*cos(eyeAround)*cos(eyeTilt); eyey = eyeDistance*sin(eyeTilt); eyex = eyeDistance*sin(eyeAround)*cos(eyeTilt); gluLookAt(eyex,eyey,eyez,coix,coiy,coiz,0.0,1.0,0.0); } // ================================================================================= // ANIMATE // ================================================================================= void Animate(void) { SetCamera(); glutPostRedisplay(); frame++; } // ================================================================================= // MY RESHAPE // ================================================================================= void myReshape(int w, int h) { SetCamera(); glViewport (0, 0, w, h); } /* --------------------------------------------------------------------------- */ /* SETUP LIGHTS */ /* --------------------------------------------------------------------------- */ void setupLights() { GLfloat light0_ambient[] = { 0.1,0.1,0.1,1.0 }; GLfloat light0_diffuse[] = { 0.3,0.3,0.3,1.0 }; GLfloat light0_specular[] = { 0.1,0.1,0.1,1.0 }; GLfloat light0_position[] = { 0.0, 1.0, 3.0, 1.0 }; GLfloat light1_ambient[] = { 0.1,0.1,0.1,1.0 }; GLfloat light1_diffuse[] = { 0.3,0.3,0.3,1.0 }; GLfloat light1_specular[] = { 0.3,0.3,0.3,1.0 }; GLfloat light1_position[] = { -1.0,3.0,2.0,1.0 }; glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glLightfv(GL_LIGHT0, GL_AMBIENT, light0_ambient); glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse); glLightfv(GL_LIGHT0, GL_SPECULAR,light0_specular); glLightfv(GL_LIGHT0, GL_POSITION,light0_position); glEnable(GL_LIGHT1); glLightfv(GL_LIGHT1, GL_AMBIENT, light1_ambient); glLightfv(GL_LIGHT1, GL_DIFFUSE, light1_diffuse); glLightfv(GL_LIGHT1, GL_SPECULAR,light1_specular); glLightfv(GL_LIGHT1, GL_POSITION,light1_position); }