Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/height_map_merge/src/world_entities/terrain.cc @ 6937

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