// torus // face z-sorting to draw faces // In the case of a red torus, no face z-sorting technique is used and // failed to complete the torus. // T. Kosaka CS TNCT 2001 #include #include #include #include "drawtools3D.h" #ifndef M_PI #define M_PI 3.141592653589793 #endif #define MAXSIZE 8192 void userdraw(void); void display(void) { glClear( GL_COLOR_BUFFER_BIT); userdraw(); glutSwapBuffers(); } ////////////////////////////////////////////////////////////////// void drawcharX(float x,float y) { drawLine(x,y,x+10,y+12);drawLine(x,y+12,x+10,y); } void drawcharY(float x,float y) { drawLine(x+5,y,x+5,y+7);drawLine(x,y+12,x+5,y+7);drawLine(x+10,y+12,x+5,y+7); } void drawcharZ(float x,float y) { drawLine(x,y+12,x+10,y+12);drawLine(x+10,y+12,x,y);drawLine(x,y,x+10,y); } void drawAxes(matrix3D_t view) { #define HALFAXIS 220 #define HALFAXIS1 (HALFAXIS-10) point3D_t axes[14]={ {-HALFAXIS,0,0},{HALFAXIS,0,0},{HALFAXIS1,5,0},{HALFAXIS1,0,0},{0,0,0}, {0,-HALFAXIS,0},{0,HALFAXIS,0},{0,HALFAXIS1,5},{0,HALFAXIS1,0},{0,0,0}, {0,0,-HALFAXIS},{0,0,HALFAXIS},{5,0,HALFAXIS1},{0,0,HALFAXIS1} }; vector3D_t vec[14]; int i; for (i=0;i<14;i++) { vec[i]=Point2Vector(axes[i]); vec[i]=view*vec[i]; } drawPolyline(vec,14); drawcharX(vec[1].v[0],vec[1].v[1]); drawcharY(vec[6].v[0],vec[6].v[1]); drawcharZ(vec[11].v[0]-14,vec[11].v[1]); } ////////////////////////////////////////////////////////////////// typedef struct { int NumberofVertices; //in the face short int pnt[8]; } face_t; typedef struct { int NumberofVertices; //of the object point3D_t pnt[MAXSIZE]; vector3D_t NormalVector[MAXSIZE]; // at the pnt int NumberofFaces; //of the object face_t fc[MAXSIZE]; } smoothpolyhedron_t; static void makeTorus(smoothpolyhedron_t &torus,float r0,float r1) { // r1 : radius of the ring // r0 : radius of the circle float circleangle; float ringangle; int NumCclEdge=20; int NumRngEdge=40; int i,j,k; vector3D_t unitx={1,0,0,1}; vector3D_t vec; matrix3D_t mat; torus.NumberofVertices=NumCclEdge*NumRngEdge; mat=translationMTX(r1,0,0)*scalingMTX(r0,r0,r0); for(j=0;j