Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/new_class_id/src/world_entities/terrain.cc @ 9768

Last change on this file since 9768 was 9727, checked in by bensch, 18 years ago

orxonox/new_class_id: new Executor construct, that is much more typesafe, faster, and easier to extend…

Also changed the LoadParam process, and adapted ScriptEngine calls

Then at the end, some missing headers appeared, and appended them to all the cc-files again.

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