CHAPTER 10 TEXTURE RECTANGLES


In Chapter 9, we explained from a variety of different angles the methods for mapping textures to polygons. In fact, it might seem like we have covered all areas of this topic. When you are putting together a game application, there often will be times when in addition to mapping textures to polygons, you will also want to display simple 2D graphics on the screen. So in this chapter, we bring up the subject of texture rectangles as a method for drawing 2D graphics.


10.1 Introduction to Texture Rectangles

With the N64, you have a number of choices when rendering textures as 2D graphics. You can:

  1. Draw them as texture rectangles
  2. Draw them as sprites, using the sprite microcode
  3. Draw them by emulating sprite functions with the 3D microcode

From the point of view of performance, the first two are the most practical choices. An explanation of how to use the sprite microcode would be very lengthy and is beyond the scope of this tutorial. Here we will limit ourselves to the topic of texture rectangles. Since we have already gone into great detail about textures, it should be easy to understand how to render using texture rectangles.

You use the gSPTextureRectangle command or the gSPScisTextureRectangle command to render with texture rectangles. Both commands define texture rectangle primitives.

  gSPTextureRectangle(  |  gSPScisTextureRectangle(
    Gfx *gdl,  |    Gfx *gdl,
    u32 ulx,  |    s32 ulx,
    u32 uly,  |    s32 uly,
    u32 lrx,  |    s32 lrx,
    u32 lry,  |    s32 lry,
    s32 tile,  |    s32 tile,
    s32 s,  |    s32 s,
    s32 t,  |    s32 t,
    s32 dsdx,  |    s32 dsdx,
    s32 dtdy)  |   s32 dtdy)

  gdl Display list pointer
  ulx Rectangle's upper-left x-coordinate (10.2, 0.0 - 1023.75)
  uly Rectangle's upper-left y-coordinate (10.2, 0.0 - 1023.75)
  lrx Rectangle's lower-right x-coordinate (10.2, 0.0 - 1023.75)
  lry Rectangle's lower-right y-coordinate (10.2, 0.0 - 1023.75)
  tile Tile descriptor index (3bit precision, 0 - 7)
  s Texture s-coordinate of upper-left corner of rectangle (s10.5)
  t Texture t-coordinate of upper-left corner of rectangle (s10.5)
  dsdx Change in s per change in x (s5.10)
  dtdy Change in t per change in y (s5.10)

The thing you need to be careful about with these two commands is that in both 1-Cycle mode and 2-Cycle mode, the right-most column and the bottom-most row are not rendered for reasons of anti-aliasing. On the other hand, in Copy mode the entire specified rectangle is rendered.

In the DrawSmallRectangle function in the gfx10.c sample program, the static variables posx and posy define the upper-left screen coordinates for the drawing of a small texture rectangle.

List 10-1

  /* Draw a texture rectangle */
  gSPTextureRectangle(glistp++,
                   posx << 2, posy << 2,
                   (posx + RECT_WD) << 2, (posy + RECT_HT) << 2,
                   G_TX_RENDERTILE,
                   0 << 5, 0 << 5,
                   1 << 10, 1 << 10);

You must turn texture perspective off when drawing texture rectangles.

List 10-2

  /* Texture rectangle, so turn texture perspective correction off  */
  gDPSetTexturePersp(glistp++, G_TP_NONE);