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