Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 8569 in orxonox.OLD


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.
Location:
branches/terrain/src/lib/graphics/importer
Files:
6 edited

Legend:

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

    r8319 r8569  
    2525                           terrain/terrain_quad.cc \
    2626                           terrain/terrain_page.cc \
    27                            terrain/frustum.cc
     27                           terrain/frustum.cc \
     28                           terrain/buffer_broker.cc
    2829
    2930libtc_a_SOURCES  = tc.cc
     
    5657                terrain/terrain_quad.h \
    5758                terrain/frustum.h \
    58                 terrain/types.h
     59                terrain/types.h \
     60      terrain/buffer_broker.h
  • branches/terrain/src/lib/graphics/importer/terrain/frustum.h

    r8328 r8569  
    2828       
    2929/**
    30  * Code borrowed from Lighthouse 3D 
     30 * Code borrowed from Lighthouse 3D. Its a very good tutorial on culling.
    3131 */
    3232class Frustum;
     
    6464                {
    6565                        int result = INSIDE;
    66                         for(int i=0; i < 6; i++) {
     66                        //for each plane do ...
     67                        for( int i = 0; i < 6; ++i ) {
    6768
     69                                // is the positive vertex outside?
    6870                                if ( planes[i].distance( _box.vertexP( planes[i].n ) ) < 0 )
    6971                                        return OUTSIDE;
    70                                 if ( planes[i].distance( _box.vertexN( planes[i].n) ) < 0 )
    71                                         result =  INTERSECT;
     72                                // is the negative vertex outside?     
     73                                else if ( planes[i].distance( _box.vertexN( planes[i].n ) ) < 0 )
     74                                        result = INTERSECT;
    7275                        }
    73                         return result;         
     76                        return result;
    7477                }
    7578               
     
    7881                        int result = INSIDE;
    7982                        for(int i=0; i < 6; i++) {
    80 
    8183                                if (planes[i].distance( _point ) < 0)
    8284                                        return OUTSIDE;
  • branches/terrain/src/lib/graphics/importer/terrain/terrain.cc

    r8548 r8569  
    129129void Terrain::draw( )
    130130{
    131         static float s = 0.0f, x = 0.0f;
     131        static float s = 0.0f;
    132132        glGetError();
    133133        pTerrainPage page = NULL;
     
    158158        pTerrainPage neighbor = NULL;
    159159        page = activePages;
     160        bool dirty;
     161        do {
     162                dirty = false;
     163                page = activePages;
     164                while ( page ) {
     165                        if ( !page->isActive() ) {
     166                                pTerrainPage tmp = page;
     167                                page = tmp->getNext();
     168                                tmp->setVisibility( false );
     169                                continue;
     170                        }
     171                        wantedLeft = wantedRight = wantedBottom = wantedTop = page->getWantedLOD();
     172                        if ( ( neighbor = page->getLeft() ) )
     173                                wantedLeft = neighbor->getWantedLOD();
     174                        if ( ( neighbor = page->getRight() ) )
     175                                wantedRight = neighbor->getWantedLOD();
     176                        if ( ( neighbor = page->getTop() ) )
     177                                wantedTop = neighbor->getWantedLOD();
     178                        if ( ( neighbor = page->getBottom() ) )
     179                                wantedBottom = neighbor->getWantedLOD();                       
     180               
     181                        minLOD = std::min( std::min( wantedBottom, wantedTop ), std::min( wantedLeft, wantedRight ) ); 
     182                        if ( minLOD < page->getWantedLOD()-1 ) {
     183                                page->setWantedLOD( minLOD+1 );
     184                                dirty = true;
     185                        }       
     186                        page = page->getNext();
     187                }
     188        } while ( dirty );
     189        page = activePages;
    160190       
    161191        while ( page ) {
    162                 if ( !page->isActive() ) {
    163                         pTerrainPage tmp = page;
    164                         page = tmp->getNext();
    165                         tmp->setVisibility( false );
    166                         continue;
    167                 }
    168                 wantedLeft = wantedRight = wantedBottom = wantedTop = page->getWantedLOD();
    169                 if ( ( neighbor = page->getLeft() ) )
    170                         wantedLeft = neighbor->getWantedLOD();
    171                 if ( ( neighbor = page->getRight() ) )
    172                         wantedRight = neighbor->getWantedLOD();
    173                 if ( ( neighbor = page->getTop() ) )
    174                         wantedTop = neighbor->getWantedLOD();
    175                 if ( ( neighbor = page->getBottom() ) )
    176                         wantedBottom = neighbor->getWantedLOD();                       
    177                
    178                 minLOD = std::min( std::min( wantedBottom, wantedTop ), std::min( wantedLeft, wantedRight ) ); 
    179                 /*if ( minLOD < page->getWantedLOD()-1 )
    180                         page->setWantedLOD( minLOD+1 );*/
    181                
    182                 page->updateTesselation();     
     192                page->updateTesselation();
    183193                page = page->getNext();
    184194        }
    185         s = 50.0f;
     195       
     196        s = 500.0f;
    186197        for ( int i = 0; i < materials.size(); ++i ) {
    187198                page = activePages;     
     
    189200                //This is a hack! Remove this as soon as possible
    190201                materials[i]->unselect();
    191                
    192202                materials[i]->select();
    193203               
     
    214224                //printf( "culled %f%% terrain pages away\n",  percentage );
    215225        }
     226        glClientActiveTextureARB( GL_TEXTURE1_ARB );   
     227        glDisableClientState( GL_VERTEX_ARRAY );
     228        glDisableClientState( GL_TEXTURE_COORD_ARRAY );
     229        glDisableClientState( GL_VERTEX_ARRAY );
     230        glDisableClientState( GL_TEXTURE_COORD_ARRAY );
     231        materials[0]->unselect();
     232        glActiveTextureARB( GL_TEXTURE0_ARB );
     233        glClientActiveTextureARB( GL_TEXTURE0_ARB );   
     234        glDisableClientState( GL_VERTEX_ARRAY );
     235        glDisableClientState( GL_TEXTURE_COORD_ARRAY );
     236        glEnable( GL_LIGHTING );
    216237}
    217238
  • branches/terrain/src/lib/graphics/importer/terrain/terrain.h

    r8548 r8569  
    1717#define _TERRAIN_H
    1818
     19#define USE_VBO
     20#ifdef USE_VBO
     21#include "buffer_broker.h"
     22#endif
    1923#include "types.h"
    2024#include "terrain_page.h"
     
    8286                        frustum = new Frustum();
    8387                        activatedCount = deactivatedCount = 0;
     88#ifdef USE_VBO
     89                        broker = new BufferBroker( 200, pageSize*pageSize*sizeof( Vertex ),
     90                                pageSize*pageSize*sizeof( short )*3 );
     91#endif                 
    8492                }
    8593               
     
    146154                        scale = _scale;
    147155                }
    148                
     156#ifdef USE_VBO
     157                inline pBufferBroker getBufferBroker() { return broker; }
     158#endif         
    149159                inline TerrainPage *getPage( int _x, int _z )
    150160                {
     
    170180                void determineVisiblePages( pTerrainQuad _node );                                               
    171181               
     182#ifdef USE_VBO
     183                pBufferBroker                   broker;
     184#endif
    172185                pTerrainQuad                    root;           // The quad-tree root node.
    173186                pTerrainPage                    *pages;         // the references to all pages
  • 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        }
  • branches/terrain/src/lib/graphics/importer/terrain/terrain_page.h

    r8548 r8569  
    1717#ifndef TERRAIN_PAGE_H
    1818#define TERRAIN_PAGE_H
    19 
     19#define USE_VBO
    2020#include "terrain_quad.h"
    2121#include <stdio.h>
     
    2626
    2727typedef TerrainPage *pTerrainPage;
     28
     29struct Vertex {
     30        TexCoord        t;     
     31        Triple          p;
     32};
     33
     34typedef Vertex *pVertex;
    2835
    2936typedef struct {
     
    208215                        if ( wantedLOD >= TerrainPage::MAX_LODS )
    209216                                wantedLOD = 4;
     217                        else if ( wantedLOD < 0 )
     218                                wantedLOD = 0;
    210219                        else
    211220                                wantedLOD = _lod;
     
    271280                bool                                            forceTesselation;
    272281                bool                                            active;
    273                 Triple                                          *vertices;
     282                pVertex                                         vertices;
    274283                unsigned short                          *indices;
    275284                unsigned short                          *indexHash;
    276                 TexCoord                                        *coords;
    277285                int                                                     numIndices;
    278286                int                                                     numVertices;
     287               
     288#ifdef  USE_VBO
     289                GLuint                                          ibIdentifier,
     290                                                                        vbIdentifier;
     291#endif         
    279292                bool                                            isVisible;
    280293                pTerrainPage                            next;
Note: See TracChangeset for help on using the changeset viewer.