/* Project: coevolutionary GP moto driving Dana Vrajitoru gl_interface.cc Some functions to manage the interaction with the OpenGL window. */ #include #include #include #include "gl_interface.h" #include "moto_geometry.h" #include "scenery.h" #include "moto_animate.h" #include "driver_behave.h" Moto_animate *moto = NULL; Driver_behave *driver = NULL; static GLfloat theta[] = {0.0,0.0,0.0}; static GLint axis = 2; static int direction = 0; static GLfloat trans[] = {0.0,0.0,1}; // Main OpenGL function that is called whenever an event occurs. void display() { /* display callback, clear frame buffer and z buffer, rotate object and draw, swap buffers */ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glClearColor(0.2, 0.2, 0.2, 0.2); glColor3f(1.0, 0.0, 0.0); gluLookAt(0, 0, 3.5, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); glTranslatef(trans[0], trans[1], 0); glScalef(trans[2], trans[2], trans[2]); glRotatef(theta[0], 1.0, 0.0, 0.0); glRotatef(theta[1], 0.0, 1.0, 0.0); glRotatef(theta[2], 0.0, 0.0, 1.0); scenery_display(); moto->display(); driver->display(); glutSwapBuffers(); } // Moves all of the objects that are animated in the scene. void move_objects() { moto->move(); moto->handlebar_loop(); glutPostRedisplay(); } // Spins the entire scene by 0.2 degrees about selected axis. void spin_objects() { moto->move(); if (direction == 0) { theta[axis] += 0.2; if( theta[axis] > 360.0 ) theta[axis] -= 360.0; } else { theta[axis] -= 0.2; if( theta[axis] <= 0 ) theta[axis] += 360.0; } move_objects(); } // Mouse callback, selects an axis about which to rotate. When the // appropriate mouse button is pushed, it starts to spin the scene and // when the mouse button is released, the spinning stops. void mouse(int btn, int state, int x, int y) { if (state == GLUT_DOWN) glutIdleFunc(spin_objects); else glutIdleFunc(move_objects); switch (btn) { case GLUT_LEFT_BUTTON: axis = 0; break; case GLUT_MIDDLE_BUTTON: axis = 1; break; case GLUT_RIGHT_BUTTON: axis = 2; } int key_state = glutGetModifiers(); if (key_state == GLUT_ACTIVE_SHIFT) direction = 1; else direction = 0; } // Callback for the keyboard. The arrow keys move the entire scene on // x and y. The page up and page down keys move the scene on z. void key(int key, int x, int y) { switch (key) { case GLUT_KEY_LEFT: trans[0] -= 0.1; break; case GLUT_KEY_RIGHT: trans[0] += 0.1; break; case GLUT_KEY_UP: trans[1] += 0.1; break; case GLUT_KEY_DOWN: trans[1] -= 0.1; break; case GLUT_KEY_PAGE_UP: trans[2] += 0.05; break; case GLUT_KEY_PAGE_DOWN: trans[2] -= 0.05; break; case GLUT_KEY_HOME: test_driver(); } display(); } // To be called whenever the window is resized. void my_reshape(int w, int h) { glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(60.0, w/h, 0.5, 50.0); /* if (w<=h) glFrustum(-10.0, 10.0, -10.0 * (GLfloat) h/ (GLfloat) w, 10.0* (GLfloat) h / (GLfloat) w, -10.0, 20.0); else glFrustum(-10.0, 10.0, -10.0 * (GLfloat) w/ (GLfloat) h, 10.0* (GLfloat) w / (GLfloat) h, -10.0, 20.0); */ glMatrixMode(GL_MODELVIEW); } void light_init() { GLfloat mat_specular[]={1.0, 1.0, 1.0, 1.0}; GLfloat mat_diffuse[]={0.0, 0.0, 1.0, 1.0}; GLfloat mat_ambient[]={0.0, 0.5, 1.0, 1.0}; GLfloat mat_shininess={100.0}; GLfloat light_ambient[]={0.5, 0.5, 0.5, 1.0}; GLfloat light_diffuse[]={1.0, 1.0, 1.0, 1.0}; GLfloat light_specular[]={1.0, 1.0, 1.0, 1.0}; GLfloat light_position[] = {0.0, 5.0, -2.0, 1.0}; /* set up ambient, diffuse, and specular components for light 0 */ glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient); glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular); glLightfv(GL_LIGHT0, GL_POSITION, light_position); /* define material proerties for front face of all polygons */ //glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); //glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient); //glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse); //glMaterialf(GL_FRONT, GL_SHININESS, mat_shininess); glShadeModel(GL_SMOOTH); /*enable smooth shading */ glEnable(GL_LIGHTING); /* enable lighting */ glEnable(GL_LIGHT0); /* enable light 0 */ glEnable(GL_DEPTH_TEST); /* enable z buffer */ glEnable(GL_COLOR_MATERIAL); glColorMaterial(GL_FRONT,GL_AMBIENT_AND_DIFFUSE); glClearColor (1.0, 1.0, 1.0, 1.0); glColor3f (1, 0.0, 0.0); } // A simple test for the driver animation void test_driver() { driver->snapshot(); driver->test_angles(); driver->snapshot(1); driver->reset_angles(); driver->set_frames(1000); } // Initialize all of the objects to be drawn. void scene_init() { moto = new Moto_animate; moto->draw(); //cout << "after moto draw" << endl; scenery_draw(); //cout << "after scenery draw" << endl; moto->set_wheel_speed(-0.1); //moto->set_handlebar_speed(0.1); //moto->set_lateral_speed(0.1); driver = new Driver_behave; cout << "in scene_init" << endl; driver->draw(); driver->set_position(0, 0, 3); driver->move_on_moto(moto->length(), moto->width(), moto->height()); }