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 | #include "vector.h" |
---|
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); |
---|
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 | else |
---|
140 | PRINTF(0)( "loaded %s\n", detailmap.c_str() ); |
---|
141 | TiXmlElement *repeat = (TiXmlElement*)detail->FirstChild( "repeat" ); |
---|
142 | if ( repeat ) { |
---|
143 | repeat->QueryFloatAttribute( "x", &info->repeatX ); |
---|
144 | repeat->QueryFloatAttribute( "z", &info->repeatZ ); |
---|
145 | } |
---|
146 | } |
---|
147 | else { |
---|
148 | PRINTF(0)( "Please specify a detail-map" ); |
---|
149 | } |
---|
150 | TiXmlElement *alpha = (TiXmlElement*)layer->FirstChild( "alpha" ); |
---|
151 | if ( alpha ) { |
---|
152 | if ( !alpha->Attribute( "full" ) ) { |
---|
153 | string alphamap( alpha->Attribute( "file" ) ); |
---|
154 | info->alpha = (Texture*)manager->load( alphamap, |
---|
155 | IMAGE, RP_GAME, (int)GL_TEXTURE_2D ); |
---|
156 | if ( !info->alpha ) |
---|
157 | PRINTF(0)( "%s not found\n", alphamap.c_str() ); |
---|
158 | else |
---|
159 | PRINTF(0)( "loaded %s\n", alphamap.c_str() ); |
---|
160 | } |
---|
161 | } |
---|
162 | terrain->addMaterialLayer( info ); |
---|
163 | layer = (TiXmlElement*)layer->NextSibling( "material_layer" ); |
---|
164 | } |
---|
165 | } |
---|
166 | |
---|
167 | void TerrainEntity::setScale(float x, float y, float z) |
---|
168 | { |
---|
169 | terrain->setScale( Vector( x, y, z ) ); |
---|
170 | } |
---|
171 | |
---|
172 | void TerrainEntity::loadElevationmap( const std::string& _eleFile ) |
---|
173 | { |
---|
174 | terrain->setHeightmap( _eleFile ); |
---|
175 | } |
---|
176 | |
---|
177 | void TerrainEntity::loadLightmap( const std::string& _lightFile ) |
---|
178 | { |
---|
179 | ResourceManager *manager = ResourceManager::getInstance(); |
---|
180 | Texture *lightmap = (Texture*)manager->load( _lightFile, |
---|
181 | IMAGE, RP_GAME, (int)GL_TEXTURE_2D ); |
---|
182 | if ( lightmap ) |
---|
183 | terrain->setLightmap( lightmap ); |
---|
184 | else { |
---|
185 | PRINTF(0)("no lightmap %s\n", _lightFile.c_str() ); |
---|
186 | |
---|
187 | } |
---|
188 | } |
---|
189 | |
---|
190 | |
---|
191 | |
---|
192 | void TerrainEntity::loadVegetation(const std::string& vegetationFile) |
---|
193 | { |
---|
194 | PRINTF(4)("loadVegetation: %s\n", vegetationFile.c_str()); |
---|
195 | if (this->vegetation) |
---|
196 | ResourceManager::getInstance()->unload(this->vegetation, RP_LEVEL); |
---|
197 | if (!vegetationFile.empty()) |
---|
198 | { |
---|
199 | PRINTF(4)("fetching %s\n", vegetationFile.c_str()); |
---|
200 | this->vegetation = dynamic_cast<Model*>(ResourceManager::getInstance()->load(vegetationFile, OBJ, RP_CAMPAIGN)); |
---|
201 | } |
---|
202 | else |
---|
203 | this->vegetation = NULL; |
---|
204 | } |
---|
205 | |
---|
206 | |
---|
207 | |
---|
208 | void TerrainEntity::tick( float _dt ) { if ( terrain ) terrain->tick( _dt ); } |
---|
209 | |
---|
210 | void TerrainEntity::draw () const |
---|
211 | { |
---|
212 | glMatrixMode( GL_MODELVIEW ); |
---|
213 | glPushMatrix(); |
---|
214 | |
---|
215 | glTranslatef( this->getAbsCoor().x, |
---|
216 | this->getAbsCoor().y, |
---|
217 | this->getAbsCoor().z ); |
---|
218 | |
---|
219 | Vector cam = State::getCameraNode()->getAbsCoor(); |
---|
220 | |
---|
221 | if ( this->terrain ) { |
---|
222 | terrain->setCameraPosition( cam ); |
---|
223 | terrain->draw( ); |
---|
224 | } |
---|
225 | glPopMatrix(); |
---|
226 | |
---|
227 | |
---|
228 | } |
---|
229 | |
---|
230 | |
---|
231 | void TerrainEntity::buildDebugTerrainEntity(DebugTerrainEntity debugTerrainEntity) |
---|
232 | { |
---|
233 | terrain->build( ); |
---|
234 | /* |
---|
235 | // if the TerrainEntity is the TerrainEntity of Dave |
---|
236 | if (debugTerrainEntity == TERRAINENTITY_DAVE) |
---|
237 | { |
---|
238 | objectList = glGenLists(1); |
---|
239 | glNewList (objectList, GL_COMPILE); |
---|
240 | |
---|
241 | glColor3f(1.0,0,0); |
---|
242 | |
---|
243 | int sizeX = 100; |
---|
244 | int sizeZ = 80; |
---|
245 | float length = 1000; |
---|
246 | float width = 200; |
---|
247 | float widthX = float (length /sizeX); |
---|
248 | float widthZ = float (width /sizeZ); |
---|
249 | |
---|
250 | float height [sizeX][sizeZ]; |
---|
251 | Vector normal_vectors[sizeX][sizeZ]; |
---|
252 | |
---|
253 | |
---|
254 | for ( int i = 0; i<sizeX-1; i+=1) |
---|
255 | for (int j = 0; j<sizeZ-1;j+=1) |
---|
256 | //height[i][j] = rand()/20046 + (j-25)*(j-25)/30; |
---|
257 | #ifdef __WIN32__ |
---|
258 | height[i][j]=(sin((float)j/3)*rand()*i/182400)*.5; |
---|
259 | #else |
---|
260 | height[i][j]=(sin((float)j/3)*rand()*(long)i/6282450500.0)*.5; |
---|
261 | #endif |
---|
262 | |
---|
263 | //Die Huegel ein wenig glaetten |
---|
264 | for (int h=1; h<2;h++) |
---|
265 | for (int i=1;i<sizeX-2 ;i+=1 ) |
---|
266 | for(int j=1;j<sizeZ-2;j+=1) |
---|
267 | height[i][j]=(height[i+1][j]+height[i][j+1]+height[i-1][j]+height[i][j-1])/4; |
---|
268 | |
---|
269 | //Berechnung von normalen Vektoren |
---|
270 | for(int i=1;i<sizeX-2;i+=1) |
---|
271 | for(int j=1;j<sizeZ-2 ;j+=1) |
---|
272 | { |
---|
273 | Vector v1 = Vector (widthX*(1), height[i][j], widthZ*(j) ); |
---|
274 | Vector v2 = Vector (widthX*(i-1), height[i-1][j], widthZ*(j)); |
---|
275 | Vector v3 = Vector (widthX*(i), height[i][j+1], widthZ*(j+1)); |
---|
276 | Vector v4 = Vector (widthX*(i+1), height[i+1][j], widthZ*(j)); |
---|
277 | Vector v5 = Vector (widthX*(i), height[i][j-1], widthZ*(j-1)); |
---|
278 | |
---|
279 | Vector c1 = v2 - v1; |
---|
280 | Vector c2 = v3 - v1; |
---|
281 | Vector c3= v4 - v1; |
---|
282 | Vector c4 = v5 - v1; |
---|
283 | Vector zero = Vector (0,0,0); |
---|
284 | normal_vectors[i][j]=c1.cross(v3-v5)+c2.cross(v4-v2)+c3.cross(v5-v3)+c4.cross(v2-v4); |
---|
285 | normal_vectors[i][j].normalize(); |
---|
286 | } |
---|
287 | |
---|
288 | glBegin(GL_QUADS); |
---|
289 | int snowheight=3; |
---|
290 | for ( int i = 0; i<sizeX; i+=1) |
---|
291 | for (int j = 0; j<sizeZ;j+=1) |
---|
292 | { |
---|
293 | Vector v1 = Vector (widthX*(i), height[i][j]-20, widthZ*(j) -width/2); |
---|
294 | Vector v2 = Vector (widthX*(i+1), height[i+1][j]-20, widthZ*(j) -width/2); |
---|
295 | Vector v3 = Vector (widthX*(i+1), height[i+1][j+1]-20, widthZ*(j+1)-width/2); |
---|
296 | Vector v4 = Vector (widthX*(i), height[i][j+1]-20, widthZ*(j+1)-width/2); |
---|
297 | float a[3]; |
---|
298 | if(height[i][j]<snowheight) |
---|
299 | { |
---|
300 | a[0]=0; |
---|
301 | a[1]=1.0-height[i][j]/10-.3; |
---|
302 | a[2]=0; |
---|
303 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
304 | } |
---|
305 | else |
---|
306 | { |
---|
307 | a[0]=1.0; |
---|
308 | a[1]=1.0; |
---|
309 | a[2]=1.0; |
---|
310 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
311 | |
---|
312 | } |
---|
313 | glNormal3f(normal_vectors[i][j].x, normal_vectors[i][j].y, normal_vectors[i][j].z); |
---|
314 | glVertex3f(v1.x, v1.y, v1.z); |
---|
315 | if(height[i+1][j]<snowheight) |
---|
316 | { |
---|
317 | a[0]=0; |
---|
318 | a[1] =1.0-height[i+1][j]/10-.3; |
---|
319 | a[2]=0; |
---|
320 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
321 | } |
---|
322 | else |
---|
323 | { |
---|
324 | a[0]=1.0; |
---|
325 | a[1]=1.0; |
---|
326 | a[2]=1.0; |
---|
327 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
328 | |
---|
329 | } |
---|
330 | glNormal3f(normal_vectors[i+1][j].x, normal_vectors[i+1][j].y, normal_vectors[i+1][j].z); |
---|
331 | glVertex3f(v2.x, v2.y, v2.z); |
---|
332 | if(height[i+1][j+1]<snowheight) |
---|
333 | { |
---|
334 | a[0]=0; |
---|
335 | a[1] =1.0-height[i+1][j+1]/10-.3; |
---|
336 | a[2]=0; |
---|
337 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
338 | } |
---|
339 | else |
---|
340 | { |
---|
341 | a[0]=1.0; |
---|
342 | a[1]=1.0; |
---|
343 | a[2]=1.0; |
---|
344 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
345 | |
---|
346 | |
---|
347 | } |
---|
348 | glNormal3f(normal_vectors[i+1][j+1].x, normal_vectors[i+1][j+1].y, normal_vectors[i+1][j+1].z); |
---|
349 | glVertex3f(v3.x, v3.y, v3.z); |
---|
350 | if(height[i][j+1]<snowheight) |
---|
351 | { |
---|
352 | a[0]=0; |
---|
353 | a[1] =1.0-height[i+1][j+1]/10-.3; |
---|
354 | a[2]=0; |
---|
355 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
356 | } |
---|
357 | else |
---|
358 | { |
---|
359 | a[0]=1.0; |
---|
360 | a[1]=1.0; |
---|
361 | a[2]=1.0; |
---|
362 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
363 | } |
---|
364 | glNormal3f(normal_vectors[i][j+1].x, normal_vectors[i][j+1].y, normal_vectors[i][j+1].z); |
---|
365 | glVertex3f(v4.x, v4.y, v4.z); |
---|
366 | |
---|
367 | } |
---|
368 | glEnd(); |
---|
369 | glEndList(); |
---|
370 | } |
---|
371 | */ |
---|
372 | if (debugTerrainEntity == TERRAINENTITY_BENSCH) |
---|
373 | { |
---|
374 | /* |
---|
375 | this->model = (OBJModel*) new Model(); |
---|
376 | this->model->setName("CUBE"); |
---|
377 | this->model->addVertex (-0.5, -0.5, 0.5); |
---|
378 | this->model->addVertex (0.5, -0.5, 0.5); |
---|
379 | this->model->addVertex (-0.5, 0.5, 0.5); |
---|
380 | this->model->addVertex (0.5, 0.5, 0.5); |
---|
381 | this->model->addVertex (-0.5, 0.5, -0.5); |
---|
382 | this->model->addVertex (0.5, 0.5, -0.5); |
---|
383 | this->model->addVertex (-0.5, -0.5, -0.5); |
---|
384 | this->model->addVertex (0.5, -0.5, -0.5); |
---|
385 | |
---|
386 | this->model->addVertexTexture (0.0, 0.0); |
---|
387 | this->model->addVertexTexture (1.0, 0.0); |
---|
388 | this->model->addVertexTexture (0.0, 1.0); |
---|
389 | this->model->addVertexTexture (1.0, 1.0); |
---|
390 | this->model->addVertexTexture (0.0, 2.0); |
---|
391 | this->model->addVertexTexture (1.0, 2.0); |
---|
392 | this->model->addVertexTexture (0.0, 3.0); |
---|
393 | this->model->addVertexTexture (1.0, 3.0); |
---|
394 | this->model->addVertexTexture (0.0, 4.0); |
---|
395 | this->model->addVertexTexture (1.0, 4.0); |
---|
396 | this->model->addVertexTexture (2.0, 0.0); |
---|
397 | this->model->addVertexTexture (2.0, 1.0); |
---|
398 | this->model->addVertexTexture (-1.0, 0.0); |
---|
399 | this->model->addVertexTexture (-1.0, 1.0); |
---|
400 | |
---|
401 | this->model->finalize(); |
---|
402 | */ |
---|
403 | } |
---|
404 | } |
---|
405 | void TerrainEntity::getAltitude( Vector& _position, Vector& _normal ) |
---|
406 | { |
---|
407 | Vector altitude( _position.x-getAbsCoor().x, 0.0f, _position.z-getAbsCoor().z ), |
---|
408 | normal( 0.0f, 0.0f, 0.0f ); |
---|
409 | if ( terrain ) |
---|
410 | terrain->getAltitude( altitude, normal ); |
---|
411 | |
---|
412 | _position.y = altitude.y+getAbsCoor().y; |
---|
413 | _normal.z = normal.z; _normal.y = normal.y; _normal.z = normal.z; |
---|
414 | } |
---|
415 | |
---|
416 | float TerrainEntity::getHeight( float x, float z ) |
---|
417 | { |
---|
418 | Vector altitude( x-getAbsCoor().x, 0.0f, z-getAbsCoor().z ), |
---|
419 | normal( 0.0f, 0.0f, 0.0f ); |
---|
420 | if ( terrain ) |
---|
421 | terrain->getAltitude( altitude, normal ); |
---|
422 | |
---|
423 | return altitude.y+getAbsCoor().y; |
---|
424 | } |
---|