Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 8745 in orxonox.OLD


Ignore:
Timestamp:
Jun 23, 2006, 4:26:49 PM (18 years ago)
Author:
ponder
Message:
  • Optimized the multi-layer renderings. pages which do not show a certain material aren't rendered anymore.
Location:
branches/terrain/src/lib/graphics/importer/terrain
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • branches/terrain/src/lib/graphics/importer/terrain/terrain.cc

    r8744 r8745  
    3333        pagesZ = (heightfield.height/(pageSize-1) );
    3434       
    35         //TODO: Determine layer visibility!     
    36         for ( unsigned int i = 0; i < layers.size(); ++i ) {
    37                
    38         }
    3935       
    4036        pages = new pTerrainPage[pagesX*pagesZ];       
     
    5248       
    5349        root = createQuadTree( 0, 0, pagesX, pagesZ );
     50       
     51        for ( unsigned int i = 0; i < layers.size(); ++i ) {
     52                determineLayerVisibility( i );
     53        }       
    5454        activePages = NULL;
    5555}
     
    321321}
    322322
    323 
     323inline Uint8 getAlpha( const SDL_Surface *_s, int _x, int _y )
     324{
     325        printf( "getalpha: %d, %d\n", _x, _y );
     326    int bpp = _s->format->BytesPerPixel;
     327    Uint8 *p = (Uint8 *)_s->pixels + _y*_s->pitch + _x * bpp;
     328        Uint32 pixel = 0;
     329    switch( bpp ) {
     330    case 1:
     331        pixel = *p;
     332                break;
     333    case 2:
     334        pixel = *(Uint16 *)p;
     335                break;
     336    case 3:
     337        if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
     338            pixel = p[0] << 16 | p[1] << 8 | p[2];
     339        else
     340            pixel = p[0] | p[1] << 8 | p[2] << 16;
     341                break;
     342    case 4:
     343        pixel = *(Uint32 *)p;
     344                break;
     345    default:
     346        return 0;       /* shouldn't happen, but avoids warnings */
     347    }
     348        Uint8 r,g,b,a;
     349        SDL_GetRGBA( pixel, _s->format, &r, &g, &b, &a );
     350        return a;
     351}
     352
     353void Terrain::determineLayerVisibility( int _layer )
     354{
     355        LayerInfo * layer = layers[_layer];
     356        if ( !layer->alpha ) {
     357                int numPages = pagesX*pagesZ;
     358                for ( int i = 0; i < numPages; ++i )
     359                        pages[i]->setLayerVisibility( _layer, LV_FULL );
     360                       
     361                return;
     362        }
     363        SDL_Surface *alpha = const_cast<SDL_Surface*>( layer->alpha->getStoredImage() );       
     364        SDL_LockSurface( alpha );
     365        float du = ( (float)alpha->w)/pagesX;
     366        float dv = ( (float)alpha->h)/pagesZ;
     367        float u = 0.0f, v = 0.0f;
     368        for ( int pageX = 0; pageX < pagesX; ++pageX ) {
     369                v = 0.0f;
     370                for ( int pageZ = 0; pageZ < pagesZ; ++pageZ ) {
     371                        bool full = true; bool has = false;
     372                        for ( int x = 0; x < (int)PAGE_SIZE; ++x ) {
     373                                for ( int z = 0; z < (int)PAGE_SIZE; ++z ) {
     374                                        Uint8 a = getAlpha( alpha, (int)u, (int)v );
     375                                        if ( a )
     376                                                has = true;
     377                                        if ( a < 250 )
     378                                                full = false;
     379                                }
     380                        }
     381                        LayerVisibility lv;
     382                        if ( has ) {
     383                                if ( full )
     384                                        lv = LV_FULL;
     385                                else
     386                                        lv = LV_PARTIAL;
     387                        }
     388                        else {
     389                                lv = LV_NO;
     390                        }
     391                        getPage( pageX, pageZ )->setLayerVisibility( _layer, lv );
     392                        v+= dv;
     393                }
     394                u+= du;
     395        }
     396        SDL_UnlockSurface( alpha );
     397}
    324398
    325399void Terrain::getAltitude( Triple& _alt, Triple& _normal )
  • branches/terrain/src/lib/graphics/importer/terrain/terrain.h

    r8744 r8745  
    3333typedef Terrain *pTerrain;
    3434       
    35 struct Heightfield {
    36         int                     width;
    37         int                     height;
    38         int                             pitch;
    39         UByte                   *data;
    40 };
    41 
    42 struct LayerInfo {
    43 
    44         int                             envmode;
    45         float                   repeatX,
    46                                         repeatZ;
    47         Texture                 *alpha;
    48         Texture                 *detail;
    49        
    50 };
    51 
    52 struct BufferInfo {
    53        
    54         BufferInfo( unsigned int _vb, unsigned int _ib,
    55                 unsigned int _numV, unsigned int _numI )
    56         {
    57                 vbIdentifier = _vb; ibIdentifier = _ib;
    58                 numIndices = _numI; numVertices =_numV;
    59                
    60         }
    61        
    62         BufferInfo()
    63         {
    64                 vbIdentifier = ibIdentifier = numIndices = numVertices = 0;
    65         }
    66         unsigned int    vbIdentifier,
    67                                         ibIdentifier,
    68                                         numIndices,
    69                                         numVertices;   
    70 };
    71 
    72 
    7335class Terrain {
    7436        public:
    75                 const static int PAGE_SIZE                      = 17;
    76                 const static int MAX_VERTICES           = PAGE_SIZE*PAGE_SIZE;
    77                 const static int MAX_INDICES            = 2*( PAGE_SIZE*PAGE_SIZE + PAGE_SIZE -2 );
     37                const static unsigned int PAGE_SIZE             = 17;
     38                const static unsigned int MAX_VERTICES          = PAGE_SIZE*PAGE_SIZE;
     39                const static unsigned int MAX_INDICES           = 2*( PAGE_SIZE*PAGE_SIZE + PAGE_SIZE -2 );
    7840                /**
    7941                 * The amount of geometry rendered is largely depending on this constant. So chose a
     
    209171                 * frustum of the camera.
    210172                 */
    211                 void determineVisiblePages( pTerrainQuad _node );                                               
     173                void determineVisiblePages( pTerrainQuad _node );                       
     174                void determineLayerVisibility( int _layer );                   
    212175               
    213176#ifdef USE_VBO
  • branches/terrain/src/lib/graphics/importer/terrain/terrain_page.cc

    r8744 r8745  
    4040        previous = NULL;
    4141        currentLOD = -1;
     42        hasfull = false;
    4243        forceTesselation = true;
    4344        vbIdentifier = 0; ibIdentifier = 0;
     
    773774        _vertex.z = position.z+scale.z*_z/( owner->getPageSize()-1 );
    774775}
    775 
     776void TerrainPage::setLayerVisibility( int _layer, LayerVisibility _lv )
     777{
     778        if ( hasfull ) return;
     779        if ( _lv == LV_FULL ) {
     780                for ( int i = 0; i < 8; ++i )
     781                        layerVisibility[i] = LV_NO;
     782                if ( _layer )
     783                        hasfull = true;
     784        }       
     785        printf( "setting layer %d vis to %d\n", _lv ); 
     786        layerVisibility[_layer] = _lv; 
     787}
     788
     789bool TerrainPage::hasMaterial( int _layer )
     790{
     791        return ( layerVisibility[_layer] != LV_NO );
     792}
    776793void TerrainPage::calculateErrors()
    777794{
  • branches/terrain/src/lib/graphics/importer/terrain/terrain_page.h

    r8741 r8745  
    2424class TerrainPage;
    2525class Terrain;
    26 
    2726typedef TerrainPage *pTerrainPage;
    2827
     
    185184                 */
    186185                inline pTerrainPage getNext() { return next; }
    187                
     186                void setLayerVisibility( int _layer, LayerVisibility _lv );
     187                bool hasMaterial( int _layer );         
    188188                /**
    189189                 * Returns the previous active page
     
    206206                 */
    207207                void activate();               
    208                 inline bool hasMaterial( int _i )
    209                 {
    210                         return true;
    211                 }
     208
    212209                inline void setWantedLOD( int _lod )
    213210                {
     
    286283                int                                                     numIndices;
    287284                int                                                     numVertices;
    288                
     285                LayerVisibility                         layerVisibility[8];
     286                bool                                            hasfull;
    289287#ifdef  USE_VBO
    290288                GLuint                                          ibIdentifier,
  • branches/terrain/src/lib/graphics/importer/terrain/types.h

    r8697 r8745  
    2020#endif
    2121#include <math.h>
    22 
     22#include "texture.h"
    2323
    2424struct Triple {
     
    139139}
    140140typedef unsigned char UByte;
     141struct Heightfield {
     142        int                     width;
     143        int                     height;
     144        int                             pitch;
     145        UByte                   *data;
     146};
     147
     148struct LayerInfo {
     149
     150        int                             envmode;
     151        float                   repeatX,
     152                                        repeatZ;
     153        Texture                 *alpha;
     154        Texture                 *detail;
     155       
     156};
     157typedef enum { LV_FULL, LV_PARTIAL, LV_NO } LayerVisibility;
     158
     159struct BufferInfo {
     160       
     161        BufferInfo( unsigned int _vb, unsigned int _ib,
     162                unsigned int _numV, unsigned int _numI )
     163        {
     164                vbIdentifier = _vb; ibIdentifier = _ib;
     165                numIndices = _numI; numVertices =_numV;
     166               
     167        }
     168       
     169        BufferInfo()
     170        {
     171                vbIdentifier = ibIdentifier = numIndices = numVertices = 0;
     172        }
     173        unsigned int    vbIdentifier,
     174                                        ibIdentifier,
     175                                        numIndices,
     176                                        numVertices;   
     177};
    141178#endif
Note: See TracChangeset for help on using the changeset viewer.