/* Project: coevolutionary GP moto driving Dana Vrajitoru moto_geometry.cc The geometrical implementation of the motorcycle containing the necessary OpenGL commands. */ #include #include #include #include "moto_geometry.h" #include "general.h" // Default constructor. Sets the id numbers and the default // colors. Does not create the display lists to be sure that we don't // want to set the colors to different values and draw the motorcycle // only afterwards. It should not be invoked before the gl is // initialized because it will create a segmentation fault. Moto_geometry::Moto_geometry() { set_ids(); set_precision(); set_saddle_rgb(); set_engine_rgb(); set_tire_rgb(); set_headlight_rgb(); set_handlebar_pos(); set_left_wheel_pos(); } // Set the ids of the display lists in case we have more than one // motorcycle. void Moto_geometry::set_ids() { fix_parts_id = glGenLists(1); wheel_id = glGenLists(1); handlebar_id = glGenLists(1); } // Set the precision of the rendering. 1 would be wireframe type, 2 // surfaced with raw polygons, 3 surfaced with lots of polygons. void Moto_geometry::set_precision(int value) { precision = value; } // Setting each of the colors and default values for each of them. void Moto_geometry::set_saddle_rgb(GLfloat r, GLfloat g, GLfloat b) { saddle_r = r; saddle_g = g; saddle_b = b; } void Moto_geometry::set_engine_rgb(GLfloat r, GLfloat g, GLfloat b) { engine_r = r; engine_g = g; engine_b = b; } void Moto_geometry::set_tire_rgb(GLfloat r, GLfloat g, GLfloat b) { tire_r = r; tire_g = g; tire_b = b; } void Moto_geometry::set_headlight_rgb(GLfloat r, GLfloat g, GLfloat b) { headlight_r = r; headlight_g = g; headlight_b = b; } // Set the positions. void Moto_geometry::set_handlebar_pos(GLfloat x, GLfloat y, GLfloat z) { set_point3f(handlebar_pos, x, y, z); } void Moto_geometry::set_left_wheel_pos(GLfloat x, GLfloat y, GLfloat z) { set_point3f(left_wheel_pos, x, y, z); } // Define some dimensions float Moto_geometry::length() { return 2*(fabs(left_wheel_pos[0]) + fabs(left_wheel_pos[1])); } float Moto_geometry::width() { return 0.567*length(); } float Moto_geometry::height() { return 0.8*length(); } // Print some of these values void Moto_geometry::cout_headlight_rgb() { cout << headlight_r << ' ' << headlight_g << ' ' << headlight_b << endl; } // Destructor. Delete the display lists hoping that it will restore // the memory. Moto_geometry::~Moto_geometry() { glDeleteLists(fix_parts_id, 1); glDeleteLists(wheel_id, 1); glDeleteLists(handlebar_id, 1); } // Creates the display lists containing the OpenGL commands for the // independent parts of the motorcycle. void Moto_geometry::draw() { // Set the polygon mode to wireframe or surface depending on the // precision. glNewList(fix_parts_id, GL_COMPILE); if (precision == 1) glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); else glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); saddle(); engine(); glEndList(); glNewList(wheel_id, GL_COMPILE); wheel(); glEndList(); glNewList(handlebar_id, GL_COMPILE); handlebar(); glEndList(); } // Calls all of the display lists. void Moto_geometry::display() { display_fix_parts(); //left_wheel glPushMatrix(); position_left_wheel(); display_wheel(); glPopMatrix(); //right_wheel glPushMatrix(); position_handlebar(); display_wheel(); display_handlebar(); glPopMatrix(); } // We may need to diplay individual components. These functions should // only call the display lists. void Moto_geometry::display_wheel() { glCallList(wheel_id); } void Moto_geometry::display_handlebar() { glCallList(handlebar_id); } void Moto_geometry::display_fix_parts() { glCallList(fix_parts_id); } // Position the handlebar and wheel so that we don't have to call the // gl command explicitly every time. void Moto_geometry::position_handlebar() { glTranslatef(handlebar_pos[0], handlebar_pos[1], handlebar_pos[2]); } void Moto_geometry::position_left_wheel() { glTranslatef(left_wheel_pos[0], left_wheel_pos[1], left_wheel_pos[2]); } // The OpenGL commands for the saddle. Not intended to be used by // itself. void Moto_geometry::saddle() { const int hor_dim=7, ver_dim=6; // Basic coordinates Point3f top_coord[hor_dim] = { {-2.7, 3.95, 0.15}, {-1.15, 3.15, 0.2}, {-0.6, 3, 0.25}, {-0.25, 3, 0.25}, {0.3, 3.15, 0.2}, {0.65, 3.2, 0.15}, {0.75, 3.2, 0.15}}; Point3f middle_coord[hor_dim] = { {-2.7, 3.90, 0.15}, {-1.15, 2.9, 0.3}, {-0.8, 2.7, 0.38}, {-0.25, 2.6, 0.45}, {0.3, 2.6, 0.45}, {0.65, 2.8, 0.55}, {0.75, 2.85, 0.62}}; Point3f lateral_coord[hor_dim] = { {-1.15, 2.6, 0.32}, {-1, 2.3, 0.5}, {-0.8, 2.7, 0.38}, {0, 2.25, 0.5}, {0.45, 2.45, 0.55}, {0.9, 2, 0.6}, {1.1, 2.6, 0.7}}; // Build the 6 lists of vectors for the saddle. The are supposed to // make a triangle grid together. Point3f saddle_coord[ver_dim][hor_dim]; Point3f saddle_normals[ver_dim][hor_dim]; int i, j, k; for (i=0; i