// ================================================ ====================================
// Title:
// Example note of OpenGL programming guide (1) -- render a light sphere
// Author:
// Norains
// Date:
/// Monday 21-June-2010
// Environment:
// Windows XP
// ================================================ ====================================
Overview
Example: render a light sphere
Serial number: 5-1
Page number: p127
Source code:
# Include ". // glut. H "<br/> # include" windows. H "</P> <p> void Init () <br/> {<br/> glfloat mat_specular [] = {1.0, 1.0, 1.0, 1.0 }; <br/> glfloat mat_shininess [] = {50.0}; <br/> glfloat light_position [] = {1.0, 1.0, 1.0, 0.0 }; <br/> glfloat white_light [] = {1.0, 1.0, 1.0, 1.0}; <br/> glfloat lmodel_ambient [] = {0.1, 0.1, 0.1, 1.0 }; <br/> glclearcolor (0.0, 0.0, 0.0, 0.0); <br/> glshademodel (gl_smooth); <br/> glmaterialfv (gl_front, gl_specular, mat_specular ); <br/> latency (gl_front, gl_shininess, mat_shininess); <br/> gllightfv (gl_light0, gl_position, light_position); <br/> gllightfv (gl_light0, gl_diffuse, white_light ); <br/> gllightfv (gl_light0, gl_specular, white_light); <br/> gllightmodelfv (gl_light_model_ambient, lmodel_ambient); </P> <p> glable (gl_lighting ); <br/> glable (gl_light0); <br/> glable (gl_depth_test); <br/>}</P> <p> void display () <br/>{< br/> glclear (gl_color_buffer_bit | gl_depth_buffer_bit); <br/> glflush (1.0, 20, 16); <br/> glflush (); <br/>}</P> <p> void reshape (int w, int h) <br/> {<br/> glviewport (0, 0, (glsizei) W, (glsizei) H); <br/> glmatrixmode (gl_projection); <br/> glloadidentity (); <br/> If (W <= H) <br/> {<br/> glortho (-1.5, 1.5,-1.5 * (glfloat) h/(glfloat) W, 1.5 * (glfloat) h/(glfloat) w,-10.0, 10.0); <br/>}< br/> else <br/>{< br/> glortho (-1.5 * (glfloat) W/(glfloat) h, 1.5 * (glfloat) W/(glfloat) h,-1.5, 1.5,-10.0, 10.0); <br/>}< br/> glmatrixmode (gl_modelview ); <br/> glloadidentity (); <br/>}</P> <p> int main (INT argc, char ** argv) <br/>{< br/> gluinit (& argc, argv); <br/> gluinitdisplaymode (glu_single | glu_rgb | glu_depth); <br/> gluinitwindowsize (500,500 ); <br/> gluinitwindowposition (200,200); <br/> glucreatewindow (argv [0]); <br/> Init (); <br/> gludisplayfunc (Display ); <br/> glureshapefunc (reshape); <br/> glumainloop (); </P> <p> return 0; <br/>}</P> <p>
Run display:
The sphere is not very circle. It can be seen that it is composed of lines. Let's change the value of the second parameter of the glusolidsphere to 300, that is, the value of the glusolidsphere (1.0, 300, 16 );
At this time, the display is smooth, like a real ball:
The light source in the original code is white. Let's change the color of the light source to see what the effect is. We change the value of white_light to red, that is:
Glfloat white_light [] = {1.0, 0.0, 0.0, 1.0 };
The following information is displayed when the program is running:
I am full of red and don't fall in the autumn ~
The original code only has one light source light0. Now we manually add another light source light1. First, restore the previous value of white_light to white, and then add a red light source. Add the following code to the end of the init function:
Glfloat red_light [] = {1.0, 0.0, 0.0, 1.0 };
Lightfv (gl_light1, gl_position, light_position );
Gllightfv (gl_light1, gl_diffuse, red_light );
Gllightfv (gl_light1, gl_specular, red_light );
Glable (gl_light1 );
Program running
Finally, let's move the X axis of the light source, that is, the value of light_position is changed:
Glfloat light_position [] = {-20.0, 1.0, 10.0, 0.0 };
When the program runs, we can see that the light source is obviously moved to the left: