Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Jun 18, 2006, 8:26:57 PM (18 years ago)
Author:
ponder
Message:
  • The terrain now uses VBO's instead of VARS. Its possible to switch back, though.
  • The multitexturing works fine.
File:
1 edited

Legend:

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

    r8548 r8569  
    1818#include <stdio.h>
    1919#include <math.h>
    20 
     20#define USE_VBO
    2121
    2222#define CHECK_GL_ERROR( _d ) do { \
     
    3333        numVertices = numIndices = 0;
    3434        errors = new LODError[TerrainPage::MAX_LODS];
    35         vertices = NULL; indices = NULL; coords = NULL;
     35        vertices = NULL; indices = NULL;
    3636        position = Triple( scale.x*_xOffset, 0.0f, scale.z*_zOffset );
    3737        isVisible = false;
     
    4545void TerrainPage::tesselateRow( int _z, int _xStride, int _zStride, bool _adaptLeft, bool _adaptRight )
    4646{
    47         int xStart = 0, xEnd = owner->getPageSize();
    48        
     47       
     48        int xStart = 0, xEnd = owner->getPageSize();   
    4949        int halfStride = _zStride >> 1;
    50 
    51 
    5250        if ( _z ) {
    5351                addAgain( );
     
    454452        determineBorderAdaption( adapt );
    455453        assert( isVisible );
     454#ifdef USE_VBO
     455       
     456        glBindBufferARB( GL_ARRAY_BUFFER_ARB, vbIdentifier );
     457       
     458        // The call to glBufferDataARB with a null argument for data is to tell the GPU, that the
     459        // old data is invalid. Then calling call glMapBuffer() tells the driver that the previous
     460        // data aren’t valid. As a consequence, if the GPU is still working on them, there won’t
     461        // be a conflict because we invalidated these data. The function glMapBuffer() returns a
     462        // new pointer that we can use while the GPU is working on the previous set of data..
     463       
     464        glBufferDataARB( GL_ARRAY_BUFFER_ARB, count*sizeof( Vertex ), NULL, GL_DYNAMIC_DRAW_ARB );
     465       
     466        vertices = (Vertex*)glMapBufferARB( GL_ARRAY_BUFFER_ARB,
     467                GL_WRITE_ONLY_ARB );
     468       
     469        glBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, ibIdentifier );
     470       
     471        glBufferDataARB( GL_ELEMENT_ARRAY_BUFFER_ARB,
     472                count*3*sizeof( short ), NULL, GL_DYNAMIC_DRAW_ARB );   
     473               
     474        indices = (unsigned short*)glMapBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB,
     475                GL_WRITE_ONLY_ARB );
     476       
     477        assert( indices );
     478        assert( vertices );
     479       
     480#endif 
    456481        if ( _lod == TerrainPage::MAX_LODS-1 ) {
    457482                tesselateLevelFourPatch( adapt );
     
    473498       
    474499
    475         if ( !adapt[TP_BOTTOM] )
    476                 return;
     500        if ( adapt[TP_BOTTOM] ) {
    477501               
    478502        addAgain( );
     
    480504       
    481505        tesselateRow( owner->getPageSize()-1-stride, stride / 2, stride, adapt[TP_LEFT], adapt[TP_RIGHT] );
     506        }
     507       
     508#ifdef USE_VBO
     509        glUnmapBufferARB( GL_ARRAY_BUFFER_ARB );
     510        glUnmapBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB );
     511#endif
    482512
    483513}
     
    488518        int count = owner->getPageSize()*owner->getPageSize();
    489519       
    490         vertices = new Triple[count];
     520#ifdef USE_VBO 
     521        owner->getBufferBroker()->acquire( vbIdentifier, ibIdentifier );
     522        vertices = NULL;
     523        indices = NULL;
     524#else
     525        vertices = new Vertex[count];
    491526        // Not the economical way, but we just want to have space for all indices.
    492527        indices = new unsigned short[count*3];
     528#endif 
     529
    493530        indexHash = new unsigned short[count]; 
    494         coords = new TexCoord[count];
    495531        forceTesselation = true;
    496532        activate();
    497533        active = true;
     534       
    498535}
    499536
    500537void TerrainPage::hide( )
    501538{
     539#ifdef USE_VBO
     540        owner->getBufferBroker()->release( vbIdentifier, ibIdentifier );
     541#else   
    502542        if ( vertices ) {
    503543                delete[] vertices;
    504544                vertices = NULL;
    505545        }
    506         delete[] coords;       
    507546        if ( indices ) {
    508547                delete[] indices;
     
    510549                numIndices = 0;
    511550        }
     551#endif
    512552        deactivate();
    513553}
     
    552592        CHECK_GL_ERROR( "1" ); 
    553593       
    554         glVertexPointer( 3, GL_FLOAT, 0, vertices );
    555         glTexCoordPointer( 2, GL_FLOAT, 0, coords );   
     594#ifdef USE_VBO
     595       
     596        glBindBufferARB( GL_ARRAY_BUFFER_ARB, vbIdentifier );
     597        glClientActiveTextureARB( GL_TEXTURE0_ARB );
     598        glInterleavedArrays( GL_T2F_V3F, 0, NULL );
    556599       
    557600        glClientActiveTextureARB( GL_TEXTURE1_ARB );
    558         glVertexPointer( 3, GL_FLOAT, 0, vertices );
    559         glTexCoordPointer( 2, GL_FLOAT, 0, coords );
     601        glInterleavedArrays( GL_T2F_V3F, 0, NULL );
     602       
     603        glBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, ibIdentifier );   
    560604       
    561605        glDrawElements( GL_TRIANGLE_STRIP, numIndices,
    562                                         GL_UNSIGNED_SHORT, indices );
     606                                        GL_UNSIGNED_SHORT, NULL );     
     607#else   
     608        glClientActiveTextureARB( GL_TEXTURE0_ARB );
     609        glInterleavedArrays( GL_T2F_V3F, 0, vertices );
     610       
     611        glClientActiveTextureARB( GL_TEXTURE1_ARB );
     612        glInterleavedArrays( GL_T2F_V3F, 0, vertices );
     613       
     614        glDrawElements( GL_TRIANGLE_STRIP, numIndices,
     615                                        GL_UNSIGNED_SHORT, indices );   
     616#endif
     617       
     618
    563619       
    564620        if ( owner->debug() )
     
    574630                //The vertex didn't exists before, lets create it.
    575631                indexHash[index] = numVertices;
    576                 getVertex( _x, _z, vertices[numVertices] );
    577                 getCoord( _x, _z, coords[numVertices] );
     632                getVertex( _x, _z, vertices[numVertices].p );
     633                getCoord( _x, _z, vertices[numVertices].t );
    578634                numVertices++;
    579635        }
Note: See TracChangeset for help on using the changeset viewer.