Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/network/src/world_entities/terrain.cc @ 6275

Last change on this file since 6275 was 6275, checked in by rennerc, 18 years ago

synchronizeable: added macros to help write/read data

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