Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/new_class_id/src/world_entities/terrain.cc @ 9716

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

more renamings

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