#include <windows.h> #include <GL/glut.h> Glfloat light_position[] = {0.0,3.0,6.0,0.0}; Glfloat no_mat[] = {0.0,0.0,0.0,1.0}; Glfloat mat_grey_ambient[] = {0.5,0.5,0.5,1.0}; Glfloat mat_red_ambient[] = {0.0,0.0,1.0,1.0}; Glfloat mat_diffuse[] = {0.8,0.2,0.5,1.0}; Glfloat mat_specular[] = {1.0,1.0,1.0,1.0}; Glfloat no_shininess[] = {0.0}; Glfloat low_shininess[] = {5.0}; Glfloat high_shininess[] = {100.0}; Glfloat mat_emission[] = {0.3,0.2,0.2,0.0}; void Myinit (void) { GLLIGHTFV (gl_light2,gl_position,light_position);//Set Light Glenable (gl_depth_test); Gldepthfunc (gl_less);//Specify the values used in the depth comparison Glenable (gl_lighting); Glenable (GL_LIGHT0); Glshademodel (Gl_smooth); } void display (void) { Glclear (Gl_color_buffer_bit|gl_depth_buffer_bit); Glpushmatrix (); /* Specify material parameters for the illumination model * * GLMATERIALFV (Gl_front,gl_diffuse,mat_diffuse); GLMATERIALFV (Gl_front,gl_specular,mat_specular); GLMATERIALFV (gl_front,gl_shininess,high_shininess); GLMATERIALFV (Gl_front,gl_emission,no_mat); Glcolormaterial (gl_front,gl_ambient);//Make material color keep track of current color Glenable (gl_color_material); /* First Sphere/* Glpushmatrix (); GLCOLOR3FV (No_mat); Gltranslatef ( -2.5,1.5,0.0); Glrotatef (15.0,1.0,0.0,0.0); Glutsolidsphere (1.2,20.0,20.0); Glpopmatrix (); /* The second sphere * * Glpushmatrix (); GLCOLOR3FV (mat_grey_ambient); Glrotatef (15.0,1.0,0.0,0.0); Glutsolidsphere (1.2,20.0,20.0); Glpopmatrix (); /* The Third sphere * * Glpushmatrix (); GLCOLOR3FV (mat_red_ambient); Gltranslatef (2.5,-1.5,0.0); Glrotatef (15.0,1.0,0.0,0.0); Glutsolidsphere (1.2,20.0,20.0); Glpopmatrix (); Gldisable (gl_color_material); Glpopmatrix (); Glflush (); } void Myreshape (int w,int h) { Glviewport (0,0, (Glsizei) W, (Glsizei) h); Glmatrixmode (gl_projection); Glloadidentity (); if (w <= h) Glortho ( -5.5,5.5,-5.5* (glfloat) h/(glfloat) w,5.5* glfloat (h/) glfloat); Else Glortho ( -5.5* (glfloat) w/(glfloat) h,5.5* glfloat (w/) glfloat); Glmatrixmode (Gl_modelview); Glloadidentity (); } int main (int argc,char * * argv) { /* Initialize * * Glutinit (&ARGC,ARGV); Glutinitdisplaymode (glut_single| GLUT_RGB); Glutinitwindowsize (400,400); Glutinitwindowposition (100,100); /* Create window * * Glutcreatewindow ("light&material"); /* Draw and display * * Myinit (); Glutreshapefunc (Myreshape); Glutdisplayfunc (display); Glutmainloop (); return 0; } |