OpenGL Lighting and Colors
Transferred from: http://www.cnblogs.com/kekec/archive/2011/08/16/2140789.html
The generation of model colors in OpenGL scenes is described in the following flowchart: + +
++++++ ++++++ ++++++ ++++++
(1) When the light is not turned on, the vertex color is used to produce the color of the entire surface.
With Glshademodel , you can set how the color of the interior pixels of the surface is generated. Gl_flat/gl_smooth.
++
(2) In general, after the light is turned on, at least one light source must be in the scene (Gl_light0 ... GL_LIGHT7)
The specified light source is turned on and off by glenable(gl_light0) gldisable(gl_light0).
Glfloat gambient[] = {0.60,60,61.0};GLLIGHTMODELFV (gl_ Light_model_ambient, gambient);
Global Ambient Light
++
(3) Setting the light component of the light source-ambient light/diffuse/specular light
By default, Gl_light0 ... The gl_ambient value of GL_LIGHT7 is (0.0, 0.0, 0.0, 1.0);
The Gl_diffuse and Gl_specular values for Gl_light0 are (1.0, 1.0, 1.0, 1.0),
Gl_light1 ... The Gl_diffuse and Gl_specular values for GL_LIGHT7 are (0.0, 0.0, 0.0, 0.0).
Glfloat lightambient[] = {1.01.01.01.0}; Glfloat lightdiffuse[] = {1.01.01.01.0= {0.5 0.5 0.5 1.0 };GLLIGHTFV (gl_light0, gl_ambient, lightambient); GLLIGHTFV (Gl_light0, Gl_diffuse, Lightdiffuse); GLLIGHTFV (GL_ LIGHT0, Gl_specular, lightspecular);
++
(4) Setting the position and orientation of the light source
-- Parallel light --no position, only direction
12 |
GLfloat lightPosition[] = {8.5, 5.0, -2.0, 0.0}; // w=0.0 glLightfv(GL_LIGHT0, GL_POSITION, lightPosition); |
-- Point Light --there is no direction in position
12 |
GLfloat lightPosition[] = {8.5, 5.0, -2.0, 1.0}; // w不为0 glLightfv(GL_LIGHT0, GL_POSITION, lightPosition); |
-- Spotlight --there's a position and direction.
12345 |
GLfloat lightPosition[] = {-6.0, 1.0, 3.0, 1.0}; // w不为0 glLightfv(GL_LIGHT0, GL_POSITION, lightPosition); GLfloat lightDirection[] = {1.0, 1.0, 0.0}; glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, lightDirection); // 聚光灯主轴方向 spot direction glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 45.0); // cutoff角度 spot cutoff |
* * Parallel light does not decay as distance d increases, but the point light and Spotlight decay.
Attenuation is the decay coefficient, the higher the system value, the faster the decay.
By default, c=1.0, l=0.0, q=0.0
123 |
glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 2.0); // c 系数 glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 1.0); // l 系数 glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, 0.5); // q 系数 |
++
(5) Setting material properties
Ambient light and scattered light are not affected by the position of the viewpoint. What is the color of the object, to a large extent by the impact of scattered light, ambient light reflection also has a certain effect on the color of the object.
Because when the light is direct, the scattering light is strongest, and the ambient light is obvious when it is non-direct. For objects in the real world, the scattered light is usually the same color as the ambient light.
Specular reflection of an object produces a highlighted area on the surface of the object. The specular reflection that the observer sees depends on the position of the viewpoint-the highest brightness in the direction of the reflected light.
By default, the Gl_ambient value of the material is (0.2, 0.2, 0.2, 1.0), and the Gl_diffuse value is (0.8, 0.8, 0.8, 1.0);
The Gl_specular value is (0.0, 0.0, 0.0, 1.0), gl_shininess value is 0.0 "The value range is [0.0, 128.0], the larger the value, the smaller the highlight area, the higher the Brightness";
Gl_gl_emission values are (0.0, 0.0, 0.0, 1.0)
123456789101112 |
GLfloat matAmbient[] = {0.6, 0.6, 0.6, 1.0};
GLfloat matDiffuse[] = {0.35, 0.35, 0.35, 1.0};
GLfloat matAmbDif[] = {0.5, 0.5, 0.5, 1.0};
GLfloat matSpecular[] = {0.2, 0.2, 0.2, 1.0};
GLfloat shine[] = {5.0};
GLfloat matEmission[] = {0.3, 0.1, 0.1, 1.0};
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, matAmbient);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, matDiffuse);
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, matAmbDif);
// 将背景颜色和散射颜色设置成同一颜色
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, matSpecular);
glMaterialfv(GL_FRONT, GL_SHININESS, shine);
glMaterialfv(GL_FRONT, GL_EMISSION, matEmission);
// 用来模拟物体发光的效果,但这不是光源
|
++
(6) Color material mode
Using a color material can quickly change the color of a model in a scene with a small cost. Its specific usage is as follows:
12345678 |
glEnable(GL_COLOR_MATERIAL); glColorMaterial(GL_FRONT, GL_DIFFUSE); glColor3f(0.2, 0.5, 0.8); /*** 绘制一些物体 ***/ glColorMaterial(GL_FRONT, GL_SPECULAR); glColor3f(0.9, 0.1, 0.3); /*** 绘制另外一些物体 ***/ glDisable(GL_COLOR_MATERIAL); |
++
(7) Reference
http://fly.cc.fer.hr/~unreal/theredbook/
Http://www.john-chapman.net/content.php?id=3
Http://www.sjbaker.org/steve/omniv/opengl_lighting.html
http://blog.csdn.net/gamesdev/article/details/9796715
OpenGL Lighting and Colors