Diffuse Lighting // OpenGL Tutorial #20

  Рет қаралды 7,417

OGLDEV

OGLDEV

Күн бұрын

In this video we will learn how to implement diffuse lighting which is the second type of light in the Phong Reflection Model. We've covered ambient lighting in the previous tutorial and specular lighting will be covered in the next one.
In this tutorial I have used the following public domain assets:
Wine barrel model - polyhaven.com/a/wine_barrel_01 by James Ray Cock
Terrain texture - polyhaven.com/a/aerial_rocks_02 by Rob Tuytel
Thanks a lot to James and Rob for providing their creations!
Make sure to watch all the previous tutorials in the "OpenGL For Beginners" playlist at • OpenGL for Beginners
Please visit ogldev.org to see more of my tutorials on modern OpenGL.
Link to source: github.com/emeiri/ogldev/blob...
If you want to get the same version that was used in the video checkout the tag TUT_20_DIFFUSE_LIGHTING.
OpenGL 4.6 specification: www.khronos.org/registry/Open...
Timecodes:
0:00 Intro
0:57 Light sources types
3:02 The diffuse light factor
3:51 Lambert’s Cosine Law
4:33 Diffusely reflecting surface
5:10 The surface normal
6:11 Calculating the diffuse factor
7:29 Surface vs Vertex normals
9:00 Transforming the normal
10:27 Diffuse lighting in local space
11:05 Generating the world-to-local matrix
12:37 Code Review
12:45 Step 1: Add a diffuse component to the material
13:36 Step 2: Create a directional light with a diffuse component
14:50 Step 3: Calculate the light direction in local space
16:01 Step 4: Add uniform locations for shader diffuse params
16:37 Step 5: Add vertex normal to the vertex shader
17:22 Step 6: Implement diffuse lighting in the fragment shader
19:39 Step 7: Minor changes to the main app code
20:17 Test and experiment
Feel free to comment below.
Email: ogldev1@gmail.com
Github: github.com/emeiri/ogldev.git
Twitter: @ogldev
One time donations (Paypal): ogldev.org/donate.html
Patreon: / ogldev
Credits:
Music - "Small Guitar" from bensound.com
Duke Nukem image - www.cleanpng.com/png-duke-nuk...
Johann Heinrich Lambert pic - en.wikipedia.org/wiki/Johann_...
Cosine pic - upload.wikimedia.org/wikipedi...
Enjoy,
Etay Meiri
#opengl #ogldev #opengtutorials

Пікірлер: 12
@OGLDEV
@OGLDEV 2 жыл бұрын
Don't miss the previous tutorial on ambient lighting! - kzbin.info/www/bejne/j5-lmX2Yhr9_q6c
@OGLDEV
@OGLDEV 2 жыл бұрын
@Unity Programmer Yeah I guess most beginner level OpenGL tutorials and books have to go through a very similar path at this stage but I believe there will be unique material in the future so stay tuned!
@OGLDEV
@OGLDEV 2 жыл бұрын
@Unity Programmer Ogre3D supports both APIs as backends but do you need direct access to the gpu? Not sure how simple that is.
@gokalpates1567
@gokalpates1567 2 жыл бұрын
keep working on this tutorials extremely helpful!
@OGLDEV
@OGLDEV 2 жыл бұрын
Sure will, thanks!
@kaede_elfen
@kaede_elfen 2 жыл бұрын
Keep up the awesome work!
@OGLDEV
@OGLDEV 2 жыл бұрын
Thank you!
@dluffy121
@dluffy121 Жыл бұрын
The series has been awesome. Just want to add a note for 6:19, we use reverse vector as we know that the angle of reflection vector wrt normal will be same as the angle of light vector. This only clicked me after you explained reflect function
@OGLDEV
@OGLDEV Жыл бұрын
Thanks! I think that the most intuitive example is that when the light vector is perpendicular to the surface. In that case the light vector and the normal are directly opposite so in theory the angle between them is 180 degrees but of course we want to refer to this case as if the angle is zero so we reverse the light vector. Some people prefer doing the reverse already in the application so save them from this operation in the shader on every pixel. This makes sense but for educational reasons I do it all in one place.
@kermalis
@kermalis 2 жыл бұрын
At 11:30 and in your other videos (like for directional lighting), you bring up transforming everything back to local space from world space. However you mentioned that this would only work for uniform scaling, and that your solution of transposing the matrix is just a temporary one. It's a little scary for me to leave it that way, since you haven't brought this back up and clarified the proper way to do it which would handle non-uniform scaling. I think having non-uniform scaling is important and so my question is, how would I properly transform to local space? The code in question is the DirectionalLight local space direction calculation, and the WorldTrans WorldToLocal(). For the DirectionalLight, is it as simple as inverting the mesh's world transform matrix4x4, rather than transposing the top-left matrix3x3? You mentioned inverting it at 11:59. I already can invert it but am not sure if it's the correct solution, or whether to invert just the 3x3 portion or the entire 4x4 portion and skip the second vector multiplication. As for the WorldTrans one, I'm not sure if it needs updating since it's only used for the camera's local position and point lights' local positions, and we calculate their directions in the fragment shader with a simple subtraction. I figured I'd ask in case you could give me an answer for both since it's a little unclear for me right now, even though I think I understand how it all works Also thank you for your resources, I am grateful, and that you also take the time to reply to comments, you are invaluable
@OGLDEV
@OGLDEV 2 жыл бұрын
Thanks for bringing up the issue of scaling because there is a problem there and I wanted to do a followup video about it but couldn't find a time to squeeze it into the agenda. The problem is mostly for point lights where the vector to the light source changes from pixel to pixel. Let's say that you have a normalized quad from (-1,0,-1) to (1,0,1) and you want to use it as a large field and run a point light along it (see the point light demo on my website). With these coordinates you just need to apply a large scaling factor so no rotation and translation whatsoever. The light source local position is equal to its world position in this case. As the light runs back and forth along the field you expect it to attenuate and be visible only locally. In addition, the vector from the light to the far end of the field should be very sharp, affecting the diffuse factor. But if you ignore scaling you will do all calculation on the original (small) quad and it wil not be as expected. If you’re using uniform scaling the solution is simply to provide the scaling factor as a scalar to the VS and multiply it by the local position. It’s still less expensive than multiplying by a matrix. In the case of non-uniform scaling you can supply a vector of scaling factors and multiply it by the local position. GLSL does component wise multiplication in this case which is exactly what we need. Regarding WorldToLocal: in the case of diffuse lighting we just need to reverse the rotation so transposing the top 3x3 matrix should be enough. Translation won’t affect the light vector so we can ignore it. In the case of specular lighting we reverse translation and rotation separately and combine them to get the inverse. I agree that this is somewhat cumbersome so if you want to use a simple inversion of the entire matrix (using the determinant, etc) in the 4x4 case go ahead.
@kermalis
@kermalis 2 жыл бұрын
@@OGLDEV Yep, I ran into that point light scaling issue in my test scene, where the light was stretching with the entire mesh, because as you said, it was using the original small mesh as a reference. However I decided that I will tackle this local space optimization another time, since it's adding a bit more complexity than I'd like at the moment. I want to get things as robust as possible for the future before I start optimizing my systems, but I was curious about it so I figured I'd ask you, and you explain everything perfectly. I look forward to your next videos even though I started learning all of these things last year, because you keep teaching me advanced things that other teachers are leaving out, like these local transformation optimizations. So once again, thank you for all you do :)
Specular Lighting  // OpenGL Tutorial #21
23:00
OGLDEV
Рет қаралды 5 М.
Multiple Point Lights  // OpenGL Tutorial #22
16:58
OGLDEV
Рет қаралды 9 М.
MOM TURNED THE NOODLES PINK😱
00:31
JULI_PROETO
Рет қаралды 19 МЛН
100❤️ #shorts #construction #mizumayuuki
00:18
MY💝No War🤝
Рет қаралды 20 МЛН
Basic Lighting And Materials // OpenGL Tutorial #19
23:33
OGLDEV
Рет қаралды 14 М.
I Made a Graphics Engine (again)
8:27
Zyger
Рет қаралды 156 М.
Basic Shadow Mapping // OpenGL Tutorial #35
16:54
OGLDEV
Рет қаралды 19 М.
Introduction to Phong Lighting
9:44
Suboptimal Engineer
Рет қаралды 12 М.
Diffuse reflection in 5 minutes
5:21
Graphics in 5 Minutes
Рет қаралды 4,5 М.
Basic Texture Mapping // OpenGL Tutorial #16
22:01
OGLDEV
Рет қаралды 32 М.
Introduction To Tessellation // OpenGL Tutorial #47
16:57
OGLDEV
Рет қаралды 4,8 М.
Camera/View Space // OpenGL Tutorial #13
21:59
OGLDEV
Рет қаралды 18 М.
MOM TURNED THE NOODLES PINK😱
00:31
JULI_PROETO
Рет қаралды 19 МЛН