Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/world_entities/terrain.cc @ 8680

Last change on this file since 8680 was 8316, checked in by bensch, 18 years ago

trunk: fixed most -Wall warnings… but there are still many missing :/

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