Lambertian or diffuse shading models how light interacts with matte surfaces by calculating the cosine of the angle between the light direction and the surface normal, where the diffuse reflection coefficient (kd) multiplied by this cosine value determines the intensity of light reflected from a surface, with the formula: I = Ia * Ka + Id * Kd * max(0, N · L), where N is the surface normal and L is the normalized light direction vector; this approach simulates materials that scatter light equally in all directions, unlike specular surfaces that reflect light in preferred directions.
Lambertian Diffuse Shading in OpenGL | Computer Graphics Tutorial
Added:all right so now that we've seen the basics of glsl and shading we're ready to start doing physically realistic models of lighting so we're going to use the shaders to actually light things and make them look like they might look in a real world scenario so if you recall our shading so far at least at the vertex level has been pretty simple so when we were doing this triangle we had three vertices connected by one triangle um even when we were doing these more complicated animations we really relied on the fragment shader to do the heavy lifting so here we still only had four vertices and two triangles and we just had a lot of pixels in between those two triangles that were shaded by the fragment shader but now in this module we're actually going to for a moment have the vertex shader do some heavy lifting so just to give you an idea if i go to this scene this scene has a lot of vertices in it so in particular you see all these vertices on the cubes and the rectangular prisms you see the vertices on the ellipsoid and and on my head and so actually now the vertex shader is going to be doing more work okay so we got a lot more vertices that we're sending there um and the shading happens when we connect these vertices with triangles so you see for example this cone is made up of a bunch it's approximated it's not a perfect cone but it's approximated by a bunch of triangles connected to each other you got these two vertices on the base and one vertex at top you can see a similar thing on the ellipsoid here a bunch of triangles stitched together and on my head you can certainly see a lot of triangles as well so that's how the shading is going to happen so the fragment shader still has a roll but we have more processing to do on the vertices first because we have many more vertices okay so that's what our scenes are going to look like so the vertex buffer is going to be doing more work um and now the question is how do we get these effects that we're seeing here so notice the shininess right as i move around this this this sort of the light is shining off of the surface here um i can also see that closer to the center of the light it's brighter and as i go out further away it's darker i can see that the lights are lighting the front of this cone in the front of the cylinder but not the back and same thing with my head over here so how do i get those effects so we're going to start to come up with some physical models of what happens when light bounces around and we're going to see if we can can translate those into math and then into code in the shader in the vertex shader okay so we're going to be talking in this first part about something called lambertian illumination so we're talking about local illumination we're just going to focus on one light at a time for now even though this scene had two lights in it we're just going to back away and think about what does one light at a time do um even before we do that there's a very simple model for shading that we can come up with which is i guess i'll call it just flat shading so this is where you say every material in your scene has a constant color um and i've said it's a vector because every color has a red green and blue component and so does every material so every material is going to have properties each of which has a red green and blue component so we call this here the ambient component k sub a and just to give you an idea of what that might look like here's a scene that has only the ambient component so we've got a green color a red color and a yellow color on different materials and so yeah i mean you certainly have the perspective here you can make things out but there's a lot of geometric detail that's lost so there's no effect of this light on everything so everything is colored exactly the same and so for example i can't even really tell from this angle that homer has a mouth i can see a silhouette but everything's the same color so i can't there's no contrast there if i go to the side i can kind of see that but now i don't even really know that he has fingers right so that sort of thing um so this is clearly not enough i mean it's a start and it's pretty simple you just set you know the vertex color to be exactly the material color um and then the fragment shader will just you know interpolate that to all the pixels uh but but we should be able to do something a little fancier than this so this is where lambertian illumination comes in and sometimes i'll also call this diffuse illumination because the assumption behind lamborghini illumination is that your materials will reflect light that's coming into them equally in all directions so this is a simplifying assumption but it's actually pretty realistic for a lot of materials in the real world like like corrugated surfaces like wood and also i have this table here that's that's all black and it's diffused so basically the opposite of a mirror it doesn't have a preferred direction it just takes light in from the light and it reflects it out in all directions maybe scaled down a little bit so if i have a red material it'll only reflect a red light back for example but it'll do so equally in all directions and that's going to make things a little bit easier to start with so let's think about that so we're going to have to think about light coming into a surface so let's put a light here um and we'll consider a bunch of rays coming from that light that go onto the surface now if and here's i'll have the normal to the surface which is important because i really need to know as you see in a moment i need to know the direction of the light with respect to how this material is oriented okay so i'm going to assume that everything is locally flat and kind of deal with them as planes i mean we'll talk more about that how to deal with that later if they're not planes but okay so so here's the light that's coming straight down right if i were to be sunbathing at this location right here i probably get totally sunburned right so light coming straight on is very concentrated but by contrast if i take the light rays and i rotate them by some angle what i see actually is the same number of rays actually covers a larger area so it's less dense so just to draw it on here see these rays take up this much area the original ones they take up that much um let me down nice and big there so you can see it but these rays actually the same number of rays but coming at an angle you see they take up a larger area so that's actually less light coming into this to a volume so actually and if you look at this original here you see that um when it's straight on there are six rays coming into it but when it's at this angle there's only four rays coming into it right i mean those six rays actually end up covering a larger area and the effect only gets more pronounced the more i rotate it so if i start again um and oops and i rotate this let's rotate it like really at quite an angle so this is like almost at sunset um and look how much more area this these six light rays take up on the same material so they take up that hole so more than twice at this point so this is why you don't get sunburn as much later in the day and this is also why you know because of the tilt of the earth that we have winter because the sun never gets head-on in the winter so that's why it's not because of the distance of the earth to the sun it's because the tilt and because because of this property so let's try to mathematically derive well can we come up with an expression in terms of the angle of incidence with respect to the normal of how much light is actually going to get absorbed per unit area okay so that's what we need to do here and i'll keep this picture here just so you can see it but but i want to draw another picture over here just considering two rays next to each other because that's all we really need to derive an expression so what i'm going to look at is two rays which if i draw a perpendicular between them i'll say okay head on they would be at a distance of d from each other but what i'm interested in is when they hit the surface there's actually another distance and i want to know okay what is this distance between the two rays when they come in at an angle i'll call that distance p okay so what i can look at here is just actually some basic trig so what i can look at is i can say okay well you see here this is the angle that it's been rotated right um actually let's see now let's be more careful so that's not true so let me say actually this is the angle that has been rotated so i'll call this angle a so you see if a is zero then it's head on right that's like rotating this back this way but here we've actually rotated it away a little bit now let me actually just do a little bit of simple geometry so i'll notice that this is a right triangle here and so what i can say is at this angle here is 90 minus a and then i have a reflex angle over here so this is also 90 minus a this angle and i can make another right triangle actually so i'll just copy that d here so i'll consider another line segment which is parallel to that one so this this is the shortest distance between the two lines here d so that's when they're head on but i want to know what's what's this segment length here so p um and then this is a right triangle because again this is the shortest distance so it's a perpendicular between the two okay so here we go this is a right triangle now i have another complementary angle and i can see that actually this angle here is a and that kind of makes sense right so the more that i rotate this you see the more this comes this comes away from this okay um so now it's just a basic trigonometry problem actually what i can see is that d is an adjacent angle here in this right triangle so we can see that d is equal to p times the cosine of a and if i rearrange that i can say that well then p is equal to d over the cosine of a okay so that length gets longer but actually we're interested in the density we're interested in how much light is absorbed per unit area so we really want the reciprocal of this so we really want to know what's 1 over the length because that's going to tell me how many rays come in over a unit area so if i just take the reciprocal both sides and say well 1 over p is equal to cosine of a times one over d and one over d is really the original density of light so that's like sort of the most dense you can have one over d d is the smallest number you can have there so this is the original density um and this is the final density uh at a particular angle a okay cool so what we see is actually the the density the intensity of the light falls off as a cosine in the angle okay so this this is like a very important thing and this is how this again how the seasons work right um so if i zoom back out well okay so i had some light rays coming here um so let me actually draw those light wrangles light uh rays at the angle that they're they're really coming in here um so let's see let's try to get this right so it seems like yeah they're coming in at about that angle here okay now and let me just look at the one that's going straight into the surface okay so what i can see is this has the angle this has the angle here that i'm looking for the angle of a right this is my angle a here um and so what i really need to look at is um so i have this vector n and i have another vector so let me call this l so this vector l is the vector from the point of intersection on the material so so we're just looking at a particular location on the material um it's the vector from that all the way up to the light okay so we'll call that vector l um but actually if we have a normalized version of that vector so i'll call this um l sub n so let me finish making it here so if i have a normalized version of this vector ooh that's kind of big okay so i'll call this l sub n so it's in the same direction as l but it's normalized so now what i can say is well ln dot n so ln dot product with the normal is equal to the magnitude of ln times the magnitude of n times the cosine of the angle between them well guess what they're both normalized so that's just going to be the cosine of a all right cool so what we can do is we can get the factor that this thing scales down by as a dot product of the normal direction and the direction to the light so the final equation for diffuse shading is as follows so for diffuse slash lambertian shading what we have is this equation so we're going to keep the ambient term there so we'll still have a ka but now we're we're going to also have okay the color of the light so i'll say that cl is the color of the light it's it's rgb um and so it's going to be the color of the light whatever is the incoming light color times a material diffuse coefficient so that's kd so that's how that's going to tell us how the material reflects light so it's reflecting equally in all directions but it doesn't have to reflect all the light like i said it could be a red material could be a green material so that's how much it's going to reflect back but we also have this term n dot ln so that's going to get us to fall off as a cosine so we've got the ambient material plus the diffuse material so let me label them here so we got our ambient and we've got our diffuse um and so that's all there is to it and so once you incorporate that um you look much nicer so now i can actually make out geometric details so one sort of caveat that i that i do have to be careful of though is i don't want this to go negative so if my light happens to be behind my scene so let me flip this the other way yeah so if i have a light that's sort of behind my material then you notice that this direction vector ln so this is the ln for that light that was huge okay hang on um notice that's going to make an angle greater than 90 degrees with n so the dot product will be negative but we actually don't want to light things on the back side of this at all we only want to light things on the front side this is why i had to do the above or below thing before so there's one caveat we'll say if n dot l n is less than zero just make make the make that zero so make the whole coefficient zero okay so that's the one caveat because we don't want to light the back faces we only want to light the things that are in front of the light so if i look on the back side of homer i only see the ambient color now i don't see any diffuse color okay all right so that's all there is to lamborghini shading but now i'd like you to try to implement lambortion shading yourself i've provided a lot of code for you but i want to show you see i want you to do this in a vertex shader so if i go to the first exercise you'll see that i've started you off with a bunch of code you don't have to edit the c in the scene it's just actually you can't i disabled editing it just has homer in the box and all that um and you see the fragment shader is quite simple as well so the vertex shader is where we're going to be doing the heavy lifting here now so what what's going to happen is we want to come up with a color and that's going to be a varying so we'll compute a color in each vertex from how that vertex is lit by the ambient and diffuse terms and that will be passed on as a varying over to the fragment shader and that will just be interpolated by very eccentric interpolation between the vertices okay so really going to focus on lighting the vertices themselves okay so i take care of a few things here um in the scene there's a composite transformation matrix that could happen to two vectors so this is the original position of the vertex um but there again there's a transformation matrix that you might have applied in the scene so i do that multiplication using homogeneous coordinates um there's also something called a viewing matrix which we'll talk about more in a couple weeks but it's that this thing is placed somewhere in the scene but also my camera is looking at it from a certain angle so i have to do one more transformation actually after i've done that original transformation and then there's something called a projection matrix so this is the viewing matrix that gets me into the camera's coordinates and this is a projection matrix with that which actually we'll also talk about a couple weeks this flattens me to 2d so i have all my i have my three-dimensional coordinates but this is actually what's going to give me a position on the grid that grid negative one to one negative one to one that we talked about um and the last thing is remember we actually have to have a special matrix to transform the normals um by whatever transformation we're doing to this material to this shape so that's called the normal matrix and i also pass that along okay so i know what the normal is now which i need to know again to determine this angle right i need to know the normal surface so you need to get that transformed properly i know the position i'm going to pull out just this isn't homogenous coordinates but i'm just going to pull out the xyz so i'm going to call it vpos i also know the position of the light which i need to know to construct this vector right so i'm going to pull out the position of the light uh by the way there's actually an array of lights we'll deal with that later right now i'm just focused on local illumination so one light at a time so i'm just going to look at the first one and now i have okay and then i also have this this diffuse coefficient right um actually yeah it's ukd this is uniform so i've got my diffuse my material um so that's this k d here uh v color that's just an extra thing you can kind of ignore that it makes it so that if you want you can specify different colors at each vertex but but you could ignore that um and then this is the coefficient that i need to compute so this this is going to be the the n dot ln clamped to zero appropriately so what you need to do is just set up the vector from the position to the light take the dot product of that vector with the normal and then do the multiplication here okay so the multiplication is actually already taken care of so all you need to do is compute this set up the vector right so it actually computes the right dot product and make sure you clamp it to zero and once you do that you'll transition from a picture that looks like this to a picture that looks like this okay and i give a lot of hints there's a lot of common mistakes doing this so when you're finished and you've read i mean you can check at any time by just clicking update shader so watch it i could do something like you know maybe i scale the ambient coefficient by 2.0 you'll see everything gets a little brighter if i click update shaders there we go everything got brighter i'm not done yet but if you want to check it'll give you a hint um so you can check at any time there it just tells me try again because that's not a very con just just shape you know ambient by two that's not a very common thing to do but if i went back to the original oh i thought it was going to tell me a hint about ambient let me try that again so if i refresh and now click submit oh i forgot to do the ambient check that's okay anyway but there's a lot of common mistakes and it'll tell you so you can check here if you get it correct it'll give you credit on canvas as usual all right so that's lamb version shading so give that a shot and then we'll talk about another kind of shooting
Up Next

The Physics of Light and Rendering | John Carmack Talk
@Quaddicted
249.8K views•2013-08-04

BitTorrent Protocol Explained: Piece Selection & Peer Choking
@StevenGordonAU
481 views•2013-02-22

HTTP Requests Explained: GET, POST, PUT, DELETE
@codecademy
103.1K views•2021-10-07

Enigma Machine Mechanics: WWII Encryption Explained
@JaredOwen
13.2M views•2021-12-11
Related Study Plans & Knowledge Roadmaps
Structured learning paths in Computer Science


















![Lecture- 04 (2020) Introduction to Computer Systems (ICs) [Minhajul Bashir] UIU](https://i.ytimg.com/vi/Jk8DedU0QuM/maxresdefault.jpg)




![[OpenGL & C Game Engine Dev] The Editor (Quiet Edition)](https://i.ytimg.com/vi/tU4dDl33cKI/maxresdefault.jpg)








![Ray tracing [C++ & SDL2] - Episode 6 - Planes & Shadows](https://i.ytimg.com/vi/9K9ZYq6KgFY/maxresdefault.jpg)





