Creating Transparent Objects

We’ve learned that in order to create transparency, we need to:

  • Enable alpha blending and select the interpolative blending function
  • Render the faces of objects back to front

How do we create transparent objects when there is nothing to blend them against? In other words, if there’s only one object, how can we make it transparent? One solution is to use face-culling.

Face-culling allows us to only render the back or front face of an object. We used this technique in the previous section when we only rendered the front face by enabling the Back Face Culling button.

Let's use the color cube from earlier in this chapter. We are going to make it transparent. For that effect, we will perform the following:

  1. Enable alpha blending and use the interpolative blending mode.
  2. Enable face-culling.
  3. Render the back face (by culling the front face).
  4. Render the front face (by culling the back face).

Similar to other options in the pipeline, culling is disabled by default. We enable it by calling the following:

gl.enable(gl.FACE_CULLING);

To render only the back faces of an object, we call gl.cullFace(gl.FRONT) before we call drawArrays or drawElements.

Similarly, to render only the front face, we use gl.cullFace(gl.BACK) before the draw call.

The following diagram summarizes the steps needed to create a transparent object with alpha blending and face-culling:

In the following section, we will see the transparent cube in action and take a look at the code that makes it possible.

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

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