// rolling pentagon // PhongModel color calculation // T. Kosaka CS TNCT 8Aug2002 #include #include #include #include "drawtools3D.h" #ifndef M_PI #define M_PI 3.141592653589793 #endif 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]); } ////////////////////////////////////////////////////////////////// void userdraw(void) { static int tick=0; float theta=0.3*sin(0.05*tick)-0.5; matrix3D_t tilting=rotationXMTX(0.5)*rotationYMTX(theta); matrix3D_t conv=scalingMTX(-1,-1,-1); 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}; color_t white={1.,1.,1.}; color_t cyan={0.,1.,1.}; color_t blue={0.,0.,1.}; point3D_t pentagon[5]; vector3D_t vec[10]; vector3D_t normalVector; matrix3D_t rot1,trans,mat; color_t color; int i; setColor(white); drawAxes(tilting); for (i=0;i<5;i++) { float theta=M_PI*(2.*i/5+0.5); pentagon[i].x=80.*cos(theta); pentagon[i].y=80.*sin(theta); pentagon[i].z=0.; } setColor(cyan); rot1=rotationXMTX(0.05*tick); trans=translationMTX(0,66,0); mat=tilting*rot1*trans; for (i=0;i<5;i++) { vec[i]=Point2Vector(pentagon[i]); vec[i]=mat*vec[i]; } normalVector=(vec[1]-vec[0])^(vec[2]-vec[0]); normalVector=unitVector(normalVector); if (0.