Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8690 was 8690, checked in by ponder, 18 years ago
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_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        Material *mat = new Material( "TERR_LM" );
143        mat->setDiffuse( 1.0f, 1.0f, 1.0f );
144        mat->setAmbient( 1.0f, 1.0f, 1.0f );
145        mat->setDiffuseMap( _lightFile, GL_TEXTURE_2D, 0 );     
146        mat->setDiffuseMap( std::string( "maps/gras_128.jpg" ), GL_TEXTURE_2D, 1 );     
147        terrain->addMaterial( mat );
148}
149
150
151
152void TerrainEntity::loadVegetation(const std::string& vegetationFile)
153{
154  PRINTF(4)("loadVegetation: %s\n", vegetationFile.c_str());
155  if (this->vegetation)
156    ResourceManager::getInstance()->unload(this->vegetation, RP_LEVEL);
157  if (!vegetationFile.empty())
158  {
159    PRINTF(4)("fetching %s\n", vegetationFile.c_str());
160    this->vegetation = dynamic_cast<Model*>(ResourceManager::getInstance()->load(vegetationFile, OBJ, RP_CAMPAIGN));
161  }
162  else
163    this->vegetation = NULL;
164}
165
166
167
168
169
170void TerrainEntity::draw () const
171{
172        glMatrixMode( GL_MODELVIEW );
173        glPushMatrix();
174
175  /* translate */
176   glTranslatef( this->getAbsCoor().x,
177                 this->getAbsCoor().y,
178                 this->getAbsCoor().z );
179
180        Vector cam = State::getCameraNode()->getAbsCoor();
181       
182        if ( this->terrain ) {
183                terrain->setCameraPosition( Triple( cam.x, cam.y, cam.z ) ); 
184                terrain->draw( );
185        }
186
187        glPopMatrix();
188  /*
189    glMatrixMode(GL_MODELVIEW);
190    glPushMatrix();
191    glLoadIdentity();
192    Vector camera =   State::getCameraNode()->getAbsCoor(); // Go on here ..........!!!
193
194    float height =    heightMap->getHeight(camera.x, camera.z);
195
196    glEnable (GL_COLOR_MATERIAL) ;
197    glBegin(GL_QUADS);            // Draw The Cube Using quads
198    glColor3f(0.0f,1.0f,0.0f);  // Color Blue
199    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)
200    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)
201    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)
202    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)
203    glEnd();                      // End Drawing The Plan
204
205    glPopMatrix();*/
206
207}
208
209
210void TerrainEntity::buildDebugTerrainEntity(DebugTerrainEntity debugTerrainEntity)
211{
212        terrain->build( );
213  /*
214  // if the TerrainEntity is the TerrainEntity of Dave
215  if (debugTerrainEntity == TERRAINENTITY_DAVE)
216  {
217    objectList = glGenLists(1);
218    glNewList (objectList, GL_COMPILE);
219
220    glColor3f(1.0,0,0);
221
222    int sizeX = 100;
223    int sizeZ = 80;
224    float length = 1000;
225    float width = 200;
226    float widthX = float (length /sizeX);
227    float widthZ = float (width /sizeZ);
228
229    float height [sizeX][sizeZ];
230    Vector normal_vectors[sizeX][sizeZ];
231
232
233    for ( int i = 0; i<sizeX-1; i+=1)
234      for (int j = 0; j<sizeZ-1;j+=1)
235        //height[i][j] = rand()/20046 + (j-25)*(j-25)/30;
236#ifdef __WIN32__
237        height[i][j]=(sin((float)j/3)*rand()*i/182400)*.5;
238#else
239        height[i][j]=(sin((float)j/3)*rand()*(long)i/6282450500.0)*.5;
240#endif
241
242    //Die Huegel ein wenig glaetten
243    for (int h=1; h<2;h++)
244      for (int i=1;i<sizeX-2 ;i+=1 )
245        for(int j=1;j<sizeZ-2;j+=1)
246          height[i][j]=(height[i+1][j]+height[i][j+1]+height[i-1][j]+height[i][j-1])/4;
247
248    //Berechnung von normalen Vektoren
249    for(int i=1;i<sizeX-2;i+=1)
250      for(int j=1;j<sizeZ-2 ;j+=1)
251      {
252        Vector v1 = Vector (widthX*(1),      height[i][j],      widthZ*(j) );
253        Vector v2 = Vector (widthX*(i-1),    height[i-1][j],    widthZ*(j));
254        Vector v3 = Vector (widthX*(i),      height[i][j+1],    widthZ*(j+1));
255        Vector v4 = Vector (widthX*(i+1),    height[i+1][j],    widthZ*(j));
256        Vector v5 = Vector (widthX*(i),      height[i][j-1],    widthZ*(j-1));
257
258        Vector c1 = v2 - v1;
259        Vector c2 = v3 - v1;
260        Vector c3=  v4 - v1;
261        Vector c4 = v5 - v1;
262        Vector zero = Vector (0,0,0);
263        normal_vectors[i][j]=c1.cross(v3-v5)+c2.cross(v4-v2)+c3.cross(v5-v3)+c4.cross(v2-v4);
264        normal_vectors[i][j].normalize();
265      }
266
267    glBegin(GL_QUADS);
268    int snowheight=3;
269    for ( int i = 0; i<sizeX; i+=1)
270      for (int j = 0; j<sizeZ;j+=1)
271      {
272        Vector v1 = Vector (widthX*(i),      height[i][j]-20,       widthZ*(j)  -width/2);
273        Vector v2 = Vector (widthX*(i+1),    height[i+1][j]-20,     widthZ*(j)  -width/2);
274        Vector v3 = Vector (widthX*(i+1),    height[i+1][j+1]-20,   widthZ*(j+1)-width/2);
275        Vector v4 = Vector (widthX*(i),      height[i][j+1]-20,     widthZ*(j+1)-width/2);
276        float a[3];
277        if(height[i][j]<snowheight)
278        {
279          a[0]=0;
280          a[1]=1.0-height[i][j]/10-.3;
281          a[2]=0;
282          glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
283        }
284        else
285        {
286          a[0]=1.0;
287          a[1]=1.0;
288          a[2]=1.0;
289          glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
290
291        }
292        glNormal3f(normal_vectors[i][j].x, normal_vectors[i][j].y, normal_vectors[i][j].z);
293        glVertex3f(v1.x, v1.y, v1.z);
294        if(height[i+1][j]<snowheight)
295        {
296          a[0]=0;
297          a[1] =1.0-height[i+1][j]/10-.3;
298          a[2]=0;
299          glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
300        }
301        else
302        {
303          a[0]=1.0;
304          a[1]=1.0;
305          a[2]=1.0;
306          glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
307
308        }
309        glNormal3f(normal_vectors[i+1][j].x, normal_vectors[i+1][j].y, normal_vectors[i+1][j].z);
310        glVertex3f(v2.x, v2.y, v2.z);
311        if(height[i+1][j+1]<snowheight)
312        {
313          a[0]=0;
314          a[1] =1.0-height[i+1][j+1]/10-.3;
315          a[2]=0;
316          glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
317        }
318        else
319        {
320          a[0]=1.0;
321          a[1]=1.0;
322          a[2]=1.0;
323          glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
324
325
326        }
327        glNormal3f(normal_vectors[i+1][j+1].x, normal_vectors[i+1][j+1].y, normal_vectors[i+1][j+1].z);
328        glVertex3f(v3.x, v3.y, v3.z);
329        if(height[i][j+1]<snowheight)
330        {
331          a[0]=0;
332          a[1] =1.0-height[i+1][j+1]/10-.3;
333          a[2]=0;
334          glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
335        }
336        else
337        {
338          a[0]=1.0;
339          a[1]=1.0;
340          a[2]=1.0;
341          glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
342        }
343        glNormal3f(normal_vectors[i][j+1].x, normal_vectors[i][j+1].y, normal_vectors[i][j+1].z);
344        glVertex3f(v4.x, v4.y, v4.z);
345
346      }
347    glEnd();
348    glEndList();
349  }
350        */
351  if (debugTerrainEntity == TERRAINENTITY_BENSCH)
352  {
353    /*
354      this->model = (OBJModel*) new Model();
355    this->model->setName("CUBE");
356    this->model->addVertex (-0.5, -0.5, 0.5);
357    this->model->addVertex (0.5, -0.5, 0.5);
358    this->model->addVertex (-0.5, 0.5, 0.5);
359    this->model->addVertex (0.5, 0.5, 0.5);
360    this->model->addVertex (-0.5, 0.5, -0.5);
361    this->model->addVertex (0.5, 0.5, -0.5);
362    this->model->addVertex (-0.5, -0.5, -0.5);
363    this->model->addVertex (0.5, -0.5, -0.5);
364
365    this->model->addVertexTexture (0.0, 0.0);
366    this->model->addVertexTexture (1.0, 0.0);
367    this->model->addVertexTexture (0.0, 1.0);
368    this->model->addVertexTexture (1.0, 1.0);
369    this->model->addVertexTexture (0.0, 2.0);
370    this->model->addVertexTexture (1.0, 2.0);
371    this->model->addVertexTexture (0.0, 3.0);
372    this->model->addVertexTexture (1.0, 3.0);
373    this->model->addVertexTexture (0.0, 4.0);
374    this->model->addVertexTexture (1.0, 4.0);
375    this->model->addVertexTexture (2.0, 0.0);
376    this->model->addVertexTexture (2.0, 1.0);
377    this->model->addVertexTexture (-1.0, 0.0);
378    this->model->addVertexTexture (-1.0, 1.0);
379
380    this->model->finalize();
381    */
382  }
383}
384
385float TerrainEntity::getHeight( float x, float z )
386{
387        Triple  altitude( x-getAbsCoor().x, 0.0f, z-getAbsCoor().z ), 
388                        normal( 0.0f, 0.0f, 0.0f );
389        if ( terrain )
390                terrain->getAltitude( altitude, normal );
391               
392  return altitude.y+getAbsCoor().y;
393}
Note: See TracBrowser for help on using the repository browser.