#include <stdlib.h> #include <GL/glut.h> Glfloat light_ambient[] = {0.1,0.1,0.1,0.0}; Glfloat light_diffuse[] = {1.0,1.0,1.0,0.0}; Glfloat light_specular[] = {1.0,1.0,1.0,0.0}; Glfloat light_position[] = { -10.0,0.0,5.0,0.0}; Glfloat material_ambient[] = {0.1745,0.01175,0.01175}; Glfloat material_diffuse[] = {0.61424,0.04136,0.04136}; Glfloat material_specular[] = {0.727811,0.626959,0.62659}; Glfloat fogcolorwhite[] = {1.0,1.0,1.0,1.0}; Glfloat fogcolorgreen[] = {0.0,1.0,0.0,1.0}; float fogdensity = 0.02; void Myinit (void) { /* Set Background color * * Glclearcolor (0.5,0.5,0.5,1.0); /* Set the Light/* GLLIGHTFV (gl_light0,gl_ambient,light_ambient); GLLIGHTFV (Gl_light0,gl_diffuse,light_diffuse); GLLIGHTFV (Gl_light0,gl_specular,light_specular); GLLIGHTFV (gl_light0,gl_position,light_position); /* Set Material * * GLMATERIALFV (gl_front,gl_ambient,material_ambient); GLMATERIALFV (Gl_front,gl_diffuse,material_diffuse); GLMATERIALFV (Gl_front,gl_specular,material_specular); Glmaterialf (gl_front,gl_shininess,0.6*128); Glshademodel (Gl_smooth); Glenable (gl_depth_test); Gldepthfunc (gl_less); Glenable (gl_lighting); Glenable (GL_LIGHT0); Glenable (Gl_auto_normal); Glenable (gl_normalize); Glfrontface (GL_CW); /* Set atomization * * Glenable (GL_FOG); Glfogi (gl_fog_mode,gl_linear); GLFOGFV (Gl_fog_color,fogcolorwhite); GLFOGF (gl_fog_density,fogdensity); GLFOGF (gl_fog_start,0.0); GLFOGF (gl_fog_end,15.0); Glhint (Gl_fog_hint,gl_dont_care); } /* Set different atomization effects according to different key values. static void MyKey (unsigned char key,int x,int y) { Switch (key) { Case://shift+1 Fogdensity *= 1.10; Glfogi (GL_FOG_MODE,GL_EXP); GLFOGF (gl_fog_density,fogdensity); Glutpostredisplay (); Break Case://shift+2 Fogdensity/= 1.10; Glfogi (GL_FOG_MODE,GL_EXP); GLFOGF (gl_fog_density,fogdensity); Glutpostredisplay (); Break Case://shift+3 Glfogi (gl_fog_start,0.0); Glfogi (gl_fog_end,8.0); Glutpostredisplay (); Break Case://shift+4 Glfogi (GL_FOG_MODE,GL_EXP2); Glutpostredisplay (); Break Case://shift+5 Glfogi (gl_fog_mode,gl_linear); Glutpostredisplay (); Break Case://shift+6 GLFOGFV (Gl_fog_color,fogcolorgreen); Glutpostredisplay (); Break Case://esc Exit (0); Default Break } } /* Draw a cone * * void Mydisplay (void) { Glclear (Gl_color_buffer_bit|gl_depth_buffer_bit); Glpushmatrix (); Gltranslatef ( -4.0,4.0,0.0); Glrotatef (30.0,1.0,1.0,0.0); Glutsolidcone (1.0,50.0,20.0,20.0); Glpopmatrix (); Glutswapbuffers (); } void Myreshape (int w,int h) { Glviewport (0,0, (Glsizei) W, (Glsizei) h); Glmatrixmode (gl_projection); Glloadidentity (); Gluperspective (100.0,1.0,1.0,20.0); Glmatrixmode (Gl_modelview); Glloadidentity (); Gltranslatef (0.0,0.0,-15.5); } int main (int argc,char * * argv) { /* Initialize * * Glutinit (&ARGC,ARGV); Glutinitdisplaymode (glut_single| glut_rgb| Glut_depth); Glutinitwindowsize (300,300); Glutinitwindowposition (100,100); /* Create window * * Glutcreatewindow ("FOG"); /* Draw and display * * Myinit (); Glutkeyboardfunc (MyKey); Glutreshapefunc (Myreshape); Glutdisplayfunc (Mydisplay); /* Enter GLUT event handling cycle * * Glutmainloop (); return 0; } |