Game Development Reference
In-Depth Information
Final Fragment Color
The final fragment color is the sum of the AmbientTerm , DiffuseTerm , and S pecularTerm . The
gl_FragColor is a reserved variable within the fragment shader that returns the fragment color.
The final color vector values are in (r, g, b, alpha) format. Alpha is the transparency level that is active
if blending is enabled.
vec4 tempColor = (vec4(DiffuseTerm,1) + vec4(SpecularTerm,1) + vec4(AmbientTerm,1));
gl_FragColor = vec4(tempColor.r,tempColor.g, tempColor.b, 1);
Materials
The materials that an object is made of affect the color that it reflects and any color that it emits.
Here, I cover the Material class, along with how materials are used in the fragment shader.
The Material Class
An object can also have a material, which is represented by the Material class. This material has the
color components of
Emissive Color: Color of the light that is emitted from the object
Ambient Color: Color of the ambient light that is reflected by the material
Diffuse Color: Color of the diffuse light that is reflected by the material
Specular Color: Color of the specular light that is reflected by the material
Alpha: The degree of opaqueness of the material, with 1 being fully opaque and 0
being completely transparent
See Listing 4-45 for the implementation of the preceding list of color components.
Listing 4-45. Material Class's Data
private float[] m_Emissive = new float[3];
private float[] m_Ambient = new float[3];
private float[] m_Diffuse = new float[3];
private float[] m_Specular = new float[3];
private float m_Alpha = 1.0f;
The Material class also has functions to set and retrieve these private data items. Please refer to the
actual code from the Chapter 3 example for more details.
Materials in the Fragment Shader
Listing 4-46 shows the additions you have to make to the fragment shader code to add in an
object's material. The key additions are in bold. A new term, the EmissiveTerm , is used to derive the
final output color. The material's ambient, diffuse, and specular properties are used to calculate the
ambient, diffuse, and specular components of the final color. The material alpha value is used for the
alpha value of the final color.
 
Search WWH ::




Custom Search