Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/world_entities/terrain.cc @ 6995

Last change on this file since 6995 was 6956, checked in by bensch, 19 years ago

orxonox/trunk: merged the HeightMapBranche back to the trunk

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