/* Project: coevolutionary GP moto driving Dana Vrajitoru gl_draw.h Some functions to help generating OpenGL commands. */ #include "gl_draw.h" #include #include #include // Draws a polygon with a given normal and any number of vertices. void gl_polygon(Point3f coords[], Point3f normal, int size) { glBegin(GL_POLYGON); for (int i=0; i 0.0001) { // Rotate about the cross product of normalized c and (0,0,1) // normalize c for(int l=0; l<3; l++) nc[l] = c[l]/c_norm; // Compute cross product. It is easy since one of the vectors // is (0,0,1) axis[0] = -nc[1]; axis[1] = nc[0]; axis[2] = 0; axis_norm = sqrt(axis[0]*axis[0] + axis[1]*axis[1] + axis[2]*axis[2]); if(axis_norm < 0.00001) angle = 0.0; else angle = (float)asin((float)axis_norm); glRotated((angle*360)/(2*M_PI),axis[0],axis[1],axis[2]); gluCylinder(Cone, radia[i], radia[i+1], c_norm, slices, stacks); } glPopMatrix(); } } // Draws a strip of cylinders. void gl_cylinder_strip(Point3f coords[], int size, float width, int precision) { static GLUquadricObj *Cylinder = NULL; int slices=0, stacks=0; if (!Cylinder) Cylinder = gluNewQuadric(); // Determine the precision of the rendering. switch (precision) { case 1: gluQuadricDrawStyle(Cylinder,(GLenum)GLU_LINE); slices = 10; stacks = 1; break; case 2: gluQuadricDrawStyle(Cylinder,(GLenum)GLU_FILL); slices = 10; stacks = 1; break; case 3: gluQuadricDrawStyle(Cylinder,(GLenum)GLU_FILL); slices = 20; stacks = 2; break; } // Enable the normals for a smooth lightning. gluQuadricNormals(Cylinder,(GLenum)GLU_SMOOTH); glEnable(GL_LIGHTING); //Draw each of the cylinders for (int i=0; i 0.0001) { // Rotate about the cross product of normalized c and (0,0,1) // normalize c for(int l=0; l<3; l++) nc[l] = c[l]/c_norm; // Compute cross product. It is easy since one of the vectors // is (0,0,1) axis[0] = -nc[1]; axis[1] = nc[0]; axis[2] = 0; axis_norm = sqrt(axis[0]*axis[0] + axis[1]*axis[1] + axis[2]*axis[2]); if(axis_norm < 0.00001) angle = 0.0; else angle = (float)asin((float)axis_norm); glRotated((angle*360)/(2*M_PI),axis[0],axis[1],axis[2]); gluCylinder(Cylinder, width, width, c_norm, slices, stacks); } glPopMatrix(); } } // Draws one sphere with specified coordinates and axes. void gl_sphere(GLfloat center_x, GLfloat center_y, GLfloat center_z, GLfloat radius, int precision) { static GLUquadricObj *Sphere = NULL; int slices=0, stacks=0; if (Sphere == NULL) Sphere = gluNewQuadric(); // Determine the precision of the rendering. switch (precision) { case 1: gluQuadricDrawStyle(Sphere,(GLenum)GLU_LINE); slices = 10; stacks = 10; break; case 2: gluQuadricDrawStyle(Sphere,(GLenum)GLU_FILL); slices = 10; stacks = 10; break; case 3: gluQuadricDrawStyle(Sphere,(GLenum)GLU_FILL); slices = 20; stacks = 20; break; } // Enable the normals for a smooth lightning. gluQuadricNormals(Sphere,(GLenum)GLU_SMOOTH); glEnable(GL_LIGHTING); glPushMatrix(); glTranslatef(center_x, center_y, center_z); gluSphere(Sphere, radius, slices, stacks); glPopMatrix(); } // Draws one ellipsoid with specified coordinates and axes. void gl_ellipsoid(GLfloat center_x, GLfloat center_y, GLfloat center_z, GLfloat axis_x, GLfloat axis_y, GLfloat axis_z, int precision) { static GLUquadricObj *Sphere = NULL; int slices=0, stacks=0; if (Sphere == NULL) Sphere = gluNewQuadric(); cout << "In ellipsoid" << endl; // Determine the precision of the rendering. switch (precision) { case 1: gluQuadricDrawStyle(Sphere,(GLenum)GLU_LINE); slices = 10; stacks = 10; break; case 2: gluQuadricDrawStyle(Sphere,(GLenum)GLU_FILL); slices = 10; stacks = 10; break; case 3: gluQuadricDrawStyle(Sphere,(GLenum)GLU_FILL); slices = 20; stacks = 20; break; } // Enable the normals for a smooth lightning. gluQuadricNormals(Sphere,(GLenum)GLU_SMOOTH); glEnable(GL_LIGHTING); glPushMatrix(); glTranslatef(center_x, center_y, center_z); glScalef(axis_x, axis_y, axis_z); gluSphere(Sphere, 1.0, slices, stacks); glPopMatrix(); } // Mirror the vectors over the z axis. void mirror_z(Point3f coord[], int size) { for (int i=0; i