Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/bsp_model/src/world_entities/terrain.cc @ 7352

Last change on this file since 7352 was 7352, checked in by bottac, 18 years ago

Branch bsp_model created.

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