// Ef 3D // This program demonstrates transformational examples. // T. Kosaka CS TNCT 3Aug2002 #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]); } ////////////////////////////////////////////////////////////////// void userdraw(void) { static int tick=0; float theta=0.1*sin(0.05*tick)-0.5; matrix3D_t tilting=rotationXMTX(0.5)*rotationYMTX(theta); color_t white={1.,1.,1.}; color_t cyan={0.,1.,1.}; setColor(white); drawAxes(tilting); int disp=(tick/200)%7; point3D_t ef[10]={ {10,100,0},{10,10,0},{30,10,0},{30,40,0},{60,40,0}, {60,60,0},{30,60,0},{30,80,0},{70,80,0},{70,100,0} }; vector3D_t vec[10]; matrix3D_t trans,rot,scale,refxaxis,refyaxis,reforg,mat; int i,loop; setColor(cyan); for (i=0;i<10;i++) { vec[i]=Point2Vector(ef[i]); vec[i]=tilting*vec[i]; } drawPolygon(vec,10); switch(disp) { case 0: glutSetWindowTitle("ef3D"); break; case 1: //translation glutSetWindowTitle("ef3D - translation"); trans=translationMTX(62,22,0); mat=tilting*trans; for (loop=0;loop<4;loop++) { for (i=0;i<10;i++) { vec[i]=Point2Vector(ef[i]); vec[i]=mat*vec[i]; } drawPolygon(vec,10); mat=mat*trans; } break; case 2: //rotationZ glutSetWindowTitle("ef3D - rotationZ"); rot=rotationZMTX(0.75); mat=tilting*rot; for (loop=0;loop<4;loop++) { for (i=0;i<10;i++) { vec[i]=Point2Vector(ef[i]); vec[i]=mat*vec[i]; } drawPolygon(vec,10); mat=mat*rot; } break; case 3: //rotationX glutSetWindowTitle("ef3D - rotationX"); rot=rotationXMTX(0.75); mat=tilting*rot; for (loop=0;loop<4;loop++) { for (i=0;i<10;i++) { vec[i]=Point2Vector(ef[i]); vec[i]=mat*vec[i]; } drawPolygon(vec,10); mat=mat*rot; } break; case 4: //rotationY glutSetWindowTitle("ef3D - rotationY"); rot=rotationYMTX(0.75); mat=tilting*rot; for (loop=0;loop<4;loop++) { for (i=0;i<10;i++) { vec[i]=Point2Vector(ef[i]); vec[i]=mat*vec[i]; } drawPolygon(vec,10); mat=mat*rot; } break; case 5: //scaling glutSetWindowTitle("ef3D - scaling"); scale=scalingMTX(1.2,1.2,1); mat=tilting*scale; for (loop=0;loop<4;loop++) { for (i=0;i<10;i++) { vec[i]=Point2Vector(ef[i]); vec[i]=mat*vec[i]; } drawPolygon(vec,10); mat=mat*scale; } break; case 6: //reflection a special case of scaling glutSetWindowTitle("ef3D - reflection"); refxaxis=scalingMTX(1.,-1.,1); refyaxis=scalingMTX(-1.,1.,1); reforg=scalingMTX(-1.,-1.,1); mat=tilting*refxaxis; for (i=0;i<10;i++) { vec[i]=Point2Vector(ef[i]); vec[i]=mat*vec[i]; } drawPolygon(vec,10); mat=tilting*refyaxis; for (i=0;i<10;i++) { vec[i]=Point2Vector(ef[i]); vec[i]=mat*vec[i]; } drawPolygon(vec,10); mat=tilting*reforg; for (i=0;i<10;i++) { vec[i]=Point2Vector(ef[i]); vec[i]=mat*vec[i]; } drawPolygon(vec,10); break; default: break; } tick++; } int main(int argc, char **argv) { glutInit(&argc,argv); glutInitDisplayMode ( GLUT_DOUBLE | GLUT_RGB ); glutInitWindowPosition(100,100); glutInitWindowSize(640,480); glutCreateWindow ("ef3D"); glClearColor(0.0, 0.0, 0.0, 0.0); gluOrtho2D(-320., 320., -240.0, 240.0); // Define the dimensions of the Orthographic Viewing Volume glutIdleFunc(display); // idle event call back glutDisplayFunc(display); glutMainLoop(); return 0; }