Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8548 was 8548, checked in by ponder, 18 years ago
  • Added multitexturing support to the terrain-class.
  • The lightmap gets screwed up and I don't know why…
File size: 7.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
17#ifndef TERRAIN_PAGE_H
18#define TERRAIN_PAGE_H
19
20#include "terrain_quad.h"
21#include <stdio.h>
22
23
24class TerrainPage;
25class Terrain;
26
27typedef TerrainPage *pTerrainPage;
28
29typedef struct {
30        Triple  correct;
31        Triple  real;
32        float   diff;
33} LODError, *pLODError;
34
35class TerrainPage : public TerrainQuad {
36        public:
37                enum { TP_LEFT = 0, TP_RIGHT = 1, TP_BOTTOM = 2, TP_TOP = 3 };
38                const static int MAX_LODS = 5;
39                /**
40                 * Creates a new terrain page with its lower left corner set
41                 * to C = ( _xOffset, _zOffset ), where the two values specify
42                 * the offset in the height-map.
43                 * The size of the page, as well as the scaling factors are read
44                 * from the _owner terrain page.
45                 */
46                TerrainPage( Terrain *_owner, int _xOffset, int _zOffset );
47               
48                /**
49                 * This is used only internally for communication between the TerrainPage and
50                 * the Terrain class itself.
51                 */
52                inline bool isActive() { return active; }
53               
54               
55                ~TerrainPage( )
56                { 
57                        if ( isVisible )
58                                hide(); 
59                }
60               
61                /**
62                 * @brief Makes the terrain look as if it were created with the given level of
63                 * detail.
64                 */
65                void mimick( int _level ) {}
66               
67                /**
68                 * @brief Draws a box around the TerrainPage. For debugging purposes.
69                 */
70                void drawBox();
71               
72                /**
73                 * @brief Calculates the smallest fitting axis aligned bounding box for this TerrainPage.
74                 */
75                virtual void calculateBounds();         
76               
77               
78                /**
79                 * @brief Sets the visibility to _flag. If the visibility changed, the vertex and index
80                 * arrays are allocated or freed, respectively.
81                 */
82                inline void setVisibility( bool _flag );
83
84                /**
85                 * @brief Prepares the page for rendering.
86                 */
87                void show( );
88               
89                /**
90                 * @brief Frees most of the memory for economomical reasons.
91                 */
92                void hide( ); 
93               
94                /**
95                 * @brief Updates the tesselation if necessary.
96                 */
97                void updateTesselation( );
98               
99                /**
100                 * @return The current tesselation level.
101                 */
102                int getLOD() { return currentLOD; }
103               
104                /**
105                 * @return The curren tween factor. This is a floating point value  between 0.0f and 1.0f
106                 */
107                float getTween() { return 0.0f; }
108               
109                /**
110                 * @brief Determines the new LOD which should be used by this terrain page based on
111                 * the distance from the camera.
112                 *
113                 * No geometry is updated in this method. You need to call
114                 * updateTesselation() in order to see a change in geometry. This method is
115                 * just a recommondation for the LOD. It might be invalid due to outer
116                 * constraints.
117                 */
118                int chooseLOD();
119               
120                /**
121                 * If the terrain pages tesselation level changed between the last and the
122                 * current frame, this function returns true, else you'll get false as the
123                 * return value.
124                 * @return True if the page needs an update and false if not.
125                 */
126                bool isDirty() { return forceTesselation; }
127               
128                /**
129                 * @brief Calculates the maximal errors for every LOD.
130                 */
131                void calculateErrors();
132               
133                /**
134                 * @brief Calculates the error for the given LOD. We just need to know the "worst"
135                 * vertex for choosing an appropriate LOD.
136                 */
137                void calculateError( int _lod );
138               
139               
140                /**
141                 * Tests if the terrain page would cull against the viewing frustum.
142                 */
143                bool cull( );
144
145                bool needsRetesselation();
146                /**
147                 * Sets the neighbors of this terrain page. pass null if a neighbor if this
148                 * pages is at the border.
149                 */
150                inline void setNeighbors( pTerrainPage _left, pTerrainPage _right, 
151                        pTerrainPage _top, pTerrainPage _bottom )
152                {
153                        left = _left; right = _right; top = _top; bottom = _bottom;
154                }
155               
156                /**
157                 * Sets the position of the TerrainPage. Is this needed?
158                 */
159                inline void setPosition( const Triple& _pos )
160                {
161                        position.x = _pos.x;
162                        position.y = _pos.y;
163                        position.z = _pos.z;
164                }
165
166                pTerrainPage getLeft() { return left; }
167                pTerrainPage getRight() { return right; }
168                pTerrainPage getBottom() { return bottom; }
169                pTerrainPage getTop() { return top; }                                           
170               
171                /**
172                 *  Does what exactly what the name says and nothing more.
173                 */
174                void draw( );
175               
176                /**
177                 * @return the next active page
178                 */
179                inline pTerrainPage getNext() { return next; }
180               
181                /**
182                 * Returns the previous active page
183                 */
184                inline pTerrainPage getPrevious() { return previous; }                 
185                inline int getCurrentLOD() { return currentLOD; }
186                /**
187                 * @return Returns the wanted LOD. Make sure you call this method after a call to
188                 * chooseLOD() or you will get screwed values.
189                 */
190                inline int getWantedLOD() { return wantedLOD; }
191               
192                /**
193                 * @brief Removes the page from the active page list.
194                 */
195                void deactivate();
196               
197                /**
198                 * @brief Inserts the page into the active page list.
199                 */
200                void activate();               
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;
284};
285
286inline void TerrainPage::setVisibility( bool _flag )
287{
288        if ( _flag ) {
289                if ( !isVisible ) {
290                        isVisible = true;
291                        show( );
292                }
293                active = true;
294        }
295        else {
296                if ( isVisible ) {
297                        isVisible = false;
298                        hide( );
299                }
300        }
301}
302
303inline void TerrainPage::addIndex( unsigned short _index )
304{
305        indices[numIndices] = _index; numIndices++;
306}
307
308inline void TerrainPage::addAgain() 
309{ 
310        indices[numIndices] = indices[numIndices-1]; numIndices++;                             
311}
312
313#endif
Note: See TracBrowser for help on using the repository browser.