forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Terrain generation using heightmaps

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PS2 Development
View previous topic :: View next topic  
Author Message
shamus88



Joined: 27 Nov 2008
Posts: 2

PostPosted: Thu Nov 27, 2008 7:23 am    Post subject: Terrain generation using heightmaps Reply with quote

Hi all!

I am new here so please forgive me if this is in the wrong forum.

I have recently begun to learn game programming on the ps2 linux devkit. I am working based on a framework written by Henry Fortuna.

However, I am wondering if anyone had any experience of expanding the framework to read heightmap information from RAW files and render it? I can currently generate a flat trianglestrip patch which I can stretch a texture over it. This is handle by a terrain class in terrain.cpp. I am unsure of how to modify this to read in information from RAW files and then to pack the necessary y-axis information into the VU.

I hope that someone have any experience here in generating a heightmap in PS2 and could lend me a helping hand in this project.

I can link/provide the necessary framework files/code if needed.

Thanks in advance!
Back to top
View user's profile Send private message
shamus88



Joined: 27 Nov 2008
Posts: 2

PostPosted: Fri Dec 05, 2008 10:11 am    Post subject: Re: Terrain generation using heightmaps Reply with quote

Hmmm, noone at all? :(

I can draw a flat terrain consisting of triangle strips zig zagging from left to right , producing a flat patch. But any attempts at modifying the code to accept data from RAW files made the PS2 crash so far. Here is the relevant part of the code I hope someone could help me with :

Code:

void CTerrain::LoadTerrainData(void)
{
   // Get the address that we can call to later.
   m_iStaticAddr = VIFStaticDMA.GetPointer();

   VIFStaticDMA.Add32(FLUSH);         // Make sure VU1 isn't busy
   VIFStaticDMA.Add32(STCYCL(1,1));   // Unpack linearly, i.e. don't skip any spaces
   VIFStaticDMA.Add32(BASE(32));      // The double buffers start at VU1 address 16 (giving us 16 QW to store data that won't change)
   VIFStaticDMA.Add32(OFFSET(496));   // The size of each buffer.

   VIFStaticDMA.AddUnpack(V4_32, 0, 1);   // Unpack 8QW of data that won't change
   VIFStaticDMA.AddVector(Vector4(2048, 2048, (0xFFFFFF / 32.0f), 1));   // 0: The scales

   // Set the PRIM field here. This may look like it's for convenience, but actually it's the only
   // way we can do it. If we set this in the GIFTag it would reset the strip every buffer.
   // As we want one huge strip, we can't use the prim setting capabilites of the GIFTag.
   VIFStaticDMA.StartDirect();
   VIFStaticDMA.StartAD();
   VIFStaticDMA.AddAD(PRIM_SET(GS_TRISTRIP, 1, 1, 0, 0, 0, 0, 0, 0), PRIM);
   VIFStaticDMA.EndAD();
   VIFStaticDMA.EndDirect();
   

   // The verts are 3QW each, and the 504QW is split in half. Therefore we have 252QW for each buffer.
   // Each buffer also has 2QW of info (GIFTag + Numverts), so we can process 83 vertices (249QW)
   // in each buffer.
   int iNumVertsPerChunk = 82;

   // The number of triangles in each dimension.
   int iSizeX = 256;
   int iSizeY = 256;

   int iNumVerts = (iSizeX * 2) * (iSizeY - 1);

   int iX = 0, iY = 0;
   int bBottomRow = 0;
   bool bFirst = true;

   while(iNumVerts > 0)
   {
      int iVertsThisChunk;

      if(iNumVerts > iNumVertsPerChunk)
      {
         iVertsThisChunk = iNumVertsPerChunk;
         iNumVerts -= iVertsThisChunk;
      }
      else
      {
         iVertsThisChunk = iNumVerts;
         iNumVerts -= iVertsThisChunk;
      }

      VIFStaticDMA.AddUnpack(V4_32, 0, iVertsThisChunk * 3 + 2, 1);
      VIFStaticDMA.Add128(iVertsThisChunk * 3);
      VIFStaticDMA.Add128(
         GS_GIFTAG_BATCH(iVertsThisChunk,
               1, 0, 0, GIF_FLG_PACKED,
               GS_BATCH_3(GIF_REG_ST, GIF_REG_RGBAQ, GIF_REG_XYZ2)));

      // Zigzag from left to right across the patch making a huge triangle strip.
      //
      //   0         2      4      6
      //  |     / |     / |     / |
        //  |   /   |   /   |   /   |
      //   1 /      3 /      5 /      7 etc.
      //
      for(int iVert = 0; iVert < iVertsThisChunk; iVert++)
      {
         float fX = (float)iX;
         float fY = (float)(iY + bBottomRow);

         // Disable the drawing kick when we move to the next row, so that we don't get a huge
         // polygon going across the patch.
         int iDontDraw = (iX == 0) ? 1 : 0;

         // Alternate between the current row and the one below.
         iX += bBottomRow;
         bBottomRow ^= 1;

         // If we have reached the end, start again on the next row.
         if(iX == iSizeX)
         {
            iX = 0;
            bBottomRow = 0;
            iY += 1;
         }

         // Add a huge number of vertices
         VIFStaticDMA.AddVector(Vector4(fX / iSizeX, fY / iSizeY, 1, *((float *)&iDontDraw)));   // Tex coords + in W, a flag to set ADC or not.
         VIFStaticDMA.AddVector(Vector4(0, 1, 0, 0));                                 // Normal
         VIFStaticDMA.AddVector(Vector4((fX - (iSizeX / 2)), 0, (fY - (iSizeY / 2)), 1));      // Position
      }

      if(bFirst)
      {
         bFirst = false;
         // If this is the first buffer then run the VU code from address 0 so that the initialisation
         // VU code is ran.
         VIFStaticDMA.Add32(MSCALL(0));
      }
      else
      {
         // For multiple times MSCNT means to continue where the VU code finished last time at the --cont.
         VIFStaticDMA.Add32(MSCNT);
      }

      // Notice that due to the quad buffer setup we don't need to flush here because we can upload, process, and render
      // all in parallel now. (the XGKick in the vector code will flush to make sure that we don't get ahead of ourselves)
   }

   VIFStaticDMA.Add32(FLUSH);
   VIFStaticDMA.DMARet();
   
   
   
   
}

Thanks.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PS2 Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group