//cube // showing cube0 ..... cube5 // T. Kosaka CS TNCT 7Aug2002 #include #include #include #include "drawtools3D.h" 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[32]; } face_t; typedef struct { int NumberofVertices; //of the object point3D_t pnt[100]; int NumberofFaces; //of the object face_t fc[32]; } polyhedron_t; void makeCube(polyhedron_t &cube,float size) { point3D_t vertex[]={// coordinates of the vertices of the cube {1,1,1},{1,1,-1},{-1,1,-1},{-1,1,1}, {1,-1,1},{1,-1,-1},{-1,-1,-1},{-1,-1,1} }; face_t face[]={// every face has four vertices and showing vertex numbers {4,{0,1,2,3}},{4,{0,4,5,1}},{4,{1,5,6,2}}, {4,{2,6,7,3}},{4,{3,7,4,0}},{4,{7,6,5,4}} }; int i; cube.NumberofVertices=8;//number of vertices of the cube for (i=0;i<8;i++) { cube.pnt[i].x = size*vertex[i].x; cube.pnt[i].y = size*vertex[i].y; cube.pnt[i].z = size*vertex[i].z; } cube.NumberofFaces=6;// number of faces of the cube for (i=0;i<6;i++) { cube.fc[i]=face[i]; } } void userdraw(void) { static int tick=0; static polyhedron_t cube; float theta=0.5; matrix3D_t tilting=rotationXMTX(theta)*rotationYMTX(-theta); color_t White={1.,1.,1.}; color_t Cyan={0,1,1}; color_t Blue={0,0,1}; color_t Yellow={1,1,0}; int disp=(tick/200)%6; int tickindisp=tick%200; vector3D_t unitz={0,0,1,1}; vector3D_t LightVector0=rotationYMTX(0.2)*rotationXMTX(-1.2)*unitz; vector3D_t LightVector=tilting*LightVector0; vector3D_t ViewVector={0,0,1,1}; vector3D_t vec[32]; vector3D_t vecbuff[32]; vector3D_t NormalVector; vector3D_t buff[32]; float normalzi;//z of normal vector of the face float normalz[32];//z of normal vector of the face matrix3D_t rot,prs,mat; color_t fillcolor; int i,j; if (tick==0) makeCube(cube,100); setColor(White); drawAxes(tilting); setColor(Cyan); switch(disp) { case 0: //with all faces glutSetWindowTitle("cube - with all faces"); rot=rotationYMTX(0.02*tickindisp); mat=tilting*rot; for (i=0;i