Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

I know this is a big commit. I'm sorry about that. Anyway…

  • Added the Terrain / TerrainQuad TerrainPage and Frustum classes to the project. The rendering frustum culling is working.
  • The types.h is here only temporary. It should be removed afterwards.
File size: 4.0 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#include "types.h"
20#include "terrain_page.h"
21#include "terrain_quad.h"
22#include <cstdlib>
23#include <string>
24#include "texture.h"
25#include "frustum.h"
26
27
28typedef struct {
29        int             width;
30        int             height;
31        int                     pitch;
32        UByte           *data;
33} Heightfield, *pHeightfield;
34
35class Terrain;
36typedef Terrain *pTerrain;
37
38class TerrainPage;
39
40class Terrain {
41        public:
42       
43                /**
44                 * The amount of geometry rendered is largely depending on this constant. So chose a
45                 * reasonable value...
46                 */
47                inline float getDetail() { return 0.05f; }
48
49       
50                inline bool debug()
51                {
52                        return false;
53                }
54               
55                inline pTerrainPage getActiveList() { return activePages; }
56
57       
58                inline void setCameraPosition( const Triple& _cameraPosition ) 
59                { 
60                        cameraPosition = _cameraPosition; 
61                }
62               
63                inline const Triple& getCameraPosition() const
64                {
65                        return cameraPosition;
66                }
67       
68                void getAltitude( Triple& _alt, Triple& _normal );
69                void showPages( int _x0, int _z0, int _width, int _height );
70                                       
71                /**
72                 * Create a new terrain.
73                 */
74                Terrain( )
75                { 
76                        pageSize = 17;
77                        scale  = Triple( 1.0f, 3.0f, 1.0f );
78                        frustum = new Frustum();
79                        activatedCount = deactivatedCount = 0;
80                }
81               
82                /**
83                 * Draws the terrain.
84                 */
85                void draw( );
86
87                void setPageSize( const Triple& _page ) { }
88               
89                void build();
90               
91                inline void setActiveList( pTerrainPage _page )
92                {
93                        activePages = _page;
94                }
95                inline pFrustum getViewingFrustum()
96                {
97                        return frustum;
98                }
99               
100                /**
101                 * Returns the normalized ( range from [0..1] ) altitude from the heightmap for the given
102                 * coordinates.
103                 */
104                inline void incActivated() { activatedCount++; }
105                inline void incDeactivated() { deactivatedCount++; }
106                inline float getAltitude( int _x, int _z ) const;
107               
108                inline void setLightmap( std::string &_lightmap )
109                {
110                        lightmapSource = _lightmap;
111                }
112                inline void setHeightmap( std::string &_heightmap ) 
113                { 
114                        heightmapSource = _heightmap; 
115                }
116               
117                inline void setPageSize( int _pageSize )
118                {
119                        pageSize = _pageSize;
120                }
121               
122                inline const Triple getScale() const { return scale; }
123                inline int getPageSize() const 
124                {
125                        return pageSize;
126                }
127                inline void getCoord( int _x, int _z, TexCoord& _coord) const
128                {
129                        _coord.u = ( ( float )_x )/( heightfield.width-1 );
130                        _coord.v = ( ( float )_z )/( heightfield.height-1 );                   
131                }
132               
133                inline void setScale( const Triple& _scale )
134                {
135                        scale = _scale;
136                }
137               
138                inline TerrainPage *getPage( int _x, int _z )
139                {
140                        return pages[pagesX*_z+_x];
141                }
142        protected:
143                       
144                /**
145                 * convenience method to create a new terrain page at position offset
146                 * p = ( _xOffset, _zOffset ).
147                 */
148                pTerrainPage createPage( int _xOffset, int _zOffset ) const;                           
149               
150                /**
151                 * Creates the quad tree structure for fast culling of the terrain pages.
152                 */             
153                pTerrainQuad createQuadTree( int _x0, int _z0, int _x1, int _z1, int _depth = 0 );
154               
155                /**
156                 * Walks the quad tree to determine which pages are visible, e.g. are in the viewing
157                 * frustum of the camera.
158                 */
159                void determineVisiblePages( pTerrainQuad _node );                                               
160               
161                pTerrainQuad            root;           // The quad-tree root node.
162                pTerrainPage            *pages;         // the references to all pages
163                std::string                     heightmapSource;
164                std::string                     lightmapSource;
165                Heightfield                     heightfield;
166                Triple                          scale;
167                int                             pagesX, 
168                                                        pagesZ;
169                int                                     pageSize;       
170                int                                     cullCount;
171               
172                int                                     activatedCount, 
173                                                        deactivatedCount; // For debugging and statistics
174                Texture                         *tex;
175                Triple                          cameraPosition;
176                pFrustum                        frustum;
177                pTerrainPage            activePages;
178};
179
180inline float Terrain::getAltitude( int _x, int _z ) const
181{
182        if ( _x > heightfield.width || _z > heightfield.height )
183                return 0.0f;
184        return heightfield.data[heightfield.pitch*_z+_x]/255.0f;       
185}
186
187#endif
Note: See TracBrowser for help on using the repository browser.