DEV Community

NoticeableSmeh
NoticeableSmeh

Posted on

Learning OpenGl part 4:

Materials:
Were talking about materials in this chapter. So essentially a material is a collection of data that determines how light should bounce of a given object. It makes sense that a wooden surfaces thats not polished will shine less than a polished metal sheet right? thats essentially what materials are.

So here inside our fragment shader we define a struct that functions as a collection for these different elements that can make up a material. Then in our code we define a uniform variable so we can change and access the material of an object during runtime. All the calculations are basically the same at this point were just exchanging raw numbers for a more customizable c++ oriented way, so now we can change the material in the code and in that change how light bounces of our object.


Were also creating a struct for the light.

now right now this may all seem abstract but what we are creating right now is modularity. It would be a pain in the ass to go through every single object in our scene and hand pick its lightsource, its material etc etc. Instead we are creating structs and variables that alow us to generate this later easier in runtime.

Then learnOpenGL decided to have som fun and make a disco with this code.

Fun stuff indeed.

So for the upcoming changes were making to the games lighting system with lighting maps, its starting to get annoyign typing everything we need to load textures and such everytime so lets actually create a function to load textures.

First things first were declaring an ID for our texture so we can quickly access it via this address. We then reserve / make a new OpenGL texture object and assign our ID as our access to that object.

Then we load the pixels from the disk with stbi_load. Now depending on how many components this file has we need to use the appropriate GL format, if it only has one component it only has the R in Rgb, its 1. If it has all three RGB, its three and ofcourse RGBA is 4.

We then make this texture our current focus in the program and bind the texture and upload it to the gpu. We generate a mipmap based on our texture and we set the wrapping behaviour and filtering.

We free the memory and if we fail at loading the teture we display that.
Finallly we return the texture ID, which means we can now access this texture via the the ID we used in connection to the function.

Lighting Maps:
So until right about now our textures have just been a big old flat surface with no form of variety in how it should be lit or anything. This does make sense if one object is only made of 1 type of material however many objects consist of different materials and should react to light differently, therefor it would be nice if we could do that. The wooden part of a box with steel borders should not shine as much as those steel parts so we should really fix this issue.

Diffuse map:
now we are loading a diffuseMap texture to our object, the diffuse texture is essentially just the base colour of what our object should loook like, its basically like a texture its not really different to how we loaded textures in earlier parts.


Specular Map:
Lets add another beautiful texture to our cube the specular map.

This tells the shader how shiny each fragment of the shader should be. If its black, its not shiny at all, the closer to white it is, the shinier it is.

so we sample our texture map image and it gives the color value from that texture. We apply essentially our previous calculation of how light should reflect and all but only on the pixels leaning towards white on our specularmap texture. This is done by doing the spec * specMap, the pixels brightness from the specular map modulates how visible that part of the reflection is. Then we multiply it by light.specular to colorize the pixel based on the lights own specular colour, which in our case is white.


this piece of code does so that whatever we assign to the shader at the 1 position (really the second position, because it starts at 0) thats our specularMap data from our texture.


and this is where we bind at that location of 1, our texture via our specularMap id.

When all this is done we should end up with this cube looking all delicious.

Top comments (0)