Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 8741 in orxonox.OLD


Ignore:
Timestamp:
Jun 23, 2006, 11:29:40 AM (18 years ago)
Author:
ponder
Message:
  • Finally, I got the terrain working completly. What a hard work… It took me lots and lots of hours to remove all bugs in the tesselation routines.
Location:
branches/terrain/src
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • branches/terrain/src/lib/collision_detection/cd_engine.cc

    r8715 r8741  
    119119        if ( tl == NULL )
    120120                return;
     121        float offset = 8.0f;
    121122        for ( ti = tl->begin(); ti != tl->end(); ++ti ) {
    122123                        TerrainEntity* terrain = dynamic_cast<TerrainEntity*>( *ti );
    123124      for( entityIterator = list1.begin(); entityIterator != list1.end(); entityIterator++ )
    124125                {
    125                                 const Vector& pos = (*entityIterator)->getAbsCoor();
    126                                
    127                                 float height = terrain->getHeight( pos.x, pos.z );
    128                                 printf( "pos.y-height: %f\n", pos.y-height );
    129                                 if ( height > pos.y ) {
    130                                         (*entityIterator)->setAbsCoor( pos.x, height, pos.z );
     126                                Vector pos( (*entityIterator)->getAbsCoor() );
     127                                Vector normal;
     128                                float height = pos.y;
     129                                terrain->getAltitude( pos, normal );
     130                                if ( height-offset < pos.y ) {
     131                                        ( *entityIterator )->registerCollision(
     132                                                *entityIterator, terrain, pos, normal );
    131133                                }
    132134      }
  • branches/terrain/src/lib/graphics/importer/terrain/terrain.cc

    r8697 r8741  
    2929        memcpy( heightfield.data, tmpData->pixels, sizeof(UByte)*dataSize );
    3030        SDL_FreeSurface( tmpData );
    31         //TODO: Perform some checks...
     31        //TODO: Perform some checks... size, bits and so on
    3232        pagesX = (heightfield.width/(pageSize-1) );
    3333        pagesZ = (heightfield.height/(pageSize-1) );
    34         //tex = (Texture*)MANAGER->load( lightmapSource );
    3534        //TODO: Determine layer visibility!     
    3635        for ( unsigned int i = 0; i < materials.size(); ++i ) {
    3736               
    3837        }
    39 
    40         printf( " * creating terrain pages ( %d, %d )...", pagesX, pagesZ );
    4138        pages = new pTerrainPage[pagesX*pagesZ];       
    4239        for ( int x = 0; x < pagesX; ++x )
    4340                for ( int z = 0; z < pagesZ; ++z )
    4441                        pages[z*pagesX+x] = createPage( x, z );
    45         printf( "looks good\n" );
    46         printf( " * inform pages about the adjacent pages..." );                       
    4742        //Inform each page about its neighbors.
    4843        for ( int x = 0; x < pagesX; ++x )
     
    5449                                z > 0                   ? getPage( x+0, z-1 ) : NULL );
    5550       
    56         printf( "looks good\n" );
    57         printf( " * creating quad_tree data structure..." );
    5851        root = createQuadTree( 0, 0, pagesX, pagesZ );
    5952        activePages = NULL;
    60         printf( "looks good\n" );
    6153}
    6254
     
    10496                glBufferDataARB( GL_ELEMENT_ARRAY_BUFFER_ARB, MAX_INDICES*sizeof( short ),
    10597                        indices, GL_DYNAMIC_DRAW_ARB );
    106                 //printf( "uploaded %d verts and %d indices\n", bi.numIndices, bi.numVertices );
    10798                BufferInfo newInfo;
    10899                broker->acquire( newInfo.vbIdentifier, newInfo.ibIdentifier );                 
     
    114105        memcpy( &vertices[bi.numVertices], _vertices,
    115106                _numVertices*sizeof( Vertex ) );
    116         bi.numVertices+=_numVertices;
    117107        //The indices need to be updated with an offset :(
    118         unsigned short *end = _indices+_numIndices;
    119         unsigned short *dst = indices+bi.numIndices;
    120         int offset = bi.numIndices;
    121         if ( bi.numIndices > 0 ) {
    122                 indices[bi.numIndices] = indices[bi.numIndices-1];
    123                 indices[bi.numIndices+1] = _indices[0]+offset;
    124                 dst+=2;
    125                 bi.numIndices+=2;
    126         }
    127         for ( unsigned short *i = _indices; i < end; ++i, ++dst ) {
    128                 *dst = *i+offset;
     108        unsigned short *dst= &indices[bi.numIndices];
     109        unsigned short offset = bi.numVertices;
     110        unsigned short *src= _indices;
     111        unsigned short *end= src+_numIndices;
     112        bi.numVertices+=_numVertices;           
     113        if ( bi.numIndices ) {
     114                dst[0] = *(dst-1);
     115                dst[1] = *src+offset;
     116                dst+=2; bi.numIndices+=2;
     117        }
     118        while ( src < end ) {
     119                *dst= *src+offset;
     120                dst++;
     121                src++;
    129122        }
    130123        bi.numIndices+=_numIndices;
     
    167160        frustum->extractPlanes();
    168161       
    169         /**
     162        /*
    170163         * Enable texture and vertex arrays for the first and the second texture
    171164         * units and disable the normal arrays.
     
    181174                glDisableClientState( GL_NORMAL_ARRAY );
    182175        glDisable( GL_CULL_FACE );
     176        glCullFace( GL_BACK );
    183177        glDisable( GL_LIGHTING );
    184178        glColor3f( 1.0f, 1.0f, 1.0f );
     
    230224        page = activePages;
    231225        while ( page ) {
     226                assert( page->isActive() );
    232227                page->updateTesselation();
    233228                page = page->getNext();
     
    236231        if ( buffers[current].numIndices != 0 ) {
    237232                BufferInfo bi = buffers[current];       
    238                 glBindBufferARB( GL_ARRAY_BUFFER_ARB,
    239                         bi.vbIdentifier );
     233                glBindBufferARB( GL_ARRAY_BUFFER_ARB, bi.vbIdentifier );
    240234                glBufferDataARB( GL_ARRAY_BUFFER_ARB, MAX_VERTICES*sizeof( Vertex ),
    241235                        vertices, GL_DYNAMIC_DRAW_ARB );
    242                 glBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB,
    243                         bi.ibIdentifier );
     236                glBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, bi.ibIdentifier );
    244237                glBufferDataARB( GL_ELEMENT_ARRAY_BUFFER_ARB, MAX_INDICES*sizeof( short ),
    245238                        indices, GL_DYNAMIC_DRAW_ARB );
    246239        }
    247         s = 200.0f;
    248240        glPushAttrib( GL_COLOR_BUFFER_BIT );
    249241        for ( unsigned int i = 0; i < materials.size(); ++i ) {
     
    258250                glMatrixMode( GL_TEXTURE );
    259251                glLoadIdentity();
    260                 glScalef( s, s, s );
     252                glScalef( pagesX, pagesZ, 1.0f );
    261253
    262254                glClientActiveTextureARB( GL_TEXTURE0_ARB );
     
    264256                glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );                         
    265257                glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
     258                glEnable( GL_CULL_FACE );
    266259                for ( unsigned j = 0; j < buffers.size(); ++j ) {
    267260                        BufferInfo bi = buffers[j];
  • branches/terrain/src/lib/graphics/importer/terrain/terrain.h

    r8697 r8741  
    7676                 * reasonable value...
    7777                 */
    78                 inline float getDetail() { return 0.05f; }
     78                inline float getDetail() { return 0.02f; }
    7979
    8080       
  • branches/terrain/src/lib/graphics/importer/terrain/terrain_page.cc

    r8697 r8741  
    4444}
    4545
    46 void TerrainPage::tesselateRow( int _z, int _xStride, int _zStride, bool _adaptLeft, bool _adaptRight )
     46void TerrainPage::tesselateRow( int _z, int _xStride, int _zStride,
     47        bool _adaptLeft, bool _adaptRight )
    4748{
    4849       
     
    5152        assert( _xStride > 0 );         assert( _zStride > 0 );
    5253        if ( _z ) {
    53                 addAgain( );
     54                addAgain();
    5455                addIndex( getIndex( 0, _z ) );
    5556        }
     
    7778        if ( _adaptRight ) {
    7879                assert( halfStride > 0 );               
    79                 addIndex( getIndex( w-_xStride, _z ) );
     80                addIndex( getIndex( xEnd-1, _z ) );
    8081                addAgain();
    8182                addIndex( getIndex( w, _z ) );
    82                 addIndex( getIndex( w-_xStride, _z+_zStride ) );
     83                addIndex( getIndex( xEnd-1, _z+_zStride ) );
    8384                addIndex( getIndex( w, _z+halfStride ) );
    8485                addIndex( getIndex( w, _z+_zStride ) );
     
    9293        _adapt[0] = _adapt[1] = _adapt[2] = _adapt[3] = false;
    9394       
    94         if ( left  && left->isVisible )
     95        if ( left  && left->isActive() ) {
    9596                _adapt[TP_LEFT] = ( wantedLOD - left->getWantedLOD() ) > 0;
    96                
    97         if ( right && right->isVisible )
     97                assert( std::abs( wantedLOD - left->wantedLOD ) < 2 );         
     98        }       
     99        if ( right && right->isActive() ) {
    98100                _adapt[TP_RIGHT] = ( wantedLOD - right->getWantedLOD() ) > 0;
    99                
    100         if ( top && top->isVisible )
     101                assert( std::abs( wantedLOD - right->wantedLOD ) < 2 );         
     102        }       
     103               
     104        if ( top && top->isActive() ) {
    101105                _adapt[TP_TOP] = ( wantedLOD - top->getWantedLOD() ) > 0;                               
    102                
    103         if ( bottom && bottom->isVisible )
     106                assert( std::abs( wantedLOD - top->wantedLOD ) < 2 );           
     107        }       
     108        if ( bottom && bottom->isActive() ) {
    104109                _adapt[TP_BOTTOM] = ( wantedLOD - bottom->getWantedLOD() ) > 0;         
     110                assert( std::abs( wantedLOD - bottom->wantedLOD ) < 2 );
     111        }       
    105112}
    106113
     
    246253                        iChanged                =  wantedLOD != currentLOD;
    247254                               
    248         return ( leftChanged || rightChanged || topChanged || bottomChanged || forceTesselation || iChanged );
     255        return ( leftChanged || rightChanged || topChanged || bottomChanged ||
     256                forceTesselation || iChanged );
    249257}
    250258
     
    256264        }       
    257265        currentLOD = wantedLOD;
     266       
    258267        //Check if the page is a level four page. If yes, copy the vertex and index data into
    259         //
     268        //the shared level four page buffers.
    260269        if ( currentLOD == TerrainPage::MAX_LODS-1 ) {
    261270                owner->addLevelFourPage( numVertices, vertices,
     
    263272        }
    264273        forceTesselation = false;
     274}
     275
     276void TerrainPage::tesselateTopRow(  int _z, int _stride,
     277        bool _adaptLeft,  bool _adaptRight )
     278{
     279        int halfStride = _stride/2;
     280        int xStart = 0, xEnd= owner->getPageSize()-1;
     281        int size = xEnd;
     282        int z = xEnd-_stride ; 
     283        addAgain();
     284        addIndex( getIndex( 0, z ) );
     285        assert(halfStride>=1);
     286
     287        if( _adaptLeft ) {
     288                addIndex( getIndex( 0, z ) );
     289                addIndex( getIndex( 0, z+halfStride ) );
     290                addIndex( getIndex( _stride, z ) );
     291                addIndex( getIndex( 0, size ) );
     292                addIndex( getIndex( halfStride, size ) );
     293                addAgain();
     294                addIndex( getIndex( _stride, z ) );
     295                addIndex( getIndex( _stride, size ) );
     296                xStart = _stride;
     297        }
     298
     299        if ( _adaptRight )
     300                xEnd -= _stride;
     301        for ( int x = xStart; x < xEnd; x+=_stride ) {
     302                addIndex( getIndex( x, z ) );
     303                addIndex( getIndex( x, size ) );
     304                addIndex( getIndex( x+_stride, z ) );
     305                addIndex( getIndex( x+halfStride, size ) );
     306                addIndex( getIndex( x+_stride, size ) );
     307                addAgain();
     308        }
     309
     310        if ( _adaptRight ) {
     311                addIndex( getIndex( xEnd, z ) );
     312                addIndex( getIndex( xEnd, size ) );
     313                addIndex( getIndex( size, z ) );
     314                addIndex( getIndex( xEnd+halfStride, size ) );
     315                addIndex( getIndex( size, size-halfStride ) );
     316                addIndex( getIndex( size, size ) );
     317        }
     318
     319}
     320void TerrainPage::tesselateBottomRow(  int _z,  int _stride, 
     321        bool _adaptLeft,  bool _adaptRight  )
     322{
     323        int halfStride = _stride/2;
     324        int xStart=0, xEnd=owner->getPageSize()-1;
     325        int size = xEnd;
     326        assert( halfStride>=1 );
     327
     328        if ( _adaptLeft ) {
     329                addIndex( getIndex( 0, 0 ) );
     330                addIndex( getIndex( 0, halfStride ) );
     331                addIndex( getIndex( halfStride, 0 ) );
     332                addIndex( getIndex( 0, _stride ) );
     333                addIndex( getIndex( _stride, 0 ) );
     334                addIndex( getIndex( _stride, _stride ) );
     335                xStart = _stride;
     336        }
     337
     338
     339        if ( _adaptRight )
     340                xEnd -= _stride;
     341
     342        for ( int x = xStart; x < xEnd; x+=_stride ) {
     343                addIndex( getIndex( x, 0 ) );
     344                addAgain();
     345                addIndex( getIndex( x+halfStride, 0 ) );
     346                addIndex( getIndex( x, _stride ) );
     347                addIndex( getIndex( x+_stride ,0 ) );
     348                addIndex( getIndex( x+_stride, _stride ) );
     349        }
     350
     351
     352        if( _adaptRight ) {
     353                addIndex( getIndex( xEnd,0 ) );
     354                addIndex( getIndex( xEnd,_stride ) );
     355                addIndex( getIndex( xEnd+halfStride, 0 ) );
     356                addIndex( getIndex( size ,0 ) );
     357                addAgain();
     358                addIndex( getIndex( xEnd, _stride ) );
     359                addIndex( getIndex( size, halfStride ) );
     360                addIndex( getIndex( size, _stride ) );
     361        }
    265362}
    266363
     
    294391                        addIndex( getIndex( 0, stride ) );
    295392                        addIndex( getIndex( stride, stride ) );
    296                         addAgain(  );
     393                        addAgain( );
    297394                        break;
    298395               
     
    303400                        addIndex( getIndex( stride, 0 ) );
    304401                        addIndex( getIndex( 0, 0 ) );
    305                         addAgain(  );
     402                        addAgain( );
    306403                        break;
    307404
     
    317414                case ADAPT_B:
    318415                        addIndex( getIndex( 0, 0 ) );
    319                         addAgain(  );
     416                        addAgain();
    320417                        addIndex( getIndex( halfStride, 0 ) );
    321418                        addIndex( getIndex( 0, stride ) );
     
    513610       
    514611        if ( adapt[TP_BOTTOM] ) {
    515                 tesselateRow( 0, stride / 2, stride, adapt[TP_LEFT], adapt[TP_RIGHT] );
     612                tesselateBottomRow( 0, stride, adapt[TP_LEFT], adapt[TP_RIGHT] );
    516613                zStart+= stride;
    517614        }
     
    525622
    526623        if ( adapt[TP_TOP] ) {
    527                 addAgain( );
    528                 addIndex( getIndex( 0, owner->getPageSize()-1-stride ) );
    529                 tesselateRow( owner->getPageSize()-1-stride, stride / 2,
     624                tesselateTopRow( owner->getPageSize()-stride-1,
    530625                        stride, adapt[TP_LEFT], adapt[TP_RIGHT] );
    531626        }
     
    621716        active = false;
    622717        CHECK_GL_ERROR( "1" ); 
    623        
    624718#ifdef USE_VBO
    625719       
  • branches/terrain/src/lib/graphics/importer/terrain/terrain_page.h

    r8697 r8741  
    233233                 */
    234234                void tesselateRow( int _z, int _xStride, int _zStride, bool _adaptLeft, bool _adaptRight );
    235 
     235                void tesselateTopRow( int _z, int _stride, bool _adaptLeft, bool _adaptRight );
     236                void tesselateBottomRow( int _z, int _stride, bool _adaptLeft, bool _adaptRight );
    236237                /**
    237238                 * @brief Returns four boolean values in the oder
  • branches/terrain/src/world_entities/terrain_entity.cc

    r8697 r8741  
    144144        mat->setAmbient( 1.0f, 1.0f, 1.0f );
    145145        mat->setDiffuseMap( _lightFile, GL_TEXTURE_2D, 0 );     
    146         mat->setDiffuseMap( std::string( "maps/gras_128.jpg" ), GL_TEXTURE_2D, 1 );     
     146        mat->setDiffuseMap( std::string( "pictures/debug.png" ), GL_TEXTURE_2D, 1 );   
    147147        terrain->addMaterial( mat );
    148148}
     
    382382  }
    383383}
     384void TerrainEntity::getAltitude( Vector& _position, Vector& _normal )
     385{
     386        Triple  altitude( _position.x-getAbsCoor().x, 0.0f, _position.z-getAbsCoor().z ),
     387                        normal( 0.0f, 0.0f, 0.0f );
     388        if ( terrain )
     389                        terrain->getAltitude( altitude, normal );
     390                       
     391        _position.y = altitude.y+getAbsCoor().y;
     392        _normal.z = normal.z; _normal.y = normal.y; _normal.z = normal.z;       
     393}
    384394
    385395float TerrainEntity::getHeight( float x, float z )
  • branches/terrain/src/world_entities/terrain_entity.h

    r8697 r8741  
    4545
    4646                float getHeight(float x, float y);
     47                void getAltitude( Vector& _position, Vector& _normal );
    4748                virtual void draw() const;
    4849
Note: See TracChangeset for help on using the changeset viewer.