void GLFunctions::disk(float _radius, int _slices ) { /* Pre-computed circle */ double *sint,*cost; fghCircleTable(&sint,&cost,-_slices); // as were using a triangle fan its vert at the center then // the points glBegin(GL_TRIANGLE_FAN); // as we are doing a tri fan this is the center Vec4 normal(0.0f,0.0f,1.0f); Vec4 vertex(0.0f,0.0f,0.0f); normal.normalGL(); vertex.vertexGL(); for (int j=0; j<=_slices; ++j) { // normals set above vertex.set(cost[j]*_radius,sint[j]*_radius,0.0f); vertex.vertexGL(); } glEnd(); /* Release sin and cos tables */ delete [] sint; delete [] cost; }
/* * Draws a solid cylinder */ void FGAPIENTRY glutSolidCylinder(GLdouble radius, GLdouble height, GLint slices, GLint stacks) { int i,j; /* Step in z and radius as stacks are drawn. */ double z0,z1; const double zStep = height / ( ( stacks > 0 ) ? stacks : 1 ); /* Pre-computed circle */ double *sint,*cost; FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSolidCylinder" ); fghCircleTable(&sint,&cost,-slices); /* Cover the base and top */ glBegin(GL_TRIANGLE_FAN); glNormal3d(0.0, 0.0, -1.0 ); glVertex3d(0.0, 0.0, 0.0 ); for (j=0; j<=slices; j++) glVertex3d(cost[j]*radius, sint[j]*radius, 0.0); glEnd(); glBegin(GL_TRIANGLE_FAN); glNormal3d(0.0, 0.0, 1.0 ); glVertex3d(0.0, 0.0, height); for (j=slices; j>=0; j--) glVertex3d(cost[j]*radius, sint[j]*radius, height); glEnd(); /* Do the stacks */ z0 = 0.0; z1 = zStep; for (i=1; i<=stacks; i++) { if (i==stacks) z1 = height; glBegin(GL_QUAD_STRIP); for (j=0; j<=slices; j++ ) { glNormal3d(cost[j], sint[j], 0.0 ); glVertex3d(cost[j]*radius, sint[j]*radius, z0 ); glVertex3d(cost[j]*radius, sint[j]*radius, z1 ); } glEnd(); z0 = z1; z1 += zStep; } /* Release sin and cos tables */ free(sint); free(cost); }
/* * Draws a wire cylinder */ void FGAPIENTRY glutWireCylinder(GLdouble radius, GLdouble height, GLint slices, GLint stacks) { int i,j; /* Step in z and radius as stacks are drawn. */ double z = 0.0; const double zStep = height / ( ( stacks > 0 ) ? stacks : 1 ); /* Pre-computed circle */ double *sint,*cost; FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutWireCylinder" ); fghCircleTable(&sint,&cost,-slices); /* Draw the stacks... */ for (i=0; i<=stacks; i++) { if (i==stacks) z = height; glBegin(GL_LINE_LOOP); for( j=0; j<slices; j++ ) { glNormal3d(cost[j], sint[j], 0.0); glVertex3d(cost[j]*radius, sint[j]*radius, z ); } glEnd(); z += zStep; } /* Draw the slices */ glBegin(GL_LINES); for (j=0; j<slices; j++) { glNormal3d(cost[j], sint[j], 0.0 ); glVertex3d(cost[j]*radius, sint[j]*radius, 0.0 ); glVertex3d(cost[j]*radius, sint[j]*radius, height); } glEnd(); /* Release sin and cos tables */ free(sint); free(cost); }
void GLFunctions::cone(float _base,float _height, int _slices, int _stacks ) { /* Step in z and radius as stacks are drawn. */ double z0,z1; double r0,r1; const double zStep = _height / ( ( _stacks > 0 ) ? _stacks : 1 ); const double rStep = _base / ( ( _stacks > 0 ) ? _stacks : 1 ); /* Pre-computed circle */ glBegin(GL_TRIANGLE_STRIP); double *sint,*cost; fghCircleTable(&sint,&cost,-_slices); z0 = 0.0; z1 = zStep; r0 = _base; r1 = r0 - rStep; float phi = (float)atan(_base/_height); float sphi= (float)sin(phi); Vec4 vertex; Vec4 normal; for(int i=0; i<_stacks; i++ ) { for(int j=0; j<=_slices; j++) { float theta = j == _slices ? 0.f : (float) j / _slices * TWO_PI; float ctheta = (float)cos(theta); float stheta = (float)sin(theta); vertex.set(cost[j]*r0,sint[j]*r0,z0); normal.set(ctheta,-stheta,sphi); normal.normalGL(); vertex.vertexGL(); // nornal the same so just set vertex vertex.set(cost[j]*r1,sint[j]*r1,z1); vertex.vertexGL(); } z0 = z1; z1 += zStep; r0 = r1; r1 -= rStep; } glEnd(); // Release sin and cos tables delete [] sint; delete [] cost; }
void drawSolidCylinder(GLdouble innter_radius, GLdouble outer_radius, GLdouble height, GLint slices, GLint stacks) { int j; /* Pre-computed circle */ double *sint, *cost; fghCircleTable(&sint, &cost, -slices); /* Do the stacks */ glCullFace(GL_FRONT); _draw(sint, cost, innter_radius, height, slices, stacks); glCullFace(GL_BACK); _draw(sint, cost, outer_radius, height, slices, stacks); /* Cover the base and top */ glBegin(GL_QUAD_STRIP); glNormal3d(0.0, 0.0, -1.0); for (j = 0; j <= slices; j++) { glVertex3d(cost[j] * innter_radius, sint[j] * innter_radius, -height * 0.5); glVertex3d(cost[j] * outer_radius, sint[j] * outer_radius, -height * 0.5); } glEnd(); glBegin(GL_QUAD_STRIP); glNormal3d(0.0, 0.0, 1.0); for (j = slices; j >= 0; j--) { glVertex3d(cost[j] * innter_radius, sint[j] * innter_radius, height * 0.5); glVertex3d(cost[j] * outer_radius, sint[j] * outer_radius, height * 0.5); } glEnd(); /* Release sin and cos tables */ free(sint); free(cost); }
void makeSolidCone( shapes::Mesh &mesh, double base, double height, int slices, int stacks ) { int i,j; /* Step in z and radius as stacks are drawn. */ double z0, z1; double r0, r1; const double zStep = height / ( ( stacks > 0 ) ? stacks : 1 ); const double rStep = base / ( ( stacks > 0 ) ? stacks : 1 ); /* Scaling factors for vertex normals */ const double cosn = ( height / sqrt ( height * height + base * base )); const double sinn = ( base / sqrt ( height * height + base * base )); /* Pre-computed circle */ double *sint,*cost; fghCircleTable(&sint,&cost,-slices); /* Cover the circular base with a triangle fan... */ z0 = -height ; r0 = base; r1 = r0 - rStep; int maxVert = 6*(slices * stacks + 10) ; mesh.vertexCount = 0 ; mesh.vertices = new double [maxVert] ; mesh.triangles = new unsigned int [maxVert] ; mesh.normals = new double [maxVert] ; int vc = 0, nc = 0, tc = 0 ; // make bottom faces mesh.vertices[vc++] = 0.0 ; mesh.vertices[vc++] = 0.0 ; mesh.vertices[vc++] = z0 ; mesh.vertexCount ++ ; for (j=0; j<slices; j++ ) { mesh.vertices[vc++] = cost[j]*r0 ; mesh.vertices[vc++] = sint[j]*r0 ; mesh.vertices[vc++] = z0 ; mesh.vertexCount ++ ; } #define CYCLE(a) (((a)==slices+1) ? 1 : (a)) for (j=0; j<slices; j++ ) { mesh.triangles[tc++] = j+1 ; mesh.triangles[tc++] = 0 ; mesh.triangles[tc++] = CYCLE(j+2) ; mesh.normals[nc++] = 0.0 ; mesh.normals[nc++] = 0.0 ; mesh.normals[nc++] = 1 ; mesh.triangleCount ++ ; } /* Cover each stack with a quad strip, except the top stack */ r1 = r0 ; z1 = z0 ; for( i=1; i<stacks-1; i++ ) { r1 -= rStep ; z1 += zStep ; for(j=0; j<slices; j++) { mesh.vertices[vc++] = cost[j]*r1 ; mesh.vertices[vc++] = sint[j]*r1 ; mesh.vertices[vc++] = z1 ; mesh.vertexCount ++ ; } for(j=0; j<slices; j++) { mesh.triangles[tc++] = (i -1)*(slices) + j + 1 ; mesh.triangles[tc++] = (i -1)*(slices) + CYCLE(j+2); mesh.triangles[tc++] = i * (slices) + CYCLE(j+2) ; mesh.triangleCount ++ ; mesh.normals[nc++] = cost[j]*cosn ; mesh.normals[nc++] = sint[j]*cosn ; mesh.normals[nc++] = sinn ; mesh.triangles[tc++] = (i -1)*(slices) + j + 1 ; mesh.triangles[tc++] = i * (slices) + CYCLE(j+2) ; mesh.triangles[tc++] = i * (slices) + j + 1 ; mesh.triangleCount ++ ; mesh.normals[nc++] = cost[j]*cosn ; mesh.normals[nc++] = sint[j]*cosn ; mesh.normals[nc++] = sinn ; } } mesh.vertices[vc++] = 0 ; mesh.vertices[vc++] = 0 ; mesh.vertices[vc++] = 0 ; mesh.vertexCount ++ ; for(j=0; j<slices; j++) { mesh.triangles[tc++] = mesh.vertexCount -1 - slices + j + 1 ; mesh.triangles[tc++] = mesh.vertexCount -1 - slices + CYCLE(j+2) ; mesh.triangles[tc++] = mesh.vertexCount -1 ; mesh.normals[nc++] = cost[j]*cosn ; mesh.normals[nc++] = sint[j]*cosn ; mesh.normals[nc++] = sinn ; mesh.triangleCount ++ ; } /* Release sin and cos tables */ free(sint); free(cost); }
/* * Draws a wire cone */ void FGAPIENTRY glutWireCone( GLdouble base, GLdouble height, GLint slices, GLint stacks) { int i,j; /* Step in z and radius as stacks are drawn. */ double z = 0.0; double r = base; const double zStep = height / ( ( stacks > 0 ) ? stacks : 1 ); const double rStep = base / ( ( stacks > 0 ) ? stacks : 1 ); /* Scaling factors for vertex normals */ const double cosn = ( height / sqrt ( height * height + base * base )); const double sinn = ( base / sqrt ( height * height + base * base )); /* Pre-computed circle */ double *sint,*cost; FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutWireCone" ); fghCircleTable(&sint,&cost,-slices); /* Draw the stacks... */ for (i=0; i<stacks; i++) { glBegin(GL_LINE_LOOP); for( j=0; j<slices; j++ ) { glNormal3d(cost[j]*sinn, sint[j]*sinn, cosn); glVertex3d(cost[j]*r, sint[j]*r, z ); } glEnd(); z += zStep; r -= rStep; } /* Draw the slices */ r = base; glBegin(GL_LINES); for (j=0; j<slices; j++) { glNormal3d(cost[j]*sinn, sint[j]*sinn, cosn ); glVertex3d(cost[j]*r, sint[j]*r, 0.0 ); glVertex3d(0.0, 0.0, height); } glEnd(); /* Release sin and cos tables */ free(sint); free(cost); }
/* * Draws a solid cone */ void FGAPIENTRY glutSolidCone( GLdouble base, GLdouble height, GLint slices, GLint stacks ) { int i,j; /* Step in z and radius as stacks are drawn. */ double z0,z1; double r0,r1; const double zStep = height / ( ( stacks > 0 ) ? stacks : 1 ); const double rStep = base / ( ( stacks > 0 ) ? stacks : 1 ); /* Scaling factors for vertex normals */ const double cosn = ( height / sqrt ( height * height + base * base )); const double sinn = ( base / sqrt ( height * height + base * base )); /* Pre-computed circle */ double *sint,*cost; FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSolidCone" ); fghCircleTable(&sint,&cost,-slices); /* Cover the circular base with a triangle fan... */ z0 = 0.0; z1 = zStep; r0 = base; r1 = r0 - rStep; glBegin(GL_TRIANGLE_FAN); glNormal3d(0.0,0.0,-1.0); glVertex3d(0.0,0.0, z0 ); for (j=0; j<=slices; j++) glVertex3d(cost[j]*r0, sint[j]*r0, z0); glEnd(); /* Cover each stack with a quad strip, except the top stack */ for( i=0; i<stacks-1; i++ ) { glBegin(GL_QUAD_STRIP); for(j=0; j<=slices; j++) { glNormal3d(cost[j]*sinn, sint[j]*sinn, cosn); glVertex3d(cost[j]*r0, sint[j]*r0, z0 ); glVertex3d(cost[j]*r1, sint[j]*r1, z1 ); } z0 = z1; z1 += zStep; r0 = r1; r1 -= rStep; glEnd(); } /* The top stack is covered with individual triangles */ glBegin(GL_TRIANGLES); glNormal3d(cost[0]*sinn, sint[0]*sinn, cosn); for (j=0; j<slices; j++) { glVertex3d(cost[j+0]*r0, sint[j+0]*r0, z0 ); glVertex3d(0, 0, height); glNormal3d(cost[j+1]*sinn, sint[j+1]*sinn, cosn ); glVertex3d(cost[j+1]*r0, sint[j+1]*r0, z0 ); } glEnd(); /* Release sin and cos tables */ free(sint); free(cost); }
/* * Draws a wire sphere */ void FGAPIENTRY glutWireSphere(GLdouble radius, GLint slices, GLint stacks) { int i,j; /* Adjust z and radius as stacks and slices are drawn. */ double r; double x,y,z; /* Pre-computed circle */ double *sint1,*cost1; double *sint2,*cost2; FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutWireSphere" ); fghCircleTable(&sint1,&cost1,-slices ); fghCircleTable(&sint2,&cost2, stacks*2); /* Draw a line loop for each stack */ for (i=1; i<stacks; i++) { z = cost2[i]; r = sint2[i]; glBegin(GL_LINE_LOOP); for(j=0; j<=slices; j++) { x = cost1[j]; y = sint1[j]; glNormal3d(x,y,z); glVertex3d(x*r*radius,y*r*radius,z*radius); } glEnd(); } /* Draw a line loop for each slice */ for (i=0; i<slices; i++) { glBegin(GL_LINE_STRIP); for(j=0; j<=stacks; j++) { x = cost1[i]*sint2[j]; y = sint1[i]*sint2[j]; z = cost2[j]; glNormal3d(x,y,z); glVertex3d(x*radius,y*radius,z*radius); } glEnd(); } /* Release sin and cos tables */ free(sint1); free(cost1); free(sint2); free(cost2); }
/* * Draws a solid sphere */ void FGAPIENTRY glutSolidSphere(GLdouble radius, GLint slices, GLint stacks) { int i,j; /* Adjust z and radius as stacks are drawn. */ double z0,z1; double r0,r1; /* Pre-computed circle */ double *sint1,*cost1; double *sint2,*cost2; FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSolidSphere" ); fghCircleTable(&sint1,&cost1,-slices); fghCircleTable(&sint2,&cost2,stacks*2); /* The top stack is covered with a triangle fan */ z0 = 1.0; z1 = cost2[(stacks>0)?1:0]; r0 = 0.0; r1 = sint2[(stacks>0)?1:0]; glBegin(GL_TRIANGLE_FAN); glNormal3d(0,0,1); glVertex3d(0,0,radius); for (j=slices; j>=0; j--) { glNormal3d(cost1[j]*r1, sint1[j]*r1, z1 ); glVertex3d(cost1[j]*r1*radius, sint1[j]*r1*radius, z1*radius); } glEnd(); /* Cover each stack with a quad strip, except the top and bottom stacks */ for( i=1; i<stacks-1; i++ ) { z0 = z1; z1 = cost2[i+1]; r0 = r1; r1 = sint2[i+1]; glBegin(GL_QUAD_STRIP); for(j=0; j<=slices; j++) { glNormal3d(cost1[j]*r1, sint1[j]*r1, z1 ); glVertex3d(cost1[j]*r1*radius, sint1[j]*r1*radius, z1*radius); glNormal3d(cost1[j]*r0, sint1[j]*r0, z0 ); glVertex3d(cost1[j]*r0*radius, sint1[j]*r0*radius, z0*radius); } glEnd(); } /* The bottom stack is covered with a triangle fan */ z0 = z1; r0 = r1; glBegin(GL_TRIANGLE_FAN); glNormal3d(0,0,-1); glVertex3d(0,0,-radius); for (j=0; j<=slices; j++) { glNormal3d(cost1[j]*r0, sint1[j]*r0, z0 ); glVertex3d(cost1[j]*r0*radius, sint1[j]*r0*radius, z0*radius); } glEnd(); /* Release sin and cos tables */ free(sint1); free(cost1); free(sint2); free(cost2); }
void GLFunctions::cylinder( float _radius,const float _height, int _slices, int _stacks ) { /* Step in z and radius as stacks are drawn. */ double z0,z1; const double zStep = _height / ( ( _stacks > 0 ) ? _stacks : 1 ); /* Pre-computed circle */ double *sint,*cost; Vec4 vertex; Vec4 normal; fghCircleTable(&sint,&cost,-_slices); /* Do the stacks */ z0 = 0.0; z1 = zStep; glBegin(GL_TRIANGLES); for(int i=1; i<=_stacks+1; ++i ) { if(i==_stacks) { z1 = _height; } for(int j=0; j<=_slices-1; ++j) { // vert 1; normal.set(sint[j],cost[j],0.0f); vertex.set(sint[j]*_radius,cost[j]*_radius,-z0/2.0); normal.normalGL(); vertex.vertexGL(); // vert 2 vertex.set(sint[j]*_radius,cost[j]*_radius,-z1/2.0); vertex.vertexGL(); vertex.set(sint[j+1]*_radius,cost[j+1]*_radius,-z0/2.0); normal.set(sint[j+1],cost[j+1],0.0f); normal.normalGL(); vertex.vertexGL(); vertex.set(sint[j+1]*_radius,cost[j+1]*_radius,-z0/2.0); normal.set(sint[j+1],cost[j+1],0.0f); normal.normalGL(); vertex.vertexGL(); vertex.set(sint[j]*_radius,cost[j]*_radius,-z1/2.0); normal.set(sint[j],cost[j],0.0f); normal.normalGL(); vertex.vertexGL(); vertex.set(sint[j+1]*_radius,cost[j+1]*_radius,-z1/2.0); normal.set(sint[j+1],cost[j+1],0.0f); normal.normalGL(); vertex.vertexGL(); } z0 = z1; z1 += zStep; } glEnd(); /* Release sin and cos tables */ delete [] sint; delete [] cost; }