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:

Drawing the Scene

The main function is used to draw the triangle to the screen.  All of the new code (as opposed to the old code from tutorial 1) used to draw the triangle will appear in bold.

int main()
{
   int frame;
   int odev;
   int r = 0, g = 0, b = 255;

   g_gp = ps2_gs_get_gparam();
   acquire();
   ps2_gs_vc_graphicsmode();

   ps2_gs_set_dbuff(&g_db, PS2_GS_PSMCT32, g_gp->width, 
        g_gp->height, PS2_GS_ZGREATER, PS2_GS_PSMZ24, 1);

   *(__u64*)&g_db.clear0.rgbaq = PS2_GS_SETREG_RGBAQ(r,g,b,0,0);
   *(__u64*)&g_db.clear1.rgbaq = PS2_GS_SETREG_RGBAQ(r,g,b,0,0);

   setTriangle();

   ps2_gs_start_display(0);

   ps2_gs_vc_enablevcswitch(acquire, release);

   for(frame = 0; ;frame++)
   {
      ps2_gs_vc_lock();
      
      odev = !ps2_gs_sync_v(0);
      
      ps2_gs_set_half_offset((frame & 1) ? &g_db.draw1 :
                                           &g_db.draw0, odev);

      ps2_gs_swap_dbuff(&g_db, frame);

      ps2_gs_put_drawenv((frame & 1) ? &g_db.giftag1 : 
                                       &g_db.giftag0);

      ps2_gs_put_drawenv(&tri.giftag);

      ps2_gs_set_finish(&g_finish);
      ps2_gs_wait_finish(&g_finish);

      ps2_gs_vc_unlock();
   }
   release();
   return 0;
}

Only three changes have been made to the main function and one of them is old code moved to a new place.  First *(__u64*)&g_db.clear0.rgbaq = PS2_GS_SETREG_RGBAQ(r,g,b,0,0) and the identical setting for buffer two have been moved out of the main loop and up into the initializing. This is done since there is no reason to set the clear color every frame.

Next up the setTriangle function is called to set up the triangle, and then in the main loop we send the triangle's GIFtag to the GS to be displayed on the screen.  Don't forget to include the acquire and release functions, they were not included in this tutorial because they remain the same as in tutorial 1.

To add more color to the finished project we will add more colors and blend them throughout the triangle using gouraud shading.  First, in setTriangle() change PS2_GS_PRIM_IIP_FLAT to PS2_GS_PRIM_IIP_GOURAUD to enable gouraud shading.  Then make each vertex of the triangle a different color.  To get the color triangle, leave vertex 0 full red (255,0,0,0,0), make vertex 1 full green(0,255,0,0,0), and vertex 2 full blue (0,0,255,0,0).  To set the triangle off from the background, set the r,b, and b variables in main to zero.  You should now have a perfectly blended triangle on a black background.




Contents
  Introduction
  Triangle Settings
  Drawing the Scene

  Printable version
  Discuss this article

The Series
  Part 1: Initialization
  Part 2: Drawing Primitives