// rotef // various rotation examples of charactor 'F' // the examples of composite transformation // In multiplication of transformation matrices the order of multiplication is important. // T. Kosaka CS TNCT 2Aug2002 #include #include #include #include "drawtools2D.h" void userdraw(void); void display(void) { glClear( GL_COLOR_BUFFER_BIT); userdraw(); glutSwapBuffers(); } ////////////////////////////////////////////////////////////////// void drawcharX(float x,float y) //文字 x の表示 { drawLine(x,y,x+10,y+12);drawLine(x,y+12,x+10,y); } void drawcharY(float x,float y) //文字 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 drawAxes(void) // xy軸の表示 { drawLine(-310,0,310,0);drawLine(310,0,300,5);drawLine(310,0,300,-5); drawcharX(300,-20); drawLine(0,-230,0,230);drawLine(0,230,5,220);drawLine(0,230,-5,220); drawcharY(-20,220); } ////////////////////////////////////////////////////////////////// void userdraw(void) { static int tick=0; int disp=(tick/1000)%6; int tickindisp=tick%1000; point2D_t ef[10]={ {10,100},{10,10},{30,10},{30,40},{60,40}, {60,60},{30,60},{30,80},{70,80},{70,100} }; matrix2D_t mat,trans1,rot1,trans2,rot2,scale; vector2D_t vec[10]; color_t white={1.,1.,1.}; color_t cyan={0.,1.,1.}; int i; float factor; setColor(white); drawAxes(); setColor(cyan); drawPolygon(ef,10); switch(disp) { case 0: //translation -> rotation glutSetWindowTitle("composition - translation -> rotation"); trans1=translationMTX(-40,-50); rot1=rotationMTX(tickindisp*0.01); mat=rot1*trans1; /*mat=multiply(rot1,trans1); この行と同じ意味*/ break; case 1: //translation -> rotation -> translation glutSetWindowTitle("composition - translation -> rotation -> translation"); trans1=translationMTX(-40,-50); rot1=rotationMTX(tickindisp*0.01); trans2=translationMTX(160,0); mat=trans2*rot1*trans1; /*mat=multiply(rot1,trans1); この2行と同じ意味*/ /*mat=multiply(trans2,mat); */ break; case 2: //translation -> rotation glutSetWindowTitle("composition - translation -> rotation"); trans1=translationMTX(120,-50); rot1=rotationMTX(tickindisp*0.01); mat=rot1*trans1; /*mat=multiply(rot1,trans1); この行と同じ意味*/ break; case 3: //translation -> rotation -> translation -> rotation glutSetWindowTitle("composition - translation -> rotation -> translation -> rotation"); trans1=translationMTX(-40,-50); rot1=rotationMTX(tickindisp*0.02); trans2=translationMTX(160,0); rot2=rotationMTX(tickindisp*0.01); mat=rot2*trans2*rot1*trans1; /*mat=multiply(rot1,trans1); この3行と同じ意味*/ /*mat=multiply(trans2,mat); */ /*mat=multiply(rot2,mat); */ break; case 4: //translation -> rotation -> translation -> rotation glutSetWindowTitle("composition - translation -> rotation -> translation -> rotation"); trans1=translationMTX(-40,-50); rot1=rotationMTX(-tickindisp*0.01); trans2=translationMTX(160,0); rot2=rotationMTX(tickindisp*0.01); mat=rot2*trans2*rot1*trans1; /*mat=multiply(rot1,trans1); この3行と同じ意味*/ /*mat=multiply(trans2,mat); */ /*mat=multiply(rot2,mat); */ break; case 5: //trans -> scaling -> rot -> trans -> rot glutSetWindowTitle("composition - trans -> scaling -> rot -> trans -> rot"); factor=0.3+0.35*(sin(tickindisp*0.1)+1); trans1=translationMTX(-40,-50); scale=scalingMTX(factor,factor); rot1=rotationMTX(-tickindisp*0.02); trans2=translationMTX(160,0); rot2=rotationMTX(tickindisp*0.01); mat=rot2*trans2*rot1*scale*trans1; /*mat=multiply(scale,trans1); この4行と同じ意味*/ /*mat=multiply(rot1,mat); */ /*mat=multiply(trans2,mat); */ /*mat=multiply(rot2,mat); */ break; default: mat=createIdentity(); break; } for (i=0;i<10;i++) { vec[i]=Point2Vector(ef[i]); vec[i]=mat*vec[i]; /*vec[i]=multiply(mat,vec[i]); この行と同じ意味*/ } drawPolygon(vec,10); tick++; } int main(int argc, char **argv) { glutInit(&argc,argv); glutInitDisplayMode ( GLUT_DOUBLE | GLUT_RGB ); glutInitWindowPosition(100,100); glutInitWindowSize(640,480); glutCreateWindow ("composition"); 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; }