glsl,opengl关于镜面光的计算,包括镜面高光

GLSL Tutorial


OpenGL Directional Lights II


Time for the specular component of the OpenGL directional light. The lighting model used is the Blinn-Phong model, which is a simplification of the Phong model. We shall take a peek at the Phong model since it makes it easier to understand the Blinn-Phong model.

//两个光照模型,BlinnPhong模型和Phong模型,前后是后者的简化版
The Phong model says that the specular component is proportional to the cosine between the light reflection vector and the eye vector. The following image shows this graphically:

L is the vector from the light to the vertex being shaded. N is the normal vector, and Eye is the vector from the vertex to the eye, or camera. R is the vector L mirror reflected on the surface. The specular component is proportional to the cosine of alpha.
L是从灯光的位置到顶点位置的向量,N是平面的法向量,Eye是从顶点到眼睛或者相机的向量,R是L关于N对称的向量

If the eye vector coincides with the reflection vector then we get the maximum specular intensity. As the
//如果eye向量和R向量相等,那么得到最大的镜面反光指数(也就是说,镜面高光与眼睛的位置有关,eye和R越相似,则镜面越亮)
eye vector diverges from the reflection vector the specular intensity decays. The rate of decay is controlled by a shininess factor. The higher the shininess factor the faster the decay. This means that with a high shininess the bright spot caused by the specular component is smaller than with a low shininess value. Simply put, the shininess (a value between 0 and 128 in OpenGL) controls the size of the bright spot.
Shininess越小,则越亮,简单的说,shininess控制了亮斑的大小

Shininess = 8 Shininess = 64 Shininess = 128

The formula for the reflection vector is as follows:

 

上式由平形四边形法则得到,各个分量都是单位向量
And the specular component in OpenGL using the Phong model would be:

 

如果用Phong模型,则镜面艳色的计算结果如下:R是灯光方向关于N的对称向量,Eye是顶点到眼睛或者相机位置的向量,s为高亮指数,
Where the s exponent is the shininess value, Ls is the lights specular intensity, and Ms is the materials specular coefficient.

Blinn proposed a simpler and faster model, knows as the Blinn-Phong model that is based on the half-vector. The half-vector is a vector with a direction half-way between the eye vector and the light vector as shown in the following figure:
half-way向量是L和eye的平均值,即H向量
R*EYe近似于N*H

The intensity of the specular component is now based on the cosine of the angle between the half vector and the normal. The formula for the half-vector is much simpler than for the reflection vector:

And the specular component in OpenGL using the Blinn-Phong model is:

This is the actual stuff as commonly used in the fixed pipeline of the graphics hardware. Since we want to emulate the OpenGL's directional light, we're going to use this last equation in our shader. There is a good news: OpenGL computes the half-vector for us! So the following snippet of code should do the trick:


	/* compute the specular term if NdotL is  larger than zero */

	if (NdotL > 0.0) {

	

		// normalize the half-vector, and then compute the 

		// cosine (dot product) with the normal

		NdotHV = max(dot(normal, gl_LightSource[0].halfVector.xyz),0.0);

		specular = gl_FrontMaterial.specular * gl_LightSource[0].specular * 

				pow(NdotHV,gl_FrontMaterial.shininess);

	}

你可能感兴趣的:(OpenGL)