Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8745 was 8744, checked in by ponder, 19 years ago
  • A few more properties can be specified in the .oxw file for the terrain.
  • several material-layers can be rendered now.
  • there is still one crack appearing now and then.. need to isolate it and send him to hell.
File size: 11.1 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, "light_map", this, TerrainEntity, loadLightmap )
123                .describe("The name of the lightmap.");
124               
125        LoadParam( root, "elevation_map", this, TerrainEntity, loadElevationmap )
126                .describe( "The name of the elevation map. Must be an 8bit image" );
127        ResourceManager *manager = ResourceManager::getInstance();
128        TiXmlElement * layer = (TiXmlElement*)root->FirstChild( "material_layer" );             
129        while ( layer ) {
130                LayerInfo *info = new LayerInfo();
131                TiXmlElement *detail = (TiXmlElement*)layer->FirstChild( "detail_map" );
132                if ( detail ) {
133                        string detailmap( detail->Attribute( "file" ) );
134                        info->detail = (Texture*)manager->load( detailmap, 
135                                IMAGE, RP_GAME, (int)GL_TEXTURE_2D );                   
136                        if ( !info->detail ) {
137                                PRINTF(0)( "%s not found\n", detailmap.c_str() );
138                        }       
139                        TiXmlElement *repeat = (TiXmlElement*)detail->FirstChild( "repeat" );
140                        if ( repeat ) {
141                                repeat->QueryFloatAttribute( "x", &info->repeatX );
142                                repeat->QueryFloatAttribute( "z", &info->repeatZ );                             
143                        }
144                }
145                else {
146                        PRINTF(0)( "Please specify a detail-map" );
147                }
148                TiXmlElement *alpha = (TiXmlElement*)layer->FirstChild( "alpha" );
149                if ( alpha ) {
150                                if ( !alpha->Attribute( "full" ) ) {
151                                        string alphamap( alpha->Attribute( "file" ) );
152                                        info->alpha = (Texture*)manager->load( alphamap, 
153                                                IMAGE, RP_GAME, (int)GL_TEXTURE_2D );
154                                }
155                }       
156                terrain->addMaterialLayer( info );
157                layer = (TiXmlElement*)layer->NextSibling( "material_layer" );
158        }
159}
160
161void TerrainEntity::setScale(float x, float y, float z)
162{
163        terrain->setScale( Triple( x, y, z ) );
164}
165
166void TerrainEntity::loadElevationmap( const std::string& _eleFile )
167{
168        terrain->setHeightmap( _eleFile );
169}
170
171void TerrainEntity::loadLightmap( const std::string& _lightFile )
172{
173        ResourceManager *manager = ResourceManager::getInstance();
174        Texture *lightmap = (Texture*)manager->load( _lightFile, 
175                IMAGE, RP_GAME, (int)GL_TEXTURE_2D );   
176        if ( lightmap ) 
177                terrain->setLightmap( lightmap );
178        else
179                PRINTF(0)("no lightmap %s\n", _lightFile.c_str() );
180}
181
182
183
184void TerrainEntity::loadVegetation(const std::string& vegetationFile)
185{
186  PRINTF(4)("loadVegetation: %s\n", vegetationFile.c_str());
187  if (this->vegetation)
188    ResourceManager::getInstance()->unload(this->vegetation, RP_LEVEL);
189  if (!vegetationFile.empty())
190  {
191    PRINTF(4)("fetching %s\n", vegetationFile.c_str());
192    this->vegetation = dynamic_cast<Model*>(ResourceManager::getInstance()->load(vegetationFile, OBJ, RP_CAMPAIGN));
193  }
194  else
195    this->vegetation = NULL;
196}
197
198
199
200
201
202void TerrainEntity::draw () const
203{
204        glMatrixMode( GL_MODELVIEW );
205        glPushMatrix();
206
207   glTranslatef( this->getAbsCoor().x,
208                 this->getAbsCoor().y,
209                 this->getAbsCoor().z );
210
211        Vector cam = State::getCameraNode()->getAbsCoor();
212       
213        if ( this->terrain ) {
214                terrain->setCameraPosition( Triple( cam.x, cam.y, cam.z ) ); 
215                terrain->draw( );
216        }
217        glPopMatrix();
218
219
220}
221
222
223void TerrainEntity::buildDebugTerrainEntity(DebugTerrainEntity debugTerrainEntity)
224{
225        terrain->build( );
226  /*
227  // if the TerrainEntity is the TerrainEntity of Dave
228  if (debugTerrainEntity == TERRAINENTITY_DAVE)
229  {
230    objectList = glGenLists(1);
231    glNewList (objectList, GL_COMPILE);
232
233    glColor3f(1.0,0,0);
234
235    int sizeX = 100;
236    int sizeZ = 80;
237    float length = 1000;
238    float width = 200;
239    float widthX = float (length /sizeX);
240    float widthZ = float (width /sizeZ);
241
242    float height [sizeX][sizeZ];
243    Vector normal_vectors[sizeX][sizeZ];
244
245
246    for ( int i = 0; i<sizeX-1; i+=1)
247      for (int j = 0; j<sizeZ-1;j+=1)
248        //height[i][j] = rand()/20046 + (j-25)*(j-25)/30;
249#ifdef __WIN32__
250        height[i][j]=(sin((float)j/3)*rand()*i/182400)*.5;
251#else
252        height[i][j]=(sin((float)j/3)*rand()*(long)i/6282450500.0)*.5;
253#endif
254
255    //Die Huegel ein wenig glaetten
256    for (int h=1; h<2;h++)
257      for (int i=1;i<sizeX-2 ;i+=1 )
258        for(int j=1;j<sizeZ-2;j+=1)
259          height[i][j]=(height[i+1][j]+height[i][j+1]+height[i-1][j]+height[i][j-1])/4;
260
261    //Berechnung von normalen Vektoren
262    for(int i=1;i<sizeX-2;i+=1)
263      for(int j=1;j<sizeZ-2 ;j+=1)
264      {
265        Vector v1 = Vector (widthX*(1),      height[i][j],      widthZ*(j) );
266        Vector v2 = Vector (widthX*(i-1),    height[i-1][j],    widthZ*(j));
267        Vector v3 = Vector (widthX*(i),      height[i][j+1],    widthZ*(j+1));
268        Vector v4 = Vector (widthX*(i+1),    height[i+1][j],    widthZ*(j));
269        Vector v5 = Vector (widthX*(i),      height[i][j-1],    widthZ*(j-1));
270
271        Vector c1 = v2 - v1;
272        Vector c2 = v3 - v1;
273        Vector c3=  v4 - v1;
274        Vector c4 = v5 - v1;
275        Vector zero = Vector (0,0,0);
276        normal_vectors[i][j]=c1.cross(v3-v5)+c2.cross(v4-v2)+c3.cross(v5-v3)+c4.cross(v2-v4);
277        normal_vectors[i][j].normalize();
278      }
279
280    glBegin(GL_QUADS);
281    int snowheight=3;
282    for ( int i = 0; i<sizeX; i+=1)
283      for (int j = 0; j<sizeZ;j+=1)
284      {
285        Vector v1 = Vector (widthX*(i),      height[i][j]-20,       widthZ*(j)  -width/2);
286        Vector v2 = Vector (widthX*(i+1),    height[i+1][j]-20,     widthZ*(j)  -width/2);
287        Vector v3 = Vector (widthX*(i+1),    height[i+1][j+1]-20,   widthZ*(j+1)-width/2);
288        Vector v4 = Vector (widthX*(i),      height[i][j+1]-20,     widthZ*(j+1)-width/2);
289        float a[3];
290        if(height[i][j]<snowheight)
291        {
292          a[0]=0;
293          a[1]=1.0-height[i][j]/10-.3;
294          a[2]=0;
295          glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
296        }
297        else
298        {
299          a[0]=1.0;
300          a[1]=1.0;
301          a[2]=1.0;
302          glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
303
304        }
305        glNormal3f(normal_vectors[i][j].x, normal_vectors[i][j].y, normal_vectors[i][j].z);
306        glVertex3f(v1.x, v1.y, v1.z);
307        if(height[i+1][j]<snowheight)
308        {
309          a[0]=0;
310          a[1] =1.0-height[i+1][j]/10-.3;
311          a[2]=0;
312          glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
313        }
314        else
315        {
316          a[0]=1.0;
317          a[1]=1.0;
318          a[2]=1.0;
319          glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
320
321        }
322        glNormal3f(normal_vectors[i+1][j].x, normal_vectors[i+1][j].y, normal_vectors[i+1][j].z);
323        glVertex3f(v2.x, v2.y, v2.z);
324        if(height[i+1][j+1]<snowheight)
325        {
326          a[0]=0;
327          a[1] =1.0-height[i+1][j+1]/10-.3;
328          a[2]=0;
329          glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
330        }
331        else
332        {
333          a[0]=1.0;
334          a[1]=1.0;
335          a[2]=1.0;
336          glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
337
338
339        }
340        glNormal3f(normal_vectors[i+1][j+1].x, normal_vectors[i+1][j+1].y, normal_vectors[i+1][j+1].z);
341        glVertex3f(v3.x, v3.y, v3.z);
342        if(height[i][j+1]<snowheight)
343        {
344          a[0]=0;
345          a[1] =1.0-height[i+1][j+1]/10-.3;
346          a[2]=0;
347          glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
348        }
349        else
350        {
351          a[0]=1.0;
352          a[1]=1.0;
353          a[2]=1.0;
354          glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
355        }
356        glNormal3f(normal_vectors[i][j+1].x, normal_vectors[i][j+1].y, normal_vectors[i][j+1].z);
357        glVertex3f(v4.x, v4.y, v4.z);
358
359      }
360    glEnd();
361    glEndList();
362  }
363        */
364  if (debugTerrainEntity == TERRAINENTITY_BENSCH)
365  {
366    /*
367      this->model = (OBJModel*) new Model();
368    this->model->setName("CUBE");
369    this->model->addVertex (-0.5, -0.5, 0.5);
370    this->model->addVertex (0.5, -0.5, 0.5);
371    this->model->addVertex (-0.5, 0.5, 0.5);
372    this->model->addVertex (0.5, 0.5, 0.5);
373    this->model->addVertex (-0.5, 0.5, -0.5);
374    this->model->addVertex (0.5, 0.5, -0.5);
375    this->model->addVertex (-0.5, -0.5, -0.5);
376    this->model->addVertex (0.5, -0.5, -0.5);
377
378    this->model->addVertexTexture (0.0, 0.0);
379    this->model->addVertexTexture (1.0, 0.0);
380    this->model->addVertexTexture (0.0, 1.0);
381    this->model->addVertexTexture (1.0, 1.0);
382    this->model->addVertexTexture (0.0, 2.0);
383    this->model->addVertexTexture (1.0, 2.0);
384    this->model->addVertexTexture (0.0, 3.0);
385    this->model->addVertexTexture (1.0, 3.0);
386    this->model->addVertexTexture (0.0, 4.0);
387    this->model->addVertexTexture (1.0, 4.0);
388    this->model->addVertexTexture (2.0, 0.0);
389    this->model->addVertexTexture (2.0, 1.0);
390    this->model->addVertexTexture (-1.0, 0.0);
391    this->model->addVertexTexture (-1.0, 1.0);
392
393    this->model->finalize();
394    */
395  }
396}
397void TerrainEntity::getAltitude( Vector& _position, Vector& _normal )
398{
399        Triple  altitude( _position.x-getAbsCoor().x, 0.0f, _position.z-getAbsCoor().z ), 
400                        normal( 0.0f, 0.0f, 0.0f );
401        if ( terrain ) 
402                        terrain->getAltitude( altitude, normal );
403                       
404        _position.y = altitude.y+getAbsCoor().y;
405        _normal.z = normal.z; _normal.y = normal.y; _normal.z = normal.z;       
406}
407
408float TerrainEntity::getHeight( float x, float z )
409{
410        Triple  altitude( x-getAbsCoor().x, 0.0f, z-getAbsCoor().z ), 
411                        normal( 0.0f, 0.0f, 0.0f );
412        if ( terrain )
413                terrain->getAltitude( altitude, normal );
414               
415  return altitude.y+getAbsCoor().y;
416}
Note: See TracBrowser for help on using the repository browser.