//cube Demo with CLASS // T. Kosaka CS TNCT 7Aug2002 #include #include #include #include "CGCore3D.h" void userdraw(void); void display(void) { glClear( GL_COLOR_BUFFER_BIT); userdraw(); glutSwapBuffers(); } ////////////////////////////////////////////////////////////////// void makeCube(C_Polyhedron &cube,float size,C_Color color,opticalAttribute_t attr) { float vertex0[][3]={// 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} }; int vertexnumber[][4]={// every face has four vertices and showing vertex numbers {0,1,2,3},{0,4,5,1},{1,5,6,2}, {2,6,7,3},{3,7,4,0},{7,6,5,4} }; C_Vertex vertex[8]; C_Face face[6]; C_Color Cyan(0,1,1); int i; for (i=0;i<8;i++) { vertex[i].x=size*vertex0[i][0]; vertex[i].y=size*vertex0[i][1]; vertex[i].z=size*vertex0[i][2]; } for (i=0;i<6;i++) face[i].createFace(vertexnumber[i],4); cube.createPolyhedron(vertex,8,face,6,color,attr); } void userdraw(void) { static int tick=0; int disp=(tick/200)%2; static C_Polyhedron cube; float phi=0.5; // for tilting float theta=-0.5; // for tilting C_Vector ViewVector(0,0,1); C_Vector LightVector(0,1,0); C_Scene myScene; if (tick==0) { opticalAttribute_t attr; C_Color Cyan(0,1,1); attr.kspe=0.7; // specular reflection coefficient attr.kdif=0.6; // diffuse reflection coefficient attr.kamb=0.4; // ambient light coefficient makeCube(cube,100,Cyan,attr); } myScene.drawAxes(); myScene.initializeTMatrix(); switch (disp) { case 0: myScene.setRotationY(0.02*tick); //Matrix A myScene.setTranslation(150,0,0); //Matrix B myScene.setRotationX(-0.05*tick); //Matrix C myScene.setScale(0.4,0.4,0.4); //Matrix D //Then the composit matrix is A*B*C*D myScene.drawPolyhedronSurface(cube); break; case 1: myScene.setRotationY(0.02*tick); //Matrix A myScene.pushMatrix(); myScene.setTranslation(150,0,0); //Matrix B myScene.setRotationX(-0.05*tick); //Matrix C myScene.setScale(0.4,0.4,0.4); //Matrix D //Then the composit matrix is A*B*C*D myScene.drawPolyhedronSurface(cube); myScene.popMatrix(); myScene.setTranslation(-150,0,0); //Matrix E myScene.setRotationX(0.10*tick); //Matrix F myScene.setScale(0.4,0.6,0.1); //Matrix G //Then the composit matrix is A*E*F*G myScene.drawPolyhedronSurface(cube); 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 ("cube"); 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; }