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.h" |
---|
19 | |
---|
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 "height_map.h" |
---|
29 | #include "material.h" |
---|
30 | |
---|
31 | #include "glincl.h" |
---|
32 | |
---|
33 | #include "state.h" |
---|
34 | |
---|
35 | using namespace std; |
---|
36 | |
---|
37 | CREATE_FACTORY(Terrain, CL_TERRAIN); |
---|
38 | |
---|
39 | /** |
---|
40 | * standard constructor |
---|
41 | */ |
---|
42 | Terrain::Terrain (const TiXmlElement* root) |
---|
43 | { |
---|
44 | this->init(); |
---|
45 | |
---|
46 | |
---|
47 | if( root != NULL) |
---|
48 | this->loadParams(root); |
---|
49 | |
---|
50 | // if (this->model != NULL) |
---|
51 | //this->ssp = new SpatialSeparation((Model*)this->model, 10.0f); |
---|
52 | } |
---|
53 | |
---|
54 | |
---|
55 | /** |
---|
56 | * Constructor for loading a Terrain out of a file |
---|
57 | * @param fileName The file to load data from. |
---|
58 | |
---|
59 | this either loads out of an OBJ-file, or loads a heightmap if no .obj-extension is found. |
---|
60 | */ |
---|
61 | Terrain::Terrain(const char* fileName) |
---|
62 | { |
---|
63 | this->init(); |
---|
64 | |
---|
65 | if (!strstr(fileName, ".obj") || !strstr(fileName, ".OBJ") ) |
---|
66 | { |
---|
67 | this->loadModel(fileName); |
---|
68 | } |
---|
69 | else |
---|
70 | { |
---|
71 | // load the hightMap here. |
---|
72 | } |
---|
73 | } |
---|
74 | |
---|
75 | /** |
---|
76 | * a Constructor for the Debug-Worlds |
---|
77 | */ |
---|
78 | Terrain::Terrain(DebugTerrain debugTerrain) |
---|
79 | { |
---|
80 | this->init(); |
---|
81 | this->buildDebugTerrain(debugTerrain); |
---|
82 | } |
---|
83 | |
---|
84 | /** |
---|
85 | * standard deconstructor |
---|
86 | |
---|
87 | */ |
---|
88 | Terrain::~Terrain () |
---|
89 | { |
---|
90 | if (objectList) |
---|
91 | glDeleteLists(this->objectList, 1); |
---|
92 | if( this->ssp) |
---|
93 | delete ssp; |
---|
94 | if (this->vegetation) |
---|
95 | { |
---|
96 | ResourceManager::getInstance()->unload(this->vegetation); |
---|
97 | } |
---|
98 | |
---|
99 | if(this->heightMap) |
---|
100 | delete heightMap; |
---|
101 | } |
---|
102 | |
---|
103 | |
---|
104 | void Terrain::init() |
---|
105 | { |
---|
106 | this->setClassID(CL_TERRAIN, "Terrain"); |
---|
107 | this->toList(OM_ENVIRON_NOTICK); |
---|
108 | |
---|
109 | this->objectList = 0; |
---|
110 | this->ssp = NULL; |
---|
111 | this->vegetation = NULL; |
---|
112 | |
---|
113 | this->heightMap = NULL; |
---|
114 | |
---|
115 | this->heightMapMaterial = new Material(); |
---|
116 | } |
---|
117 | |
---|
118 | |
---|
119 | void Terrain::loadParams(const TiXmlElement* root) |
---|
120 | { |
---|
121 | WorldEntity::loadParams(root); |
---|
122 | |
---|
123 | LoadParam(root, "scale", this, Terrain, setScale) |
---|
124 | .describe("The scale in x,y,z direction"); |
---|
125 | |
---|
126 | LoadParam(root, "texture", this, Terrain, loadTexture) |
---|
127 | .describe("The name of the Texture for this heightMap"); |
---|
128 | |
---|
129 | LoadParam(root, "vegetation", this, Terrain, loadVegetation) |
---|
130 | .describe("the fileName of the vegetation, that should be loaded onto this terrain. (must be relative to the data-dir)") ; |
---|
131 | |
---|
132 | LoadParam(root, "height-map", this, Terrain, loadHeightMap) |
---|
133 | .describe("The HeightMap, splitted into two strings seperated by ','. 1: HeighMap, 2: ColorMap"); |
---|
134 | |
---|
135 | } |
---|
136 | |
---|
137 | void Terrain::setScale(float x, float y, float z) |
---|
138 | { |
---|
139 | this->terrainScale = Vector(x, y, z); |
---|
140 | } |
---|
141 | |
---|
142 | void Terrain::loadHeightMap(const char* heightMapFile, const char* colorMap) |
---|
143 | { |
---|
144 | if (this->heightMap != NULL) |
---|
145 | delete this->heightMap; |
---|
146 | this->heightMap = NULL; |
---|
147 | |
---|
148 | std::string hmName = ResourceManager::getFullName(heightMapFile); |
---|
149 | std::string hmColorName = ResourceManager::getFullName(colorMap); |
---|
150 | |
---|
151 | |
---|
152 | this->heightMap = new HeightMap(hmName, hmColorName); |
---|
153 | // heightMap->scale(Vector(43.0f,4.7f,43.0f)); |
---|
154 | heightMap->scale(this->terrainScale); |
---|
155 | heightMap->setAbsCoor(this->getAbsCoor()); |
---|
156 | heightMap->load(); |
---|
157 | } |
---|
158 | |
---|
159 | |
---|
160 | void Terrain::loadTexture(const char* textureName) |
---|
161 | { |
---|
162 | PRINTF(0)("Load texture: %s\n", textureName); |
---|
163 | |
---|
164 | heightMapMaterial->setDiffuse(1.0,1.0,1.0); |
---|
165 | heightMapMaterial->setAmbient(1.0,1.0,1.0 ); |
---|
166 | heightMapMaterial->setSpecular(1.0,1.0,1.0); |
---|
167 | heightMapMaterial->setShininess(.5); |
---|
168 | heightMapMaterial->setTransparency(1.0); |
---|
169 | |
---|
170 | heightMapMaterial->setDiffuseMap(textureName); |
---|
171 | heightMapMaterial->setAmbientMap(textureName); |
---|
172 | heightMapMaterial->setSpecularMap(textureName); |
---|
173 | } |
---|
174 | |
---|
175 | |
---|
176 | |
---|
177 | void Terrain::loadVegetation(const std::string& vegetationFile) |
---|
178 | { |
---|
179 | PRINTF(0)("loadVegetation: %s\n", vegetationFile.c_str()); |
---|
180 | if (this->vegetation) |
---|
181 | ResourceManager::getInstance()->unload(this->vegetation, RP_LEVEL); |
---|
182 | if (!vegetationFile.empty()) |
---|
183 | { |
---|
184 | PRINTF(4)("fetching %s\n", vegetationFile.c_str()); |
---|
185 | this->vegetation = dynamic_cast<Model*>(ResourceManager::getInstance()->load(vegetationFile, OBJ, RP_CAMPAIGN)); |
---|
186 | } |
---|
187 | else |
---|
188 | this->vegetation = NULL; |
---|
189 | } |
---|
190 | |
---|
191 | |
---|
192 | |
---|
193 | |
---|
194 | |
---|
195 | void Terrain::draw () const |
---|
196 | { |
---|
197 | glPushMatrix(); |
---|
198 | |
---|
199 | /* translate */ |
---|
200 | glTranslatef (this->getAbsCoor ().x, |
---|
201 | this->getAbsCoor ().y, |
---|
202 | this->getAbsCoor ().z); |
---|
203 | /* rotate */ |
---|
204 | // Vector tmpRot = this->getAbsDir().getSpacialAxis(); |
---|
205 | //glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z ); |
---|
206 | |
---|
207 | if (this->objectList) |
---|
208 | glCallList(this->objectList); |
---|
209 | else if (this->getModel()) |
---|
210 | this->getModel()->draw(); |
---|
211 | |
---|
212 | if (this->vegetation) |
---|
213 | this->vegetation->draw(); |
---|
214 | |
---|
215 | if( this->heightMap) |
---|
216 | { |
---|
217 | this->heightMapMaterial->select(); |
---|
218 | |
---|
219 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); |
---|
220 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); |
---|
221 | this->heightMap->draw(); |
---|
222 | } |
---|
223 | glPopMatrix(); |
---|
224 | |
---|
225 | |
---|
226 | /* |
---|
227 | glMatrixMode(GL_MODELVIEW); |
---|
228 | glPushMatrix(); |
---|
229 | glLoadIdentity(); |
---|
230 | Vector camera = State::getCameraNode()->getAbsCoor(); // Go on here ..........!!! |
---|
231 | |
---|
232 | float height = heightMap->getHeight(camera.x, camera.z); |
---|
233 | |
---|
234 | glEnable (GL_COLOR_MATERIAL) ; |
---|
235 | glBegin(GL_QUADS); // Draw The Cube Using quads |
---|
236 | glColor3f(0.0f,1.0f,0.0f); // Color Blue |
---|
237 | glVertex3f(camera.x + 63.0f,Terrain->getHeight(camera.x+63.0f, camera.z-10.0f)+13.0f,camera.z-10.0f); // Top Right Of The Quad (Top) |
---|
238 | glVertex3f(camera.x-63.0f, getHeight(camera.x+63.0f, camera.z-10.0f)+13.0f,camera.z-10.0f); // Top Left Of The Quad (Top) |
---|
239 | glVertex3f(camera.x-63.0f, getHeight(camera.x+63.0f, camera.z+10.0f)+13.0f, camera.z+10.0f); // Bottom Left Of The Quad (Top) |
---|
240 | glVertex3f(camera.x+ 63.0f, getHeight(camera.x+63.0f, camera.z+10.0f)+13.0f, camera.z+10.0f); // Bottom Right Of The Quad (Top) |
---|
241 | glEnd(); // End Drawing The Plan |
---|
242 | |
---|
243 | glPopMatrix();*/ |
---|
244 | |
---|
245 | |
---|
246 | /* THIS IS ONLY FOR DEBUGGING INFORMATION */ |
---|
247 | if (this->ssp != NULL) |
---|
248 | this->ssp->drawQuadtree(); |
---|
249 | } |
---|
250 | |
---|
251 | |
---|
252 | void Terrain::buildDebugTerrain(DebugTerrain debugTerrain) |
---|
253 | { |
---|
254 | // if the terrain is the Terrain of Dave |
---|
255 | if (debugTerrain == TERRAIN_DAVE) |
---|
256 | { |
---|
257 | objectList = glGenLists(1); |
---|
258 | glNewList (objectList, GL_COMPILE); |
---|
259 | |
---|
260 | glColor3f(1.0,0,0); |
---|
261 | |
---|
262 | int sizeX = 100; |
---|
263 | int sizeZ = 80; |
---|
264 | float length = 1000; |
---|
265 | float width = 200; |
---|
266 | float widthX = float (length /sizeX); |
---|
267 | float widthZ = float (width /sizeZ); |
---|
268 | |
---|
269 | float height [sizeX][sizeZ]; |
---|
270 | Vector normal_vectors[sizeX][sizeZ]; |
---|
271 | |
---|
272 | |
---|
273 | for ( int i = 0; i<sizeX-1; i+=1) |
---|
274 | for (int j = 0; j<sizeZ-1;j+=1) |
---|
275 | //height[i][j] = rand()/20046 + (j-25)*(j-25)/30; |
---|
276 | #ifdef __WIN32__ |
---|
277 | height[i][j]=(sin((float)j/3)*rand()*i/182400)*.5; |
---|
278 | #else |
---|
279 | height[i][j]=(sin((float)j/3)*rand()*(long)i/6282450500.0)*.5; |
---|
280 | #endif |
---|
281 | |
---|
282 | //Die Huegel ein wenig glaetten |
---|
283 | for (int h=1; h<2;h++) |
---|
284 | for (int i=1;i<sizeX-2 ;i+=1 ) |
---|
285 | for(int j=1;j<sizeZ-2;j+=1) |
---|
286 | height[i][j]=(height[i+1][j]+height[i][j+1]+height[i-1][j]+height[i][j-1])/4; |
---|
287 | |
---|
288 | //Berechnung von normalen Vektoren |
---|
289 | for(int i=1;i<sizeX-2;i+=1) |
---|
290 | for(int j=1;j<sizeZ-2 ;j+=1) |
---|
291 | { |
---|
292 | Vector v1 = Vector (widthX*(1), height[i][j], widthZ*(j) ); |
---|
293 | Vector v2 = Vector (widthX*(i-1), height[i-1][j], widthZ*(j)); |
---|
294 | Vector v3 = Vector (widthX*(i), height[i][j+1], widthZ*(j+1)); |
---|
295 | Vector v4 = Vector (widthX*(i+1), height[i+1][j], widthZ*(j)); |
---|
296 | Vector v5 = Vector (widthX*(i), height[i][j-1], widthZ*(j-1)); |
---|
297 | |
---|
298 | Vector c1 = v2 - v1; |
---|
299 | Vector c2 = v3 - v1; |
---|
300 | Vector c3= v4 - v1; |
---|
301 | Vector c4 = v5 - v1; |
---|
302 | Vector zero = Vector (0,0,0); |
---|
303 | normal_vectors[i][j]=c1.cross(v3-v5)+c2.cross(v4-v2)+c3.cross(v5-v3)+c4.cross(v2-v4); |
---|
304 | normal_vectors[i][j].normalize(); |
---|
305 | } |
---|
306 | |
---|
307 | glBegin(GL_QUADS); |
---|
308 | int snowheight=3; |
---|
309 | for ( int i = 0; i<sizeX; i+=1) |
---|
310 | for (int j = 0; j<sizeZ;j+=1) |
---|
311 | { |
---|
312 | Vector v1 = Vector (widthX*(i), height[i][j]-20, widthZ*(j) -width/2); |
---|
313 | Vector v2 = Vector (widthX*(i+1), height[i+1][j]-20, widthZ*(j) -width/2); |
---|
314 | Vector v3 = Vector (widthX*(i+1), height[i+1][j+1]-20, widthZ*(j+1)-width/2); |
---|
315 | Vector v4 = Vector (widthX*(i), height[i][j+1]-20, widthZ*(j+1)-width/2); |
---|
316 | float a[3]; |
---|
317 | if(height[i][j]<snowheight){ |
---|
318 | a[0]=0; |
---|
319 | a[1]=1.0-height[i][j]/10-.3; |
---|
320 | a[2]=0; |
---|
321 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
322 | } |
---|
323 | else{ |
---|
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][j].x, normal_vectors[i][j].y, normal_vectors[i][j].z); |
---|
331 | glVertex3f(v1.x, v1.y, v1.z); |
---|
332 | if(height[i+1][j]<snowheight){ |
---|
333 | a[0]=0; |
---|
334 | a[1] =1.0-height[i+1][j]/10-.3; |
---|
335 | a[2]=0; |
---|
336 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
337 | } |
---|
338 | else{ |
---|
339 | a[0]=1.0; |
---|
340 | a[1]=1.0; |
---|
341 | a[2]=1.0; |
---|
342 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
343 | |
---|
344 | } |
---|
345 | glNormal3f(normal_vectors[i+1][j].x, normal_vectors[i+1][j].y, normal_vectors[i+1][j].z); |
---|
346 | glVertex3f(v2.x, v2.y, v2.z); |
---|
347 | if(height[i+1][j+1]<snowheight){ |
---|
348 | a[0]=0; |
---|
349 | a[1] =1.0-height[i+1][j+1]/10-.3; |
---|
350 | a[2]=0; |
---|
351 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
352 | } |
---|
353 | else{ |
---|
354 | a[0]=1.0; |
---|
355 | a[1]=1.0; |
---|
356 | a[2]=1.0; |
---|
357 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
358 | |
---|
359 | |
---|
360 | } |
---|
361 | glNormal3f(normal_vectors[i+1][j+1].x, normal_vectors[i+1][j+1].y, normal_vectors[i+1][j+1].z); |
---|
362 | glVertex3f(v3.x, v3.y, v3.z); |
---|
363 | if(height[i][j+1]<snowheight){ |
---|
364 | a[0]=0; |
---|
365 | a[1] =1.0-height[i+1][j+1]/10-.3; |
---|
366 | a[2]=0; |
---|
367 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
368 | } |
---|
369 | else{ |
---|
370 | a[0]=1.0; |
---|
371 | a[1]=1.0; |
---|
372 | a[2]=1.0; |
---|
373 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
374 | } |
---|
375 | glNormal3f(normal_vectors[i][j+1].x, normal_vectors[i][j+1].y, normal_vectors[i][j+1].z); |
---|
376 | glVertex3f(v4.x, v4.y, v4.z); |
---|
377 | |
---|
378 | } |
---|
379 | glEnd(); |
---|
380 | glEndList(); |
---|
381 | } |
---|
382 | |
---|
383 | if (debugTerrain == TERRAIN_BENSCH) |
---|
384 | { |
---|
385 | /* |
---|
386 | this->model = (OBJModel*) new Model(); |
---|
387 | this->model->setName("CUBE"); |
---|
388 | this->model->addVertex (-0.5, -0.5, 0.5); |
---|
389 | this->model->addVertex (0.5, -0.5, 0.5); |
---|
390 | this->model->addVertex (-0.5, 0.5, 0.5); |
---|
391 | this->model->addVertex (0.5, 0.5, 0.5); |
---|
392 | this->model->addVertex (-0.5, 0.5, -0.5); |
---|
393 | this->model->addVertex (0.5, 0.5, -0.5); |
---|
394 | this->model->addVertex (-0.5, -0.5, -0.5); |
---|
395 | this->model->addVertex (0.5, -0.5, -0.5); |
---|
396 | |
---|
397 | this->model->addVertexTexture (0.0, 0.0); |
---|
398 | this->model->addVertexTexture (1.0, 0.0); |
---|
399 | this->model->addVertexTexture (0.0, 1.0); |
---|
400 | this->model->addVertexTexture (1.0, 1.0); |
---|
401 | this->model->addVertexTexture (0.0, 2.0); |
---|
402 | this->model->addVertexTexture (1.0, 2.0); |
---|
403 | this->model->addVertexTexture (0.0, 3.0); |
---|
404 | this->model->addVertexTexture (1.0, 3.0); |
---|
405 | this->model->addVertexTexture (0.0, 4.0); |
---|
406 | this->model->addVertexTexture (1.0, 4.0); |
---|
407 | this->model->addVertexTexture (2.0, 0.0); |
---|
408 | this->model->addVertexTexture (2.0, 1.0); |
---|
409 | this->model->addVertexTexture (-1.0, 0.0); |
---|
410 | this->model->addVertexTexture (-1.0, 1.0); |
---|
411 | |
---|
412 | this->model->finalize(); |
---|
413 | */ |
---|
414 | } |
---|
415 | } |
---|
416 | |
---|
417 | int Terrain::writeBytes( const byte * data, int length, int sender ) |
---|
418 | { |
---|
419 | setRequestedSync( false ); |
---|
420 | setIsOutOfSync( false ); |
---|
421 | |
---|
422 | SYNCHELP_READ_BEGIN(); |
---|
423 | SYNCHELP_READ_FKT( WorldEntity::writeState, NWT_TER_WE_STATE ); |
---|
424 | |
---|
425 | return SYNCHELP_READ_N; |
---|
426 | } |
---|
427 | |
---|
428 | int Terrain::readBytes( byte * data, int maxLength, int * reciever ) |
---|
429 | { |
---|
430 | if ( isOutOfSync() && !requestedSync() && this->getHostID()!=this->getOwner() ) |
---|
431 | { |
---|
432 | (NetworkGameManager::getInstance())->sync( this->getUniqueID(), this->getOwner() ); |
---|
433 | setRequestedSync( true ); |
---|
434 | } |
---|
435 | |
---|
436 | int rec = this->getRequestSync(); |
---|
437 | if ( rec > 0 ) |
---|
438 | { |
---|
439 | *reciever = rec; |
---|
440 | |
---|
441 | SYNCHELP_WRITE_BEGIN(); |
---|
442 | SYNCHELP_WRITE_FKT( WorldEntity::readState, NWT_TER_WE_STATE ); |
---|
443 | return SYNCHELP_WRITE_N; |
---|
444 | |
---|
445 | } |
---|
446 | |
---|
447 | *reciever = 0; |
---|
448 | return 0; |
---|
449 | } |
---|
450 | |
---|
451 | void Terrain::writeDebug( ) const |
---|
452 | { |
---|
453 | } |
---|
454 | |
---|
455 | void Terrain::readDebug( ) const |
---|
456 | { |
---|
457 | } |
---|
458 | |
---|
459 | float Terrain::getHeight(float x, float y) |
---|
460 | { |
---|
461 | if(this->heightMap != NULL) |
---|
462 | return (this->heightMap->getHeight(x, y)); |
---|
463 | return 0; |
---|
464 | } |
---|