OpenGL can set at least 8 light sources, which are labeled gl_light0,gl_light1,gl_light2 .... Here we use two kinds of light sources, one is ambient light and the other is a spotlight.
When setting up lighting, we need to consider three kinds of light: ambient reflection light, specular light, diffuse light. In the Phong illumination model, the real illumination is simulated by the value of these three components. In which, the ambient reflection light light source after multiple reflections, can be understood as the background light, specular reflection and diffuse reflection of the surface roughness/smoothness of the object, both the and is certain.
In OpenGL, if we want to use a light source, we need to enter the following statement to turn on light mode:
Glenable (gl_lighting); //Turn on light mode
For a light source, we first need to determine its location, the light source can be a point light source, can also be a parallel light source, we write the following function, if it is a point light, the fourth parameter is 1, the first three parameters represent the position of the light source, if it is a parallel light source, the fourth parameter is 0, the first three parameters Where Light_pos is an array.
In this experiment, we used a point light source.
GLLIGHTFV (gl_light0,gl_position, light_pos); // Set up Section 0 illumination position of the light source
Also, set the color parameters for diffuse, specular, and ambient reflections:
GLLIGHTFV (gl_light0,gl_specular, color); // Set Specular illumination
GLLIGHTFV (gl_light0,gl_diffuse, color); // set Diffuse light composition
GLLIGHTFV (gl_light0,gl_ambient, color); // set The light color (ambient light color) after multiple reflections of light Source No. 0
In this experiment, we set the ambient light to the specified color (where we chose white and green), and both specular and diffuse light are set to white.
Use the following function to turn on the light:
glenable (gl_light0); // Open Section 0 Number Light source
Spotlight We also use the gl_light1 light source. In addition to the basic three types of light, other properties are included, namely, the clipping angle, the direction of the light source, and the aggregation degree:
GLLIGHTF (gl_light1 , gl_spot_cutoff Spotangle); // cut angle
GLLIGHTFV (gl_light1 , gl_spot_direction Lightdir); // Light direction
GLLIGHTF (gl_light1,gl_spot_exponent, 2.); // aggregation degree
For spotlights, we set the same as ambient light.
After the basic lighting is set, we focus on the material of the object, where the material does not mean that we understand the meaning of the material, but the color of the object reflected in different light. We know that the display of objects is very much related to light, such as in the dark, the object we see is black. The color of the object we see is determined by the light and the reflected color of the object.
GLMATERIALFV (gl_front_and_back,gl_specular, color);
GLMATERIALFV (gl_front_and_back,gl_diffuse, color);
GLMATERIALFV (gl_front_and_back,gl_AMBIENT, color);
Where the first parameter represents the front or back of the operand, the parameters include Gl_front, Gl_back, Gl_front_and_back, and the second parameter represents the light under which the color of the material is displayed, and the third parameter is the color of the material under illumination.
For the teapot, we set the mirror index to 50, the specular reflection is 0.6f, 0.6f, 0.6f, diffuse reflection for 0.85f, 0.65f, 0.2f.
For the desktop and the legs, we set the specular and diffuse colors to the same specified color.
Experimental data recording and processing
(Data settings are for reference only)
At present, the effect of the table leg is not very real, but the experimental requirements are only one color, so did not do too much optimization. If you have good data settings, you can share it.
|
Environmental reflection |
Diffuse reflection |
Specular reflection |
White Ambient light/spotlight |
(1,1,1) |
(1,1,1) |
(1,1,1) |
Green Ambient Light/spotlight |
(0,1,0)
|
(1,1,1) |
(1,1,1) |
Teapot |
/ |
(0.6, 0.6, 0.6) |
(0.85, 0.65, 0.2) |
Desktop |
/ |
(1,0,0) |
(1,0,0) |
Table Foot 1 |
/ |
(0,1,0) |
(0,1,0) |
Table Foot 2 |
/ |
(1,1,0) |
(1,1,0) |
Table Foot 3 |
/ |
(0,1,1) |
(0,1,1) |
Table Foot 4 |
|
(0,0,1) |
(0,0,1) |
GlutEx1.cpp: Defines the entry point of the console application. #include <stdlib.h> #include "glut.h" #include <stdio.h>float ftranslate;float frotate;float fscale = 1.0f ; float Color_b = 1.0f;bool BPERSP = False;bool Banim = False;bool Bwire = False;bool Iswhite = true;int wHeight = 0;int WW idth = 0; Glfloat color[] = {1.0, 1.0, 1.0, 1.0}; Definition color float eye[] = {0, 0, 8};float center[] = {0, 0, 0}; Glfloat spotangle = 5.0f; Angle//ambient light position glfloat light_x = 0.0f; Glfloat light_y = 0.0f; Glfloat light_z = 0.0f;//Spotlight direction Glfloat dir_x = 0.0f; Glfloat dir_y = 0.0f; Glfloat dir_z = 0.0f;void draw_leg (), void Draw_triangle ()//This function draws a Triangle with RGB colors{glfloat mat_sp Ecular[] = {0.6f, 0.6f, 0.6f, 1.0f}; Glfloat mat_diffuse0[] = {0.85f, 0.65f, 0.2f, 1.0f}; Glfloat mat_diffuse1[] = {1.0f, 0.0f, 0.0f}; Glfloat mat_diffuse2[] = {0.0f, 1.0f, 0.0f}; Glfloat mat_diffuse3[] = {1.0f, 1.0f, 0.0f}; Glfloat mat_diffuse4[] = {0.0f, 1.0f, 1.0f}; Glfloat mat_diffuse5[] = {0.0f, 0.0f, 1.0f};//painted teapot GlpusHmatrix (); GLMATERIALFV (Gl_front, Gl_specular, mat_specular);//Set specular properties on the front of the polygon Glmateriali (Gl_front_and_back, GL_ Shininess, 50);//Specify Mirror index GLMATERIALFV (Gl_front, Gl_diffuse, MAT_DIFFUSE0); Set polygon Front Diffuse properties Gltranslatef (0, 0, 4+1), Glrotatef (1, 0, 0) glutsolidteapot (1); Glpopmatrix ();//Draw Desktop Glpushmatrix (); GLMATERIALFV (Gl_front, Gl_specular, mat_diffuse1);//Set specular properties for polygon front GLMATERIALFV (Gl_front, Gl_diffuse, Mat_diffuse1) ;//Set Polygon Front Diffuse property Gltranslatef (0, 0, 3.5); Glscalef (5, 4, 1); Glutsolidcube (1.0); Glpopmatrix ();//Draw Four legs glpushmatrix (); GLMATERIALFV (Gl_front, Gl_specular, MAT_DIFFUSE2);//Set specular properties for polygon front GLMATERIALFV (Gl_front, Gl_diffuse, Mat_diffuse2) ;//Set Polygon Front Diffuse property Gltranslatef (1.5, 1, 1.5);D Raw_leg (); Glpopmatrix (); Glpushmatrix (); GLMATERIALFV (Gl_front, Gl_ Specular, MAT_DIFFUSE3);//Set the specular properties of the polygon front GLMATERIALFV (Gl_front, Gl_diffuse, MAT_DIFFUSE3);// Set polygon Front Diffuse properties Gltranslatef ( -1.5, 1, 1.5);D Raw_leg (); Glpopmatrix (); Glpushmatrix (); GLMATERIALFV (Gl_front, Gl_specular , mat_diffuse4);//sets the specular reflection property of the front of the polygon GLMATERIALFV(Gl_front, Gl_diffuse, MAT_DIFFUSE4);//Set Polygon Front diffuse properties Gltranslatef (1.5,-1, 1.5);D Raw_leg (); Glpopmatrix (); Glpushmatrix (); GLMATERIALFV (Gl_front_and_back, Gl_specular, MAT_DIFFUSE5);//Set specular properties on the front of the polygon GLMATERIALFV (GL_FRONT_ And_back, Gl_diffuse, MAT_DIFFUSE5);//Set Polygon Front diffuse properties Gltranslatef ( -1.5,-1, 1.5);D Raw_leg (); Glpopmatrix ();} Draw leg void Draw_leg () {Glscalef (1, 1, 3); Glutsolidcube (1.0);} void Updateview (int width, int height) {glviewport (0,0,width,height);//Set Window size Glmatrixmode (gl_projection);// Set the matrix mode to projection glloadidentity ();//initialization matrix is the unit matrix float whratio = (glfloat) width/(glfloat) height; Set the display scale if (BPERSP) {gluperspective (45.0f, whratio,0.1f,100.0f);//Perspective projection//glfrustum ( -3, 3, -3, 3, 3,100);} else {Glorth O (-3, 3,-3, 3,-100,100); Positive projection}glmatrixmode (Gl_modelview); Set the matrix mode to model}void reshape (int width, int height) {if (height==0)//If the height is 0{height=1;//let height be 1 (avoid the phenomenon of denominator 0)}wheight = height ; Wwidth = Width;updateview (Wheight, wwidth); Update View}void idle () {glutpostredisplay ();//Call the current drawing function}void key (unsigned chAR k, int x, int y) {switch (k) {case 27:case ' Q ': {exit (0); Case ' P ': {bpersp =!BPERSP; Case ': {banim =!banim; Case ' O ': {bwire =!bwire; Case ' a ': {//Overall left shift eye[0] + = 0.2f;center[0] + = 0.2f;break;} Case ' d ': {//Overall right shift eye[0]-= 0.2f;center[0]-= 0.2f;break;} Case ' W ': {//Move up eye[1]-= 0.2f;center[1]-= 0.2f;break;} Case ' s ': {//whole Move Down eye[1] + = 0.2f;center[1] + = 0.2f;break;} Case ' Z ': {//whole forward eye[2]-= 0.2f;center[2]-= 0.2f;break;} Case ' C ': {//Overall back eye[2] + = 0.2f;center[2] + = 0.2f;break;} Case ' J ': {//ambient light left shifted light_x = Light_x-0.2f;break;} Case ' L ': {//ambient light shifted right light_x = light_x + 0.2f;break;} Case ' I ': {//ambient light moves up light_y = light_y + 0.2f;break;} Case ' K ': {//ambient light moves down light_y = light_y-0.2f;break;} Case ' n ': {//ambient light moves forward light_z = Light_z + 0.2f;break;} Case ' m ': {//ambient light shift light_z = light_z-0.2f;break;} Case ' R ': {//ambient light color switch iswhite =!iswhite;break;} Case ' F ': {//Spotlight left shift dir_x = Dir_x-0.05f;break;} Case ' h ': {//Spotlight right Shift dir_x = dir_x + 0.05f;break;} Case ' t ': {//Spotlight Move Up dir_y = Dir_y-0.05f;breaK;} Case ' G ': {//Spotlight down dir_y = Dir_y + 0.05f;break;} Case ' V ': {//spotlight-shifted dir_z = Dir_z-0.05f;break;} Case ' B ': {//Spotlight forward Dir_z = Dir_z + 0.05f;break;} Case ' X ': {//Spotlight angle becomes larger if (Spotangle <= 89.0f) {spotangle = Spotangle + 0.2f;} break;} Case ' Y ': {//Spotlight angle becomes smaller if (Spotangle >= 1.0f) {spotangle = spotangle-0.2f;} Break;}} Updateview (Wheight, wwidth);} void Redraw () {Glclear (Gl_color_buffer_bit | Gl_depth_buffer_bit); Clear the color cache and depth cache glloadidentity (); The initialization matrix is the unit matrix Glulookat (Eye[0], eye[1], eye[2],center[0], center[1], center[2],0, 1, 0);//Scene (0,0,0) of the Viewpoint Center (0,5,50), Y-Axis up if (Bwire) {Glpolygonmode (gl_front_and_back, gl_line);//Set Polygon drawing mode: Front and back, Linetype}else {glpolygonmode (gl_front_and_back , gl_fill);//Set Polygon drawing mode: positive and negative, fill}glenable (gl_depth_test);//Open depth Test glenable (gl_lighting); Turn on light mode//glfloat specular[] = {1.0f, 1.0f, 1.0f, 1.0f}; Glfloat light_pos[] = {5.0 + light_x, 5.0 + light_y,5.0 + light_z,1}; Define ambient light position glfloat light_pos1[] = {0.0f, 5.0f, 0.0f, 1.0f}; Define Spotlight Position Glfloat lightdir[] = {0.0f + dir_X, -1.0f + dir_y, 0.0f + dir_z}; Angle Glfloat white[] = {1.0f, 1.0f, 1.0f, 1.0f}; Define white if (Iswhite) {color[0] = 1.0f, color[1] = 1.0f, color[2] = 1.0f, color[3] = 1.0f;} else {color[0] = 0.0f, color[1] = 1.0f, color[2] = 0.0f, color[3] = 1.0f;} GLLIGHTFV (Gl_light0, gl_position, Light_pos); Set the light position of the No. 0 light source GLLIGHTFV (gl_light0, gl_specular, white); Set Specular light color GLLIGHTFV (gl_light0, Gl_diffuse, white); Set Diffuse light composition GLLIGHTFV (gl_light0, gl_ambient, color); Set the light color (ambient light color) glenable (gl_light0) after multiple reflections of light source number No. 0; Open the No. 0 light source GLLIGHTFV (gl_light1, gl_ambient, color); Set Ambient light composition GLLIGHTFV (gl_light1, gl_specular, white); Set Specular light composition GLLIGHTFV (gl_light1, Gl_diffuse, white); Set Diffuse light composition GLLIGHTFV (gl_light1, gl_position, LIGHT_POS1); GLLIGHTF (gl_light1, Gl_spot_cutoff, Spotangle); Cut Angle GLLIGHTFV (gl_light1, gl_spot_direction, Lightdir); Light source Direction GLLIGHTF (gl_light1, Gl_spot_exponent, 2.); Aggregation degree glenable (gl_light1); gLrotatef (frotate, 0, 1.0f, 0);//Rotate Glrotatef (-90, 1, 0, 0); Rotary Glscalef (0.2, 0.2, 0.2); Scale Draw_triangle ();//Draw Scene if (Banim) frotate + = 0.5f; Rotation factor Change glutswapbuffers (); Swap buffer}int Main (int argc, char *argv[]) {glutinit (&ARGC, argv);//Initialize GLUT Glutinitdisplaymode (Glut_rgba | glut_depth | glut_double);//Initialize display mode: RGB color model, depth test, double buffered glutinitwindowsize (480,480);//Set window size int windowhandle = Glutcreatewindow ("S Imple GLUT App ");//Set Window caption Glutdisplayfunc (redraw); Register Draw callback function Glutreshapefunc (reshape);//Register Redraw callback function Glutkeyboardfunc (key); Register key callback function Glutidlefunc (idle);//Register Global callback function: Call Glutmainloop () when idle; Glut Event processing loop return 0;}
[OpenGL] teapot and light