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