1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollers |
---|
3 | |
---|
4 | Copyright (C) 2004 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: Benjamin Grauer |
---|
13 | co-programmer: ... |
---|
14 | */ |
---|
15 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY |
---|
16 | |
---|
17 | |
---|
18 | #include "terrain_entity.h" |
---|
19 | #include "terrain/terrain.h" |
---|
20 | #include "util/loading/load_param.h" |
---|
21 | #include "util/loading/factory.h" |
---|
22 | #include "spatial_separation.h" |
---|
23 | |
---|
24 | #include "util/loading/resource_manager.h" |
---|
25 | #include "model.h" |
---|
26 | #include "network_game_manager.h" |
---|
27 | |
---|
28 | #include "material.h" |
---|
29 | |
---|
30 | #include "glincl.h" |
---|
31 | |
---|
32 | #include "state.h" |
---|
33 | |
---|
34 | using namespace std; |
---|
35 | |
---|
36 | CREATE_FACTORY( TerrainEntity, CL_TERRAIN ); |
---|
37 | |
---|
38 | /** |
---|
39 | * standard constructor |
---|
40 | */ |
---|
41 | TerrainEntity::TerrainEntity (const TiXmlElement* root) |
---|
42 | { |
---|
43 | this->init(); |
---|
44 | |
---|
45 | |
---|
46 | if( root != NULL) |
---|
47 | this->loadParams(root); |
---|
48 | terrain->build( ); |
---|
49 | } |
---|
50 | |
---|
51 | |
---|
52 | /** |
---|
53 | * Constructor for loading a TerrainEntity out of a file |
---|
54 | * @param fileName The file to load data from. |
---|
55 | |
---|
56 | this either loads out of an OBJ-file, or loads a heightmap if no .obj-extension is found. |
---|
57 | */ |
---|
58 | TerrainEntity::TerrainEntity(const std::string& fileName ) |
---|
59 | { |
---|
60 | this->init(); |
---|
61 | |
---|
62 | if (fileName.rfind(".obj" ) != -1 || fileName.rfind(".OBJ") != -1 ) |
---|
63 | { |
---|
64 | this->loadModel(fileName); |
---|
65 | } |
---|
66 | else |
---|
67 | { |
---|
68 | // load the hightMap here. |
---|
69 | } |
---|
70 | } |
---|
71 | |
---|
72 | /** |
---|
73 | * a Constructor for the Debug-Worlds |
---|
74 | */ |
---|
75 | TerrainEntity::TerrainEntity(DebugTerrainEntity debugTerrainEntity) |
---|
76 | { |
---|
77 | this->init(); |
---|
78 | this->buildDebugTerrainEntity(debugTerrainEntity); |
---|
79 | } |
---|
80 | |
---|
81 | /** |
---|
82 | * standard deconstructor |
---|
83 | |
---|
84 | */ |
---|
85 | TerrainEntity::~TerrainEntity () |
---|
86 | { |
---|
87 | if (objectList) |
---|
88 | glDeleteLists(this->objectList, 1); |
---|
89 | |
---|
90 | if (this->vegetation) |
---|
91 | { |
---|
92 | ResourceManager::getInstance()->unload( this->vegetation ); |
---|
93 | } |
---|
94 | |
---|
95 | if( this->terrain ) |
---|
96 | delete terrain; |
---|
97 | } |
---|
98 | |
---|
99 | |
---|
100 | void TerrainEntity::init() |
---|
101 | { |
---|
102 | this->setClassID( CL_TERRAIN, "TerrainEntity"); |
---|
103 | this->toList(OM_ENVIRON_NOTICK); |
---|
104 | this->toReflectionList(); |
---|
105 | |
---|
106 | this->objectList = 0; |
---|
107 | this->vegetation = NULL; |
---|
108 | |
---|
109 | this->terrain = new Terrain(); |
---|
110 | |
---|
111 | //this->heightMapMaterial = new Material(); |
---|
112 | } |
---|
113 | |
---|
114 | |
---|
115 | void TerrainEntity::loadParams(const TiXmlElement* root) |
---|
116 | { |
---|
117 | WorldEntity::loadParams( root ); |
---|
118 | |
---|
119 | LoadParam(root, "scale", this, TerrainEntity, setScale) |
---|
120 | .describe("The scale in x,y,z direction"); |
---|
121 | |
---|
122 | LoadParam( root, "light_map", this, TerrainEntity, loadLightmap ) |
---|
123 | .describe("The name of the lightmap."); |
---|
124 | |
---|
125 | LoadParam( root, "elevation_map", this, TerrainEntity, loadElevationmap ) |
---|
126 | .describe( "The name of the elevation map. Must be an 8bit image" ); |
---|
127 | ResourceManager *manager = ResourceManager::getInstance(); |
---|
128 | TiXmlElement * layer = (TiXmlElement*)root->FirstChild( "material_layer" ); |
---|
129 | while ( layer ) { |
---|
130 | LayerInfo *info = new LayerInfo(); |
---|
131 | TiXmlElement *detail = (TiXmlElement*)layer->FirstChild( "detail_map" ); |
---|
132 | if ( detail ) { |
---|
133 | string detailmap( detail->Attribute( "file" ) ); |
---|
134 | info->detail = (Texture*)manager->load( detailmap, |
---|
135 | IMAGE, RP_GAME, (int)GL_TEXTURE_2D ); |
---|
136 | if ( !info->detail ) { |
---|
137 | PRINTF(0)( "%s not found\n", detailmap.c_str() ); |
---|
138 | } |
---|
139 | TiXmlElement *repeat = (TiXmlElement*)detail->FirstChild( "repeat" ); |
---|
140 | if ( repeat ) { |
---|
141 | repeat->QueryFloatAttribute( "x", &info->repeatX ); |
---|
142 | repeat->QueryFloatAttribute( "z", &info->repeatZ ); |
---|
143 | } |
---|
144 | } |
---|
145 | else { |
---|
146 | PRINTF(0)( "Please specify a detail-map" ); |
---|
147 | } |
---|
148 | TiXmlElement *alpha = (TiXmlElement*)layer->FirstChild( "alpha" ); |
---|
149 | if ( alpha ) { |
---|
150 | if ( !alpha->Attribute( "full" ) ) { |
---|
151 | string alphamap( alpha->Attribute( "file" ) ); |
---|
152 | info->alpha = (Texture*)manager->load( alphamap, |
---|
153 | IMAGE, RP_GAME, (int)GL_TEXTURE_2D ); |
---|
154 | } |
---|
155 | } |
---|
156 | terrain->addMaterialLayer( info ); |
---|
157 | layer = (TiXmlElement*)layer->NextSibling( "material_layer" ); |
---|
158 | } |
---|
159 | } |
---|
160 | |
---|
161 | void TerrainEntity::setScale(float x, float y, float z) |
---|
162 | { |
---|
163 | terrain->setScale( Triple( x, y, z ) ); |
---|
164 | } |
---|
165 | |
---|
166 | void TerrainEntity::loadElevationmap( const std::string& _eleFile ) |
---|
167 | { |
---|
168 | terrain->setHeightmap( _eleFile ); |
---|
169 | } |
---|
170 | |
---|
171 | void TerrainEntity::loadLightmap( const std::string& _lightFile ) |
---|
172 | { |
---|
173 | ResourceManager *manager = ResourceManager::getInstance(); |
---|
174 | Texture *lightmap = (Texture*)manager->load( _lightFile, |
---|
175 | IMAGE, RP_GAME, (int)GL_TEXTURE_2D ); |
---|
176 | if ( lightmap ) |
---|
177 | terrain->setLightmap( lightmap ); |
---|
178 | else { |
---|
179 | PRINTF(0)("no lightmap %s\n", _lightFile.c_str() ); |
---|
180 | |
---|
181 | } |
---|
182 | } |
---|
183 | |
---|
184 | |
---|
185 | |
---|
186 | void TerrainEntity::loadVegetation(const std::string& vegetationFile) |
---|
187 | { |
---|
188 | PRINTF(4)("loadVegetation: %s\n", vegetationFile.c_str()); |
---|
189 | if (this->vegetation) |
---|
190 | ResourceManager::getInstance()->unload(this->vegetation, RP_LEVEL); |
---|
191 | if (!vegetationFile.empty()) |
---|
192 | { |
---|
193 | PRINTF(4)("fetching %s\n", vegetationFile.c_str()); |
---|
194 | this->vegetation = dynamic_cast<Model*>(ResourceManager::getInstance()->load(vegetationFile, OBJ, RP_CAMPAIGN)); |
---|
195 | } |
---|
196 | else |
---|
197 | this->vegetation = NULL; |
---|
198 | } |
---|
199 | |
---|
200 | |
---|
201 | |
---|
202 | |
---|
203 | |
---|
204 | void TerrainEntity::draw () const |
---|
205 | { |
---|
206 | glMatrixMode( GL_MODELVIEW ); |
---|
207 | glPushMatrix(); |
---|
208 | |
---|
209 | glTranslatef( this->getAbsCoor().x, |
---|
210 | this->getAbsCoor().y, |
---|
211 | this->getAbsCoor().z ); |
---|
212 | |
---|
213 | Vector cam = State::getCameraNode()->getAbsCoor(); |
---|
214 | |
---|
215 | if ( this->terrain ) { |
---|
216 | terrain->setCameraPosition( Triple( cam.x, cam.y, cam.z ) ); |
---|
217 | terrain->draw( ); |
---|
218 | } |
---|
219 | glPopMatrix(); |
---|
220 | |
---|
221 | |
---|
222 | } |
---|
223 | |
---|
224 | |
---|
225 | void TerrainEntity::buildDebugTerrainEntity(DebugTerrainEntity debugTerrainEntity) |
---|
226 | { |
---|
227 | terrain->build( ); |
---|
228 | /* |
---|
229 | // if the TerrainEntity is the TerrainEntity of Dave |
---|
230 | if (debugTerrainEntity == TERRAINENTITY_DAVE) |
---|
231 | { |
---|
232 | objectList = glGenLists(1); |
---|
233 | glNewList (objectList, GL_COMPILE); |
---|
234 | |
---|
235 | glColor3f(1.0,0,0); |
---|
236 | |
---|
237 | int sizeX = 100; |
---|
238 | int sizeZ = 80; |
---|
239 | float length = 1000; |
---|
240 | float width = 200; |
---|
241 | float widthX = float (length /sizeX); |
---|
242 | float widthZ = float (width /sizeZ); |
---|
243 | |
---|
244 | float height [sizeX][sizeZ]; |
---|
245 | Vector normal_vectors[sizeX][sizeZ]; |
---|
246 | |
---|
247 | |
---|
248 | for ( int i = 0; i<sizeX-1; i+=1) |
---|
249 | for (int j = 0; j<sizeZ-1;j+=1) |
---|
250 | //height[i][j] = rand()/20046 + (j-25)*(j-25)/30; |
---|
251 | #ifdef __WIN32__ |
---|
252 | height[i][j]=(sin((float)j/3)*rand()*i/182400)*.5; |
---|
253 | #else |
---|
254 | height[i][j]=(sin((float)j/3)*rand()*(long)i/6282450500.0)*.5; |
---|
255 | #endif |
---|
256 | |
---|
257 | //Die Huegel ein wenig glaetten |
---|
258 | for (int h=1; h<2;h++) |
---|
259 | for (int i=1;i<sizeX-2 ;i+=1 ) |
---|
260 | for(int j=1;j<sizeZ-2;j+=1) |
---|
261 | height[i][j]=(height[i+1][j]+height[i][j+1]+height[i-1][j]+height[i][j-1])/4; |
---|
262 | |
---|
263 | //Berechnung von normalen Vektoren |
---|
264 | for(int i=1;i<sizeX-2;i+=1) |
---|
265 | for(int j=1;j<sizeZ-2 ;j+=1) |
---|
266 | { |
---|
267 | Vector v1 = Vector (widthX*(1), height[i][j], widthZ*(j) ); |
---|
268 | Vector v2 = Vector (widthX*(i-1), height[i-1][j], widthZ*(j)); |
---|
269 | Vector v3 = Vector (widthX*(i), height[i][j+1], widthZ*(j+1)); |
---|
270 | Vector v4 = Vector (widthX*(i+1), height[i+1][j], widthZ*(j)); |
---|
271 | Vector v5 = Vector (widthX*(i), height[i][j-1], widthZ*(j-1)); |
---|
272 | |
---|
273 | Vector c1 = v2 - v1; |
---|
274 | Vector c2 = v3 - v1; |
---|
275 | Vector c3= v4 - v1; |
---|
276 | Vector c4 = v5 - v1; |
---|
277 | Vector zero = Vector (0,0,0); |
---|
278 | normal_vectors[i][j]=c1.cross(v3-v5)+c2.cross(v4-v2)+c3.cross(v5-v3)+c4.cross(v2-v4); |
---|
279 | normal_vectors[i][j].normalize(); |
---|
280 | } |
---|
281 | |
---|
282 | glBegin(GL_QUADS); |
---|
283 | int snowheight=3; |
---|
284 | for ( int i = 0; i<sizeX; i+=1) |
---|
285 | for (int j = 0; j<sizeZ;j+=1) |
---|
286 | { |
---|
287 | Vector v1 = Vector (widthX*(i), height[i][j]-20, widthZ*(j) -width/2); |
---|
288 | Vector v2 = Vector (widthX*(i+1), height[i+1][j]-20, widthZ*(j) -width/2); |
---|
289 | Vector v3 = Vector (widthX*(i+1), height[i+1][j+1]-20, widthZ*(j+1)-width/2); |
---|
290 | Vector v4 = Vector (widthX*(i), height[i][j+1]-20, widthZ*(j+1)-width/2); |
---|
291 | float a[3]; |
---|
292 | if(height[i][j]<snowheight) |
---|
293 | { |
---|
294 | a[0]=0; |
---|
295 | a[1]=1.0-height[i][j]/10-.3; |
---|
296 | a[2]=0; |
---|
297 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
298 | } |
---|
299 | else |
---|
300 | { |
---|
301 | a[0]=1.0; |
---|
302 | a[1]=1.0; |
---|
303 | a[2]=1.0; |
---|
304 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
305 | |
---|
306 | } |
---|
307 | glNormal3f(normal_vectors[i][j].x, normal_vectors[i][j].y, normal_vectors[i][j].z); |
---|
308 | glVertex3f(v1.x, v1.y, v1.z); |
---|
309 | if(height[i+1][j]<snowheight) |
---|
310 | { |
---|
311 | a[0]=0; |
---|
312 | a[1] =1.0-height[i+1][j]/10-.3; |
---|
313 | a[2]=0; |
---|
314 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
315 | } |
---|
316 | else |
---|
317 | { |
---|
318 | a[0]=1.0; |
---|
319 | a[1]=1.0; |
---|
320 | a[2]=1.0; |
---|
321 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
322 | |
---|
323 | } |
---|
324 | glNormal3f(normal_vectors[i+1][j].x, normal_vectors[i+1][j].y, normal_vectors[i+1][j].z); |
---|
325 | glVertex3f(v2.x, v2.y, v2.z); |
---|
326 | if(height[i+1][j+1]<snowheight) |
---|
327 | { |
---|
328 | a[0]=0; |
---|
329 | a[1] =1.0-height[i+1][j+1]/10-.3; |
---|
330 | a[2]=0; |
---|
331 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
332 | } |
---|
333 | else |
---|
334 | { |
---|
335 | a[0]=1.0; |
---|
336 | a[1]=1.0; |
---|
337 | a[2]=1.0; |
---|
338 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
339 | |
---|
340 | |
---|
341 | } |
---|
342 | glNormal3f(normal_vectors[i+1][j+1].x, normal_vectors[i+1][j+1].y, normal_vectors[i+1][j+1].z); |
---|
343 | glVertex3f(v3.x, v3.y, v3.z); |
---|
344 | if(height[i][j+1]<snowheight) |
---|
345 | { |
---|
346 | a[0]=0; |
---|
347 | a[1] =1.0-height[i+1][j+1]/10-.3; |
---|
348 | a[2]=0; |
---|
349 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
350 | } |
---|
351 | else |
---|
352 | { |
---|
353 | a[0]=1.0; |
---|
354 | a[1]=1.0; |
---|
355 | a[2]=1.0; |
---|
356 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
357 | } |
---|
358 | glNormal3f(normal_vectors[i][j+1].x, normal_vectors[i][j+1].y, normal_vectors[i][j+1].z); |
---|
359 | glVertex3f(v4.x, v4.y, v4.z); |
---|
360 | |
---|
361 | } |
---|
362 | glEnd(); |
---|
363 | glEndList(); |
---|
364 | } |
---|
365 | */ |
---|
366 | if (debugTerrainEntity == TERRAINENTITY_BENSCH) |
---|
367 | { |
---|
368 | /* |
---|
369 | this->model = (OBJModel*) new Model(); |
---|
370 | this->model->setName("CUBE"); |
---|
371 | this->model->addVertex (-0.5, -0.5, 0.5); |
---|
372 | this->model->addVertex (0.5, -0.5, 0.5); |
---|
373 | this->model->addVertex (-0.5, 0.5, 0.5); |
---|
374 | this->model->addVertex (0.5, 0.5, 0.5); |
---|
375 | this->model->addVertex (-0.5, 0.5, -0.5); |
---|
376 | this->model->addVertex (0.5, 0.5, -0.5); |
---|
377 | this->model->addVertex (-0.5, -0.5, -0.5); |
---|
378 | this->model->addVertex (0.5, -0.5, -0.5); |
---|
379 | |
---|
380 | this->model->addVertexTexture (0.0, 0.0); |
---|
381 | this->model->addVertexTexture (1.0, 0.0); |
---|
382 | this->model->addVertexTexture (0.0, 1.0); |
---|
383 | this->model->addVertexTexture (1.0, 1.0); |
---|
384 | this->model->addVertexTexture (0.0, 2.0); |
---|
385 | this->model->addVertexTexture (1.0, 2.0); |
---|
386 | this->model->addVertexTexture (0.0, 3.0); |
---|
387 | this->model->addVertexTexture (1.0, 3.0); |
---|
388 | this->model->addVertexTexture (0.0, 4.0); |
---|
389 | this->model->addVertexTexture (1.0, 4.0); |
---|
390 | this->model->addVertexTexture (2.0, 0.0); |
---|
391 | this->model->addVertexTexture (2.0, 1.0); |
---|
392 | this->model->addVertexTexture (-1.0, 0.0); |
---|
393 | this->model->addVertexTexture (-1.0, 1.0); |
---|
394 | |
---|
395 | this->model->finalize(); |
---|
396 | */ |
---|
397 | } |
---|
398 | } |
---|
399 | void TerrainEntity::getAltitude( Vector& _position, Vector& _normal ) |
---|
400 | { |
---|
401 | Triple altitude( _position.x-getAbsCoor().x, 0.0f, _position.z-getAbsCoor().z ), |
---|
402 | normal( 0.0f, 0.0f, 0.0f ); |
---|
403 | if ( terrain ) |
---|
404 | terrain->getAltitude( altitude, normal ); |
---|
405 | |
---|
406 | _position.y = altitude.y+getAbsCoor().y; |
---|
407 | _normal.z = normal.z; _normal.y = normal.y; _normal.z = normal.z; |
---|
408 | } |
---|
409 | |
---|
410 | float TerrainEntity::getHeight( float x, float z ) |
---|
411 | { |
---|
412 | Triple altitude( x-getAbsCoor().x, 0.0f, z-getAbsCoor().z ), |
---|
413 | normal( 0.0f, 0.0f, 0.0f ); |
---|
414 | if ( terrain ) |
---|
415 | terrain->getAltitude( altitude, normal ); |
---|
416 | |
---|
417 | return altitude.y+getAbsCoor().y; |
---|
418 | } |
---|