#include "Shape.h" void Shape::draw_cube() { static const float pt[8][3] = { {-1, -1, -1}, { 1, -1, -1}, { 1, -1, 1}, {-1, -1, 1}, {-1, 1, -1}, { 1, 1, -1}, { 1, 1, 1}, {-1, 1, 1} }; static const int f[6][4] = { {0, 1, 2, 3}, {5, 4, 7, 6}, {1, 5, 6, 2}, {0, 4, 7, 3}, {3, 2, 6, 7}, {0, 4, 5, 1} }; static const float n[6][3] = { { 0, -1, 0}, { 0, 1, 0}, { 1, 0, 0}, {-1, 0, 0}, { 0, 0, 1}, { 0, 0, -1} }; static const float uv[4][2] = { {0, 0}, {0, 1}, {1, 1}, {1, 0} }; glBegin(GL_QUADS); for (int i = 0; i < 6; i++) { glNormal3f(n[i][0], n[i][1], n[i][2]); for (int j = 0; j < 4; j++) { glTexCoord2f(uv[j][0], uv[j][1]); glVertex3f(pt[f[i][j]][0], pt[f[i][j]][1], pt[f[i][j]][2]); } } glEnd(); } void Shape::draw_spiral() { const float nbSteps = 200.0; //glColor3f(1.0f, 1.0f, 1.0f); glBegin(GL_QUAD_STRIP); for (float i = 0; i < nbSteps; ++i) { float ratio = i / nbSteps; float angle = 21.0 * ratio; float c = cos(angle); float s = sin(angle); float r1 = 1.0 - 0.8 * ratio; float r2 = 0.8 - 0.8 * ratio; float alt = ratio - 0.5; const float nor = .5; const float up = sqrt(1.0 - nor * nor); //glColor3f(1.0-ratio, 0.2 , ratio); glNormal3f(nor * c, up, nor * s); glVertex3f(r1 * c, alt, r1 * s); glVertex3f(r2 * c, alt + 0.05, r2 * s); } glEnd(); } void Shape::draw_cone() { const int nbSteps = 20; const float theta = 2 * M_PI / nbSteps; float pt[nbSteps+1][2]; /* Calcul préliminaire */ for(int i = 0; i <= nbSteps; ++i) { pt[i][0] = cos(i * theta); pt[i][1] = sin(i * theta); } /* les bases */ glBegin(GL_TRIANGLE_FAN); glNormal3f( 0, 1, 0); glVertex3f( 0, 1, 0); for(int i = 0; i <= nbSteps; ++i) { glNormal3f(pt[i][0], 2/sqrt(5), pt[i][1]); glVertex3f(pt[i][0], -1, pt[i][1]); } glEnd(); glBegin(GL_TRIANGLE_FAN); glNormal3f( 0, -1, 0); glVertex3f( 0, -1, 0); for(int i = 0; i <= nbSteps; ++i) { glNormal3f( 0, -1, 0); glVertex3f(pt[i][0], -1, pt[i][1]); } glEnd(); } void Shape::draw_cylinder() { const int nbSteps = 20; const float theta = 2 * M_PI / nbSteps; float pt[nbSteps+1][2]; /* Calcul préliminaire */ for(int i = 0; i <= nbSteps; ++i) { pt[i][0] = cos(i * theta); pt[i][1] = sin(i * theta); } /* Le fût */ glBegin(GL_QUAD_STRIP); for(int i = 0; i <= nbSteps; ++i) { glNormal3f(pt[i][0], 0, pt[i][1]); glVertex3f(pt[i][0], 1, pt[i][1]); glNormal3f(pt[i][0], 0, pt[i][1]); glVertex3f(pt[i][0], -1, pt[i][1]); } glEnd(); /* les bases */ glBegin(GL_TRIANGLE_FAN); glNormal3f( 0, 1, 0); glVertex3f( 0, 1, 0); for(int i = 0; i <= nbSteps; ++i) { glNormal3f( 0, 1, 0); glVertex3f(pt[i][0], 1, pt[i][1]); } glEnd(); glBegin(GL_TRIANGLE_FAN); glNormal3f( 0, -1, 0); glVertex3f( 0, -1, 0); for(int i = 0; i <= nbSteps; ++i) { glNormal3f( 0, -1, 0); glVertex3f(pt[i][0], -1, pt[i][1]); } glEnd(); } void Shape::draw_sphere() { int nbStepsa = 30; int nbStepsb = 30; const float da = ( 2 * M_PI / nbStepsa ); const float db = ( M_PI / nbStepsb ); float cost[nbStepsa]; float sint[nbStepsa]; float cosp[nbStepsb]; float sinp[nbStepsb]; /* Précalcul */ for( int i = 0; i <= nbStepsa ; ++i ) { cost[i] = cos( i * da ); sint[i] = sin( i * da ); } for( int j = 0; j <= nbStepsb ; ++j ) { cosp[j] = cos( j * db + M_PI / 2 ); sinp[j] = sin( j * db + M_PI / 2 ); } /* On fait les sommets avec des GL_TRIANGLE_FAN, c'est plus logique */ glBegin(GL_TRIANGLE_FAN); glNormal3f(0, 0, 1); glVertex3f(0, 0, 1); for( int i = 0; i <= nbStepsa ; ++i ) { float x = sint[i] * cosp[1]; float y = cost[i] * cosp[1]; float z = sinp[1]; glNormal3f(x, y, z); glVertex3f(x, y, z); } glEnd(); glBegin(GL_TRIANGLE_FAN); glNormal3f(0, 0, -1); glVertex3f(0, 0, -1); for( int i = 0; i <= nbStepsa ; ++i ) { float x = sint[i] * cosp[nbStepsb-1]; float y = cost[i] * cosp[nbStepsb-1]; float z = sinp[nbStepsb-1]; glNormal3f(x, y, z); glVertex3f(x, y, z); } glEnd(); /* Le reste de la sphère */ for( int i = 0; i < nbStepsa ; ++i ) { glBegin(GL_QUAD_STRIP); for( int j = 1; j < nbStepsb ; ++j ) { float x0 = sint[i] * cosp[j]; float y0 = cost[i] * cosp[j]; float z0 = sinp[j]; float x1 = sint[i+1] * cosp[j]; float y1 = cost[i+1] * cosp[j]; float z1 = sinp[j]; glNormal3f(x0, y0, z0); glVertex3f(x0, y0, z0); glNormal3f(x1, y1, z1); glVertex3f(x1, y1, z1); } glEnd(); } } void Shape::draw_tore() { int nbStepsa = 30; int nbStepsb = 30; const float da = 2.0f * M_PI / nbStepsa; const float db = 2.0f * M_PI / nbStepsb; const float ra = 2; const float rb = 1; float cost[nbStepsa]; float sint[nbStepsa]; float cosp[nbStepsb]; float sinp[nbStepsb]; /* Précalcul */ for( int i = 0; i <= nbStepsa ; ++i ) { cost[i] = cos( i * da ); sint[i] = sin( i * da ); } for( int j = 0; j <= nbStepsb ; ++j ) { cosp[j] = cos( j * db ); sinp[j] = sin( j * db ); } for(int i = 0; i < nbStepsa; ++i) { glBegin(GL_QUAD_STRIP); for(int j = 0; j <= nbStepsb; ++j) { float x0 = sint[i] * (ra+rb*cosp[j]); float y0 = cost[i] * (ra+rb*cosp[j]); float z0 = rb * sinp[j]; float x1 = sint[i+1] * (ra+rb*cosp[j]); float y1 = cost[i+1] * (ra+rb*cosp[j]); float z1 = rb * sinp[j]; glNormal3f(cosp[j]*cost[i], cosp[j]*sint[i], sinp[j]); glVertex3f(x0, y0, z0); glNormal3f(cosp[j]*cost[i+1], cosp[j]*sint[i+1], sinp[j]); glVertex3f(x1, y1, z1); } glEnd(); } } void Shape::draw_gear() { int nbStepsg = 50; int nbTeeth = 10; float in_r = 1; float out_r = 2; float teeth_r = 0.5; const float dg = 2.0f * M_PI / nbStepsg; const float dt = 2.0f * M_PI / (nbTeeth * 2); float cosg[nbStepsg+1]; float sing[nbStepsg+1]; float cost[nbTeeth * 2 + 1]; float sint[nbTeeth * 2 + 1]; /* Précalcul */ for(int i = 0; i <= nbStepsg; ++i) { cosg[i] = cos( i * dg ); sing[i] = sin( i * dg ); } for(int i = 0; i <= nbTeeth * 2; ++i) { cost[i] = cos( i * dt ); sint[i] = sin( i * dt ); } /* Les deux faces */ glBegin(GL_QUAD_STRIP); for(int i = 0; i <= nbStepsg; ++i) { glNormal3f(0, 0, 1); glVertex3f(in_r * cosg[i], in_r * sing[i], 1); glVertex3f(out_r * cosg[i], out_r * sing[i], 1); } glEnd(); glBegin(GL_QUAD_STRIP); for(int i = 0; i <= nbStepsg; ++i) { glNormal3f(0, 0, -1); glVertex3f(in_r * cosg[i], in_r * sing[i], -1); glVertex3f(out_r * cosg[i], out_r * sing[i], -1); } glEnd(); /* on ferme le tout */ glBegin(GL_QUAD_STRIP); for(int i = 0; i <= nbStepsg; ++i) { glNormal3f(in_r * cosg[i], in_r * sing[i], 1); glVertex3f(in_r * cosg[i], in_r * sing[i], 1); glNormal3f(in_r * cosg[i], in_r * sing[i], -1); glVertex3f(in_r * cosg[i], in_r * sing[i], -1); } glEnd(); glBegin(GL_QUAD_STRIP); for(int i = 0; i <= nbStepsg; ++i) { glNormal3f(out_r * cosg[i], out_r * sing[i], 1); glVertex3f(out_r * cosg[i], out_r * sing[i], 1); glNormal3f(out_r * cosg[i], out_r * sing[i], -1); glVertex3f(out_r * cosg[i], out_r * sing[i], -1); } glEnd(); /* les dents */ for(int j = 0; j < nbTeeth; j++) { int i = j * 2; float u, v; glBegin(GL_QUAD_STRIP); glVertex3f(out_r * cost[i], out_r * sint[i], 1); glVertex3f(out_r * cost[i], out_r * sint[i], -1); u = out_r * cost[i] - (out_r + teeth_r) * cost[i]; v = out_r * sint[i] - (out_r + teeth_r) * sint[i]; glNormal3f(-u,-v,0); glVertex3f((teeth_r + out_r) * cost[i], (teeth_r + out_r) * sint[i], 1); glVertex3f((teeth_r + out_r) * cost[i], (teeth_r + out_r) * sint[i], -1); glNormal3f(cost[i],sint[i],0); glVertex3f((teeth_r + out_r) * cost[i+1], (teeth_r + out_r) * sint[i+1], 1); glVertex3f((teeth_r + out_r) * cost[i+1], (teeth_r + out_r) * sint[i+1], -1); u = out_r * cost[i+1] - (out_r + teeth_r) * cost[i+1]; v = out_r * sint[i+1] - (out_r + teeth_r) * sint[i+1]; glNormal3f(-u,-v,0); glVertex3f(out_r * cost[i+1], out_r * sint[i+1], 1); glVertex3f(out_r * cost[i+1], out_r * sint[i+1], -1); glEnd(); glBegin(GL_QUADS); glNormal3f(0,0,1); glVertex3f(out_r * cost[i], out_r * sint[i], 1); glVertex3f((teeth_r + out_r) * cost[i], (teeth_r + out_r) * sint[i], 1); glVertex3f((teeth_r + out_r) * cost[i+1], (teeth_r + out_r) * sint[i+1], 1); glVertex3f(out_r * cost[i+1], out_r * sint[i+1], 1); glNormal3f(0,0,-1); glVertex3f(out_r * cost[i], out_r * sint[i], -1); glVertex3f((teeth_r + out_r) * cost[i], (teeth_r + out_r) * sint[i], -1); glVertex3f((teeth_r + out_r) * cost[i+1], (teeth_r + out_r) * sint[i+1], -1); glVertex3f(out_r * cost[i+1], out_r * sint[i+1], -1); glEnd(); } }