//torussphere2 // Z-Buffer // This method improves the error in "torussphere1". // T. Kosaka CS TNCT 2001 #include #include #include #include "drawtools3D.h" #ifndef M_PI #define M_PI 3.141592653589793 #endif #define MAXSIZE 8192 int interpolateInteger(int p1,int p2,float s1,float s2) { if (s1<0.) s1=0.; if (s2<0.) s2=0.; if (s1+s2<1.) return p1; return (int)((p1*s2+p2*s1)/(s1+s2)+.5); } float interpolateFloat(float p1,float p2,float s1,float s2) { if (s1<0.) s1=0.; if (s2<0.) s2=0.; if (s1+s2<1.) return p1; return (p1*s2+p2*s1)/(s1+s2); } color_t interpolateColor(color_t c1,color_t c2,float s1,float s2) { color_t ret; if (s1<0.) s1=0.; if (s2<0.) s2=0.; if (s1+s2<1.) { ret=c1; } else { ret.r=(1./(s1+s2))*(c1.r*s2+c2.r*s1); ret.g=(1./(s1+s2))*(c1.g*s2+c2.g*s1); ret.b=(1./(s1+s2))*(c1.b*s2+c2.b*s1); } return ret; } void drawDot(float x,float y) { glBegin(GL_POINTS); glVertex2f(x,y); glEnd(); } void fillPolygonZBuff(point3D_t pnt[],int num,color_t color,float zbuffer[640][480]) { int top,bottom; int topleft,topright,bottomleft,bottomright; int ymax,ymin,xleft,xright; float zleft,zright,z; int i,y,x; top=0,bottom=0; for(i=0;izbuffer[x+320][y+240])&&(-320<=x)&&(x<320)&&(-240<=y)&&(y<240)) { drawDot(x,y); zbuffer[x+320][y+240]=z; } } } } void gradatePolygonZBuff(point3D_t pnt[],color_t col[],int num,float zbuffer[640][480]) { int top,bottom; int topleft,topright,bottomleft,bottomright; int ymax,ymin,xleft,xright; float zleft,zright,z; color_t colorleft,colorright,color; int i,y,x; top=0,bottom=0; for(i=0;izbuffer[x+320][y+240])&&(-320<=x)&&(x<320)&&(-240<=y)&&(y<240)) { color=interpolateColor(colorleft,colorright,x-xleft,xright-x); setColor(color); drawDot(x,y); zbuffer[x+320][y+240]=z; } } } } //////////// End of OpenGL drawShape Functions ver 2 //////////// 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 makeSphere(smoothpolyhedron_t &sphere,float r) { // r : radius of the sphere float latitude; float longitude; int NumLatEdge=18; int NumLngEdge=32; int i,j,k; vector3D_t unitz={0,0,1,1}; vector3D_t unityn={0,1,0,1}; vector3D_t unitys={0,-1,0,1}; matrix3D_t mat; sphere.NumberofVertices=1+(NumLatEdge-1)*NumLngEdge+1; sphere.NormalVector[0]=unityn; sphere.pnt[0]=Vector2Point(r*sphere.NormalVector[0]); for(i=1;i