2.2 Primitive Color

Primitive color gives color to the entire "drawn primitive" (e.g., one triangle). You can set it using the gDPSetPrimColor command. The gDPSetPrimColor pseudo-prototype is shown below (it is a macro, so no warning is given even if the types do not agree).

  gDPSetPrimColor(Gfx * gdl, unsigned int m, unsigned int l,
     unsigned int r, unsigned int g, unsigned int b, unsigned int a);

As you can guess from the names, "r, g, b and a" are the red, green, blue and alpha values of the primitive color. The existence of "m" and "l" parameters will not be of concern until LOD is explained in Chapter 9. The primitive color settings are extremely simple, so for an abbreviated example of the use of gDPSetPrimColor, some source extracted from the gfx2.c sample program is provided below:

List 2-1

  /*
   *  To attach color using primitive color, try assigning
   *  a different primitive color to each surface.   */

  /* Create surface 1 */
  gDPSetPrimColor(glistp++, 0, 0, 255, 0, 0, 255);
  gSP2Triangles(glistp++, 4, 6, 7, 0, 4, 5, 6, 0);
  gDPPipeSync(glistp++);
  /* Create surface 2 */
  gDPSetPrimColor(glistp++, 0, 0, 0, 255, 0, 255);
  gSP2Triangles(glistp++, 6, 5, 2, 1, 2, 5, 1, 1);
  gDPPipeSync(glistp++);

In this example, surface 1 is colored red and surface 2 is colored green. When you color a model this way, the colors are not affected by the direction in which the model is facing or by the presence or absence of lights.

Next, the meaning of DPPipeSync, which is inserted between gSP2Triangles and gDPSetPrimColor will be explained. To do this, observe what happens when DPPipeSync is deleted from the display list:

List 2-2

  /* Create surface 1 */
  gDPSetPrimColor(glistp++, 0, 0, 255, 0, 0, 255);
  gSP2Triangles(glistp++, 4, 6, 7, 0, 4, 5, 6, 0);
  /* No gDPPipeSync */
  /* Create surface 2 */
  gDPSetPrimColor(glistp++, 0, 0, 0, 255, 0, 255);
  gSP2Triangles(glistp++, 6, 5, 2, 1, 2, 5, 1, 1);

When the image is rendered according to this display list, the rendering of surface 1 begins after the primitive color has been set to red. One might think that once the rendering of surface 1 is completed, the primitive color of surface 2 (red) is set and then the rendering of surface 2 begins. However, pipeline processing occurs within the RDP, so sometimes the next command starts to execute before the previous command has been completed. In other words, the primitive color for surface 2 might be set while surface 1 is still being rendered, in which case surface 1 would be colored both red and green.

Figure 2-2 Schematic of Pipeline Processing

To counter this problem, you insert the gDPPipeSync command between the command that defines the primitive and the command that updates the primitive attributes.

As its name implies, the gDPPipeSync command acts to synchronize pipeline processing and attribute updates, waiting when necessary in order to prevent the kind of inconsistent rendering mentioned above.