region.hh

Go to the documentation of this file.
00001 /*
00002   region.hh
00003   data structures supporting multi-resolution ray tracing in world class.
00004   Copyright Richard Vaughan 2008
00005 */
00006 
00007 const uint32_t RBITS = 6; // regions contain (2^RBITS)^2 pixels
00008 const uint32_t SBITS = 5; // superregions contain (2^SBITS)^2 regions
00009 const uint32_t SRBITS = RBITS+SBITS;
00010 
00011 typedef struct
00012 {
00013   GSList* list;
00014 }  stg_cell_t;
00015 
00016 class Stg::Region
00017 {
00018   friend class SuperRegion;
00019   
00020 private:  
00021   static const uint32_t REGIONWIDTH = 1<<RBITS;
00022   static const uint32_t REGIONSIZE = REGIONWIDTH*REGIONWIDTH;
00023   
00024   stg_cell_t cells[REGIONSIZE];
00025   
00026 public:
00027   uint32_t count; // number of blocks rendered into these cells
00028   
00029   Region()
00030   {
00031     bzero( cells, REGIONSIZE * sizeof(stg_cell_t));
00032     count = 0;
00033   }
00034   
00035   stg_cell_t* GetCell( int32_t x, int32_t y )
00036   {
00037     uint32_t index = x + (y*REGIONWIDTH);
00038     assert( index >=0 );
00039     assert( index < REGIONSIZE );    
00040     return &cells[index];    
00041   }
00042   
00043   // add a block to a region cell specified in REGION coordinates
00044   void AddBlock( StgBlock* block, int32_t x, int32_t y, unsigned int* count2 )
00045   {
00046     stg_cell_t* cell = GetCell( x, y );
00047     cell->list = g_slist_prepend( cell->list, block ); 
00048     block->RecordRenderPoint( &cell->list, cell->list, &this->count, count2 );    
00049     count++;
00050   }  
00051 };
00052 
00053 
00054 class Stg::SuperRegion
00055 {
00056   friend class StgWorld;
00057  
00058 private:
00059   static const uint32_t SUPERREGIONWIDTH = 1<<SBITS;
00060   static const uint32_t SUPERREGIONSIZE = SUPERREGIONWIDTH*SUPERREGIONWIDTH;
00061   
00062   Region regions[SUPERREGIONSIZE];  
00063   uint32_t count; // number of blocks rendered into these regions
00064   
00065   stg_point_int_t origin;
00066   
00067 public:
00068   
00069   SuperRegion( int32_t x, int32_t y )
00070   {
00071     count = 0;
00072     origin.x = x;
00073     origin.y = y;
00074   }
00075   
00076   ~SuperRegion()
00077   {
00078   }
00079 
00080   // get the region x,y from the region array
00081   Region* GetRegion( int32_t x, int32_t y )
00082   {
00083     int32_t index =  x + (y*SUPERREGIONWIDTH);
00084     assert( index >=0 );
00085     assert( index < (int)SUPERREGIONSIZE );
00086     return &regions[ index ];
00087   } 
00088   
00089   // add a block to a cell specified in superregion coordinates
00090   void AddBlock( StgBlock* block, int32_t x, int32_t y )
00091   {
00092     GetRegion( x>>RBITS, y>>RBITS )
00093       ->AddBlock( block,  
00094           x - ((x>>RBITS)<<RBITS),
00095           y - ((y>>RBITS)<<RBITS),
00096           &count);         
00097     count++;
00098   }
00099 
00100   // callback wrapper for Draw()
00101   static void Draw_cb( gpointer dummykey, 
00102                SuperRegion* sr, 
00103                gpointer dummyval )
00104   {
00105     sr->Draw();
00106   }
00107   
00108   void Draw()
00109   {
00110     glPushMatrix();
00111     glTranslatef( origin.x<<SRBITS, origin.y<<SRBITS,0);
00112         
00113     glColor3f( 1,0,0 );    
00114 
00115     for( unsigned int x=0; x<SUPERREGIONWIDTH; x++ )
00116       for( unsigned int y=0; y<SUPERREGIONWIDTH; y++ )
00117     {
00118       Region* r = GetRegion(x,y);
00119       
00120       for( unsigned int p=0; p<Region::REGIONWIDTH; p++ )
00121         for( unsigned int q=0; q<Region::REGIONWIDTH; q++ )
00122           if( r->cells[p+(q*Region::REGIONWIDTH)].list )
00123         glRecti( p+(x<<RBITS), q+(y<<RBITS),
00124              1+p+(x<<RBITS), 1+q+(y<<RBITS) );
00125     }
00126 
00127     // outline regions
00128     glColor3f( 0,1,0 );    
00129     for( unsigned int x=0; x<SUPERREGIONWIDTH; x++ )
00130       for( unsigned int y=0; y<SUPERREGIONWIDTH; y++ )
00131     glRecti( x<<RBITS, y<<RBITS, 
00132          (x+1)<<RBITS, (y+1)<<RBITS );
00133     
00134     // outline superregion
00135     glColor3f( 0,0,1 );    
00136     glRecti( 0,0, 1<<SRBITS, 1<<SRBITS );
00137 
00138     glPopMatrix();    
00139   }
00140   
00141   static void Floor_cb( gpointer dummykey, 
00142             SuperRegion* sr, 
00143             gpointer dummyval )
00144   {
00145     sr->Floor();
00146   }
00147   
00148   void Floor()
00149   {
00150     glPushMatrix();
00151     glTranslatef( origin.x<<SRBITS, origin.y<<SRBITS, 0 );        
00152     glRecti( 0,0, 1<<SRBITS, 1<<SRBITS );
00153     glPopMatrix();    
00154   }
00155 };

Generated on Thu Jan 7 17:42:16 2010 for Stage by  doxygen 1.5.5