Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 8548 in orxonox.OLD


Ignore:
Timestamp:
Jun 16, 2006, 11:35:49 PM (18 years ago)
Author:
ponder
Message:
  • Added multitexturing support to the terrain-class.
  • The lightmap gets screwed up and I don't know why…
Location:
branches/terrain/src
Files:
7 edited

Legend:

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

    r8037 r8548  
    6363    void setBump(const std::string& bump);
    6464    GLuint getDiffuseTexture(unsigned int i = 0) const { return (this->textures.size() > i)? this->textures[i].getTexture() : 0; };
    65 
    6665    static void addTexturePath(const std::string& pathName);
    6766
  • branches/terrain/src/lib/graphics/importer/terrain/terrain.cc

    r8349 r8548  
    55#include "util/loading/resource_manager.h"
    66#include "debug.h"
    7 
     7#include <math.h>
    88#ifdef HAVE_SDL_SDL_IMAGE_H
    99#include <SDL/SDL_image.h>
     
    3232        pagesX = (heightfield.width/(pageSize-1) );
    3333        pagesZ = (heightfield.height/(pageSize-1) );
    34         tex = (Texture*)MANAGER->load( lightmapSource );
    35        
    36         //TODO: Determine layer visibility!
     34        //tex = (Texture*)MANAGER->load( lightmapSource );
     35        //TODO: Determine layer visibility!     
     36        for ( int i = 0; i < materials.size(); ++i ) {
     37               
     38        }
     39
    3740        printf( " * creating terrain pages ( %d, %d )...", pagesX, pagesZ );
    3841        pages = new pTerrainPage[pagesX*pagesZ];       
     
    126129void Terrain::draw( )
    127130{
     131        static float s = 0.0f, x = 0.0f;
    128132        glGetError();
    129133        pTerrainPage page = NULL;
    130134        frustum->extractPlanes();
    131         glEnableClientState( GL_VERTEX_ARRAY );
    132 
    133         glEnableClientState( GL_TEXTURE_COORD_ARRAY );
    134         glEnable( GL_TEXTURE_2D );
    135         glEnable( GL_DEPTH_TEST );
    136         glDisable( GL_CULL_FACE );
    137         glBindTexture( GL_TEXTURE_2D, tex->getTexture() );
     135       
     136        /**
     137         * Enable texture and vertex arrays for the first and the second texture
     138         * units and disable the normal arrays.
     139         */
     140        glClientActiveTextureARB( GL_TEXTURE0_ARB );   
     141                glEnableClientState( GL_VERTEX_ARRAY );
     142                glEnableClientState( GL_TEXTURE_COORD_ARRAY );
     143                glDisableClientState( GL_NORMAL_ARRAY );
     144                               
     145        glClientActiveTextureARB( GL_TEXTURE1_ARB );   
     146                glEnableClientState( GL_VERTEX_ARRAY );
     147                glEnableClientState( GL_TEXTURE_COORD_ARRAY );
     148                glDisableClientState( GL_NORMAL_ARRAY );
     149               
    138150        glDisable( GL_LIGHTING );
    139         glEnable( GL_COLOR_MATERIAL );
    140151        glColor3f( 1.0f, 1.0f, 1.0f );
     152        //glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
    141153        cullCount = 0;
     154       
    142155        // Lets see which pages are visible.
    143156        determineVisiblePages( root );
    144                
    145         page = activePages;     
     157        int wantedLeft, wantedRight, wantedBottom, wantedTop, minLOD;
     158        pTerrainPage neighbor = NULL;
     159        page = activePages;
     160       
    146161        while ( page ) {
    147162                if ( !page->isActive() ) {
     
    150165                        tmp->setVisibility( false );
    151166                        continue;
    152                 }       
    153                 page->updateTesselation();
    154                 //Draw the page. TODO: It would be nice if all the pages with an LOD of 4 could be merged
    155                 //in one index buffer. This would give us a speed boost for sure...
    156                 page->draw();
     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();     
    157183                page = page->getNext();
    158184        }
    159         activatedCount = 0; deactivatedCount = 0;
    160         //float percentage = (float)cullCount/(float)(pagesX*pagesZ)*100.0f;
    161         //printf( "culled %f%% terrain pages away\n",  percentage );
     185        s = 50.0f;
     186        for ( int i = 0; i < materials.size(); ++i ) {
     187                page = activePages;     
     188               
     189                //This is a hack! Remove this as soon as possible
     190                materials[i]->unselect();
     191               
     192                materials[i]->select();
     193               
     194                glActiveTextureARB( GL_TEXTURE1_ARB );
     195                glClientActiveTextureARB( GL_TEXTURE1_ARB );
     196                glMatrixMode( GL_TEXTURE );
     197                glLoadIdentity();
     198                glScalef( s, s, s );
     199               
     200                glClientActiveTextureARB( GL_TEXTURE0_ARB );
     201                glActiveTextureARB( GL_TEXTURE0_ARB );
     202                glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );                         
     203                glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
     204                while ( page ) {
     205                        //Draw the page. TODO: It would be nice if all the pages with an LOD of 4 could be merged
     206                        //in one index buffer. This would give us a speed boost for sure...
     207                        if ( page->hasMaterial( i ) )
     208                                page->draw();
     209                        page = page->getNext();
     210                }
     211                activatedCount = 0; deactivatedCount = 0;
     212                //printf( "%d of %d pages drawn\n", pagesX*pagesZ-cullCount, pagesX*pagesZ );
     213                //float percentage = (float)cullCount/(float)(pagesX*pagesZ)*100.0f;
     214                //printf( "culled %f%% terrain pages away\n",  percentage );
     215        }
    162216}
    163217
     
    182236                        getAltitude( xOff+1, zOff+0 )*scale.y,
    183237                        getAltitude( xOff+1, zOff+1 )*scale.y };
    184                 _alt.y = (1.0f-u)*(1.0-v)*alt[0]+u*(1.0f-v)*alt[1]+u*v*alt[2];
     238                _alt.y = (1.0f-u-v)*alt[0]+u*alt[1]+v*alt[2];
    185239               
    186240                //Since we know about the directions of some x and z-coordinates,
     
    196250                        getAltitude( xOff+0, zOff+1 )*scale.y,
    197251                        getAltitude( xOff+1, zOff+1 )*scale.y };                       
    198                 _alt.y = (1.0f-u)*(1.0-v)*alt[0]+(1.0f-u)*v*alt[1]+u*v*alt[2];
     252                _alt.y = (1.0f-u-v)*alt[0]+v*alt[1]+u*alt[2];
    199253                //Since we know about the directions of some x and z-coordinates,
    200254                //not the whole cross products needs to be calculated. Grab yourself
  • branches/terrain/src/lib/graphics/importer/terrain/terrain.h

    r8328 r8548  
    2222#include <cstdlib>
    2323#include <string>
     24#include <list>
     25#include "material.h"
    2426#include "texture.h"
    2527#include "frustum.h"
     
    3436
    3537class Terrain;
     38
    3639typedef Terrain *pTerrain;
    3740
     
    107110                inline float getAltitude( int _x, int _z ) const;
    108111               
    109                 inline void setLightmap( const std::string &_lightmap )
     112                inline void addMaterial( Material *_material )
     113                {
     114                        materials.push_back( _material );
     115                }
     116               
     117                /*inline void setLightmap( const std::string &_lightmap )
    110118                {
    111119                        lightmapSource = _lightmap;
    112                 }
     120                }*/
    113121                inline void setHeightmap( const std::string &_heightmap )
    114122                {
     
    122130               
    123131                inline const Triple getScale() const { return scale; }
     132               
    124133                inline int getPageSize() const
    125134                {
    126135                        return pageSize;
    127136                }
     137               
    128138                inline void getCoord( int _x, int _z, TexCoord& _coord) const
    129139                {
     
    160170                void determineVisiblePages( pTerrainQuad _node );                                               
    161171               
    162                 pTerrainQuad            root;           // The quad-tree root node.
    163                 pTerrainPage            *pages;         // the references to all pages
    164                 std::string                     heightmapSource;
    165                 std::string                     lightmapSource;
    166                 Heightfield                     heightfield;
    167                 Triple                          scale;
    168                 int                             pagesX,
    169                                                         pagesZ;
    170                 int                                     pageSize;       
    171                 int                                     cullCount;
     172                pTerrainQuad                    root;           // The quad-tree root node.
     173                pTerrainPage                    *pages;         // the references to all pages
     174                std::string                             heightmapSource;
     175                std::string                             lightmapSource;
     176                Heightfield                             heightfield;
     177                Triple                                  scale;
     178                int                                     pagesX,
     179                                                                pagesZ;
     180                int                                             pageSize;       
     181                int                                             cullCount;
    172182               
    173                 int                                     activatedCount,
    174                                                         deactivatedCount; // For debugging and statistics
    175                 Texture                         *tex;
    176                 Triple                          cameraPosition;
    177                 pFrustum                        frustum;
    178                 pTerrainPage            activePages;
     183                int                                             activatedCount,
     184                                                                deactivatedCount; // For debugging and statistics
     185                Texture                                 *tex;
     186                Triple                                  cameraPosition;
     187                pFrustum                                frustum;
     188                pTerrainPage                    activePages;
     189                std::vector<Material*>  materials;
    179190};
    180191
  • branches/terrain/src/lib/graphics/importer/terrain/terrain_page.cc

    r8349 r8548  
    1919#include <math.h>
    2020
     21
    2122#define CHECK_GL_ERROR( _d ) do { \
    2223        GLenum __err = glGetError(); \
     
    8788//TODO: Perform benchmark to measure if the isVisible test should be included
    8889//in this method.
    89 
    9090void TerrainPage::determineBorderAdaption( bool _adapt[] )
    9191{
     
    544544void TerrainPage::draw( )
    545545{
    546         //These give an error
    547546        assert( glIsEnabled( GL_VERTEX_ARRAY ) );
    548547        assert( !glIsEnabled( GL_NORMAL_ARRAY ) );
     548       
    549549        assert( isVisible ); assert( numIndices > 0 );
    550550        assert( active );
    551551        active = false;
    552552        CHECK_GL_ERROR( "1" ); 
     553       
    553554        glVertexPointer( 3, GL_FLOAT, 0, vertices );
    554555        glTexCoordPointer( 2, GL_FLOAT, 0, coords );   
     556       
     557        glClientActiveTextureARB( GL_TEXTURE1_ARB );
     558        glVertexPointer( 3, GL_FLOAT, 0, vertices );
     559        glTexCoordPointer( 2, GL_FLOAT, 0, coords );
     560       
    555561        glDrawElements( GL_TRIANGLE_STRIP, numIndices,
    556562                                        GL_UNSIGNED_SHORT, indices );
  • branches/terrain/src/lib/graphics/importer/terrain/terrain_page.h

    r8349 r8548  
    199199                 */
    200200                void activate();               
    201                
    202                 protected:
    203                        
    204                         /**
    205                          * @brief Tesselates one row of the terrain page.
    206                          * @param _z                    The z-offset of the row
    207                          * @param _xStride              Determines the step-size horizontally
    208                          * @param _zStride              Determines the step-size vertically.
    209                          * @param _adaptRight   True if the right neighbor has a coarser
    210                          *                                              tesselation level.
    211                          * @param _adaptLeft    True if the left neighbor has a coarser
    212                          *                                              tesselation level.                     
    213                          */
    214                         void tesselateRow( int _z, int _xStride, int _zStride, bool _adaptLeft, bool _adaptRight );
    215 
    216                         /**
    217                          * @brief Returns four boolean values in the oder
    218                          */
    219                         void determineBorderAdaption( bool _adapt[] );
    220                        
    221                         /**
    222                          * @brief Adds the given index to the index-array
    223                          */
    224                         inline void addIndex( unsigned short _index );
    225                        
    226                         /**
    227                          * @brief We programmers are very lazy :) This method just adds the last added index
    228                          * again.
    229                          */
    230                         inline void addAgain();
    231                        
    232                        
    233                         void getCoord( int _x, int _z, TexCoord& _coord) const;
    234                        
    235                         /**
    236                          * Fills _vertex with the vertex information at index.
    237                          */
    238                         void getVertex( int _x, int _z, Triple& _vertex ) const;
    239                        
    240                         /**
    241                          * Use this method to safely get a vertex at location ( _x, _z ). If it wasn't
    242                          * created before, this method does that for you.
    243                          */                     
    244                         short getIndex( int _x, int _z );
    245                         void tesselateLevelFourPatch( bool _adapt[] );
    246                    /**
    247                         * Generates the tesselation for the given level of detail.
    248                         */
    249                         void tesselate( int _lod );                     
    250                        
    251                         float getAltitude( int _x, int _z ) const;
    252                        
    253                         int                                                     currentLOD,
    254                                                                                 wantedLOD;
    255                         float                                           tween;
    256                         pTerrainPage                            left,
    257                                                                                 right,
    258                                                                                 top,
    259                                                                                 bottom;
    260                         bool                                            forceTesselation;
    261                         bool                                            active;
    262                         Triple                                          *vertices;
    263                         unsigned short                          *indices;
    264                         unsigned short                          *indexHash;
    265                         TexCoord                                        *coords;
    266                         int                                                     numIndices;
    267                         int                                                     numVertices;
    268                         bool                                            isVisible;
    269                         pTerrainPage                            next;
    270                         pTerrainPage                            previous;
    271                         LODError                                        *errors;
    272                         Triple                                          position;
     201                inline bool hasMaterial( int _i )
     202                {
     203                        return true;
     204                }
     205               
     206                inline void setWantedLOD( int _lod )
     207                {
     208                        if ( wantedLOD >= TerrainPage::MAX_LODS )
     209                                wantedLOD = 4;
     210                        else
     211                                wantedLOD = _lod;
     212                }               
     213        protected:
     214               
     215                /**
     216                 * @brief Tesselates one row of the terrain page.
     217                 * @param _z                    The z-offset of the row
     218                 * @param _xStride              Determines the step-size horizontally
     219                 * @param _zStride              Determines the step-size vertically.
     220                 * @param _adaptRight   True if the right neighbor has a coarser
     221                 *                                              tesselation level.
     222                 * @param _adaptLeft    True if the left neighbor has a coarser
     223                 *                                              tesselation level.                     
     224                 */
     225                void tesselateRow( int _z, int _xStride, int _zStride, bool _adaptLeft, bool _adaptRight );
     226
     227                /**
     228                 * @brief Returns four boolean values in the oder
     229                 */
     230                void determineBorderAdaption( bool _adapt[] );
     231               
     232                /**
     233                 * @brief Adds the given index to the index-array
     234                 */
     235                inline void addIndex( unsigned short _index );
     236               
     237                /**
     238                 * @brief We programmers are very lazy :) This method just adds the last added index
     239                 * again.
     240                 */
     241                inline void addAgain();
     242               
     243               
     244                void getCoord( int _x, int _z, TexCoord& _coord) const;
     245               
     246                /**
     247                 * Fills _vertex with the vertex information at index.
     248                 */
     249                void getVertex( int _x, int _z, Triple& _vertex ) const;
     250               
     251                /**
     252                 * Use this method to safely get a vertex at location ( _x, _z ). If it wasn't
     253                 * created before, this method does that for you.
     254                 */                     
     255                short getIndex( int _x, int _z );
     256                void tesselateLevelFourPatch( bool _adapt[] );
     257           /**
     258                * Generates the tesselation for the given level of detail.
     259                */
     260                void tesselate( int _lod );                     
     261
     262                float getAltitude( int _x, int _z ) const;
     263               
     264                int                                                     currentLOD,
     265                                                                        wantedLOD;
     266                float                                           tween;
     267                pTerrainPage                            left,
     268                                                                        right,
     269                                                                        top,
     270                                                                        bottom;
     271                bool                                            forceTesselation;
     272                bool                                            active;
     273                Triple                                          *vertices;
     274                unsigned short                          *indices;
     275                unsigned short                          *indexHash;
     276                TexCoord                                        *coords;
     277                int                                                     numIndices;
     278                int                                                     numVertices;
     279                bool                                            isVisible;
     280                pTerrainPage                            next;
     281                pTerrainPage                            previous;
     282                LODError                                        *errors;
     283                Triple                                          position;
    273284};
    274285
  • branches/terrain/src/world_entities/camera.cc

    r8349 r8548  
    3434  this->subscribeEvent(ES_GAME, KeyMapper::PEV_VIEW5);
    3535
    36   this->setFovy(90);
     36  this->setFovy(60.0);
    3737  this->setAspectRatio(1.2f);
    38   this->setClipRegion( .1, 100.0f );
     38  this->setClipRegion( .1, 80.0f );
    3939
    4040  this->setViewMode(Camera::ViewNormal);
  • branches/terrain/src/world_entities/terrain_entity.cc

    r8328 r8548  
    140140void TerrainEntity::loadLightmap( const std::string& _lightFile )
    141141{
    142         terrain->setLightmap( _lightFile );
     142        Material *mat = new Material( "TERR_LM" );
     143        mat->setDiffuse( 1.0f, 1.0f, 1.0f );
     144        mat->setAmbient( 1.0f, 1.0f, 1.0f );
     145        mat->setDiffuseMap( _lightFile, GL_TEXTURE_2D, 0 );     
     146        mat->setDiffuseMap( std::string( "pictures/lehm.png" ), GL_TEXTURE_2D, 1 );     
     147        terrain->addMaterial( mat );
    143148}
    144149
Note: See TracChangeset for help on using the changeset viewer.