Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/osX/src/world_entities/terrain.cc @ 7341

Last change on this file since 7341 was 6956, checked in by bensch, 19 years ago

orxonox/trunk: merged the HeightMapBranche back to the trunk

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