How it works...

In OpenGL 3 and later versions, fixed function lighting no longer exists. You can no longer call glEnable(GL_LIGHT1) to add light to your 3D scene. The new method for adding light is to calculate your own lighting in the shader. This gives you the flexibility of creating all types of lights, depending on your needs. The old method has a limitation of up to 16 lights in most hardware but, with the new programmable pipeline, you can have any number of lights in the scene; however, the lighting model will need to be coded entirely by you in the shaders, which is not an easy task.

Other than that, we also need to add a surface normal value to every surface of the cube. Surface normal indicates where the surface is facing and is used for lighting calculations.

The preceding example is very simplified to let you understand how lighting works in OpenGL. In actual use cases, you may need to pass some variables such as light intensity, light color, and light position from C++ or load it from a material file instead of hardcoding it in the shader code.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset