Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6730 was 6730, checked in by bottac, 18 years ago
File size: 13.3 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
172
173   this->heightMapMaterial = new Material(); 
174   heightMapMaterial->setTransparency(1.0);
175   heightMapMaterial->setIllum(0.2);
176
177   heightMapMaterial->setDiffuse(1.0,1.0,1.0);
178   heightMapMaterial->setAmbient(1.0,1.0,1.0 );
179   heightMapMaterial->setSpecular(1.0,1.0,1.0);
180   heightMapMaterial->setShininess(.5);
181   heightMapMaterial->setTransparency(1.0);
182
183   heightMapMaterial->diffuseTexture = NULL;
184   heightMapMaterial->ambientTexture = NULL;
185   heightMapMaterial->specularTexture = NULL;
186 
187   
188   heightMapMaterial->setDiffuseMap(textureName);
189   heightMapMaterial->setAmbientMap(textureName);
190   heightMapMaterial->setSpecularMap(textureName);     
191 
192
193
194}
195
196
197
198void Terrain::loadVegetation(const char* vegetationFile)
199{
200  PRINTF(0)("loadVegetation: %s\n", vegetationFile);
201  if (this->vegetation)
202    ResourceManager::getInstance()->unload(this->vegetation, RP_LEVEL);
203  if (vegetationFile != NULL)
204  {
205    PRINTF(4)("fetching %s\n", vegetationFile);
206    this->vegetation = (Model*)ResourceManager::getInstance()->load(vegetationFile, OBJ, RP_CAMPAIGN);
207  }
208  else
209    this->vegetation = NULL;
210}
211
212
213
214
215
216void Terrain::draw () const
217{
218 
219   
220 
221  glMatrixMode(GL_MODELVIEW);
222  glPushMatrix();
223
224  /* translate */
225  glTranslatef (this->getAbsCoor ().x,
226                this->getAbsCoor ().y,
227                this->getAbsCoor ().z);
228  /* rotate */
229 // Vector tmpRot = this->getAbsDir().getSpacialAxis();
230  //glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
231
232  if (this->objectList)
233    glCallList(this->objectList);
234  else if (this->getModel())
235    this->getModel()->draw();
236  if (this->vegetation)
237    this->vegetation->draw();
238
239  if(this->heightMap)
240        {
241        this->heightMapMaterial->select();
242        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
243        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
244        glEnable(GL_LIGHTING);
245        glColorMaterial ( GL_FRONT_AND_BACK, GL_EMISSION ) ;
246        glEnable (GL_COLOR_MATERIAL) ;
247        this->heightMap->draw();
248        }
249  glPopMatrix();
250
251 
252 
253 
254 
255 glMatrixMode(GL_MODELVIEW);
256glPushMatrix(); 
257glLoadIdentity();
258 Vector camera =   State::getCamera()->getAbsCoor(); // Go on here ..........!!!
259    float height =    heightMap->getHeight(camera.x, camera.z);
260   
261  glEnable (GL_COLOR_MATERIAL) ;
262    glBegin(GL_QUADS);            // Draw The Cube Using quads
263    glColor3f(0.0f,1.0f,0.0f);  // Color Blue
264    glVertex3f(camera.x + 13.0f,height+13.0f,camera.z-13.0f);      // Top Right Of The Quad (Top)
265    glVertex3f(camera.x-13.0f, height+13.0f,camera.z-13.0f);      // Top Left Of The Quad (Top)
266    glVertex3f(camera.x-13.0f, height+13.0f, camera.z+13.0f);      // Bottom Left Of The Quad (Top)
267    glVertex3f(camera.x+ 13.0f, height+13.0f, camera.z+13.0f);      // Bottom Right Of The Quad (Top)
268   glEnd();                      // End Drawing The Plan
269   
270   
271glPopMatrix();
272  /* THIS IS ONLY FOR DEBUGGING INFORMATION */
273  if (this->ssp != NULL)
274    this->ssp->drawQuadtree();
275}
276
277
278void Terrain::buildDebugTerrain(DebugTerrain debugTerrain)
279{
280  // if the terrain is the Terrain of Dave
281  if (debugTerrain == TERRAIN_DAVE)
282    {
283      objectList = glGenLists(1);
284      glNewList (objectList, GL_COMPILE);
285
286      glColor3f(1.0,0,0);
287
288      int sizeX = 100;
289      int sizeZ = 80;
290      float length = 1000;
291      float width = 200;
292      float widthX = float (length /sizeX);
293      float widthZ = float (width /sizeZ);
294
295      float height [sizeX][sizeZ];
296      Vector normal_vectors[sizeX][sizeZ];
297
298
299      for ( int i = 0; i<sizeX-1; i+=1)
300        for (int j = 0; j<sizeZ-1;j+=1)
301          //height[i][j] = rand()/20046 + (j-25)*(j-25)/30;
302#ifdef __WIN32__
303          height[i][j]=(sin((float)j/3)*rand()*i/182400)*.5;
304#else
305      height[i][j]=(sin((float)j/3)*rand()*(long)i/6282450500.0)*.5;
306#endif
307
308      //Die Huegel ein wenig glaetten
309      for (int h=1; h<2;h++)
310        for (int i=1;i<sizeX-2 ;i+=1 )
311          for(int j=1;j<sizeZ-2;j+=1)
312            height[i][j]=(height[i+1][j]+height[i][j+1]+height[i-1][j]+height[i][j-1])/4;
313
314      //Berechnung von normalen Vektoren
315      for(int i=1;i<sizeX-2;i+=1)
316        for(int j=1;j<sizeZ-2 ;j+=1)
317          {
318            Vector v1 = Vector (widthX*(1),      height[i][j],      widthZ*(j) );
319            Vector v2 = Vector (widthX*(i-1),    height[i-1][j],    widthZ*(j));
320            Vector v3 = Vector (widthX*(i),      height[i][j+1],    widthZ*(j+1));
321            Vector v4 = Vector (widthX*(i+1),    height[i+1][j],    widthZ*(j));
322            Vector v5 = Vector (widthX*(i),      height[i][j-1],    widthZ*(j-1));
323
324            Vector c1 = v2 - v1;
325            Vector c2 = v3 - v1;
326            Vector c3=  v4 - v1;
327            Vector c4 = v5 - v1;
328            Vector zero = Vector (0,0,0);
329            normal_vectors[i][j]=c1.cross(v3-v5)+c2.cross(v4-v2)+c3.cross(v5-v3)+c4.cross(v2-v4);
330            normal_vectors[i][j].normalize();
331          }
332
333      glBegin(GL_QUADS);
334      int snowheight=3;
335      for ( int i = 0; i<sizeX; i+=1)
336        for (int j = 0; j<sizeZ;j+=1)
337          {
338            Vector v1 = Vector (widthX*(i),      height[i][j]-20,       widthZ*(j)  -width/2);
339            Vector v2 = Vector (widthX*(i+1),    height[i+1][j]-20,     widthZ*(j)  -width/2);
340            Vector v3 = Vector (widthX*(i+1),    height[i+1][j+1]-20,   widthZ*(j+1)-width/2);
341            Vector v4 = Vector (widthX*(i),      height[i][j+1]-20,     widthZ*(j+1)-width/2);
342            float a[3];
343            if(height[i][j]<snowheight){
344              a[0]=0;
345              a[1]=1.0-height[i][j]/10-.3;
346              a[2]=0;
347              glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
348            }
349            else{
350              a[0]=1.0;
351              a[1]=1.0;
352              a[2]=1.0;
353              glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
354
355            }
356            glNormal3f(normal_vectors[i][j].x, normal_vectors[i][j].y, normal_vectors[i][j].z);
357            glVertex3f(v1.x, v1.y, v1.z);
358            if(height[i+1][j]<snowheight){
359              a[0]=0;
360              a[1] =1.0-height[i+1][j]/10-.3;
361              a[2]=0;
362              glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
363            }
364            else{
365              a[0]=1.0;
366              a[1]=1.0;
367              a[2]=1.0;
368              glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
369
370            }
371            glNormal3f(normal_vectors[i+1][j].x, normal_vectors[i+1][j].y, normal_vectors[i+1][j].z);
372            glVertex3f(v2.x, v2.y, v2.z);
373            if(height[i+1][j+1]<snowheight){
374              a[0]=0;
375              a[1] =1.0-height[i+1][j+1]/10-.3;
376              a[2]=0;
377              glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
378            }
379            else{
380              a[0]=1.0;
381              a[1]=1.0;
382              a[2]=1.0;
383              glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
384
385
386            }
387            glNormal3f(normal_vectors[i+1][j+1].x, normal_vectors[i+1][j+1].y, normal_vectors[i+1][j+1].z);
388            glVertex3f(v3.x, v3.y, v3.z);
389            if(height[i][j+1]<snowheight){
390              a[0]=0;
391              a[1] =1.0-height[i+1][j+1]/10-.3;
392              a[2]=0;
393              glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
394            }
395            else{
396              a[0]=1.0;
397              a[1]=1.0;
398              a[2]=1.0;
399              glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
400            }
401            glNormal3f(normal_vectors[i][j+1].x, normal_vectors[i][j+1].y, normal_vectors[i][j+1].z);
402            glVertex3f(v4.x, v4.y, v4.z);
403
404          }
405      glEnd();
406      glEndList();
407    }
408
409  if (debugTerrain == TERRAIN_BENSCH)
410    {
411      /*
412        this->model = (OBJModel*) new Model();
413      this->model->setName("CUBE");
414      this->model->addVertex (-0.5, -0.5, 0.5);
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
423      this->model->addVertexTexture (0.0, 0.0);
424      this->model->addVertexTexture (1.0, 0.0);
425      this->model->addVertexTexture (0.0, 1.0);
426      this->model->addVertexTexture (1.0, 1.0);
427      this->model->addVertexTexture (0.0, 2.0);
428      this->model->addVertexTexture (1.0, 2.0);
429      this->model->addVertexTexture (0.0, 3.0);
430      this->model->addVertexTexture (1.0, 3.0);
431      this->model->addVertexTexture (0.0, 4.0);
432      this->model->addVertexTexture (1.0, 4.0);
433      this->model->addVertexTexture (2.0, 0.0);
434      this->model->addVertexTexture (2.0, 1.0);
435      this->model->addVertexTexture (-1.0, 0.0);
436      this->model->addVertexTexture (-1.0, 1.0);
437
438      this->model->finalize();
439      */
440    }
441}
442
443int Terrain::writeBytes( const byte * data, int length, int sender )
444{
445  setRequestedSync( false );
446  setIsOutOfSync( false );
447
448  SYNCHELP_READ_BEGIN();
449  SYNCHELP_READ_FKT( WorldEntity::writeState );
450
451  return SYNCHELP_READ_N;
452}
453
454int Terrain::readBytes( byte * data, int maxLength, int * reciever )
455{
456  if ( isOutOfSync() && !requestedSync() && this->getHostID()!=this->getOwner() )
457  {
458    (NetworkGameManager::getInstance())->sync( this->getUniqueID(), this->getOwner() );
459    setRequestedSync( true );
460  }
461
462  int rec = this->getRequestSync();
463  if ( rec > 0 )
464  {
465    *reciever = rec;
466
467    return WorldEntity::readState( data, maxLength );
468
469  }
470
471  *reciever = 0;
472  return 0;
473}
474
475void Terrain::writeDebug( ) const
476{
477}
478
479void Terrain::readDebug( ) const
480{
481}
482
483float Terrain::getHeight(float x, float y)
484{
485        if(this->heightMap != NULL)
486                return (this->heightMap->getHeight(x, y));
487        return 0;
488}
Note: See TracBrowser for help on using the repository browser.