Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

terrain loads hm from ResourceManager::gertFullName

File size: 10.6 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  char* hmName = ResourceManager::getFullName(heightMapFile);
133  char* hmColorName = ResourceManager::getFullName(colorMap);
134
135
136  this->heightMap = new HeightMap(hmName, hmColorName);
137  delete[] hmName;
138  delete[] hmColorName;
139
140}
141
142
143void Terrain::loadTexture(const char* textureName)
144{
145  if (this->heightMapMaterial != NULL)
146    delete this->heightMapMaterial;
147}
148
149
150
151void Terrain::loadVegetation(const char* vegetationFile)
152{
153  PRINTF(0)("loadVegetation: %s\n", vegetationFile);
154  if (this->vegetation)
155    ResourceManager::getInstance()->unload(this->vegetation, RP_LEVEL);
156  if (vegetationFile != NULL)
157  {
158    PRINTF(4)("fetching %s\n", vegetationFile);
159    this->vegetation = (Model*)ResourceManager::getInstance()->load(vegetationFile, OBJ, RP_CAMPAIGN);
160  }
161  else
162    this->vegetation = NULL;
163}
164
165
166
167
168
169void Terrain::draw () const
170{
171  glMatrixMode(GL_MODELVIEW);
172  glPushMatrix();
173
174  /* translate */
175  glTranslatef (this->getAbsCoor ().x,
176                this->getAbsCoor ().y,
177                this->getAbsCoor ().z);
178  /* rotate */
179  Vector tmpRot = this->getAbsDir().getSpacialAxis();
180  glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
181
182  if (this->objectList)
183    glCallList(this->objectList);
184  else if (this->getModel())
185    this->getModel()->draw();
186  if (this->vegetation)
187    this->vegetation->draw();
188
189  if(this->heightMap != NULL)
190    this->heightMap->draw();
191  glPopMatrix();
192
193
194
195  /* THIS IS ONLY FOR DEBUGGING INFORMATION */
196  if (this->ssp != NULL)
197    this->ssp->drawQuadtree();
198}
199
200
201void Terrain::buildDebugTerrain(DebugTerrain debugTerrain)
202{
203  // if the terrain is the Terrain of Dave
204  if (debugTerrain == TERRAIN_DAVE)
205    {
206      objectList = glGenLists(1);
207      glNewList (objectList, GL_COMPILE);
208
209      glColor3f(1.0,0,0);
210
211      int sizeX = 100;
212      int sizeZ = 80;
213      float length = 1000;
214      float width = 200;
215      float widthX = float (length /sizeX);
216      float widthZ = float (width /sizeZ);
217
218      float height [sizeX][sizeZ];
219      Vector normal_vectors[sizeX][sizeZ];
220
221
222      for ( int i = 0; i<sizeX-1; i+=1)
223        for (int j = 0; j<sizeZ-1;j+=1)
224          //height[i][j] = rand()/20046 + (j-25)*(j-25)/30;
225#ifdef __WIN32__
226          height[i][j]=(sin((float)j/3)*rand()*i/182400)*.5;
227#else
228      height[i][j]=(sin((float)j/3)*rand()*(long)i/6282450500.0)*.5;
229#endif
230
231      //Die Huegel ein wenig glaetten
232      for (int h=1; h<2;h++)
233        for (int i=1;i<sizeX-2 ;i+=1 )
234          for(int j=1;j<sizeZ-2;j+=1)
235            height[i][j]=(height[i+1][j]+height[i][j+1]+height[i-1][j]+height[i][j-1])/4;
236
237      //Berechnung von normalen Vektoren
238      for(int i=1;i<sizeX-2;i+=1)
239        for(int j=1;j<sizeZ-2 ;j+=1)
240          {
241            Vector v1 = Vector (widthX*(1),      height[i][j],      widthZ*(j) );
242            Vector v2 = Vector (widthX*(i-1),    height[i-1][j],    widthZ*(j));
243            Vector v3 = Vector (widthX*(i),      height[i][j+1],    widthZ*(j+1));
244            Vector v4 = Vector (widthX*(i+1),    height[i+1][j],    widthZ*(j));
245            Vector v5 = Vector (widthX*(i),      height[i][j-1],    widthZ*(j-1));
246
247            Vector c1 = v2 - v1;
248            Vector c2 = v3 - v1;
249            Vector c3=  v4 - v1;
250            Vector c4 = v5 - v1;
251            Vector zero = Vector (0,0,0);
252            normal_vectors[i][j]=c1.cross(v3-v5)+c2.cross(v4-v2)+c3.cross(v5-v3)+c4.cross(v2-v4);
253            normal_vectors[i][j].normalize();
254          }
255
256      glBegin(GL_QUADS);
257      int snowheight=3;
258      for ( int i = 0; i<sizeX; i+=1)
259        for (int j = 0; j<sizeZ;j+=1)
260          {
261            Vector v1 = Vector (widthX*(i),      height[i][j]-20,       widthZ*(j)  -width/2);
262            Vector v2 = Vector (widthX*(i+1),    height[i+1][j]-20,     widthZ*(j)  -width/2);
263            Vector v3 = Vector (widthX*(i+1),    height[i+1][j+1]-20,   widthZ*(j+1)-width/2);
264            Vector v4 = Vector (widthX*(i),      height[i][j+1]-20,     widthZ*(j+1)-width/2);
265            float a[3];
266            if(height[i][j]<snowheight){
267              a[0]=0;
268              a[1]=1.0-height[i][j]/10-.3;
269              a[2]=0;
270              glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
271            }
272            else{
273              a[0]=1.0;
274              a[1]=1.0;
275              a[2]=1.0;
276              glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
277
278            }
279            glNormal3f(normal_vectors[i][j].x, normal_vectors[i][j].y, normal_vectors[i][j].z);
280            glVertex3f(v1.x, v1.y, v1.z);
281            if(height[i+1][j]<snowheight){
282              a[0]=0;
283              a[1] =1.0-height[i+1][j]/10-.3;
284              a[2]=0;
285              glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
286            }
287            else{
288              a[0]=1.0;
289              a[1]=1.0;
290              a[2]=1.0;
291              glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
292
293            }
294            glNormal3f(normal_vectors[i+1][j].x, normal_vectors[i+1][j].y, normal_vectors[i+1][j].z);
295            glVertex3f(v2.x, v2.y, v2.z);
296            if(height[i+1][j+1]<snowheight){
297              a[0]=0;
298              a[1] =1.0-height[i+1][j+1]/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            }
310            glNormal3f(normal_vectors[i+1][j+1].x, normal_vectors[i+1][j+1].y, normal_vectors[i+1][j+1].z);
311            glVertex3f(v3.x, v3.y, v3.z);
312            if(height[i][j+1]<snowheight){
313              a[0]=0;
314              a[1] =1.0-height[i+1][j+1]/10-.3;
315              a[2]=0;
316              glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
317            }
318            else{
319              a[0]=1.0;
320              a[1]=1.0;
321              a[2]=1.0;
322              glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
323            }
324            glNormal3f(normal_vectors[i][j+1].x, normal_vectors[i][j+1].y, normal_vectors[i][j+1].z);
325            glVertex3f(v4.x, v4.y, v4.z);
326
327          }
328      glEnd();
329      glEndList();
330    }
331
332  if (debugTerrain == TERRAIN_BENSCH)
333    {
334      /*
335        this->model = (OBJModel*) new Model();
336      this->model->setName("CUBE");
337      this->model->addVertex (-0.5, -0.5, 0.5);
338      this->model->addVertex (0.5, -0.5, 0.5);
339      this->model->addVertex (-0.5, 0.5, 0.5);
340      this->model->addVertex (0.5, 0.5, 0.5);
341      this->model->addVertex (-0.5, 0.5, -0.5);
342      this->model->addVertex (0.5, 0.5, -0.5);
343      this->model->addVertex (-0.5, -0.5, -0.5);
344      this->model->addVertex (0.5, -0.5, -0.5);
345
346      this->model->addVertexTexture (0.0, 0.0);
347      this->model->addVertexTexture (1.0, 0.0);
348      this->model->addVertexTexture (0.0, 1.0);
349      this->model->addVertexTexture (1.0, 1.0);
350      this->model->addVertexTexture (0.0, 2.0);
351      this->model->addVertexTexture (1.0, 2.0);
352      this->model->addVertexTexture (0.0, 3.0);
353      this->model->addVertexTexture (1.0, 3.0);
354      this->model->addVertexTexture (0.0, 4.0);
355      this->model->addVertexTexture (1.0, 4.0);
356      this->model->addVertexTexture (2.0, 0.0);
357      this->model->addVertexTexture (2.0, 1.0);
358      this->model->addVertexTexture (-1.0, 0.0);
359      this->model->addVertexTexture (-1.0, 1.0);
360
361      this->model->finalize();
362      */
363    }
364}
365
366int Terrain::writeBytes( const byte * data, int length, int sender )
367{
368  setRequestedSync( false );
369  setIsOutOfSync( false );
370
371  SYNCHELP_READ_BEGIN();
372  SYNCHELP_READ_FKT( WorldEntity::writeState );
373
374  return SYNCHELP_READ_N;
375}
376
377int Terrain::readBytes( byte * data, int maxLength, int * reciever )
378{
379  if ( isOutOfSync() && !requestedSync() && this->getHostID()!=this->getOwner() )
380  {
381    (NetworkGameManager::getInstance())->sync( this->getUniqueID(), this->getOwner() );
382    setRequestedSync( true );
383  }
384
385  int rec = this->getRequestSync();
386  if ( rec > 0 )
387  {
388    *reciever = rec;
389
390    return WorldEntity::readState( data, maxLength );
391
392  }
393
394  *reciever = 0;
395  return 0;
396}
397
398void Terrain::writeDebug( ) const
399{
400}
401
402void Terrain::readDebug( ) const
403{
404}
Note: See TracBrowser for help on using the repository browser.