基本的光照计算公式

简介: The Basic Lighting Model      OpenGL and Direct3D provide almost identical fixed-function lighting models.
The Basic Lighting Model

     OpenGL and Direct3D provide almost identical fixed-function lighting models. In our example, we will use a simplified version that we will refer to as the "Basic" model. The Basic model, like the OpenGL and Direct3D models, modifies and extends the classic Phong model. In the Basic model, an object's surface color is the sum of emissive, ambient, diffuse, and specular lighting contributions. Each contribution depends on the combination of the surface's material properties (such as shininess and material color) and the light source's properties (such as light color and position). We represent each contribution as a float3 vector that contains the red, green, and blue color components.

This high-level equation describes the Basic model mathematically:

surfaceColor = emissive + ambient + diffuse + specular

The Emissive Term

     The emissive term represents light emitted or given off by a surface. This contribution is independent of all light sources. The emissive term is an RGB value that indicates the color of the emitted light. If you were to view an emissive material in a completely dark room, it would appear to be this color. The emissive term can simulate glowing. Figure 5-2 illustrates the emissive term conceptually, and Figure 5-3 shows a rendering of a purely emissive object. The rendering is understandably boring, because the emissive color is the same all over the object. Unlike in the real world, an object's emissive glow does not actually illuminate other nearby objects in the scene. An emissive object is not itself a light source—it does not illuminate other objects or cast shadows. Another way to think of the emissive term is that it is a color added after computing all the other lighting terms. More advanced global illumination models would simulate how the emitted light affects the rest of the scene, but these models are beyond the scope of this book.

fig5_2.jpg

Figure 5-2 The Emissive Term

fig5_3.jpg

Figure 5-3 Rendering the Emissive Term

Here is the mathematical formulation we use for the emissive term:

emissive = Ke

where:

  • Ke is the material's emissive color.
The Ambient Term

      The ambient term accounts for light that has bounced around so much in the scene that it seems to come from everywhere. Ambient light does not appear to come from any particular direction; rather, it appears to come from all directions. Because of this, the ambient lighting term does not depend on the light source position. Figure 5-4 illustrates this concept, and Figure 5-5 shows a rendering of an object that receives only ambient light. The ambient term depends on a material's ambient reflectance, as well as the color of the ambient light that is incident on the material. Like the emissive term, the ambient term on its own is just a constant color. Unlike the emissive color, however, the ambient term is affected by the global ambient lighting.

fig5_4.jpg

Figure 5-4 The Ambient Term

fig5_5.jpg

Figure 5-5 Rendering the Ambient Term

Here is the mathematical formulation we use for the ambient term:

ambient = Ka x globalAmbient

where:

  • Ka is the material's ambient reflectance and
  • globalAmbient is the color of the incoming ambient light.
The Diffuse Term

    The diffuse term accounts for directed light reflected off a surface equally in all directions. In general, diffuse surfaces are rough on a microscopic scale, with small nooks and crannies that reflect light in many directions. When incoming rays of light hit these nooks and crannies, the light bounces off in all directions, as shown in Figure 5-6.

fig5_6.jpg

Figure 5-6 Diffuse Light Scattering

     The amount of light reflected is proportional to the angle of incidence of the light striking the surface. Surfaces with a dull finish, such as a dusty chalkboard, are said to be diffuse. The diffuse contribution at any particular point on a surface is the same, regardless of where the viewpoint is. Figure 5-7 illustrates the diffuse term, and Figure 5-8 shows a rendering of a diffuse object.

fig5_7.jpg

Figure 5-7 The Diffuse Term

fig5_8.jpg

Figure 5-8 Rendering the Diffuse Term

Here is the mathematical formulation we use for the diffuse term (illustrated in Figure 5-9):

diffuse = Kd x lightColor x max(N · L, 0)

where:

  • Kd is the material's diffuse color,
  • lightColor is the color of the incoming diffuse light,
  • N is the normalized surface normal,
  • L is the normalized vector toward the light source, and
  • P is the point being shaded.
fig5_9.jpg

Figure 5-9 Calculating Diffuse Lighting

    The vector dot product (or inner product) of the normalized vectors N and L is a measure of the angle between the two vectors. The smaller the angle between the vectors, the greater the dot-product value will be, and the more incident light the surface will receive. Surfaces that face away from the light will produce negative dot-product values, so the max(N · L, 0) in the equation ensures that these surfaces show no diffuse lighting.

The Specular Term

The specular term represents light scattered from a surface predominantly around the mirror direction. The specular term is most prominent on very smooth and shiny surfaces, such as polished metals. Figure 5-10 illustrates the concept of specular reflection, and Figure 5-11 shows a rendering of a completely specular object.

fig5_10.jpg

Figure 5-10 The Specular Term

fig5_11.jpg

Figure 5-11 Rendering the Specular Term

      Unlike the emissive, ambient, and diffuse lighting terms, the specular contribution depends on the location of the viewer. If the viewer is not at a location that receives the reflected rays, the viewer will not see a specular highlight on the surface. The specular term is affected not only by the specular color properties of the light source and material, but also by how shiny the surface is. Shinier materials have smaller, tighter highlights, whereas less shiny materials have highlights that are more spread out. Figure 5-12 shows some examples of shininess, with the shininess exponent increasing from left to right.

fig5_12.jpg

Figure 5-12 Examples of Different Shininess Exponents

Here is the mathematical formulation we use for the specular term (illustrated in Figure 5-13):

specular = Ks x lightColor x facing x (max(N · H, 0)) shininess

where:

  • Ks is the material's specular color,
  • lightColor is the color of the incoming specular light,
  • N is the normalized surface normal,
  • V is the normalized vector toward the viewpoint,
  • L is the normalized vector toward the light source,
  • H is the normalized vector that is halfway between V and L,
  • P is the point being shaded, and
  • facing is 1 if N · L is greater than 0, and 0 otherwise.
fig5_13.jpg

Figure 5-13 Calculating the Specular Term

     When the angle between the view vector V and the half-angle vector H is small, the specular appearance of the material becomes apparent. The exponentiation of the dot product of N and H ensures that the specular appearance falls off quickly as H and V move farther apart.

Additionally, the specular term is forced to zero if the diffuse term is zero because N · L (from diffuse lighting) is negative. This ensures that specular highlights do not appear on geometry that faces away from the light.

基本的光照的ps代码:

void C5E3f_basicLight(float4 position  : TEXCOORD0,

float3 normal : TEXCOORD1,


out float4 color : COLOR,


uniform float3 globalAmbient,

uniform float3 lightColor,

uniform float3 lightPosition,

uniform float3 eyePosition,

uniform float3 Ke,

uniform float3 Ka,

uniform float3 Kd,

uniform float3 Ks,

uniform float shininess)

{

float3 P = position.xyz;

float3 N = normalize(normal);


// Compute the emissive term

float3 emissive = Ke;


// Compute the ambient term

float3 ambient = Ka * globalAmbient;


// Compute the diffuse term

float3 L = normalize(lightPosition - P);

float diffuseLight = max(dot(N, L), 0);

float3 diffuse = Kd * lightColor * diffuseLight;


// Compute the specular term

float3 V = normalize(eyePosition - P);

float3 H = normalize(L + V);

float specularLight = pow(max(dot(N, H), 0),

shininess);

if (diffuseLight <= 0) specularLight = 0;

float3 specular = Ks * lightColor * specularLight;


color.xyz = emissive + ambient + diffuse + specular;

color.w = 1;

}


相关文章
|
7月前
|
存储 人工智能 算法
聚类的k值确定之轮廓系数
聚类的k值确定之轮廓系数
|
6天前
|
数据安全/隐私保护 Perl
批量计算地震波PGA/PGV/PGD、PSA/PSV/PSD、特征周期、卓越频率、Arias强度、特征强度、能量密度、Housner强度等30+参数
地震波格式转换、时程转换、峰值调整、规范反应谱、计算反应谱、计算持时、生成人工波、时频域转换、数据滤波、基线校正、Arias截波、傅里叶变换、耐震时程曲线、脉冲波合成与提取、三联反应谱、地震动参数、延性反应谱、地震波缩尺、功率谱密度
|
27天前
极值分析:分块极大值BLOCK-MAXIMA、阈值超额法、广义帕累托分布GPD拟合降雨数据时间序列
极值分析:分块极大值BLOCK-MAXIMA、阈值超额法、广义帕累托分布GPD拟合降雨数据时间序列
极值分析:分块极大值BLOCK-MAXIMA、阈值超额法、广义帕累托分布GPD拟合降雨数据时间序列
|
1月前
比例和比例定理
比例和比例定理
35 9
|
5月前
|
数据安全/隐私保护
滞回曲线处理器,骨架曲线,延性系数,耗能面积
为了批量处理拟静力试验得到的滞回曲线,计算骨架曲线,延性系数,耗能等指标,开发了“滞回曲线处理器”软件,具体功能介绍如下,软件在文末获取。
滞回曲线处理器,骨架曲线,延性系数,耗能面积
|
5月前
|
算法 数据可视化 C#
C# | Chaikin算法 —— 计算折线对应的平滑曲线坐标点
本文将介绍一种计算折线对应的平滑曲线坐标点的算法。该算法使用Chaikin曲线平滑处理的方法,通过控制张力因子和迭代次数来调整曲线的平滑程度和精度。通过对原始点集合进行切割和插值操作,得到平滑的曲线坐标点集合。实验结果表明,该算法能够有效地平滑折线,并且具有较高的精度和可控性。
67 0
C# | Chaikin算法 —— 计算折线对应的平滑曲线坐标点
|
9月前
|
算法 数据库 计算机视觉
舌象图片自适应调节——gamma校正算法(五)
舌象图片自适应调节——gamma校正算法(五)
144 0
|
11月前
计算一幅图像的信噪比
计算一幅图像的信噪比
420 0
失真函数、失真矩阵与平均失真
失真函数、失真矩阵与平均失真
168 0
|
BI
信息率失真函数与平均互信息
信息率失真函数与平均互信息
143 0