Upcoming Events
Unite 2010
11/10 - 11/12 @ Montréal, Canada

GDC China
12/5 - 12/7 @ Shanghai, China

Asia Game Show 2010
12/24 - 12/27  

GDC 2011
2/28 - 3/4 @ San Francisco, CA

More events...
Quick Stats
103 people currently visiting GDNet.
2406 articles in the reference section.

Help us fight cancer!
Join SETI Team GDNet!
Link to us Events 4 Gamers
Intel sponsors gamedev.net search:


Part two of the series covers drawing primitives, enabling settings like Gouraud shading, and basic controller access.  This tutorial builds upon the source code from Part 1, and while all the source code will be listed, only the new code will be explained.

The Triangle Structure

First we will draw a single triangle to the screen.  The first thing we need is a structure to hold it.

#include <ps2gs.h>

int g_fd_gs;
ps2_gs_gparam *g_gp;
ps2_gs_finish g_finish;

int acquire();
int release();

typedef struct
   ps2_giftag giftag;
   __u64 prim;
   char  prim_adrs;
   __u64 rgb0;
   char  rgb0_adrs;
   __u64 v0;
   char  v0_adrs;
   __u64 rgb1;
   char rgb1_adrs;
   __u64 v1;
   char  v1_adrs;
   __u64 rgb2;
   char  rgb2_adrs;
   __u64 v2;
   char  v2_adrs;
} Triangle;

Triangle tri;

The structure Triangle is used to hold all the data for the triangle.  The first variable in the structure is a GIFtag. The GIF is the interface between the PS2's main processor (Emotion Engine) and the Graphics Synthesizer (GS).  Anytime a primitive is sent to the GS to be displayed at must have a GIFtag before it.  The GIFtag specifies the structure, size, and format of the data following it.  The GIFtag will be explained in detail when we get to the relevant code.

Next are the variables to hold the primitive structure.  Prim is a 64-bit variable used to hold the settings for the primitive being drawn.  Settings like primitive type, alpha blending, shading, and fog are stored here.  Next is the variable to hold the address of the prim register.  The rest of the data is stored in the same way.  First a 64-bit variable to hold the data, and then an 8-bit (char) variable to store the register address.  The data consists of the vertex color (rgb0 through rgb2), the address of the register used to set the colors (rgb0_adrs through rgb2_adrs),   the vertex coordinates (v0 through v2), and the address of the register used to set the vertex coordinates (v0_adrs through v2_adrs).

The format of the Triangle structure follows the PS2 packing format A+D, it is one qword long(128 bits).  The following diagram shows the A+D format.



First, bits 0 through 63 hold the output data.  Then bits 64 through 71 hold the address of the register.  Because the format is 128 bits long, it is important to note that it is possible to make both the data and address variables 64 bits long.  It is then easier to make large primitives like a triangle strip or fan.  Instead of making a new set of data and address variables for each vertex, you can make a 64 bit array.

Triangle Settings

  Triangle Settings
  Drawing the Scene

  Printable version
  Discuss this article

The Series
  Part 1: Initialization
  Part 2: Drawing Primitives