Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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