Computer Modeling, Simulation, and Visualization - Lighting and Materials

Computer Modeling, Simulation, and Visualization - Lighting and Materials
Slide Note
Embed
Share

This content delves into classic lighting models, three types of light in detail, and the use of vertex and fragment shaders in computer modeling and simulation. It covers ambient, diffuse, and specular lighting components, along with examples of shaders with no lighting effects.

  • Computer Modeling
  • Simulation
  • Visualization
  • Lighting
  • Materials

Uploaded on Mar 06, 2025 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. EECS 700: Computer Modeling, Simulation, and Visualization Dr. Shontz Chapter 3: Lighting and Materials

  2. Classic Lighting Model Adds up set of independently computed lighting components to get total lighting effect Adds up set of independently computed lighting components to get total lighting effect Three components: Three components: ambient, diffuse, and specular light ambient, diffuse, and specular light

  3. Three Types of Light (in more detail) Ambient light Ambient light doesn t come from a particular direction. doesn t come from a particular direction. Is constant throughout the scene. scene. Is constant throughout the Diffuse light Diffuse light light that is scattered by the surface equally in all directions for a light that is scattered by the surface equally in all directions for a particular light source. particular light source. Computation depends on direction of surface normal and Computation depends on direction of surface normal and direction of light source. direction of light source. Specular highlighting Specular highlighting light reflected directly by the surface. Refers to how much the light reflected directly by the surface. Refers to how much the surface material acts like a mirror. surface material acts like a mirror. Strength of specular highlighting Strength of specular highlighting depends on angle and is known as and is known as shininess shininess. . Computation depends on direction of surface normal, Computation depends on direction of surface normal, direction of light source, and direction of eye. direction of light source, and direction of eye. Might or might not incorporate surface color. color. depends on angle Might or might not incorporate surface

  4. Vertex and Fragment Shaders: No Lighting No lighting: No lighting: Just draw objects with color unmodulated by lighting effects. Base to Just draw objects with color unmodulated by lighting effects. Base to bulid bulidon. on. Example 7.1: Setting Final Color Values with No Lighting Example 7.1: Setting Final Color Values with No Lighting // Vertex // Vertex shader shader with no lighting with no lighting #version 330 core #version 330 core u uniform mat4 niform mat4 MVPMatrix MVPMatrix; // model ; // model- -view i in vec4 n vec4 VertexColor VertexColor; ; i in vec4 n vec4 VertexPosition VertexPosition; ; out vec4 Color; out vec4 Color; // sent to the rasterizer for interpolation // sent to the rasterizer for interpolation v void main() oid main() { { Color = Color = VertexColor VertexColor; ; gl_Position gl_Position= = MVPMatrix MVPMatrix* * VertexPosition } } view- -projection transform projection transform // sent from the application, includes alpha // sent from the application, includes alpha // pre // pre- -transformed position transformed position VertexPosition; ;

  5. Example 7.1 (continued) // Fragment // Fragment shader shader with no lighting with no lighting #version 330 core #version 330 core i in vec4 Color; n vec4 Color; // interpolated between vertices // interpolated between vertices o out vec4 ut vec4 FragColor FragColor; ; // color result for this fragment // color result for this fragment v void main() oid main() { { FragColor FragColor = Color; = Color; } }

  6. Vertex and Fragment Shaders: Ambient Lighting Example 7.2: Ambient Example 7.2: Ambient Lightint // Vertex // Vertex shader shader for ambient light #version 330 core #version 330 core u uniform mat4 niform mat4 MVPMatrix MVPMatrix; ; i in vec4 n vec4 VertexColor VertexColor; ; in vec4 in vec4 VertexPosition VertexPosition; ; o out vec4 Color; ut vec4 Color; Lightint for ambient light v void main() oid main() { { Color = Color = VertexColor VertexColor; ; gl_Position gl_Position= = MVPMatrix MVPMatrix* * VertexPosition VertexPosition; ; // the light is considered constant // the light is considered constant // throughout the scene // throughout the scene } }

  7. Example 7.2 (continued) // Fragment // Fragment shader #version 330 core #version 330 core u uniform vec4 Ambient; niform vec4 Ambient; // sets lighting level, same across many vertices // sets lighting level, same across many vertices shader for global ambient lighting for global ambient lighting i in vec4 Color; n vec4 Color; o out vec4 ut vec4 FragColor FragColor; ; v void main() oid main() { { vec4 vec4 scatterLight scatterLight = Ambient; // modulate surface color with light, but saturate at white // modulate surface color with light, but saturate at white FragColor FragColor = min(Color * = min(Color * scatterLight scatterLight, vec4(1.0)); = Ambient; // this is the only light // this is the only light , vec4(1.0)); } }

  8. Dealing with the alpha value If you are using a If you are using a vec4 for color vec4 for color, set , set alpha = 1.0 alpha = 1.0 (as the default value). (as the default value). Alternatively, if you want to use Alternatively, if you want to use vec3 for color vec3 vec3 scatteredLight scatteredLight = vec3(Ambient); vec3 vec3 rgb rgb = min( = min(Color.rgb Color.rgb * * scatteredLight FragColor FragColor = vec4( = vec4(rgb vec3 for color: : = vec3(Ambient); // this is the only light // this is the only light scatteredLight, vec3(1.0)); rgb, , Color.a Color.a); ); // convert to vec4 for fragment color // convert to vec4 for fragment color , vec3(1.0));

  9. Directional Light Source Lighting Directional Light Source Lighting: Directional Light Source Lighting: Far away Far away light light Same direction Same direction from every point on surface from every point on surface Keep calculations in the range [ Keep calculations in the range [- -90 90o o, 90 , 90o o] ] Example 7.3: Directional Light Source Lighting Example 7.3: Directional Light Source Lighting // Vertex // Vertex shader shader for a directional light computed in the fragment for a directional light computed in the fragment shader #version330 core #version330 core shader u uniform mat4 niform mat4 MVPMatrix u uniform mat3 niform mat3 NormalMatrix MVPMatrix; ; NormalMatrix; // to transform normal vectors, pre ; // to transform normal vectors, pre- -perspective perspective i in vec4 n vec4 VertexColor VertexColor; ; i in vec3 n vec3 VertexNormal VertexNormal; ; o out vec4 Color; ut vec4 Color; o out vec3 Normal; ut vec3 Normal; // surface normal is now needed // surface normal is now needed // interpolate the normalized surface normal // interpolate the normalized surface normal

  10. Example 7.3 (continued) void main () { void main () { Color = Color = VertexColor VertexColor; ; // transform the normal, without perspective, and normalize it // transform the normal, without perspective, and normalize it Normal = normalize( Normal = normalize(NormalMatrix NormalMatrix* * VertexNormal gl_Position gl_Position= = MVPMatrix MVPMatrix* * VertexPosition VertexPosition; ; } } VertexNormal); ); // Fragment // Fragment shader #version 330 core #version 330 core shadercomputing lighting for a directional light computing lighting for a directional light u uniform vec3 Ambient; niform vec3 Ambient; u uniform vec3 niform vec3 LightColor uniform vec3 uniform vec3 LightDirection uniform vec3 uniform vec3 HalfVector uniform float Shininess; uniform float Shininess; uniform float Strength; uniform float Strength; LightColor; ; LightDirection; ; HalfVector; ; // direction toward the light // direction toward the light // surface orientation for shiniest spots // surface orientation for shiniest spots // exponent for sharping highlights // exponent for sharping highlights // extra factor to adjust shininess // extra factor to adjust shininess in vec4 Color; in vec4 Color; in vec3 Normal; in vec3 Normal; // surface normal, interpolated between vertices // surface normal, interpolated between vertices out vec4 out vec4 FragColor FragColor; ;

  11. Example 7.3 (continued) v void main() oid main() { { // compute cosine of the directions, using dot products to see how much light would be reflected // compute cosine of the directions, using dot products to see how much light would be reflected float diffuse = max(0.0, dot(Normal, float diffuse = max(0.0, dot(Normal, LightDirection LightDirection)); )); float specular = max(0.0, dot(Normal, float specular = max(0.0, dot(Normal, HalfVector HalfVector)); )); // surfaces facing away from the light (negative dot products) won t be lit by directional light // surfaces facing away from the light (negative dot products) won t be lit by directional light if (diffuse == 0.0) if (diffuse == 0.0) specular = 0.0; specular = 0.0; else else specular = pow(specular, Shininess); specular = pow(specular, Shininess);// sharpen the highlight // sharpen the highlight vec3 vec3scatteredLight scatteredLight = Ambient + vec3 vec3 reflectedLight reflectedLight = = LightColor = Ambient + LightColor LightColor* specular * Strength; * specular * Strength; LightColor * diffuse; * diffuse; // don t modulate the underlying color with reflected light // don t modulate the underlying color with reflected light only with scattered light vec3 vec3 rgb rgb= min( = min(Color.rgb Color.rgb* * scatteredLight scatteredLight + + reflectedLight FragColor FragColor = vec4( = vec4(rgb rgb, , Color.a Color.a); ); only with scattered light , vec3(1.0)); reflectedLight, vec3(1.0)); } }

  12. Point Light Sources Point light sources: Point light sources: Lights Lights near or within scene near or within scene For point lights, the For point lights, the direction of light is different for each direction of light is different for each point on the surface point on the surface. Light received at surface is expected . Light received at surface is expected to decrease as the surface gets farther and farther from to decrease as the surface gets farther and farther from the light. the light. Attenuation: Attenuation: Fading of reflected light based on increasing distance Fading of reflected light based on increasing distance Light attenuates as the square of the distances. Light attenuates as the square of the distances.

  13. Example 7.4: Point-Light Source Lighting // Vertex // Vertex shader // fragment // fragment shader shader for a point for a point- -light (local) lighting source, with computation done in the light (local) lighting source, with computation done in the shader #version 330 core #version 330 core u uniform mat4 niform mat4 MVPMatrix u uniform matr4 niform matr4 MVMatrix u uniform mat3 niform mat3 NormalMatrix MVPMatrix; ; MVMatrix; ; NormalMatrix; ; // now need the transform, minus perspective // now need the transform, minus perspective i in vec4 n vec4 VertexColor VertexColor; ; i in vec3 n vec3 VertexNormal VertexNormal; ; i in vec4 n vec4 VertexPosition VertexPosition; ; o out vec4 Color; ut vec4 Color; o out vec3 Normal; ut vec3 Normal; o out vec4 Position; ut vec4 Position; // adding position, so we know where we are // adding position, so we know where we are

  14. Example 7.4: Point-Light Source Lighting (continued) v void main() oid main() { { Color = Color = VertexColor VertexColor; ; Normal = normalize( Normal = normalize(NormalMatrix Position = Position = MVMatrix MVMatrix * * VertexPosition gl_Position gl_Position = = MVPMatrix MVPMatrix * * VertexPosition NormalMatrix * * VertexNormal VertexPosition; ; VertexPosition; ; VertexNormal); ); // pre // pre- -perspective space perspective space // includes perspective // includes perspective } } // Fragment // Fragment shader #version 330 core #version 330 core u uniform vec3 Ambient; niform vec3 Ambient; u uniform vec3 niform vec3 LightColor u uniform vec3 niform vec3 LightPosition u uniform float Shininess; niform float Shininess; u uniform float Strength; niform float Strength; shader computing a point computing a point- -light (local) source light light (local) source light LightColor; ; LightPosition; ; // location of the light, eye space // location of the light, eye space

  15. Example 7.4: Point-Light Source Lighting (continued) u uniform vec3 niform vec3 EyeDirection u uniform float niform float ConstantAttenuation ConstantAttenuation; ; u uniform float niform float LinearAttenuation LinearAttenuation; ; u uniform float niform float QuadraticAttenuation QuadraticAttenuation; ; EyeDirection; ; // attenuation coefficients // attenuation coefficients i in vec4 Color; n vec4 Color; i in vec3 Normal; n vec3 Normal; i in vec4 Position; n vec4 Position; v void main() oid main() { { // find direction and distance of the light which changes // find direction and distance of the light which changes fragmentwise vec3 vec3 lightDirection lightDirection = = LightPosition LightPosition vec3(Position); float float lightDistance lightDistance = length( = length(lightDirection lightDirection); ); fragmentwise for local light for local light vec3(Position);

  16. Example 7.4: Point-Light Source Lighting (continued) // normalize the light direction vector so dot products yield cosines // normalize the light direction vector so dot products yield cosines lightDirection lightDirection = = lightDirection lightDirection / / lightDistance lightDistance; ; // model how much light is available for this fragment // model how much light is available for this fragment float attenuation = 1.0 / float attenuation = 1.0 / ( (ConstantAttenuation ConstantAttenuation+ + LinearAttenuation LinearAttenuation* * lightDistance QuadraticAttenuation QuadraticAttenuation * * lightDistance lightDistance + + lightDistance * * lightDistance lightDistance); ); // direction of maximum highlight changes per fragment // direction of maximum highlight changes per fragment vec3 vec3 halfVector halfVector = normalize( = normalize(lightDirection lightDirection+ + EyeDirection EyeDirection); ); float diffuse = max(0.0, dot(Normal, float diffuse = max(0.0, dot(Normal, lightDirection float specular = max(0.0, dot(Normal, float specular = max(0.0, dot(Normal, halfVector lightDirection)); )); halfVector)); ));

  17. Example 7.4: Point-Light Source Lighting (continued) if (diffuse == 0.0) if (diffuse == 0.0) specular = 0.0; specular = 0.0; else else specular = pow(specular, Shininess) * Strength; specular = pow(specular, Shininess) * Strength; vec3 vec3 scatteredLight scatteredLight = Ambient + vec3 vec3 reflectedLight reflectedLight= = LightColor vec3 vec3 rgb rgb = min( = min(Color.rgb Color.rgb * * scatteredLight vec3(1.0)); vec3(1.0)); FragColor FragColor = vec4( = vec4(rgb rgb, , Color.a = Ambient + LightColor LightColor * specular * attenuation; * specular * attenuation; scatteredLight + + reflectedLight LightColor * diffuse * attenuation; * diffuse * attenuation; reflectedLight), ), Color.a); ); } } Note: Can leave out some of the attenuation terms. Can also attenuate the Ambient term. Note: Can leave out some of the attenuation terms. Can also attenuate the Ambient term. Attenuate per Attenuate per- -light ambient colors. Could also attenuate global ambient color. light ambient colors. Could also attenuate global ambient color.

  18. Spotlights Spotlights: Spotlights: project cone of light in a particular direction. cone of light in a particular direction. project strong beam of light strong beam of light that illuminates a well that illuminates a well- -defined area. defined area. Produce a Produce a Example 7.5: Spot Lighting Example 7.5: Spot Lighting // Vertex // Vertex shader shader for spotlight computed in fragment for spotlight computed in fragment shader #version 330 core #version 330 core shader uniform mat4 uniform mat4 MVPMatrix u uniform mat4 niform mat4 MVMatrix u uniform mat3 niform mat3 NormalMatrix MVPMatrix; ; MVMatrix; ; NormalMatrix; ; i in vec4 n vec4 VertexColor VertexColor; ; i in vec3 n vec3 VertexNormal VertexNormal; ; i in vec4 n vec4 VertexPosition VertexPosition; ;

  19. Example 7.5 (continued) out vec4 Color; out vec4 Color; o out vec3 Normal; ut vec3 Normal; o out vec4 Position; ut vec4 Position; v void main() oid main() { { Color = Color = VertexColor VertexColor; ; Normal = normalize( Normal = normalize(NormalMatrix Position = Position = MVMatrix MVMatrix * * VertexPosition gl_Position gl_Position = = MVPMatrix MVPMatrix * * VertexPosition NormalMatrix * * VertexNormal VertexPosition; ; VertexPosition; ; VertexNormal); ); } }

  20. Example 7.5 (continued) // Fragment // Fragment shader #version 330 core #version 330 core shader computing a spotlight s effect computing a spotlight s effect u uniform vec3 Ambient; niform vec3 Ambient; u uniform vec3 niform vec3 LightColor u uniform vec3 niform vec3 LightPosition u uniform float Shininess; niform float Shininess; u uniform float Strength; niform float Strength; LightColor; ; LightPosition; ; u uniform vec3 niform vec3 EyeDirection u uniform float niform float ConstantAttenuation ConstantAttenuation; ; u uniform float niform float LinearAttenuation LinearAttenuation; ; u uniform float niform float QuadraticAttenuation QuadraticAttenuation; ; EyeDirection; ;

  21. Example 7.5 (continued) i in vec4 Color; n vec4 Color; i in vec3 Normal; n vec3 Normal; in vec4 Position; in vec4 Position; o out vec4 ut vec4 FragColor FragColor; ; v void main() oid main() { { vec3 vec3 lightDirection lightDirection = = LightPosition float float lightDistance lightDistance = length( lightDirection lightDirection = = lightDirection lightDirection / / lightDistance LightPosition vec3(Position); = length(lightDirection lightDirection); ); lightDistance; ; vec3(Position);

  22. Example 7.5 (continued) float attenuation = 1.0 / float attenuation = 1.0 / ( (ConstantAttenuation ConstantAttenuation+ + LinearAttenuation LinearAttenuation* * lightDistance QuadraticAttenuation QuadraticAttenuation* * lightDistance lightDistance + + lightDistance * * lightDistance lightDistance); ); // how close are we to being in the // how close are we to being in the splot float float spotCos spotCos = dot( = dot(lightDirection lightDirection, , - -ConeDirection splot? ? ConeDirection); ); // attenuate more, based on spot // attenuate more, based on spot- -relative position if ( if (spotCos spotCos < < spotCosCutoff spotCosCutoff) ) attenuation = 0.0; attenuation = 0.0; else else attenuation *= pow( attenuation *= pow(spotCos relative position spotCos, , SpotExponent SpotExponent); );

  23. Example 7.5 (continued) vec3 vec3 halfVector halfVector = normalize( = normalize(lightDirection lightDirection+ + E EyeDirection yeDirection); ); float diffuse = max(0.0, dot(Normal, float diffuse = max(0.0, dot(Normal, lightDirection float specular = max(0.0, dot(Normal, float specular = max(0.0, dot(Normal, halfVector lightDirection)); )); halfVector)); )); if (diffuse = 0.0) if (diffuse = 0.0) specular = 0.0; specular = 0.0; else else specular = pow(specular, Shininess) * Strength; specular = pow(specular, Shininess) * Strength; vec3 vec3 scatteredLight scatteredLight = Ambient + vec3 vec3 reflectedLight reflectedLight = = LightColor vec3 vec3 rgb rgb = min( = min(Color.rgb Color.rgb * * scatteredLight FragColor FragColor = vec4( = vec4(rgb rgb, , Color.a = Ambient + LightColor LightColor * specular * attenuation; * specular * attenuation; scatteredLight + + reflectedLight Color.a); ); LightColor * diffuse * attenuation; * diffuse * attenuation; reflectedLight, vec3(1.0)); , vec3(1.0)); } }

  24. Omit: Example 7.6 Omit: Example 7.6 Moving calculations to Moving calculations to vertex vertex shader shader Note: There is flexibility as to whether the Note: There is flexibility as to whether the calculations appear in the vertex calculations appear in the vertex shader the fragment the fragment shader shader. . shaderor or

  25. Example 7.8: Multiple Mixed Light Sources // Vertex // Vertex shader #version 330 core #version 330 core shader for multiple lights stays the same with all lighting done in the vertex for multiple lights stays the same with all lighting done in the vertex shader shader u uniform mat4 niform mat4 MVPMatrix uniform mat4 uniform mat4 MVMatrix u uniform mat3 niform mat3 NormalMatrix MVPMatrix; ; MVMatrix; ; NormalMatrix; ; i in vec4 n vec4 VertexColor VertexColor; ; i in vec3 n vec3 VertexNormal VertexNormal; ; i in vec4 n vec4 VertexPosition VertexPosition; ; o out vec4 Color; ut vec4 Color; o out vec3 Normal; ut vec3 Normal; o out vec4 Position; ut vec4 Position;

  26. Example 7.8 (continued) v void main() oid main() { { Color = Color = VertexColor VertexColor; ; Normal = normalize( Normal = normalize(NormalMatrix Position = Position = MVMatrix MVMatrix * *VertexPosition gl_Position gl_Position = = MVPMatrix MVPMatrix * * VertexPosition NormalMatrix * * VertexNormal VertexPosition; ; VertexPosition; ; VertexNormal); ); } } // Fragment // Fragment shader #version 330 core #version 330 core shader for multiple lights for multiple lights

  27. Example 7.8 (continued) s struct truct LightProperties LightProperties { { bool bool isEnabled bool bool isLocal bool bool isSpot vec3 ambient; vec3 ambient; vec3 color; vec3 color; vec3 position; vec3 position; vec3 vec3 halfVector vec3 vec3 coneDirection coneDirection; ; float float splotCosCutoff splotCosCutoff; ; float float spotExponent spotExponent; ; float float constantAttenuation constantAttenuation; ; float float linearAttenuation linearAttenuation; ; float float quadraticAttenuation quadraticAttenuation; ; }; }; // structure for holding light properties // structure for holding light properties isEnabled; ; isLocal; ; isSpot; ; halfVector; ;

  28. Example 7.8 (continued) // the set of lights to apply, per invocation of this // the set of lights to apply, per invocation of this shader const const int int MaxLights MaxLights = 10; = 10; u uniform niform LightProperties LightProperties Lights( Lights(MaxLights shader MaxLights); ); u uniform float Shininess; niform float Shininess; u uniform float Strength; niform float Strength; uniform vec3 uniform vec3 EyeDirection EyeDirection; ; i in vec4 Color; n vec4 Color; i in vec3 Normal; n vec3 Normal; i in vec4 Position; n vec4 Position; out vec4 out vec4 FragColor FragColor; ;

  29. Example 7.8 (continued) v void main() oid main() { { vec3 vec3 s scatteredLight catteredLight = vec3(0.0); vec3 vec3 reflectedLight reflectedLight = vec3(0.0); = vec3(0.0); = vec3(0.0); // or, to a global ambient light // or, to a global ambient light // loop over all the lights // loop over all the lights for ( for (int int light = 0; light < light = 0; light < MaxLights if (! Lights[light]. if (! Lights[light].isEnabled continue; continue; vec3 vec3 halfVector halfVector; ; vec3 vec3 lightDirection lightDirection = Lights[light].position; float attenuation = 1.0; float attenuation = 1.0; MaxLights; ++light) { isEnabled) ) ; ++light) { = Lights[light].position;

  30. Example 7.8 (continued) // for local lights, compute per // for local lights, compute per- -fragment direction, // and attenuation // and attenuation if (Lights[light]. if (Lights[light].isLocal isLocal) { ) { lightDirection lightDirection = = lightDirection lightDirection = vec3(Position); float float lightDistance lightDistance = length( = length(lightDirection lightDirection lightDirection = = lightDirection lightDirection / / lightDistance fragment direction, halfVector halfVector, , = vec3(Position); lightDirection); ); lightDistance; ; attenuation = 1.0 / attenuation = 1.0 / (Lights[light]. (Lights[light].constantAttenuation + Lights[light]. + Lights[light].linearAttenuation + Lights[light]. + Lights[light].quadraticAttenuation * * lightDistance lightDistance); ); constantAttenuation linearAttenuation * * lightDistance quadraticAttenuation * * lightDistance lightDistance lightDistance

  31. Example 7.8 (continued) if (Lights[light]. if (Lights[light].isSpot float float spotCos if ( if (spotCos spotCos < Lights[light]. attenuation = 0.0; attenuation = 0.0; else else attenuation *= pow( attenuation *= pow(spotCos isSpot) { ) { spotCos = dot( < Lights[light].spotCosCutoff = dot(lightDirection lightDirection, , - -Lights[light]. spotCosCutoff) ) Lights[light].coneDirection coneDirection); ); spotCos, , spotExponent); ); Lights[light]. Lights[light].spotExponent } } halfVector halfVector = normalize( = normalize(lightDirection } else { } else { halfVector halfVector = Light[light]. = Light[light].halfVector } } lightDirection + + EyeDirection EyeDirection); ); halfVector; ;

  32. Example 7.8 (continued) float diffuse = max(0.0, dot(Normal, float diffuse = max(0.0, dot(Normal, lightDirection float specular = max(0.0, dot(Normal, float specular = max(0.0, dot(Normal, halfVector lightDirection)); )); halfVector)); )); if (diffuse == 0.0) if (diffuse == 0.0) specular = 0.0; specular = 0.0; else else specular = pow(specular, Shininess) * Strength; specular = pow(specular, Shininess) * Strength; // Accumulate all the lights effects // Accumulate all the lights effects scatteredLight scatteredLight += Lights[light].ambient * attenuation + += Lights[light].ambient * attenuation + Lights[light].color * diffuse * attenuation; Lights[light].color * diffuse * attenuation; reflectedLight reflectedLight += Lights[light].color * specular * attenuation; += Lights[light].color * specular * attenuation; } } vec3 vec3 rgb FragColor FragColor = vec4( rgb = min( = min(Color.rgb = vec4(rgb Color.rgb * * scatteredLight scatteredLight + + reflectedLight rgb, , Color.a Color.a); ); reflectedLight, vec3(1.0)); , vec3(1.0)); } }

  33. Material Properties differently sized specular highlights. material- -specific modulation of the color specific modulation of the color of ambient, diffuse, and specular lighting. specular lighting. Types of material properties: emission, ambient, diffuse, specular, shininess Types of material properties: emission, ambient, diffuse, specular, shininess Different materials have Different materials have differently sized specular highlights. Can also have Can also have material of ambient, diffuse, and Emission Emission the material emits light the material emits light Ambient Ambient how ambient light is how ambient light is reflected off the object reflected off the object Diffuse Diffuse how diffuse light is reflected how diffuse light is reflected off the object off the object Specular Specular color of the highlight color of the highlight Shininess Shininess specular exponent specular exponent

  34. Material Properties Example Example 7.10: Code Snippets for Using an Array of Material Properties Example 7.10: Code Snippets for Using an Array of Material Properties // Fragment // Fragment shader shader snippets selecting what material to shade with multiple lights snippets selecting what material to shade with multiple lights #version 330 core #version 330 core s struct truct MaterialProperties MaterialProperties { { vec3 emission; vec3 emission; vec3 ambient; vec3 ambient; vec3 diffuse; vec3 diffuse; vec3 specular; vec3 specular; float shininess; float shininess; }; };

  35. Example 7.10 (continued) shader invocation NumMaterials = 14; = 14; Uniform MaterialProperties MaterialProperties Material[ Material[NumMaterials // a set of materials to select between, per // a set of materials to select between, per shader Const Const int int NumMaterials Uniform invocation NumMaterials]; ]; f flat in lat in int . . . . . . v void main() oid main() { { int MatIndex MatIndex; ; // input material index from vertex // input material index from vertex shader shader . . . . . .

  36. Example 7.10 (continued) // Accumulate all the lights effects // Accumulate all the lights effects scatteredLight scatteredLight += += Lights[light].ambient * Material[ Lights[light].ambient * Material[MatIndex attenuation + attenuation + Lights[light].color * Material[ Lights[light].color * Material[MatIndex diffuse * attenuation; diffuse * attenuation; reflectedLight reflectedLight += += Lights[light].color * Material[ Lights[light].color * Material[MatIndex specular * attenuation; specular * attenuation; MatIndex].ambient * ].ambient * MatIndex].diffuse * ].diffuse * MatIndex].specular * ].specular * } } rgb = min(Material[ = min(Material[MatIndex + + Color.rgb Color.rgb * * scatteredLight FragColor FragColor = vec4( = vec4(rgb rgb, , Color.a } } Vec3 Vec3 rgb MatIndex].emission ].emission scatteredLight + + reflectedLight Color.a); ); reflectedLight, vec3(1.0)); , vec3(1.0));

  37. Advanced Topic: Two-Sighted Lighting See Example 7.11 for an example OpenGL code for this. See Example 7.11 for an example OpenGL code for this.

More Related Content