Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8569 was 8548, checked in by ponder, 18 years ago
  • Added multitexturing support to the terrain-class.
  • The lightmap gets screwed up and I don't know why…
File size: 10.9 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( "pictures/lehm.png" ), 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  /* rotate */
180  // Vector tmpRot = this->getAbsDir().getSpacialAxis();
181  //glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
182
183  /*if (this->objectList)
184    glCallList(this->objectList);
185  else if (this->getModel())
186    this->getModel()->draw();
187
188  if (this->vegetation)
189    this->vegetation->draw();
190*/
191
192        Vector cam = State::getCameraNode()->getAbsCoor();
193       
194        if ( this->terrain ) {
195                terrain->setCameraPosition( Triple( cam.x, cam.y, cam.z ) ); 
196                terrain->draw( );
197        }
198
199        glPopMatrix();
200  /*
201    glMatrixMode(GL_MODELVIEW);
202    glPushMatrix();
203    glLoadIdentity();
204    Vector camera =   State::getCameraNode()->getAbsCoor(); // Go on here ..........!!!
205
206    float height =    heightMap->getHeight(camera.x, camera.z);
207
208    glEnable (GL_COLOR_MATERIAL) ;
209    glBegin(GL_QUADS);            // Draw The Cube Using quads
210    glColor3f(0.0f,1.0f,0.0f);  // Color Blue
211    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)
212    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)
213    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)
214    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)
215    glEnd();                      // End Drawing The Plan
216
217    glPopMatrix();*/
218
219}
220
221
222void TerrainEntity::buildDebugTerrainEntity(DebugTerrainEntity debugTerrainEntity)
223{
224        terrain->build( );
225  /*
226  // if the TerrainEntity is the TerrainEntity of Dave
227  if (debugTerrainEntity == TERRAINENTITY_DAVE)
228  {
229    objectList = glGenLists(1);
230    glNewList (objectList, GL_COMPILE);
231
232    glColor3f(1.0,0,0);
233
234    int sizeX = 100;
235    int sizeZ = 80;
236    float length = 1000;
237    float width = 200;
238    float widthX = float (length /sizeX);
239    float widthZ = float (width /sizeZ);
240
241    float height [sizeX][sizeZ];
242    Vector normal_vectors[sizeX][sizeZ];
243
244
245    for ( int i = 0; i<sizeX-1; i+=1)
246      for (int j = 0; j<sizeZ-1;j+=1)
247        //height[i][j] = rand()/20046 + (j-25)*(j-25)/30;
248#ifdef __WIN32__
249        height[i][j]=(sin((float)j/3)*rand()*i/182400)*.5;
250#else
251        height[i][j]=(sin((float)j/3)*rand()*(long)i/6282450500.0)*.5;
252#endif
253
254    //Die Huegel ein wenig glaetten
255    for (int h=1; h<2;h++)
256      for (int i=1;i<sizeX-2 ;i+=1 )
257        for(int j=1;j<sizeZ-2;j+=1)
258          height[i][j]=(height[i+1][j]+height[i][j+1]+height[i-1][j]+height[i][j-1])/4;
259
260    //Berechnung von normalen Vektoren
261    for(int i=1;i<sizeX-2;i+=1)
262      for(int j=1;j<sizeZ-2 ;j+=1)
263      {
264        Vector v1 = Vector (widthX*(1),      height[i][j],      widthZ*(j) );
265        Vector v2 = Vector (widthX*(i-1),    height[i-1][j],    widthZ*(j));
266        Vector v3 = Vector (widthX*(i),      height[i][j+1],    widthZ*(j+1));
267        Vector v4 = Vector (widthX*(i+1),    height[i+1][j],    widthZ*(j));
268        Vector v5 = Vector (widthX*(i),      height[i][j-1],    widthZ*(j-1));
269
270        Vector c1 = v2 - v1;
271        Vector c2 = v3 - v1;
272        Vector c3=  v4 - v1;
273        Vector c4 = v5 - v1;
274        Vector zero = Vector (0,0,0);
275        normal_vectors[i][j]=c1.cross(v3-v5)+c2.cross(v4-v2)+c3.cross(v5-v3)+c4.cross(v2-v4);
276        normal_vectors[i][j].normalize();
277      }
278
279    glBegin(GL_QUADS);
280    int snowheight=3;
281    for ( int i = 0; i<sizeX; i+=1)
282      for (int j = 0; j<sizeZ;j+=1)
283      {
284        Vector v1 = Vector (widthX*(i),      height[i][j]-20,       widthZ*(j)  -width/2);
285        Vector v2 = Vector (widthX*(i+1),    height[i+1][j]-20,     widthZ*(j)  -width/2);
286        Vector v3 = Vector (widthX*(i+1),    height[i+1][j+1]-20,   widthZ*(j+1)-width/2);
287        Vector v4 = Vector (widthX*(i),      height[i][j+1]-20,     widthZ*(j+1)-width/2);
288        float a[3];
289        if(height[i][j]<snowheight)
290        {
291          a[0]=0;
292          a[1]=1.0-height[i][j]/10-.3;
293          a[2]=0;
294          glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
295        }
296        else
297        {
298          a[0]=1.0;
299          a[1]=1.0;
300          a[2]=1.0;
301          glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
302
303        }
304        glNormal3f(normal_vectors[i][j].x, normal_vectors[i][j].y, normal_vectors[i][j].z);
305        glVertex3f(v1.x, v1.y, v1.z);
306        if(height[i+1][j]<snowheight)
307        {
308          a[0]=0;
309          a[1] =1.0-height[i+1][j]/10-.3;
310          a[2]=0;
311          glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
312        }
313        else
314        {
315          a[0]=1.0;
316          a[1]=1.0;
317          a[2]=1.0;
318          glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
319
320        }
321        glNormal3f(normal_vectors[i+1][j].x, normal_vectors[i+1][j].y, normal_vectors[i+1][j].z);
322        glVertex3f(v2.x, v2.y, v2.z);
323        if(height[i+1][j+1]<snowheight)
324        {
325          a[0]=0;
326          a[1] =1.0-height[i+1][j+1]/10-.3;
327          a[2]=0;
328          glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
329        }
330        else
331        {
332          a[0]=1.0;
333          a[1]=1.0;
334          a[2]=1.0;
335          glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
336
337
338        }
339        glNormal3f(normal_vectors[i+1][j+1].x, normal_vectors[i+1][j+1].y, normal_vectors[i+1][j+1].z);
340        glVertex3f(v3.x, v3.y, v3.z);
341        if(height[i][j+1]<snowheight)
342        {
343          a[0]=0;
344          a[1] =1.0-height[i+1][j+1]/10-.3;
345          a[2]=0;
346          glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
347        }
348        else
349        {
350          a[0]=1.0;
351          a[1]=1.0;
352          a[2]=1.0;
353          glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
354        }
355        glNormal3f(normal_vectors[i][j+1].x, normal_vectors[i][j+1].y, normal_vectors[i][j+1].z);
356        glVertex3f(v4.x, v4.y, v4.z);
357
358      }
359    glEnd();
360    glEndList();
361  }
362        */
363  if (debugTerrainEntity == TERRAINENTITY_BENSCH)
364  {
365    /*
366      this->model = (OBJModel*) new Model();
367    this->model->setName("CUBE");
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    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
377    this->model->addVertexTexture (0.0, 0.0);
378    this->model->addVertexTexture (1.0, 0.0);
379    this->model->addVertexTexture (0.0, 1.0);
380    this->model->addVertexTexture (1.0, 1.0);
381    this->model->addVertexTexture (0.0, 2.0);
382    this->model->addVertexTexture (1.0, 2.0);
383    this->model->addVertexTexture (0.0, 3.0);
384    this->model->addVertexTexture (1.0, 3.0);
385    this->model->addVertexTexture (0.0, 4.0);
386    this->model->addVertexTexture (1.0, 4.0);
387    this->model->addVertexTexture (2.0, 0.0);
388    this->model->addVertexTexture (2.0, 1.0);
389    this->model->addVertexTexture (-1.0, 0.0);
390    this->model->addVertexTexture (-1.0, 1.0);
391
392    this->model->finalize();
393    */
394  }
395}
396
397float TerrainEntity::getHeight( float x, float z )
398{
399        Triple altitude( x, 0.0f, z ), normal( 0.0f, 0.0f, 0.0f );
400        if ( terrain )
401                terrain->getAltitude( altitude, normal );
402               
403  return altitude.y;
404}
Note: See TracBrowser for help on using the repository browser.