////------------------------------------------------------- ////------------------------------------------------------- ////------------------------------------------------------- ////------------------------------------------------------- void Torus(double const majorRadius, double const minorRadius, double const resolution, double const angle) { double majorArcLength(fabs(angle)*(majorRadius+minorRadius)); double minorArcLength(TwoPi*minorRadius); int majorSegments(ceil(majorArcLength/resolution)); int minorSegments(ceil(minorArcLength/resolution)); return Torus(majorRadius, minorRadius, majorSegments, minorSegments, angle); }
static void render_image(void) { GLfloat light_ambient[] = { 0.0, 0.0, 0.0, 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[] = { 1.0, 1.0, 1.0, 0.0 }; GLfloat red_mat[] = { 1.0, 0.2, 0.2, 1.0 }; GLfloat green_mat[] = { 0.2, 1.0, 0.2, 1.0 }; GLfloat blue_mat[] = { 0.2, 0.2, 1.0, 1.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); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glEnable(GL_DEPTH_TEST); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-2.5, 2.5, -2.5, 2.5, -10.0, 10.0); glMatrixMode(GL_MODELVIEW); glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); glPushMatrix(); glRotatef(20.0, 1.0, 0.0, 0.0); glPushMatrix(); glTranslatef(-0.75, 0.5, 0.0); glRotatef(90.0, 1.0, 0.0, 0.0); glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, red_mat ); Torus(0.275, 0.85, 20, 20); glPopMatrix(); glPushMatrix(); glTranslatef(-0.75, -0.5, 0.0); glRotatef(270.0, 1.0, 0.0, 0.0); glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, green_mat ); Cone(1.0, 2.0, 16, 1); glPopMatrix(); glPushMatrix(); glTranslatef(0.75, 0.0, -1.0); glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, blue_mat ); Sphere(1.0, 20, 20); glPopMatrix(); glPopMatrix(); /* This is very important!!! * Make sure buffered commands are finished!!! */ glFinish(); }
void ShapesUI::draw() { // Sets up the viewport and background color setup3DDrawing( Color( 0,0,0 ), width, height, true ); // Changes the way triangles are drawn switch ( shapesUI->getDisplayType() ) { case DISPLAY_WIREFRAME: { glDisable(GL_LIGHTING); glPolygonMode(GL_FRONT, GL_LINE); glColor3f(1.0f, 1.0f, 1.0f); } break; case DISPLAY_FLAT_SHADING: { glEnable(GL_LIGHTING); glPolygonMode(GL_FRONT, GL_FILL); glColor3f(1.0f, 1.0f, 1.0f); glShadeModel(GL_FLAT); } break; case DISPLAY_SMOOTH_SHADING: { glEnable(GL_LIGHTING); glPolygonMode(GL_FRONT, GL_FILL); glColor3f(1.0f, 1.0f, 1.0f); glShadeModel(GL_SMOOTH); } break; default: break; } // Setup the camera gluLookAt( 3.5 * cos( shapesUI->getYRot() ) * cos( shapesUI->getXRot() ), 3.5 * sin( shapesUI->getYRot() ), 3.5 * cos( shapesUI->getYRot() ) * sin( shapesUI->getXRot() ), 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); // ToDo: draw your shape here // DO NOT put the actual draw OpenGL code here - put it in the shape class and call the draw method if (current != 0){ if(shapesUI->getShapeType() != SHAPE_TORUS) { current->draw(); } else Torus(5,5); } endDrawing(); }
/* * Draw scene * light>0 lit colors * light<0 shaded colors * light=0 shadow volumes */ void Scene(int Light) { int k; // Counters used to draw floor // Set global light switch (used in DrawPolyShadow) light = Light; // Texture (pi.bmp) glBindTexture(GL_TEXTURE_2D,tex2d[2]); // Enable textures if not drawing shadow volumes if (light) glEnable(GL_TEXTURE_2D); // Draw objects x y z th,ph dims if (obj&0x01) Cube(-0.8,+0.8,0.0 , -0.25*zh, 0 , 0.3 ); if (obj&0x02) Cylinder(+0.8,+0.5,0.0 , 0.5*zh,zh , 0.2,0.5); if (obj&0x04) Torus(+0.5,-0.8,0.0 , 0,zh , 0.5,0.2); if (obj&0x08) Teapot(-0.5,-0.5,0.0 , 2*zh, 0 , 0.25 ); // Disable textures if (light) glDisable(GL_TEXTURE_2D); // The floor, ceiling and walls don't cast a shadow, so bail here if (!light) return; // Always enable textures glEnable(GL_TEXTURE_2D); Color(1,1,1); // Water texture for floor and ceiling glBindTexture(GL_TEXTURE_2D,tex2d[0]); for (k=-1;k<=box;k+=2) Wall(0,0,0, 0,90*k , 8,8,box?6:2 , 4); // Crate texture for walls glBindTexture(GL_TEXTURE_2D,tex2d[1]); for (k=0;k<4*box;k++) Wall(0,0,0, 90*k,0 , 8,box?6:2,8 , 1); // Disable textures glDisable(GL_TEXTURE_2D); }