//cubewithlight // Demonstration of shading and lighting with moving cube // The function "myInitView" prepares the view with lighting. // The function "putFlatPolygon" draw a polygon with setting the normal // vector to the polygon. // In the function "glMaterialfv" the keyword "GL_AMBIENT" means the reflection color // for the ambient light. // flat shading // T. Kosaka CS TNCT 16Aug2002 #include #include #include #include "GLDrawingtool3D.h" #ifndef M_PI #define M_PI 3.141592653589793 #endif void userdraw(void); GLfloat light0pos[] = { 500.0, 500.0, 1500.0, 0.0 }; // x,y,z,d GLfloat White[] = { 1.0, 1.0, 1.0, 1.0 }; // R,G,B,A GLfloat Cyan[] = { 0.0, 1.0, 1.0, 1.0 }; // R,G,B,A GLfloat Gray[] = { 0.2, 0.2, 0.2, 1.0 }; // R,G,B,A GLfloat Red[] = { 1.0, 0.0, 0.0, 1.0 }; // R,G,B,A GLfloat Shine[] = { 20.0}; //0.0(large highlight) ... 128.0(small highlight) void display(void) { glClear( GL_COLOR_BUFFER_BIT); glClear(GL_DEPTH_BUFFER_BIT); // depth check userdraw(); glutSwapBuffers(); } void userdraw(void) { static int tick=0; float k1=tick/200.; polyhedron_t cube={ 8, //number of vertices of the cube { {50,50,50},{50,50,-50},{-50,50,-50},{-50,50,50}, {50,-50,50},{50,-50,-50},{-50,-50,-50},{-50,-50,50} }, // coordinates of the vertices of the cube 6, // number of faces of the cube { {4,{0,1,2,3},{0,1,0}}, {4,{0,4,5,1},{1,0,0}}, {4,{1,5,6,2},{0,0,-1}}, {4,{2,6,7,3},{-1,0,0}}, {4,{3,7,4,0},{0,0,1}}, {4,{7,6,5,4},{0,-1,0}} } // every face has four vertices and showing vertex numbers }; myInitView(1); glRotatef(k1*360.0, 0.0, 1.0, 0.0); // Matrix A glTranslatef(200.0, 0.0, 0.0); // Matrix B glRotatef(k1*360.0, 0.0, 0.0, 1.0); // Matrix C // Then Transformation Matrix for the cube is A*B*C glMaterialfv(GL_FRONT, GL_AMBIENT, Gray); //Attribute of the cube glMaterialfv(GL_FRONT, GL_DIFFUSE, Cyan); glMaterialfv(GL_FRONT, GL_SPECULAR, White); glMaterialfv(GL_FRONT, GL_SHININESS, Shine); putFlatPolyhedron(cube); tick++; } int main(int argc, char **argv) { glutInit(&argc,argv); glutInitDisplayMode ( GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); // depth check glutInitWindowPosition(100,100); glutInitWindowSize(640,480); glutCreateWindow ("cubewithlight"); glClearColor(0.0, 0.0, 0.0, 0.0); glViewport(0,0,640,480); glutIdleFunc(display); // idle event call back glutDisplayFunc(display); glEnable(GL_DEPTH_TEST); // depth check (OpenGL inner Z-Buffer) glutMainLoop(); return 0; }