Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/terrain/src/world_entities/terrain_entity.cc @ 8453

Last change on this file since 8453 was 8328, checked in by ponder, 18 years ago
  • Renamed Terrain into TerrainEntity in order to avoid conflicts between the importer/terrain class and the world_entities/terrain.
  • Changed TerrainEntity to support the new terrain class.
File size: 10.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_entity.h"
19#include "terrain/terrain.h"
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 "material.h"
29
30#include "glincl.h"
31
32#include "state.h"
33
34using namespace std;
35
36CREATE_FACTORY( TerrainEntity, CL_TERRAIN );
37
38/**
39 *  standard constructor
40 */
41TerrainEntity::TerrainEntity (const TiXmlElement* root)
42{
43        this->init();
44
45
46        if( root != NULL)
47                this->loadParams(root);
48        terrain->build( );
49}
50
51
52/**
53 *  Constructor for loading a TerrainEntity out of a file
54 * @param fileName The file to load data from.
55
56   this either loads out of an OBJ-file, or loads a heightmap if no .obj-extension is found.
57*/
58TerrainEntity::TerrainEntity(const std::string& fileName )
59{
60  this->init();
61
62  if (fileName.rfind(".obj" ) != -1 || fileName.rfind(".OBJ") != -1 )
63  {
64    this->loadModel(fileName);
65  }
66  else
67  {
68    // load the hightMap here.
69  }
70}
71
72/**
73 *  a Constructor for the Debug-Worlds
74 */
75TerrainEntity::TerrainEntity(DebugTerrainEntity debugTerrainEntity)
76{
77  this->init();
78  this->buildDebugTerrainEntity(debugTerrainEntity);
79}
80
81/**
82 *  standard deconstructor
83
84*/
85TerrainEntity::~TerrainEntity ()
86{
87  if (objectList)
88    glDeleteLists(this->objectList, 1);
89
90  if (this->vegetation)
91  {
92    ResourceManager::getInstance()->unload( this->vegetation );
93  }
94
95  if( this->terrain )
96    delete terrain;
97}
98
99
100void TerrainEntity::init()
101{
102  this->setClassID( CL_TERRAIN, "TerrainEntity");
103  this->toList(OM_ENVIRON_NOTICK);
104  this->toReflectionList();
105
106  this->objectList = 0;
107  this->vegetation = NULL;
108
109  this->terrain = new Terrain();
110
111  //this->heightMapMaterial = new Material();
112}
113
114
115void TerrainEntity::loadParams(const TiXmlElement* root)
116{
117        WorldEntity::loadParams(root);
118
119        LoadParam(root, "scale", this, TerrainEntity, setScale)
120                .describe("The scale in x,y,z direction");
121       
122        LoadParam( root, "lightmap", this, TerrainEntity, loadLightmap )
123                .describe("The name of the lightmap.");
124               
125        LoadParam( root, "elevationmap", this, TerrainEntity, loadElevationmap )
126                .describe( "The name of the elevation map. Must be an 8bit image" );
127               
128}
129
130void TerrainEntity::setScale(float x, float y, float z)
131{
132        terrain->setScale( Triple( x, y, z ) );
133}
134
135void TerrainEntity::loadElevationmap( const std::string& _eleFile )
136{
137        terrain->setHeightmap( _eleFile );
138}
139
140void TerrainEntity::loadLightmap( const std::string& _lightFile )
141{
142        terrain->setLightmap( _lightFile );
143}
144
145
146
147void TerrainEntity::loadVegetation(const std::string& vegetationFile)
148{
149  PRINTF(4)("loadVegetation: %s\n", vegetationFile.c_str());
150  if (this->vegetation)
151    ResourceManager::getInstance()->unload(this->vegetation, RP_LEVEL);
152  if (!vegetationFile.empty())
153  {
154    PRINTF(4)("fetching %s\n", vegetationFile.c_str());
155    this->vegetation = dynamic_cast<Model*>(ResourceManager::getInstance()->load(vegetationFile, OBJ, RP_CAMPAIGN));
156  }
157  else
158    this->vegetation = NULL;
159}
160
161
162
163
164
165void TerrainEntity::draw () const
166{
167        glMatrixMode( GL_MODELVIEW );
168        glPushMatrix();
169
170  /* translate */
171 /* glTranslatef (this->getAbsCoor ().x,
172                this->getAbsCoor ().y,
173                this->getAbsCoor ().z );*/
174  /* rotate */
175  // Vector tmpRot = this->getAbsDir().getSpacialAxis();
176  //glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
177
178  /*if (this->objectList)
179    glCallList(this->objectList);
180  else if (this->getModel())
181    this->getModel()->draw();
182
183  if (this->vegetation)
184    this->vegetation->draw();
185*/
186
187        Vector cam = State::getCameraNode()->getAbsCoor();
188       
189        if ( this->terrain ) {
190                terrain->setCameraPosition( Triple( cam.x, cam.y, cam.z ) ); 
191                terrain->draw( );
192        }
193
194        glPopMatrix();
195  /*
196    glMatrixMode(GL_MODELVIEW);
197    glPushMatrix();
198    glLoadIdentity();
199    Vector camera =   State::getCameraNode()->getAbsCoor(); // Go on here ..........!!!
200
201    float height =    heightMap->getHeight(camera.x, camera.z);
202
203    glEnable (GL_COLOR_MATERIAL) ;
204    glBegin(GL_QUADS);            // Draw The Cube Using quads
205    glColor3f(0.0f,1.0f,0.0f);  // Color Blue
206    glVertex3f(camera.x + 63.0f,TerrainEntity->getHeight(camera.x+63.0f, camera.z-10.0f)+13.0f,camera.z-10.0f);      // Top Right Of The Quad (Top)
207    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)
208    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)
209    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)
210    glEnd();                      // End Drawing The Plan
211
212    glPopMatrix();*/
213
214}
215
216
217void TerrainEntity::buildDebugTerrainEntity(DebugTerrainEntity debugTerrainEntity)
218{
219        terrain->build( );
220  /*
221  // if the TerrainEntity is the TerrainEntity of Dave
222  if (debugTerrainEntity == TERRAINENTITY_DAVE)
223  {
224    objectList = glGenLists(1);
225    glNewList (objectList, GL_COMPILE);
226
227    glColor3f(1.0,0,0);
228
229    int sizeX = 100;
230    int sizeZ = 80;
231    float length = 1000;
232    float width = 200;
233    float widthX = float (length /sizeX);
234    float widthZ = float (width /sizeZ);
235
236    float height [sizeX][sizeZ];
237    Vector normal_vectors[sizeX][sizeZ];
238
239
240    for ( int i = 0; i<sizeX-1; i+=1)
241      for (int j = 0; j<sizeZ-1;j+=1)
242        //height[i][j] = rand()/20046 + (j-25)*(j-25)/30;
243#ifdef __WIN32__
244        height[i][j]=(sin((float)j/3)*rand()*i/182400)*.5;
245#else
246        height[i][j]=(sin((float)j/3)*rand()*(long)i/6282450500.0)*.5;
247#endif
248
249    //Die Huegel ein wenig glaetten
250    for (int h=1; h<2;h++)
251      for (int i=1;i<sizeX-2 ;i+=1 )
252        for(int j=1;j<sizeZ-2;j+=1)
253          height[i][j]=(height[i+1][j]+height[i][j+1]+height[i-1][j]+height[i][j-1])/4;
254
255    //Berechnung von normalen Vektoren
256    for(int i=1;i<sizeX-2;i+=1)
257      for(int j=1;j<sizeZ-2 ;j+=1)
258      {
259        Vector v1 = Vector (widthX*(1),      height[i][j],      widthZ*(j) );
260        Vector v2 = Vector (widthX*(i-1),    height[i-1][j],    widthZ*(j));
261        Vector v3 = Vector (widthX*(i),      height[i][j+1],    widthZ*(j+1));
262        Vector v4 = Vector (widthX*(i+1),    height[i+1][j],    widthZ*(j));
263        Vector v5 = Vector (widthX*(i),      height[i][j-1],    widthZ*(j-1));
264
265        Vector c1 = v2 - v1;
266        Vector c2 = v3 - v1;
267        Vector c3=  v4 - v1;
268        Vector c4 = v5 - v1;
269        Vector zero = Vector (0,0,0);
270        normal_vectors[i][j]=c1.cross(v3-v5)+c2.cross(v4-v2)+c3.cross(v5-v3)+c4.cross(v2-v4);
271        normal_vectors[i][j].normalize();
272      }
273
274    glBegin(GL_QUADS);
275    int snowheight=3;
276    for ( int i = 0; i<sizeX; i+=1)
277      for (int j = 0; j<sizeZ;j+=1)
278      {
279        Vector v1 = Vector (widthX*(i),      height[i][j]-20,       widthZ*(j)  -width/2);
280        Vector v2 = Vector (widthX*(i+1),    height[i+1][j]-20,     widthZ*(j)  -width/2);
281        Vector v3 = Vector (widthX*(i+1),    height[i+1][j+1]-20,   widthZ*(j+1)-width/2);
282        Vector v4 = Vector (widthX*(i),      height[i][j+1]-20,     widthZ*(j+1)-width/2);
283        float a[3];
284        if(height[i][j]<snowheight)
285        {
286          a[0]=0;
287          a[1]=1.0-height[i][j]/10-.3;
288          a[2]=0;
289          glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
290        }
291        else
292        {
293          a[0]=1.0;
294          a[1]=1.0;
295          a[2]=1.0;
296          glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
297
298        }
299        glNormal3f(normal_vectors[i][j].x, normal_vectors[i][j].y, normal_vectors[i][j].z);
300        glVertex3f(v1.x, v1.y, v1.z);
301        if(height[i+1][j]<snowheight)
302        {
303          a[0]=0;
304          a[1] =1.0-height[i+1][j]/10-.3;
305          a[2]=0;
306          glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
307        }
308        else
309        {
310          a[0]=1.0;
311          a[1]=1.0;
312          a[2]=1.0;
313          glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
314
315        }
316        glNormal3f(normal_vectors[i+1][j].x, normal_vectors[i+1][j].y, normal_vectors[i+1][j].z);
317        glVertex3f(v2.x, v2.y, v2.z);
318        if(height[i+1][j+1]<snowheight)
319        {
320          a[0]=0;
321          a[1] =1.0-height[i+1][j+1]/10-.3;
322          a[2]=0;
323          glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
324        }
325        else
326        {
327          a[0]=1.0;
328          a[1]=1.0;
329          a[2]=1.0;
330          glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
331
332
333        }
334        glNormal3f(normal_vectors[i+1][j+1].x, normal_vectors[i+1][j+1].y, normal_vectors[i+1][j+1].z);
335        glVertex3f(v3.x, v3.y, v3.z);
336        if(height[i][j+1]<snowheight)
337        {
338          a[0]=0;
339          a[1] =1.0-height[i+1][j+1]/10-.3;
340          a[2]=0;
341          glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
342        }
343        else
344        {
345          a[0]=1.0;
346          a[1]=1.0;
347          a[2]=1.0;
348          glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
349        }
350        glNormal3f(normal_vectors[i][j+1].x, normal_vectors[i][j+1].y, normal_vectors[i][j+1].z);
351        glVertex3f(v4.x, v4.y, v4.z);
352
353      }
354    glEnd();
355    glEndList();
356  }
357        */
358  if (debugTerrainEntity == TERRAINENTITY_BENSCH)
359  {
360    /*
361      this->model = (OBJModel*) new Model();
362    this->model->setName("CUBE");
363    this->model->addVertex (-0.5, -0.5, 0.5);
364    this->model->addVertex (0.5, -0.5, 0.5);
365    this->model->addVertex (-0.5, 0.5, 0.5);
366    this->model->addVertex (0.5, 0.5, 0.5);
367    this->model->addVertex (-0.5, 0.5, -0.5);
368    this->model->addVertex (0.5, 0.5, -0.5);
369    this->model->addVertex (-0.5, -0.5, -0.5);
370    this->model->addVertex (0.5, -0.5, -0.5);
371
372    this->model->addVertexTexture (0.0, 0.0);
373    this->model->addVertexTexture (1.0, 0.0);
374    this->model->addVertexTexture (0.0, 1.0);
375    this->model->addVertexTexture (1.0, 1.0);
376    this->model->addVertexTexture (0.0, 2.0);
377    this->model->addVertexTexture (1.0, 2.0);
378    this->model->addVertexTexture (0.0, 3.0);
379    this->model->addVertexTexture (1.0, 3.0);
380    this->model->addVertexTexture (0.0, 4.0);
381    this->model->addVertexTexture (1.0, 4.0);
382    this->model->addVertexTexture (2.0, 0.0);
383    this->model->addVertexTexture (2.0, 1.0);
384    this->model->addVertexTexture (-1.0, 0.0);
385    this->model->addVertexTexture (-1.0, 1.0);
386
387    this->model->finalize();
388    */
389  }
390}
391
392float TerrainEntity::getHeight( float x, float z )
393{
394        Triple altitude( x, 0.0f, z ), normal( 0.0f, 0.0f, 0.0f );
395        if ( terrain )
396                terrain->getAltitude( altitude, normal );
397               
398  return altitude.y;
399}
Note: See TracBrowser for help on using the repository browser.