Assigning One Color per Object in the Scene

To keep things simple, we will pick an object based on its primitive color. That is, we discard the shiny reflections or shadows, and render the object with its uniform color. This is important, because to pick an object based on a color, we need to make sure that the color is constant per object and that each object has a different unique color.

We achieve constant coloring by telling the fragment shader to use only the material diffuse property to set the ESSL output color variable. We are assuming that each object has a unique diffuse property.

In situations where objects may share the same diffuse color, we can create a new ESSL uniform to store the picking color and make it unique for every object that's rendered into the offscreen framebuffer. This way, the objects will look the same when they are rendered on the screen, but their colors will be unique every time they are rendered into the offscreen framebuffer. Later in this chapter, we will implement this strategy along with other approaches for uniquely identifying objects.

For now, let's assume that the objects in our scene have unique diffuse colors, as shown in the following diagram:

Let's see how to render the scene offscreen by using the framebuffer we just set up.