Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/terrain/src/lib/graphics/importer/terrain/terrain.h @ 9140

Last change on this file since 9140 was 9140, checked in by bensch, 18 years ago

merged back

File size: 5.4 KB
RevLine 
[8319]1/*
2        orxonox - the future of 3D-vertical-scrollers
3 
4        Copyright (C) 2006 orx
5 
6        This program is free software; you can redistribute it and/or modify
7        it under the terms of the GNU General Public License as published by
8        the Free Software Foundation; either version 2, or (at your option)
9        any later version.
10 
11        ### File Specific:
12        main programmer: Marco Biasini
13
14 */
15
16#ifndef _TERRAIN_H
17#define _TERRAIN_H
18
[8569]19#define USE_VBO
20#ifdef USE_VBO
21#include "buffer_broker.h"
22#endif
[8319]23#include "types.h"
24#include "terrain_page.h"
25#include "terrain_quad.h"
26#include <cstdlib>
27#include <string>
[8744]28#include <vector>
[8319]29#include "texture.h"
30#include "frustum.h"
31
[8744]32class Terrain;
33typedef Terrain *pTerrain;
34       
[8319]35class Terrain {
36        public:
[8745]37                const static unsigned int PAGE_SIZE             = 17;
38                const static unsigned int MAX_VERTICES          = PAGE_SIZE*PAGE_SIZE;
39                const static unsigned int MAX_INDICES           = 2*( PAGE_SIZE*PAGE_SIZE + PAGE_SIZE -2 );
[8319]40                /**
41                 * The amount of geometry rendered is largely depending on this constant. So chose a
42                 * reasonable value...
43                 */
[8741]44                inline float getDetail() { return 0.02f; }
[8319]45
46       
47                inline bool debug()
48                {
49                        return false;
50                }
51               
52                inline pTerrainPage getActiveList() { return activePages; }
53
54       
55                inline void setCameraPosition( const Triple& _cameraPosition ) 
56                { 
57                        cameraPosition = _cameraPosition; 
58                }
59               
60                inline const Triple& getCameraPosition() const
61                {
62                        return cameraPosition;
63                }
64       
65                void getAltitude( Triple& _alt, Triple& _normal );
[8328]66               
[8744]67                inline void setLightmap( Texture *_lightmap )
68                {
69                        lightmap = _lightmap;
70                }
[8641]71                //What a bad name: Need to rename that :(
72                void addLevelFourPage( int _numVertices, Vertex *_vertices, 
73                        int _numIndices, unsigned short *_indices );
[8319]74                void showPages( int _x0, int _z0, int _width, int _height );
75                                       
76                /**
77                 * Create a new terrain.
78                 */
79                Terrain( )
80                { 
81                        pageSize = 17;
[8770]82                        pagesX = pagesZ = 0;
83                        lightmap = NULL;
84                        heightmapSource = "";
85                        root = NULL;
86                        current = 0;
87                        activePages = NULL;
[8319]88                        scale  = Triple( 1.0f, 3.0f, 1.0f );
89                        frustum = new Frustum();
90                        activatedCount = deactivatedCount = 0;
[8641]91                        vertices = new Vertex[MAX_VERTICES];
92                        indices = new unsigned short[MAX_INDICES];
[8569]93#ifdef USE_VBO
[9071]94                        broker = new BufferBroker( 300, MAX_VERTICES*sizeof( Vertex ), 
[8641]95                                MAX_INDICES*sizeof( short ) );
[8569]96#endif                 
[8319]97                }
98               
[8775]99                ~Terrain( ) 
100                {
101                        //The pages are deleted through the SAVE_DELETE( root ) call.
102                        SAVE_DELETE_ARRAY( pages );
103                        SAVE_DELETE_ARRAY( vertices );
104                        SAVE_DELETE_ARRAY( indices );
105                        SAVE_DELETE( lightmap );
106                        SAVE_DELETE( root );
107#ifdef USE_VBO
108                        SAVE_DELETE( broker );
109#endif                 
110                }
[8319]111                /**
112                 * Draws the terrain.
113                 */
114                void draw( );
115
116                void setPageSize( const Triple& _page ) { }
117               
118                void build();
[8929]119                void tick( float _dt );
[8319]120                inline void setActiveList( pTerrainPage _page )
121                {
122                        activePages = _page;
123                }
124                inline pFrustum getViewingFrustum()
125                {
126                        return frustum;
127                }
128               
129                /**
130                 * Returns the normalized ( range from [0..1] ) altitude from the heightmap for the given
131                 * coordinates.
132                 */
133                inline float getAltitude( int _x, int _z ) const;
134               
[8744]135                inline void addMaterialLayer( LayerInfo *_layer )
136                { layers.push_back( _layer ); }
[8548]137               
[8328]138                inline void setHeightmap( const std::string &_heightmap ) 
[8319]139                { 
140                        heightmapSource = _heightmap; 
141                }
142               
143                inline void setPageSize( int _pageSize )
144                {
145                        pageSize = _pageSize;
146                }
147               
148                inline const Triple getScale() const { return scale; }
[8548]149               
[8319]150                inline int getPageSize() const 
151                {
152                        return pageSize;
153                }
[8548]154               
[8319]155                inline void getCoord( int _x, int _z, TexCoord& _coord) const
156                {
157                        _coord.u = ( ( float )_x )/( heightfield.width-1 );
158                        _coord.v = ( ( float )_z )/( heightfield.height-1 );                   
159                }
160               
161                inline void setScale( const Triple& _scale )
[8775]162                { scale = _scale; }
[8569]163#ifdef USE_VBO
164                inline pBufferBroker getBufferBroker() { return broker; }
165#endif         
[8319]166                inline TerrainPage *getPage( int _x, int _z )
167                {
168                        return pages[pagesX*_z+_x];
169                }
170        protected:
171                       
172                /**
173                 * convenience method to create a new terrain page at position offset
174                 * p = ( _xOffset, _zOffset ).
175                 */
176                pTerrainPage createPage( int _xOffset, int _zOffset ) const;                           
177               
178                /**
179                 * Creates the quad tree structure for fast culling of the terrain pages.
180                 */             
[8748]181                pTerrainQuad createQuadTree( int _x0, int _z0, 
182                        int _x1, int _z1, int _depth = 0 );
[8319]183               
184                /**
185                 * Walks the quad tree to determine which pages are visible, e.g. are in the viewing
186                 * frustum of the camera.
187                 */
[8745]188                void determineVisiblePages( pTerrainQuad _node );                       
189                void determineLayerVisibility( int _layer );                   
[8319]190               
[8569]191#ifdef USE_VBO
[8641]192                pBufferBroker                           broker;
[8569]193#endif
[8641]194                pTerrainQuad                            root;           // The quad-tree root node.
195                pTerrainPage                            *pages;         // the references to all pages
196                std::string                                     heightmapSource;
[8744]197                Texture                                         *lightmap;
[8641]198                Heightfield                                     heightfield;
199                Triple                                          scale;
200                int                                             pagesX, 
201                                                                        pagesZ;
202                int                                                     pageSize;       
203                int                                                     cullCount;
[8319]204               
[8641]205                int                                                     activatedCount, 
206                                                                        deactivatedCount; // For debugging and statistics
207                                                               
208                //The next five members are for the level 4 pages.                                             
209                Vertex                                          *vertices;                                             
210                unsigned short                          *indices;
211                int                                                     current;
212                                                                       
213                std::vector<BufferInfo>         buffers;                       
214                       
215                Triple                                          cameraPosition;
216                pFrustum                                        frustum;
217                pTerrainPage                            activePages;
[8744]218                std::vector<LayerInfo*>         layers;
[8319]219};
220
221inline float Terrain::getAltitude( int _x, int _z ) const
222{
[8770]223        assert( heightfield.data );
[8748]224        if ( _x < 0 || _x >= heightfield.width || _z < 0 || _z >= heightfield.height ) {
[8747]225                return 0.0f;
[8748]226                printf( "outside!\n" );
227        }       
[8319]228        return heightfield.data[heightfield.pitch*_z+_x]/255.0f;       
229}
230
231#endif
Note: See TracBrowser for help on using the repository browser.