Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6703 was 6695, checked in by patrick, 19 years ago

merged the network branche to the trunk

File size: 9.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
[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
[6341]28
[5511]29#include "glincl.h"
30
[1856]31using namespace std;
[1853]32
[5750]33CREATE_FACTORY(Terrain, CL_TERRAIN);
[1856]34
[3245]35/**
[4836]36 *  standard constructor
[5357]37 */
[4607]38Terrain::Terrain (const TiXmlElement* root)
[3365]39{
[3564]40  this->init();
[6695]41  if( root != NULL)
42    this->loadParams(root);
[4844]43
[5308]44//  if (this->model != NULL)
[6022]45    //this->ssp = new SpatialSeparation((Model*)this->model, 10.0f);
[3365]46}
[1853]47
[4855]48
[3564]49/**
[4836]50 *  Constructor for loading a Terrain out of a file
51 * @param fileName The file to load data from.
[4597]52
[3566]53   this either loads out of an OBJ-file, or loads a heightmap if no .obj-extension is found.
54*/
[4597]55Terrain::Terrain(const char* fileName)
[3566]56{
57  this->init();
58
[5415]59  if (!strstr(fileName, ".obj") || !strstr(fileName, ".OBJ") )
[3566]60    {
[5308]61      this->loadModel(fileName);
[3566]62    }
63  else
64    {
65      // load the hightMap here.
66    }
67}
68
69/**
[4836]70 *  a Constructor for the Debug-Worlds
[5308]71 */
[3564]72Terrain::Terrain(DebugTerrain debugTerrain)
73{
74  this->init();
75  this->buildDebugTerrain(debugTerrain);
76}
77
[3245]78/**
[4836]79 *  standard deconstructor
[1853]80
[3245]81*/
[4746]82Terrain::~Terrain ()
[3543]83{
[3564]84  if (objectList)
85    glDeleteLists(this->objectList, 1);
[4889]86  if( this->ssp)
87    delete ssp;
[5465]88  if (this->vegetation)
89  {
90    ResourceManager::getInstance()->unload(this->vegetation);
91  }
[3543]92}
[1853]93
[3245]94
[4746]95void Terrain::init()
[3559]96{
[4320]97  this->setClassID(CL_TERRAIN, "Terrain");
[6142]98  this->toList(OM_ENVIRON_NOTICK);
[4597]99
[4136]100  this->objectList = 0;
[5308]101  this->ssp = NULL;
[5465]102  this->vegetation = NULL;
[3559]103}
104
[4924]105
[4607]106void Terrain::loadParams(const TiXmlElement* root)
107{
[6512]108  WorldEntity::loadParams(root);
[3559]109
[5671]110  LoadParam(root, "vegetation", this, Terrain, loadVegetation)
[5465]111      .describe("the fileName of the vegetation, that should be loaded onto this terrain. (must be relative to the data-dir)") ;
[4607]112}
113
[5465]114void Terrain::loadVegetation(const char* vegetationFile)
115{
[6341]116  PRINTF(0)("loadVegetation: %s\n", vegetationFile);
[5465]117  if (this->vegetation)
118    ResourceManager::getInstance()->unload(this->vegetation, RP_LEVEL);
119  if (vegetationFile != NULL)
120  {
121    PRINTF(4)("fetching %s\n", vegetationFile);
[6341]122    this->vegetation = (Model*)ResourceManager::getInstance()->load(vegetationFile, OBJ, RP_CAMPAIGN);
[5465]123  }
124  else
125    this->vegetation = NULL;
126}
127
128
129
[5500]130void Terrain::draw () const
[4597]131{
[3559]132  glMatrixMode(GL_MODELVIEW);
133  glPushMatrix();
[4597]134
[3559]135  /* translate */
[4597]136  glTranslatef (this->getAbsCoor ().x,
137                this->getAbsCoor ().y,
138                this->getAbsCoor ().z);
[3559]139  /* rotate */
[4998]140  Vector tmpRot = this->getAbsDir().getSpacialAxis();
141  glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
[3559]142
[4136]143  if (this->objectList)
144    glCallList(this->objectList);
[5994]145  else if (this->getModel())
146    this->getModel()->draw();
[5465]147  if (this->vegetation)
148    this->vegetation->draw();
[3559]149  glPopMatrix();
[4904]150
[4924]151  /* THIS IS ONLY FOR DEBUGGING INFORMATION */
[5308]152  if (this->ssp != NULL)
153    this->ssp->drawQuadtree();
[3559]154}
[3564]155
156
157void Terrain::buildDebugTerrain(DebugTerrain debugTerrain)
158{
159  // if the terrain is the Terrain of Dave
160  if (debugTerrain == TERRAIN_DAVE)
161    {
162      objectList = glGenLists(1);
163      glNewList (objectList, GL_COMPILE);
[4597]164
[3564]165      glColor3f(1.0,0,0);
[4597]166
[3564]167      int sizeX = 100;
168      int sizeZ = 80;
169      float length = 1000;
170      float width = 200;
171      float widthX = float (length /sizeX);
172      float widthZ = float (width /sizeZ);
[4597]173
[3564]174      float height [sizeX][sizeZ];
175      Vector normal_vectors[sizeX][sizeZ];
[4597]176
177
[3564]178      for ( int i = 0; i<sizeX-1; i+=1)
[4597]179        for (int j = 0; j<sizeZ-1;j+=1)
180          //height[i][j] = rand()/20046 + (j-25)*(j-25)/30;
[3564]181#ifdef __WIN32__
[4597]182          height[i][j]=(sin((float)j/3)*rand()*i/182400)*.5;
[3564]183#else
184      height[i][j]=(sin((float)j/3)*rand()*(long)i/6282450500.0)*.5;
185#endif
[4597]186
[3564]187      //Die Huegel ein wenig glaetten
188      for (int h=1; h<2;h++)
[4597]189        for (int i=1;i<sizeX-2 ;i+=1 )
190          for(int j=1;j<sizeZ-2;j+=1)
191            height[i][j]=(height[i+1][j]+height[i][j+1]+height[i-1][j]+height[i][j-1])/4;
192
[3564]193      //Berechnung von normalen Vektoren
194      for(int i=1;i<sizeX-2;i+=1)
[4597]195        for(int j=1;j<sizeZ-2 ;j+=1)
196          {
197            Vector v1 = Vector (widthX*(1),      height[i][j],      widthZ*(j) );
198            Vector v2 = Vector (widthX*(i-1),    height[i-1][j],    widthZ*(j));
199            Vector v3 = Vector (widthX*(i),      height[i][j+1],    widthZ*(j+1));
200            Vector v4 = Vector (widthX*(i+1),    height[i+1][j],    widthZ*(j));
201            Vector v5 = Vector (widthX*(i),      height[i][j-1],    widthZ*(j-1));
202
203            Vector c1 = v2 - v1;
204            Vector c2 = v3 - v1;
205            Vector c3=  v4 - v1;
206            Vector c4 = v5 - v1;
207            Vector zero = Vector (0,0,0);
208            normal_vectors[i][j]=c1.cross(v3-v5)+c2.cross(v4-v2)+c3.cross(v5-v3)+c4.cross(v2-v4);
209            normal_vectors[i][j].normalize();
210          }
211
[3564]212      glBegin(GL_QUADS);
213      int snowheight=3;
214      for ( int i = 0; i<sizeX; i+=1)
[4597]215        for (int j = 0; j<sizeZ;j+=1)
216          {
217            Vector v1 = Vector (widthX*(i),      height[i][j]-20,       widthZ*(j)  -width/2);
218            Vector v2 = Vector (widthX*(i+1),    height[i+1][j]-20,     widthZ*(j)  -width/2);
219            Vector v3 = Vector (widthX*(i+1),    height[i+1][j+1]-20,   widthZ*(j+1)-width/2);
220            Vector v4 = Vector (widthX*(i),      height[i][j+1]-20,     widthZ*(j+1)-width/2);
221            float a[3];
222            if(height[i][j]<snowheight){
223              a[0]=0;
224              a[1]=1.0-height[i][j]/10-.3;
225              a[2]=0;
226              glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
227            }
228            else{
229              a[0]=1.0;
230              a[1]=1.0;
231              a[2]=1.0;
232              glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
233
234            }
235            glNormal3f(normal_vectors[i][j].x, normal_vectors[i][j].y, normal_vectors[i][j].z);
236            glVertex3f(v1.x, v1.y, v1.z);
237            if(height[i+1][j]<snowheight){
238              a[0]=0;
239              a[1] =1.0-height[i+1][j]/10-.3;
240              a[2]=0;
241              glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
242            }
243            else{
244              a[0]=1.0;
245              a[1]=1.0;
246              a[2]=1.0;
247              glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
248
249            }
250            glNormal3f(normal_vectors[i+1][j].x, normal_vectors[i+1][j].y, normal_vectors[i+1][j].z);
251            glVertex3f(v2.x, v2.y, v2.z);
252            if(height[i+1][j+1]<snowheight){
253              a[0]=0;
254              a[1] =1.0-height[i+1][j+1]/10-.3;
255              a[2]=0;
256              glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
257            }
258            else{
259              a[0]=1.0;
260              a[1]=1.0;
261              a[2]=1.0;
262              glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
263
264
265            }
266            glNormal3f(normal_vectors[i+1][j+1].x, normal_vectors[i+1][j+1].y, normal_vectors[i+1][j+1].z);
267            glVertex3f(v3.x, v3.y, v3.z);
268            if(height[i][j+1]<snowheight){
269              a[0]=0;
270              a[1] =1.0-height[i+1][j+1]/10-.3;
271              a[2]=0;
272              glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
273            }
274            else{
275              a[0]=1.0;
276              a[1]=1.0;
277              a[2]=1.0;
278              glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
279            }
280            glNormal3f(normal_vectors[i][j+1].x, normal_vectors[i][j+1].y, normal_vectors[i][j+1].z);
281            glVertex3f(v4.x, v4.y, v4.z);
282
283          }
[3564]284      glEnd();
285      glEndList();
286    }
287
288  if (debugTerrain == TERRAIN_BENSCH)
289    {
290      /*
[4597]291        this->model = (OBJModel*) new Model();
[3564]292      this->model->setName("CUBE");
293      this->model->addVertex (-0.5, -0.5, 0.5);
294      this->model->addVertex (0.5, -0.5, 0.5);
295      this->model->addVertex (-0.5, 0.5, 0.5);
296      this->model->addVertex (0.5, 0.5, 0.5);
297      this->model->addVertex (-0.5, 0.5, -0.5);
298      this->model->addVertex (0.5, 0.5, -0.5);
299      this->model->addVertex (-0.5, -0.5, -0.5);
300      this->model->addVertex (0.5, -0.5, -0.5);
[4597]301
[3564]302      this->model->addVertexTexture (0.0, 0.0);
303      this->model->addVertexTexture (1.0, 0.0);
304      this->model->addVertexTexture (0.0, 1.0);
305      this->model->addVertexTexture (1.0, 1.0);
306      this->model->addVertexTexture (0.0, 2.0);
307      this->model->addVertexTexture (1.0, 2.0);
308      this->model->addVertexTexture (0.0, 3.0);
309      this->model->addVertexTexture (1.0, 3.0);
310      this->model->addVertexTexture (0.0, 4.0);
311      this->model->addVertexTexture (1.0, 4.0);
312      this->model->addVertexTexture (2.0, 0.0);
313      this->model->addVertexTexture (2.0, 1.0);
314      this->model->addVertexTexture (-1.0, 0.0);
315      this->model->addVertexTexture (-1.0, 1.0);
316
317      this->model->finalize();
318      */
319    }
320}
[6341]321
322int Terrain::writeBytes( const byte * data, int length, int sender )
323{
324  setRequestedSync( false );
325  setIsOutOfSync( false );
326
327  SYNCHELP_READ_BEGIN();
328  SYNCHELP_READ_FKT( WorldEntity::writeState );
329
330  return SYNCHELP_READ_N;
331}
332
333int Terrain::readBytes( byte * data, int maxLength, int * reciever )
334{
335  if ( isOutOfSync() && !requestedSync() && this->getHostID()!=this->getOwner() )
336  {
337    (NetworkGameManager::getInstance())->sync( this->getUniqueID(), this->getOwner() );
338    setRequestedSync( true );
339  }
340
341  int rec = this->getRequestSync();
342  if ( rec > 0 )
343  {
344    *reciever = rec;
345
346    return WorldEntity::readState( data, maxLength );
347
348  }
349
350  *reciever = 0;
351  return 0;
352}
353
354void Terrain::writeDebug( ) const
355{
356}
357
358void Terrain::readDebug( ) const
359{
360}
Note: See TracBrowser for help on using the repository browser.