Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Jul 24, 2006, 12:59:44 PM (18 years ago)
Author:
bensch
Message:

some newlines

File:
1 edited

Legend:

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

    r9414 r9415  
    11/*
    22        orxonox - the future of 3D-vertical-scrollers
    3  
     3
    44        Copyright (C) 2006 orx
    5  
     5
    66        This program is free software; you can redistribute it and/or modify
    77        it under the terms of the GNU General Public License as published by
    88        the Free Software Foundation; either version 2, or (at your option)
    99        any later version.
    10  
     10
    1111        ### File Specific:
    1212        main-programmer: Marco Biasini
    13  
     13
    1414 */
    1515#include "terrain_page.h"
     
    2626        }\
    2727        while ( 0 )
    28        
     28
    2929TerrainPage::~TerrainPage( )
    30 { 
     30{
    3131        SAVE_DELETE_ARRAY( indices );
    3232        SAVE_DELETE_ARRAY( vertices );
    33         SAVE_DELETE_ARRAY( indexHash );         
    34         SAVE_DELETE_ARRAY( errors );   
     33        SAVE_DELETE_ARRAY( indexHash );
     34        SAVE_DELETE_ARRAY( errors );
    3535}
    3636
     
    4141        numVertices = numIndices = 0;
    4242        errors = new LODError[TerrainPage::MAX_LODS];
    43         vertices = NULL; indices = NULL; 
     43        vertices = NULL; indices = NULL;
    4444        left = top = bottom = right = NULL;
    4545        for ( int i = 0; i < 8; ++i )
    46                 layerVisibility[i] = LV_NO;     
    47                
    48         position = Vector( scale.x*_xOffset, 
     46                layerVisibility[i] = LV_NO;
     47
     48        position = Vector( scale.x*_xOffset,
    4949                0.0f, scale.z*_zOffset );
    5050        isVisible = false;
    5151        next = NULL;
    5252        active = false;
    53         previous = NULL; 
     53        previous = NULL;
    5454        currentLOD = -1; wantedLOD = -1;
    5555        hasfull = false;
     
    5858}
    5959
    60 void TerrainPage::tesselateRow( int _z, int _xStride, int _zStride, 
     60void TerrainPage::tesselateRow( int _z, int _xStride, int _zStride,
    6161        bool _adaptLeft, bool _adaptRight )
    6262{
    63        
    64         int xStart = 0, xEnd = owner->getPageSize();   
     63
     64        int xStart = 0, xEnd = owner->getPageSize();
    6565        int halfStride = _zStride >> 1;
    6666        assert( _xStride > 0 );         assert( _zStride > 0 );
     
    7070        }
    7171        if ( _adaptLeft ) {
    72                 assert( halfStride > 0 );               
     72                assert( halfStride > 0 );
    7373                addIndex( getIndex( 0, _z ) );
    7474                addIndex( getIndex( 0, _z+halfStride ) );
     
    7979                xStart = _xStride;
    8080        }
    81        
     81
    8282        if ( _adaptRight )
    8383                xEnd-=_xStride;
    84                
     84
    8585        for ( int x = xStart; x < xEnd; x+=_xStride ) {
    8686                addIndex( getIndex( x, _z) );
    8787                addIndex( getIndex( x, _z+_zStride ) );
    8888        }
    89        
     89
    9090        int w = owner->getPageSize()-1;
    91        
     91
    9292        if ( _adaptRight ) {
    93                 assert( halfStride > 0 );               
     93                assert( halfStride > 0 );
    9494                addIndex( getIndex( xEnd-1, _z ) );
    9595                addAgain();
     
    106106{
    107107        _adapt[0] = _adapt[1] = _adapt[2] = _adapt[3] = false;
    108        
     108
    109109        if ( left  && left->isActive() ) {
    110110                _adapt[TP_LEFT] = ( wantedLOD - left->getWantedLOD() ) > 0;
    111                 assert( std::abs( wantedLOD - left->wantedLOD ) < 2 );         
    112         }       
     111                assert( std::abs( wantedLOD - left->wantedLOD ) < 2 );
     112        }
    113113        if ( right && right->isActive() ) {
    114114                _adapt[TP_RIGHT] = ( wantedLOD - right->getWantedLOD() ) > 0;
    115                 assert( std::abs( wantedLOD - right->wantedLOD ) < 2 );         
    116         }       
    117                
     115                assert( std::abs( wantedLOD - right->wantedLOD ) < 2 );
     116        }
     117
    118118        if ( top && top->isActive() ) {
    119                 _adapt[TP_TOP] = ( wantedLOD - top->getWantedLOD() ) > 0;                               
    120                 assert( std::abs( wantedLOD - top->wantedLOD ) < 2 );           
    121         }       
     119                _adapt[TP_TOP] = ( wantedLOD - top->getWantedLOD() ) > 0;
     120                assert( std::abs( wantedLOD - top->wantedLOD ) < 2 );
     121        }
    122122        if ( bottom && bottom->isActive() ) {
    123                 _adapt[TP_BOTTOM] = ( wantedLOD - bottom->getWantedLOD() ) > 0;         
     123                _adapt[TP_BOTTOM] = ( wantedLOD - bottom->getWantedLOD() ) > 0;
    124124                assert( std::abs( wantedLOD - bottom->wantedLOD ) < 2 );
    125         }       
     125        }
    126126}
    127127
     
    130130        wantedLOD = -1;
    131131        Vector cam( owner->getCameraPosition() );
    132        
     132
    133133        for ( int i = TerrainPage::MAX_LODS-1; i >= 0; --i ) {
    134134                Vector distance( cam.x-errors[i].correct.x,
    135135                                                 cam.y-errors[i].correct.y,
    136136                                                 cam.z-errors[i].correct.z );
    137                
     137
    138138                float d = distance.len();
    139                
     139
    140140                float err =  errors[i].diff / d ;
    141                
     141
    142142                if ( err*scale.y < owner->getDetail() ) {
    143143                        wantedLOD = i;
    144144                        break;
    145                 }       
     145                }
    146146        }
    147147        if ( wantedLOD < 0 ) {
    148148                wantedLOD = TerrainPage::MAX_LODS-1;
    149         }       
     149        }
    150150        return wantedLOD;
    151151}
     
    153153void TerrainPage::activate()
    154154{
    155        
     155
    156156        pTerrainPage list = owner->getActiveList();
    157157        next = list;
     
    164164{
    165165        pTerrainPage list = owner->getActiveList();
    166        
     166
    167167        if ( previous ) {
    168168                previous->next = next;
     
    175175        next = NULL;
    176176        previous = NULL;
    177        
     177
    178178}
    179179
     
    186186        {
    187187                int stride = 1 << _lod, x0, y0, xi, yi;
    188                 // Altough these four nested loops look very scary, they're not 
    189                 // that bad and require only about O(n^2). 
     188                // Altough these four nested loops look very scary, they're not
     189                // that bad and require only about O(n^2).
    190190                for( y0 = 0 ; y0 < size-stride; y0 += stride ) {
    191191                        for( x0 = 0; x0 < size-stride; x0 += stride ) {
     
    197197                                                float   fx0 = ( float )xi/( float )stride, fx1 = 1.0f-fx0,
    198198                                                                fy0 = ( float )yi/( float )stride, fy1 = 1.0f-fy0;
    199                                                
     199
    200200                                                float   height00 = getAltitude( x0, y0 ),
    201201                                                                height10 = getAltitude( x0+stride,y0 ),
    202202                                                                height01 = getAltitude( x0,y0+stride ),
    203203                                                                height11 = getAltitude( x0+stride, y0+stride );
    204                                                
     204
    205205                                                float   paintHeight =   fx1*fy1 * height00 +
    206206                                                        fx0*fy1 * height10 +
     
    208208                                                        fx0*fy0 * height11,
    209209                                                        correctHeight =  getAltitude( x, y );
    210                                                
     210
    211211                                                float   er = ( float )fabs( correctHeight - paintHeight );
    212                                                
     212
    213213                                                numErrors++;
    214214                                                sumError += er;
     
    229229        int size = owner->getPageSize();
    230230        float alt = 0.0f;
    231        
    232         Vector  min( xOffset*scale.x ,0.0f, zOffset*scale.z ), 
     231
     232        Vector  min( xOffset*scale.x ,0.0f, zOffset*scale.z ),
    233233                        max( (xOffset+1)*scale.x, 0.0f, (zOffset+1)*scale.z);
    234        
     234
    235235        min.y = max.y = getAltitude( 0, 0 );
    236236        for ( int x = 0; x < size; ++x ) {
     
    248248        assert( _x >= 0 && _x < 17 );
    249249        assert( _z >= 0 && _z < 17 );
    250         return position.y+scale.y*owner->getAltitude( 
    251                         _x+(owner->getPageSize()-1)*xOffset, 
     250        return position.y+scale.y*owner->getAltitude(
     251                        _x+(owner->getPageSize()-1)*xOffset,
    252252                        _z+(owner->getPageSize()-1)*zOffset );
    253253}
     
    255255bool TerrainPage::needsRetesselation()
    256256{
    257        
     257
    258258        bool    leftChanged     = ( left && left->isActive() ) &&
    259259                                ( left->wantedLOD != left->currentLOD ),
    260                         rightChanged    = ( right && right->isActive() ) && 
     260                        rightChanged    = ( right && right->isActive() ) &&
    261261                                ( right->wantedLOD != right->currentLOD ),
    262                         topChanged              = ( top && top->isActive() ) && 
     262                        topChanged              = ( top && top->isActive() ) &&
    263263                                ( top->wantedLOD != currentLOD ),
    264                         bottomChanged   = ( bottom && bottom->isActive() ) && 
     264                        bottomChanged   = ( bottom && bottom->isActive() ) &&
    265265                                ( bottom->wantedLOD != bottom->currentLOD ),
    266266                        iChanged                =  wantedLOD != currentLOD;
    267                                
    268         return ( leftChanged || rightChanged || topChanged || bottomChanged || 
     267
     268        return ( leftChanged || rightChanged || topChanged || bottomChanged ||
    269269                forceTesselation || iChanged );
    270270}
     
    275275        if ( needsRetesselation() ) {
    276276                tesselate( wantedLOD );
    277         }       
     277        }
    278278        currentLOD = wantedLOD;
    279        
    280         //Check if the page is a level four page. If yes, copy the vertex and index data into 
     279
     280        //Check if the page is a level four page. If yes, copy the vertex and index data into
    281281        //the shared level four page buffers.
    282282        if ( currentLOD == TerrainPage::MAX_LODS-1 ) {
    283                 owner->addLevelFourPage( numVertices, vertices, 
     283                owner->addLevelFourPage( numVertices, vertices,
    284284                        numIndices, indices );
    285285        }
     
    287287}
    288288
    289 void TerrainPage::tesselateTopRow(  int _z, int _stride, 
     289void TerrainPage::tesselateTopRow(  int _z, int _stride,
    290290        bool _adaptLeft,  bool _adaptRight )
    291291{
     
    293293        int xStart = 0, xEnd= owner->getPageSize()-1;
    294294        int size = xEnd;
    295         int z = xEnd-_stride ; 
     295        int z = xEnd-_stride ;
    296296        addAgain();
    297297        addIndex( getIndex( 0, z ) );
     
    331331
    332332}
    333 void TerrainPage::tesselateBottomRow(  int _z,  int _stride, 
     333void TerrainPage::tesselateBottomRow(  int _z,  int _stride,
    334334        bool _adaptLeft,  bool _adaptRight  )
    335335{
     
    380380        numIndices = numVertices = 0;
    381381        const int       halfStride = 8, stride = 16;
    382        
     382
    383383        enum { ADAPT_L = 1, ADAPT_R = 2, ADAPT_B = 4, ADAPT_T = 8,
    384                 ADAPT_LR = 3, ADAPT_LB = 5, ADAPT_LT = 9, ADAPT_RB = 6, 
    385                 ADAPT_RT = 10, ADAPT_BT = 12, ADAPT_LRB = 7, ADAPT_LBT = 13, ADAPT_LRT = 11, 
     384                ADAPT_LR = 3, ADAPT_LB = 5, ADAPT_LT = 9, ADAPT_RB = 6,
     385                ADAPT_RT = 10, ADAPT_BT = 12, ADAPT_LRB = 7, ADAPT_LBT = 13, ADAPT_LRT = 11,
    386386                ADAPT_RBT = 14, ADAPT_LRBT = 15, ADAPT_NONE = 0 };
    387                
    388         int                     code =  ( _adapt[TP_LEFT]       ? ADAPT_L : 0 ) | 
    389                                                 ( _adapt[TP_RIGHT]      ? ADAPT_R : 0 ) | 
    390                                                 ( _adapt[TP_BOTTOM] ? ADAPT_B : 0 ) | 
     387
     388        int                     code =  ( _adapt[TP_LEFT]       ? ADAPT_L : 0 ) |
     389                                                ( _adapt[TP_RIGHT]      ? ADAPT_R : 0 ) |
     390                                                ( _adapt[TP_BOTTOM] ? ADAPT_B : 0 ) |
    391391                                                ( _adapt[TP_TOP]        ? ADAPT_T : 0 );
    392392        switch( code ) {
     
    397397                        addIndex( getIndex( stride, stride ) );
    398398                        break;
    399                        
     399
    400400                case ADAPT_L:
    401401                        addIndex( getIndex( 0, 0 ) );
     
    406406                        addAgain( );
    407407                        break;
    408                
     408
    409409                case ADAPT_R:
    410410                        addIndex( getIndex( stride, stride ) );
     
    556556{
    557557
    558         memset( indexHash, 0xffff, 
     558        memset( indexHash, 0xffff,
    559559                        sizeof(unsigned short)*Terrain::MAX_VERTICES );
    560        
     560
    561561        numVertices = 0; numIndices = 0;
    562        
     562
    563563        //Calculate the pace, based on the lod.
    564564        int stride = 1 << _lod;
    565                
     565
    566566        bool adapt[4];
    567567        determineBorderAdaption( adapt );
     
    573573                        vbIdentifier = ibIdentifier = 0;
    574574                }
    575                 if ( !vertices ) 
    576                         vertices =  new Vertex[8]; 
     575                if ( !vertices )
     576                        vertices =  new Vertex[8];
    577577                if ( !indices )
    578578                        indices = new unsigned short[8];
     
    585585                        if ( indices )
    586586                                delete[] indices;
    587                         vertices = NULL; indices = NULL;       
     587                        vertices = NULL; indices = NULL;
    588588                }
    589589                assert( vbIdentifier ); assert( ibIdentifier );
     
    591591
    592592                // The call to glBufferDataARB with a null argument for data is to make things faster.
    593                 // Then calling call glMapBuffer() tells the driver that the previous 
    594                 // data aren’t valid. As a consequence, if the GPU is still working on them, there won’t 
    595                 // be a conflict because we invalidated these data. The function glMapBuffer() returns a 
    596                 // new pointer that we can use while the GPU is working on the previous set of data.. 
    597 
    598                 glBufferDataARB( GL_ARRAY_BUFFER_ARB, Terrain::MAX_VERTICES*sizeof( Vertex ), 
     593                // Then calling call glMapBuffer() tells the driver that the previous
     594                // data aren’t valid. As a consequence, if the GPU is still working on them, there won’t
     595                // be a conflict because we invalidated these data. The function glMapBuffer() returns a
     596                // new pointer that we can use while the GPU is working on the previous set of data..
     597
     598                glBufferDataARB( GL_ARRAY_BUFFER_ARB, Terrain::MAX_VERTICES*sizeof( Vertex ),
    599599                        NULL, GL_DYNAMIC_DRAW_ARB );
    600600
    601                 vertices = (Vertex*)glMapBufferARB( GL_ARRAY_BUFFER_ARB, 
     601                vertices = (Vertex*)glMapBufferARB( GL_ARRAY_BUFFER_ARB,
    602602                        GL_WRITE_ONLY_ARB );
    603603
    604604                glBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, ibIdentifier );
    605605
    606                 glBufferDataARB( GL_ELEMENT_ARRAY_BUFFER_ARB, 
    607                         Terrain::MAX_INDICES*sizeof( short ), NULL, GL_DYNAMIC_DRAW_ARB );     
    608 
    609                 indices = (unsigned short*)glMapBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, 
     606                glBufferDataARB( GL_ELEMENT_ARRAY_BUFFER_ARB,
     607                        Terrain::MAX_INDICES*sizeof( short ), NULL, GL_DYNAMIC_DRAW_ARB );
     608
     609                indices = (unsigned short*)glMapBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB,
    610610                        GL_WRITE_ONLY_ARB );
    611611
    612         }       
    613 
    614 #endif 
     612        }
     613
     614#endif
    615615
    616616        assert( indices );      assert( vertices );
     
    619619                return;
    620620        }
    621        
     621
    622622        int zStart = 0, zEnd = owner->getPageSize()-stride;
    623        
     623
    624624        if ( adapt[TP_BOTTOM] ) {
    625625                tesselateBottomRow( 0, stride, adapt[TP_LEFT], adapt[TP_RIGHT] );
    626626                zStart+= stride;
    627627        }
    628        
     628
    629629        if ( adapt[TP_TOP] )
    630630                zEnd-= stride;
    631                
     631
    632632        for ( int z = zStart; z < zEnd; z+=stride )
    633633                tesselateRow( z, stride, stride, adapt[TP_LEFT], adapt[TP_RIGHT] );
    634        
     634
    635635
    636636        if ( adapt[TP_TOP] ) {
    637                 tesselateTopRow( owner->getPageSize()-stride-1, 
     637                tesselateTopRow( owner->getPageSize()-stride-1,
    638638                        stride, adapt[TP_LEFT], adapt[TP_RIGHT] );
    639639        }
    640        
     640
    641641#ifdef USE_VBO
    642642        if ( vbIdentifier && ibIdentifier ) {
     
    645645                glUnmapBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB );
    646646                vertices = NULL;
    647         }       
     647        }
    648648#endif
    649649
     
    653653void TerrainPage::show( )
    654654{
    655        
    656 #ifdef USE_VBO 
     655
     656#ifdef USE_VBO
    657657        //This is done later on...
    658658        //owner->getBufferBroker()->acquire( vbIdentifier, ibIdentifier );
     
    662662        vertices = new Vertex[Terrain::MAX_VERTICES];
    663663        indices = new unsigned short[Terrain::MAX_INDICES];
    664 #endif 
     664#endif
    665665        vbIdentifier = 0; ibIdentifier = 0;
    666         indexHash = new unsigned short[Terrain::MAX_VERTICES]; 
     666        indexHash = new unsigned short[Terrain::MAX_VERTICES];
    667667        forceTesselation = true;
    668668        activate();
     
    673673{
    674674#ifdef USE_VBO
    675         owner->getBufferBroker()->release( vbIdentifier, ibIdentifier );       
     675        owner->getBufferBroker()->release( vbIdentifier, ibIdentifier );
    676676#endif
    677677        SAVE_DELETE_ARRAY( vertices );
     
    692692                glVertex3f( bounds.x, bounds.y, 0.0f );
    693693                glVertex3f( 0.0f, bounds.y, 0.0f );
    694                
     694
    695695                glVertex3f( 0.0f, 0.0f, bounds.z );
    696696                glVertex3f(  bounds.x, 0.0f, bounds.z );
    697697                glVertex3f( bounds.x, bounds.y, bounds.z );
    698                 glVertex3f( 0.0f, bounds.y, bounds.z );         
    699                
     698                glVertex3f( 0.0f, bounds.y, bounds.z );
     699
    700700                glVertex3f( 0.0f, 0.0f, 0.0 );
    701701                glVertex3f(  0.0, 0.0f, bounds.z );
    702702                glVertex3f( 0.0f, bounds.y, bounds.z );
    703                 glVertex3f( 0.0f, bounds.y, 0.0f );                             
    704                
     703                glVertex3f( 0.0f, bounds.y, 0.0f );
     704
    705705                glVertex3f( bounds.x, 0.0f, 0.0 );
    706706                glVertex3f(  bounds.x, 0.0f, bounds.z );
    707707                glVertex3f( bounds.x, bounds.y, bounds.z );
    708                 glVertex3f( bounds.x, bounds.y, 0.0f );                                         
     708                glVertex3f( bounds.x, bounds.y, 0.0f );
    709709        glEnd();
    710710        glPopMatrix();
     
    716716        assert( glIsEnabled( GL_VERTEX_ARRAY ) );
    717717        assert( !glIsEnabled( GL_NORMAL_ARRAY ) );
    718        
     718
    719719        if ( currentLOD == TerrainPage::MAX_LODS-1 )
    720720                return;
    721                
     721
    722722        assert( isVisible ); assert( numIndices > 0 );
    723723        active = false;
    724         CHECK_GL_ERROR( "1" ); 
     724        CHECK_GL_ERROR( "1" );
    725725#ifdef USE_VBO
    726        
     726
    727727        glBindBufferARB( GL_ARRAY_BUFFER_ARB, vbIdentifier );
    728728        glClientActiveTextureARB( GL_TEXTURE0_ARB );
    729729        glInterleavedArrays( GL_T2F_V3F, 0, NULL );
    730        
     730
    731731        glClientActiveTextureARB( GL_TEXTURE1_ARB );
    732732        glInterleavedArrays( GL_T2F_V3F, 0, NULL );
    733        
    734         glBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, ibIdentifier );   
    735        
    736         glDrawElements( GL_TRIANGLE_STRIP, numIndices, 
    737                                         GL_UNSIGNED_SHORT, NULL );     
    738 #else   
     733
     734        glBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, ibIdentifier );
     735
     736        glDrawElements( GL_TRIANGLE_STRIP, numIndices,
     737                                        GL_UNSIGNED_SHORT, NULL );
     738#else
    739739        glClientActiveTextureARB( GL_TEXTURE0_ARB );
    740740        glInterleavedArrays( GL_T2F_V3F, 0, vertices );
    741        
     741
    742742        glClientActiveTextureARB( GL_TEXTURE1_ARB );
    743743        glInterleavedArrays( GL_T2F_V3F, 0, vertices );
    744        
    745         glDrawElements( GL_TRIANGLE_STRIP, numIndices, 
    746                                         GL_UNSIGNED_SHORT, indices );   
     744
     745        glDrawElements( GL_TRIANGLE_STRIP, numIndices,
     746                                        GL_UNSIGNED_SHORT, indices );
    747747#endif
    748        
    749 
    750        
     748
     749
     750
    751751        if ( owner->debug() )
    752752                drawBox( );
    753                
    754         CHECK_GL_ERROR( "2" ); 
     753
     754        CHECK_GL_ERROR( "2" );
    755755}
    756756
     
    768768}
    769769
    770 void TerrainPage::getCoord( int _x, int _z, TexCoord& _coord ) const 
    771 {
    772         owner->getCoord( _x+xOffset*(owner->getPageSize()-1 ), 
     770void TerrainPage::getCoord( int _x, int _z, TexCoord& _coord ) const
     771{
     772        owner->getCoord( _x+xOffset*(owner->getPageSize()-1 ),
    773773                                         _z+zOffset*(owner->getPageSize()-1 ), _coord );
    774774}
     
    789789                if ( _layer )
    790790                        hasfull = true;
    791         }       
    792         layerVisibility[_layer] = _lv; 
     791        }
     792        layerVisibility[_layer] = _lv;
    793793}
    794794
    795795bool TerrainPage::hasMaterial( int _layer )
    796 { 
     796{
    797797        return ( layerVisibility[_layer] != LV_NO );
    798798}
Note: See TracChangeset for help on using the changeset viewer.