Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 9147 was 9147, checked in by ponder, 18 years ago

To rid of the triple and plane struct in types.h. now using the generic ones

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