Basic illumination Calculation Formula

Source: Internet
Author: User
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 asFloat3Vector 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 wowould appear to be this color. the Emissive term can simulate glowing. figure 5-2 extends strates 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 wocould simulate how the emitted light affects the rest of the scene, but these models are beyond the scope of this book.

Figure 5-2 The Emissive term

Figure 5-3 rendering the emissive term

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

Emissive=Ke

Where:

  • KeIs the material's emissive color.
The ambient term

The ambient term accounts for light that has bounced CED und so much in the scene that it seems to come from everywhere. ambient light does not appear to come from any particle ction; rather, it appears to come from all directions ctions. because of this, the ambient lighting term does not depend on the light source position. figure 5-4 tables strates this concept, and figure 5-5 shows a rendering of an object that has es 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.

Figure 5-4 The ambient term

Figure 5-5 rendering the ambient term

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

Ambient=KaXGlobalambient

Where:

  • KaIs the material's ambient reflectance and
  • GlobalambientIs 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 ctions. in general, diffuse surfaces are rough on a microscopic scale, with small nooks and crannies that reflect light in limit directions ctions. when incoming rays of light hit these nooks and crannies, the light bounces off in all directions ctions, as shown in Figure 5-6.

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 participant point on a surface is the same, regardless of where the viewpoint is. figure 5-7 into strates the diffuse term, and figure 5-8 shows a rendering of a diffuse object.

Figure 5-7 the diffuse term

Figure 5-8 rendering the diffuse term

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

Diffuse=KDXLightcolorX max (N·L, 0)

Where:

  • KDIs the material's diffuse color,
  • LightcolorIs the color of the incoming diffuse light,
  • NIs the normalized surface normal,
  • LIs the normalized vector toward the light source, and
  • PIs the point being shaded.

Figure 5-9 calculating diffuse Lighting

The vector dot product (or Inner Product) of the normalized VectorsNAndLIs 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, soMax(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 ction. the specular term is most prominent on very smooth and shiny surfaces, such as polished metals. figure 5-10 extends strates the concept of specular reflection, and figure 5-11 shows a rendering of a completely specular object.

Figure 5-10 the specular term

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 determines es 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.

Figure 5-12 examples of different shininess Exponents

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

Specular=KSXLightcolorXFacingX (max (N·H, 0 ))Shininess

Where:

  • KSIs the material's specular color,
  • LightcolorIs the color of the incoming specular light,
  • NIs the normalized surface normal,
  • VIs the normalized vector toward the viewpoint,
  • LIs the normalized vector toward the light source,
  • HIs the normalized vector that is halfwayVAndL,
  • PIs the point being shaded, and
  • FacingIs 1 ifN·LIs greater than 0, and 0 otherwise.

Figure 5-13 calculating the specular term

When the angle between the view VectorVAnd the half-angle vectorHIs small, the specular appearance of the material becomes apparent. The exponentiation of the dot productNAndHEnsures that the specular appearance falls off quicklyHAndVMove farther apart.

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

Basic illumination PS code:

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;

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.