Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10556 was 10114, checked in by patrick, 18 years ago

merged network back to trunk

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