Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/bsp_model_old/src/world_entities/terrain.cc @ 8328

Last change on this file since 8328 was 8081, checked in by bottac, 19 years ago

Loadable from .oxw

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