Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/height_map_merge/src/world_entities/terrain.cc @ 6727

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