User Tools

Site Tools


tutorial_206_20-_20lighting

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
tutorial_206_20-_20lighting [2018/03/31 13:19] – external edit 127.0.0.1tutorial_206_20-_20lighting [2024/01/05 00:21] (current) – external edit 127.0.0.1
Line 9: Line 9:
 \\  In this tutorial, the most basic type of lighting will be introduced: lambertian lighting. Lambertian lighting has uniform intensity irrespective of the distance away from the light. When the light hits the surface, the amount of light reflected is calculated by the angle of incidence the light has on the surface. When a light is shone directly on a surface, it is shown to reflect all the light back, with maximum intensity. However, as the angle of the light is increased, the intensity of the light will fade away.\\ \\  \\  In this tutorial, the most basic type of lighting will be introduced: lambertian lighting. Lambertian lighting has uniform intensity irrespective of the distance away from the light. When the light hits the surface, the amount of light reflected is calculated by the angle of incidence the light has on the surface. When a light is shone directly on a surface, it is shown to reflect all the light back, with maximum intensity. However, as the angle of the light is increased, the intensity of the light will fade away.\\ \\ 
 ===== Initializing the Lights ===== ===== Initializing the Lights =====
-\\  In this tutorial, there will be two light sources. One will be statically placed above and behind the cube, and another one will be orbiting the center cube. Note that the orbiting cube in the previous tutorial has been replaced with this light source.\\ \\  Since lighting is computed by the shaders, the variables would have to be declared and then bound to the variables within the technique. In this sample, we just require the direction of the light source, as well as its colour value. The first light is grey and not moving, while the second one is an orbiting red light.\\ \\ +\\  In this tutorial, there will be two light sources. One will be statically placed above and behind the cube, and another one will be orbiting the center cube. Note that the orbiting cube in the previous tutorial has been replaced with this light source.\\ \\  Since lighting is computed by the shaders, the variables would have to be declared and then bound to the variables within the technique. In this sample, we just require the direction of the light source, as well as its colour value. The first light is grey and not moving, while the second one is an orbiting red light. 
 + 
 +<code bb4w>
         REM Setup our lighting parameters:         REM Setup our lighting parameters:
         DIM vLightDir0(3), vLightDir1(3), vLightColor0(3), vLightColor1(3)         DIM vLightDir0(3), vLightDir1(3), vLightColor0(3), vLightColor1(3)
Line 16: Line 18:
         vLightColor0() = 0.5, 0.5, 0.5, 1.0         vLightColor0() = 0.5, 0.5, 0.5, 1.0
         vLightColor1() = 0.5, 0.0, 0.0, 1.0         vLightColor1() = 0.5, 0.0, 0.0, 1.0
-\\  The orbiting light is rotated just like the cube in the last tutorial. The rotation matrix applied will change the direction of the light, to show the effect that it is always shining towards the center:\\ \\ +</code> 
 + 
 +The orbiting light is rotated just like the cube in the last tutorial. The rotation matrix applied will change the direction of the light, to show the effect that it is always shining towards the center: 
 + 
 +<code bb4w>
           REM Rotate the second light around the origin           REM Rotate the second light around the origin
           PROC_MatrixRotation(mRotate(), 0, 2*t, 0)           PROC_MatrixRotation(mRotate(), 0, 2*t, 0)
           vLightDir1() = 0.0, 0.0, -1.0, 1.0           vLightDir1() = 0.0, 0.0, -1.0, 1.0
           vLightDir1() = vLightDir1() . mRotate()           vLightDir1() = vLightDir1() . mRotate()
-\\  The lights' direction and colour are both passed into the shader just like the matrices. The associated variable is called to set, and the parameter is passed in.\\ \\ +</code> 
 + 
 +The lights' direction and colour are both passed into the shader just like the matrices. The associated variable is called to set, and the parameter is passed in. 
 + 
 +<code bb4w>
           REM Update matrix and lighting variables:           REM Update matrix and lighting variables:
           PROC_MatrixTranspose(ConstantBuffer{}, ConstantBuffer.mWorld{}, mWorld())           PROC_MatrixTranspose(ConstantBuffer{}, ConstantBuffer.mWorld{}, mWorld())
Line 27: Line 37:
           SYS ID3D11DeviceContext.UpdateSubresource%, pImmediateContext%, pConstantBuffer%, \           SYS ID3D11DeviceContext.UpdateSubresource%, pImmediateContext%, pConstantBuffer%, \
           \                                           0, NULL, ConstantBuffer{}, 0, 0           \                                           0, NULL, ConstantBuffer{}, 0, 0
-\\ +</code> 
 ===== Rendering the Lights in the Pixel Shader ===== ===== Rendering the Lights in the Pixel Shader =====
 \\  Once we have all the data set up and the shader properly fed with data, we can compute the lambertian lighting term on each pixel from the light sources. We'll be using the dot product rule discussed previously.\\ \\  Once we've taken the dot product of the light versus the normal, it can then be multiplied with the color of the light to calculate the effect of that light. That value is passed through the saturate function, which converts the range to [0, 1]. Finally, the results from the two separate lights are summed together to create the final pixel color.\\ \\  Consider that the material of the surface itself is not factored into this light calculation. The final color of the surface is a result of the light's colors.\\ \\  \\  Once we have all the data set up and the shader properly fed with data, we can compute the lambertian lighting term on each pixel from the light sources. We'll be using the dot product rule discussed previously.\\ \\  Once we've taken the dot product of the light versus the normal, it can then be multiplied with the color of the light to calculate the effect of that light. That value is passed through the saturate function, which converts the range to [0, 1]. Finally, the results from the two separate lights are summed together to create the final pixel color.\\ \\  Consider that the material of the surface itself is not factored into this light calculation. The final color of the surface is a result of the light's colors.\\ \\ 
 +<code glsl>
       //       //
       // Pixel Shader       // Pixel Shader
Line 44: Line 56:
           return finalColor;           return finalColor;
       }       }
 +</code>
 \\  Once through the pixel shader, the pixels will be modulated by the lights, and you can see the effect of each light on the cube surface. Note that the light in this case looks flat because pixels on the same surface will have the same normal. Diffuse is a very simple and easy lighting model to compute. You can use more complex lighting models to achieve richer and more realistic materials. \\  Once through the pixel shader, the pixels will be modulated by the lights, and you can see the effect of each light on the cube surface. Note that the light in this case looks flat because pixels on the same surface will have the same normal. Diffuse is a very simple and easy lighting model to compute. You can use more complex lighting models to achieve richer and more realistic materials.
tutorial_206_20-_20lighting.1522502387.txt.gz · Last modified: 2024/01/05 00:16 (external edit)