LearnOpenGL

Translation in progress of learnopengl.com.
git clone https://git.mtkn.jp/LearnOpenGL
Log | Files | Refs

Materials.html (12420B)


      1     <h1 id="content-title">Materials</h1>
      2 <h1 id="content-url" style='display:none;'>Lighting/Materials</h1>
      3 <p>
      4   In the real world, each object has a different reaction to light. Steel objects are often shinier than a clay vase for example and a wooden container doesn't react the same to light as a steel container. Some objects reflect the light without much scattering resulting in small specular highlights and others scatter a lot giving the highlight a larger radius. If we want to simulate several types of objects in OpenGL we have to define <def>material</def> properties specific to each surface.
      5 </p>
      6 
      7 <p>
      8   In the previous chapter we defined an object and light color to define the visual output of the object, combined with an ambient and specular intensity component. When describing a surface we can define a material color for each of the 3 lighting components: ambient, diffuse and specular lighting. By specifying a color for each of the components we have fine-grained control over the color output of the surface. Now add a shininess component to those 3 colors and we have all the material properties we need:
      9 </p>
     10 
     11 <pre><code>
     12 #version 330 core
     13 struct Material {
     14     vec3 ambient;
     15     vec3 diffuse;
     16     vec3 specular;
     17     float shininess;
     18 }; 
     19   
     20 uniform Material material;
     21 </code></pre>
     22 
     23 <p>
     24   In the fragment shader we create a <code>struct</code> to store the material properties of the surface. We can also store them as individual uniform values, but storing them as a struct keeps it more organized. We first define the layout of the struct and then simply declare a uniform variable with the newly created struct as its type.
     25 </p> 
     26 
     27 <p>
     28   As you can see, we define a color vector for each of the Phong lighting's components. The <var>ambient</var> material vector defines what color the surface reflects under ambient lighting; this is usually the same as the surface's color. The <var>diffuse</var> material vector defines the color of the surface under diffuse lighting. The diffuse color is (just like ambient lighting) set to the desired surface's color. The <var>specular</var> material vector sets the color of the specular highlight on the surface (or possibly even reflect a surface-specific color). Lastly, the <var>shininess</var> impacts the scattering/radius of the specular highlight.
     29 </p>
     30 
     31 <p>
     32   With these 4 components that define an object's material we can simulate many real-world materials. A table as found at <a href="http://devernay.free.fr/cours/opengl/materials.html" target="_blank">devernay.free.fr</a> shows a list of material properties that simulate real materials found in the outside world. The following image shows the effect several of these real world material values have on our cube:
     33 </p>
     34 
     35 <img src="/img/lighting/materials_real_world.png"/>
     36 
     37 <p>
     38   As you can see, by correctly specifying the material properties of a surface it seems to change the perception we have of the object. The effects are clearly noticeable, but for the more realistic results we'll need to replace the cube with something more complicated. In the <a href="https://learnopengl.com/Model-Loading/Assimp" target="_blank">Model Loading</a> chapters we'll discuss more complicated shapes.
     39 </p>
     40 
     41 <p>
     42   Figuring out the right material settings for an object is a difficult feat that mostly requires experimentation and a lot of experience. It's not that uncommon to completely destroy the visual quality of an object by a misplaced material.
     43 </p>
     44 
     45 <p>
     46   Let's try implementing such a material system in the shaders.
     47 </p>
     48 
     49 <h1>Setting materials</h1>
     50 <p>
     51   We created a uniform material struct in the fragment shader so next we want to change the lighting calculations to comply with the new material properties. Since all the material variables are stored in a struct we can access them from the <var>material</var> uniform:
     52 </p>
     53 
     54 <pre><code>
     55 void main()
     56 {    
     57     // ambient
     58     vec3 ambient = lightColor * material.ambient;
     59   	
     60     // diffuse 
     61     vec3 norm = normalize(Normal);
     62     vec3 lightDir = normalize(lightPos - FragPos);
     63     float diff = max(dot(norm, lightDir), 0.0);
     64     vec3 diffuse = lightColor * (diff * material.diffuse);
     65     
     66     // specular
     67     vec3 viewDir = normalize(viewPos - FragPos);
     68     vec3 reflectDir = reflect(-lightDir, norm);  
     69     float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
     70     vec3 specular = lightColor * (spec * material.specular);  
     71         
     72     vec3 result = ambient + diffuse + specular;
     73     FragColor = vec4(result, 1.0);
     74 }
     75 </code></pre>
     76 
     77 <p>
     78   As you can see we now access all of the material struct's properties wherever we need them and this time calculate the resulting output color with the help of the material's colors. Each of the object's material attributes are multiplied with their respective lighting components.
     79 </p>
     80 
     81 <p>
     82   We can set the material of the object in the application by setting the appropriate uniforms. A struct in GLSL however is not special in any regard when setting uniforms; a struct only really acts as a namespace of uniform variables. If we want to fill the struct we will have to set the individual uniforms, but prefixed with the struct's name:
     83 </p>
     84 
     85 <pre><code>
     86 lightingShader.setVec3("material.ambient", 1.0f, 0.5f, 0.31f);
     87 lightingShader.setVec3("material.diffuse", 1.0f, 0.5f, 0.31f);
     88 lightingShader.setVec3("material.specular", 0.5f, 0.5f, 0.5f);
     89 lightingShader.setFloat("material.shininess", 32.0f);
     90 </code></pre>
     91 
     92 <p>
     93   We set the ambient and diffuse component to the color we'd like the object to have and set the specular component of the object to a medium-bright color; we don't want the specular component to be too strong. We also keep the shininess at <code>32</code>. 
     94 </p>
     95 
     96 <p>
     97   We can now easily influence the object's material from the application. Running the program gives you something like this:
     98 </p>
     99 
    100 <img src="/img/lighting/materials_with_material.png" class="clean"/>
    101 
    102 <p>
    103   It doesn't really look right though?
    104 </p>
    105 
    106 <h2>Light properties</h2>
    107 <p>
    108   The object is way too bright. The reason for the object being too bright is that the ambient, diffuse and specular colors are reflected with full force from any light source. Light sources also have different intensities for their ambient, diffuse and specular components respectively. In the previous chapter we solved this by varying the ambient and specular intensities with a strength value. We want to do something similar, but this time by specifying intensity vectors for each of the lighting components. If we'd visualize <var>lightColor</var> as <code>vec3(1.0)</code> the code would look like this:
    109 </p>
    110 
    111 <pre><code>
    112 vec3 ambient  = vec3(1.0) * material.ambient;
    113 vec3 diffuse  = vec3(1.0) * (diff * material.diffuse);
    114 vec3 specular = vec3(1.0) * (spec * material.specular); 
    115 </code></pre>
    116 
    117 <p>
    118   So each material property of the object is returned with full intensity for each of the light's components. These <code>vec3(1.0)</code> values can be influenced individually as well for each light source and this is usually what we want. Right now the ambient component of the object is fully influencing the color of the cube. The ambient component shouldn't really have such a big impact on the final color so we can restrict the ambient color by setting the light's ambient intensity to a lower value:
    119 </p>
    120 
    121 <pre><code>
    122 vec3 ambient = vec3(0.1) * material.ambient;  
    123 </code></pre>
    124 
    125 <p>
    126   We can influence the diffuse and specular intensity of the light source in the same way. This is closely similar to what we did in the <a href="https://learnopengl.com/Lighting/Basic-Lighting" target="_blank">previous</a> chapter; you could say we already created some light properties to influence each lighting component individually. We'll want to create something similar to the material struct for the light properties:
    127 </p>
    128 
    129 <pre><code>
    130 struct Light {
    131     vec3 position;
    132   
    133     vec3 ambient;
    134     vec3 diffuse;
    135     vec3 specular;
    136 };
    137 
    138 uniform Light light;  
    139 </code></pre>
    140 
    141 <p>
    142   A light source has a different intensity for its <var>ambient</var>, <var>diffuse</var> and <var>specular</var> components. The ambient light is usually set to a low intensity because we don't want the ambient color to be too dominant. The diffuse component of a light source is usually set to the exact color we'd like a light to have; often a bright white color. The specular component is usually kept at <code>vec3(1.0)</code> shining at full intensity. Note that we also added the light's position vector to the struct.
    143   
    144 <p>
    145   Just like with the material uniform we need to update the fragment shader:
    146 </p>
    147 
    148 <pre><code>
    149 vec3 ambient  = light.ambient * material.ambient;
    150 vec3 diffuse  = light.diffuse * (diff * material.diffuse);
    151 vec3 specular = light.specular * (spec * material.specular);  
    152 </code></pre>
    153 
    154 <p>
    155   We then want to set the light intensities in the application:
    156 </p>
    157 
    158 <pre><code> 
    159 lightingShader.setVec3("light.ambient",  0.2f, 0.2f, 0.2f);
    160 lightingShader.setVec3("light.diffuse",  0.5f, 0.5f, 0.5f); // darken diffuse light a bit
    161 lightingShader.setVec3("light.specular", 1.0f, 1.0f, 1.0f); 
    162 </code></pre>
    163 
    164 <p>
    165   Now that we modulated how the light influences the object's material we get a visual output that looks much like the output from the previous chapter. This time however we got full control over the lighting and the material of the object:
    166 </p>
    167 
    168 <img src="/img/lighting/materials_light.png" class="clean"/>
    169 
    170 <p>
    171   Changing the visual aspects of objects is relatively easy right now. Let's spice things up a bit!
    172 </p>
    173 
    174 <h2>Different light colors</h2>
    175 <p>
    176   So far we used light colors to only vary the intensity of their individual components by choosing colors that range from white to gray to black, not affecting the actual colors of the object (only its intensity). Since we now have easy access to the light's properties we can change their colors over time to get some really interesting effects. Since everything is already set up in the fragment shader, changing the light's colors is easy and immediately creates some funky effects:
    177 </p>
    178 
    179 <div class="video paused" onclick="ClickVideo(this)">
    180   <video width="600" height="450" loop>
    181     <source src="/video/lighting/materials.mp4" type="video/mp4" />
    182     <img src="/img/lighting/materials_light_colors.png"/>
    183   </video>
    184 </div>
    185 
    186 
    187 <p>
    188   As you can see, a different light color greatly influences the object's color output. Since the light color directly influences what colors the object can reflect (as you may remember from the <a href="https://learnopengl.com/Lighting/Colors" target="_blank">Colors</a> chapter) it has a significant impact on the visual output.
    189 </p>
    190 
    191 <p>
    192   We can easily change the light's colors over time by changing the light's ambient and diffuse colors via <fun>sin</fun> and <fun><function id='47'>glfwGetTime</function></fun>:
    193 </p>
    194 
    195 <pre><code>
    196 glm::vec3 lightColor;
    197 lightColor.x = sin(<function id='47'>glfwGetTime</function>() * 2.0f);
    198 lightColor.y = sin(<function id='47'>glfwGetTime</function>() * 0.7f);
    199 lightColor.z = sin(<function id='47'>glfwGetTime</function>() * 1.3f);
    200   
    201 glm::vec3 diffuseColor = lightColor   * glm::vec3(0.5f); 
    202 glm::vec3 ambientColor = diffuseColor * glm::vec3(0.2f); 
    203   
    204 lightingShader.setVec3("light.ambient", ambientColor);
    205 lightingShader.setVec3("light.diffuse", diffuseColor);
    206 </code></pre>
    207 
    208 <p>
    209   Try and experiment with several lighting and material values and see how they affect the visual output. You can find the source code of the application <a href="/code_viewer_gh.php?code=src/2.lighting/3.1.materials/materials.cpp" target="_blank">here</a>.
    210 </p>
    211 
    212 <h2>Exercises</h2>
    213 <p>
    214   <ul>
    215     <li>Can you make it so that changing the light color changes the color of the light's cube object?</li>
    216     <li>Can you simulate some of the real-world objects by defining their respective materials like we've seen at the start of this chapter? Note that the <a href="http://devernay.free.fr/cours/opengl/materials.html" target="_blank">table</a>'s ambient values are not the same as the diffuse values; they didn't take light intensities into account. To correctly set their values you'd have to set all the light intensities to <code>vec3(1.0)</code> to get the same output: <a href="/code_viewer_gh.php?code=src/2.lighting/3.2.materials_exercise1/materials_exercise1.cpp" target="_blank">solution</a> of cyan plastic container.</li>
    217   </ul>
    218 </p>       
    219 
    220     </div>
    221