So color:
Color is light that bounces from an object right into your eyeballs. The object absorbs some rays and then what bounces of is the color you see in your eye.
Ambient Lighting:
So this is lighting that like bounces around from places to places and end up scattered everywhere, light you know reflects. Kind of the main attribute of light is that it yknow reflects. Anyways doing this calculation for Global Illumination is really expensive so we fake it with Ambient Lighting.
So heres where we apply the ambient lighting. Its a really cheap thing we are doing were essentially not even simulating lighting were just saying, "Make this 0.1 brighter on each RGB value so its not completely dark"
Diffuse Lighting:
So if you hold a lightsource really close to a surface, the point that which is closest to the lightsource would be brightest. And as you get further and further away it gets slightly less bright. Thats diffuse lighting.
Also we need to measure the angle at which the lightsource is hitting a fragment on our object. To do this we take the position of the lightsource and lets say a point on an object. Perpendicular to that point we calculate the angle in which the lightsource is at, We do this with a Dot product calculation. Which basically means that the closer to 1 the dot product is the perpendicular line from the point we calculated and the lightsource are at the same angle. The closer to 0, the more its closer to being at a 90 degree angle. So the dot product returns a scalar which we can use in order to determine how much light should hit a certain fragment of our object.
Normal Vectors:
So in order to do proper lighting calculations we need to determine which way our surfaces are facing, this is done via a cross product. However since were doing simple cubes right now we can just cheat this. Essentially were adding some new vertex data in our vertices array, these determine which way our planes are facing. So we have Nx Ny and Nz values, if nx is positive its facing to the right (picture you are standing in front of a cube). If the Nz is positive, its pointing towards you (z Position is positive if its facing you in opengl and negative if its facing away). We just store these vectors in the array.
Now from our cubes vertex shader we pass this normal with an out declared vec3 variable to the fragment shader. We also establish a layout with location 1 so we can input in our program the current normal vectors. We take that in with a vec3 variable inside our fragment shader and we also declare a uniform for our vector3 that can get updated in the rendering loop for our lightposition.
So another important point to remember if youve worked with unity and such is that the defined positions of objects such as cubes are only defined in their position via another vector that defines the world space. Essentially we have vectors upon vectors, we need to define the location of something in our world space coordinate system and then define that actual object in its own coordinate system with vectors to have something to draw.
the model variable here is essentially the offset needed to apply to other coordinate systems that derive an objects "world space", Im thinking of the world space calculations here as an offset in order to understand it better. So right now the Model variable carries the vector of 0, 0, 0. So if you want the object to be at a certain location you say like to the program "Draw this cube here using these vectors and i want it at the default world position of (0, 0, 0) but a - 1 on the x coordinate" and boom, the cube is now on the left side of the screen instead.
So this piece of code here would do exactly that which i mentioned, lets make that cube on the left there innit.
model = glm::translate(glm::mat4(1.0f), glm::vec3(-1.0f, 0.0f, 0.0f));
Anyways im really sick and slightly delirious so forgive me if i forgot something but now our scene looks like this.
Specular Lighting:
So now its time to think about your eyes. Specular lightin is all about calculating the angle in which light bounces of a surface then into your balleyes. Ever notice when something is shiny that that shiny spot seems to follow your head when youre walking around? Ye its that. So we calculate both the angle in which the the light comes from and hits a fragment, then perpendicular to that point (using our normal) we can calculate the reflecting light, then we calculate that reflected lights direction contra our eyes direction (vectors again.
So to get our location for our eyes (Aka our camera or viewport or whatever) Well its really simple actually, we already have written that variable in our camera class. So we pass that into our fragment shader via a uniform.
Alright now lets apply or specular lighting. So we have our view direction which we take the viewPos we are looking through minus the fragments position and we normalize it. Why normalize it then? Cuz we dont care about the length of the distance between the two positions only the angle and direction! Anyways our light direction then comes from the lightsource and hits the fragment on our object. Now when something is heading towards an object what do you have to do in order to reflect it the opposite way? Well you send it bouncing at the same angle it got there at. The problem with that is that our lightDir doesnt count from the lightsource to the object, it does the opposite, it count from the object to the lightsource. How do we fix that? Its actually really simple we stick a - in front of it. So the reflect function takes into account the direction in which the light comes from and then compares that to the normal which is the way our fragment is facing and then it does a little behind the scenes math that calculates the angle in which it should bounce of in
All thats the left is actually calculating the specular component. We calculate the dot product between our view direction and the reflect direction and we have to make sure this is never negative. Then we raise it to the power of 32, cuz 32 is apparently the value of the shininess. The example from the openGL website uses the options of 2, 4, 8, 16, 32, 64, 128 and 256 to showcase. Some very suspicious numbers hmm I wonder why they chose just too numbers scratches chin.
then finally we add this to our result
and the end product looks something like this.
That does it for this part, I have had some personal things come in the way of studying this and right after all that cleared up I got sick, and I'm currently sick but I'm trying to ignore the fact that im sick and just power through, we will see how that goes. Anyways gg go next see you in the next part.
Top comments (0)