Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6495 was 6495, checked in by bensch, 18 years ago

terrain updated, so it can load a new HeightMap from an XML file

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