Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6579 was 6579, checked in by bottac, 18 years ago

HeightMap may now be loaded from a .oxw .

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