Understanding of light and color mixing in OpenGL

Source: Internet
Author: User

From OpenGL game programming

 

Illumination

// Illumination variable

Float ambientlight [] = {0.3f, 0.5f, 0.8f, 1.0f}; // ambient light

Float diffuselight [] = {0.25f, 0.25f, 0.25f, 1.0f); // scattered light

These variables define the properties of light. The ambientlight variable declares the environmental light attribute. The red weight is 0.3, the green weight is 0.5, and the blue weight is 0.8. This means that the global light that shines on all objects has a blue color. The diffuselight variable sets the values of the red, green, and blue components to 0.25. The effect is that the surface of the cube is directly illuminated by scattered light, which is much brighter than the surface not illuminated.

 

// Material variable

Float matambient [] = {1.0f, 1.0f, 1.0f, 1.0f };

Float matdiff [] = {1.0f, 1.0f, 1.0f, 1.0f };

The first variable matambient defines how the material surface reacts to the ambient light component in the light. In this example, each value in the variable is 1.0, meaning that the red, green, and blue components of the market environment light are all reflected on this surface. Similarly, in this example, the red, green, and blue components of the scattered light are all reflected on the surface.

 

Color Mixing

Color Mixing in OpenGL can bring visual effects like transparency to scenes. With transparency, we can simulate water, windows, glass, and other things in the world that we can see through.

To properly mix a scenario in OpenGL, You need to enable and disable the deep cache. To this end, we first draw all opaque objects in the normal state of the deep cache, and then call the gldepthmask () function to set the deep cache to read-only, this protects the depth of an opaque object. When the depth cache is read-only, the painting of transparent objects does not affect the non-transparent objects that have been drawn, because the depth cache cannot be changed. But remember that transparent objects will still undergo deep detection. Compared with the values in the depth cache, they will not be drawn if they are behind the opaque objects. If it is located in front of an opaque object, the transparent object will be mixed with the opaque object. When using the gldepthmask () function, if you want to set it to read-only, the gl_false parameter value will be passed to it. If you want to set it to normal, the gl_true parameter value will be passed.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.