Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10770 was 10618, checked in by bknecht, 18 years ago

merged cleanup into trunk (only improvements)

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