MATLAB: How do the AmbientStrength, DiffuseStrength, and SpecularStrength properties of the MATERIAL command affect an object in MATLAB 7.1 (R14SP3)

ambientstrengthdiffusestrengthlightMATLABscenespecularstrength

AmbientStrength, DiffuseStrength, and SpecularStrength are properties of the MATERIAL command that sets the lighting characteristics of SURFACE and PATCH objects. I want to know how these properties affect lighting calculations and rendering of those objects.

Best Answer

The color of a point drawn in MATLAB is defined by the following equation, which uses those property intensities:
color = clamp(amb + diff + spec)
where
 
amb = ka * Cm
diff = sum(kd * (N.*L) * Cm * Cl, lights)
spec = sum(ks * (R.*V)^exp * Cs * Cl, lights)
diff and spec are not functions in this pseudocode. Diff is an abbreviation for diffusion, and spec is an abbreviation for specular color contributions to vertex color. The use of sum(...) in the pseudocode is meant to indicate that the expressions are computed for each light source in your scene, and then summed together. So you can think of lights as a vector of multiple light objects.
In particular,
diff = sum(kd * (N.*L) * Cm * Cl, lights)
is meant to indicate code equivalent to:
 
Diff = 0;
For z = 1:length(lights)
Light_source = lights(1)
Cl = get(LightSource,Color)
Diff = Diff + (kd * (N.*L) * Cm * Cl); %this equation will be done for every component of the color vectors
End
The variables have the following meaning:
ka = AmbientStrength => [0:1]
kd = DiffuseStrength => [0:1]
ks = SpecularStrength => [0:1]
Cm is the color of the object. Normally, Cm should be represented in the RGB (Red Green Blue) format. Cm is a vector containing three elements, where first element represents red color intensity, second element represents green color intensity and third element represents blue color intensity. The values for these colors would be in range from [0 to 1], with zero meaning no intensity and 1 meaning full intensity. [0,0,0] is black, [1,1,1] is white. Gray colors are achieved by keeping the values of all the three components equal, e.g., [0.2, 0.2, 0.2] or [0.3, 0.3, 0.3].
Cl is the color of the light. Cl vector is the color of the light and its values are also represented in the RGB format. Since you are using grayscale the light source should be white and be represented by the values of [1,1,1]. To obtain the color of the light you can use the GET function to obtain the Color property of the light object.
Cs is the "specular color". This is the same as Cm when SpecularColorReflectance == 0, [1 1 1] when SpecularColorReflectance == 1, and linear interpolation between.
N is the vertex normal vector at each point of surface (it is stored for each vertex on surface). MATLAB allows you to access the normal vectors for all vertices of the surface using the ‘VertexNormals’ property. These vectors are stored in a NxMx3 matrix, where N is number of vertices in X direction, M is the number of vertices in Y direction. The third dimension of the matrix stores the [X,Y,Z] components of the normal vectors. There is a one to one correspondence between vertices of the surface and Vertex normal vectors.
L is the vector between the location of the light and the vertex for which you are computing the color. To obtain the location of the light you can use the GET function to obtain the Position property of the light object. The computation of the light vector will be dependent on the setting of the Style property of the light. For example, for the ‘local’ light Style, vector L will be computed as (VertexPosition – LightPosition). Please refer to the light documentation for more information on the Style and Position properties:
You may access the same page locally by typing the following at the MATLAB prompt:
web([docroot,'/techdoc/ref/light_props.html'])
R is the reflection of the light vector L through the normal vector N, i.e., it is the reflection vector of the light by the surface represented by vector N. There are many resources available online that explain this equation. For your convenience I am attaching a link to description on Wikipedia:
V is the view vector, which is a vector that describes the position from which the camera is looking at a scene and the orientation that the camera has. In MATLAB you can compute the viewpoint vector by using the CAMTARGET and CAMPOS functions in MATLAB. The viewpoint can be computed as CAMTARGET – CAMPOS;
For more information on CAMTARGET and CAMPOS type:
>> doc campos
>> doc camtarget
on the MATLAB command prompt.
This lighting model is the same for the Z-Buffer & OpenGL renderers.
Some more information on lighting models:
This lighting model is sometimes called the Phong lighting model (not to be confused with the Phong shading model). There's also the Blinn lighting model which computes the specular term with the dot product of the "half vector" and the normal. Blinn & Phong look very similar (modulo a factor of 2 in the exponent), except for cases where the light & view vectors are nearly tangent to the surface. Neither the Phong or Blinn papers had the specular color term originally. That is a correction that comes from the Cook-Torrance paper to account for the dielectric properties of the material. The actual Cook-Torrance lighting model is a bit different though. It's similar to the Blinn one with a correction based on the statistical distribution of the microfacets. After that is the Ward anisotropic model which allows for cases where the microfacets are oriented. After that there are many techniques based on measured BRDFs (Bidirectional Reflectance Distribution Function).