Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/world_entities/terrain.cc @ 5465

Last change on this file since 5465 was 5465, checked in by bensch, 19 years ago

orxonox/trunk: vegetation rendering in the terrain (trees are now rendered correctly)

File size: 8.8 KB
RevLine 
[4597]1/*
[1853]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.
[1855]10
11   ### File Specific:
[3565]12   main-programmer: Benjamin Grauer
[1855]13   co-programmer: ...
[1853]14*/
[5357]15#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY
[1853]16
17
[3559]18#include "terrain.h"
[4607]19
[5356]20#include "load_param.h"
[4607]21#include "factory.h"
[4842]22#include "spatial_separation.h"
23
[5465]24#include "resource_manager.h"
25
[1856]26using namespace std;
[1853]27
[4607]28CREATE_FACTORY(Terrain);
[1856]29
[3245]30/**
[4836]31 *  standard constructor
[5357]32 */
[4607]33Terrain::Terrain (const TiXmlElement* root)
[3365]34{
[3564]35  this->init();
[4607]36  this->loadParams(root);
[4844]37
[5308]38//  if (this->model != NULL)
39    //this->ssp = new SpatialSeparation((AbstractModel*)this->model, 10.0f);
[3365]40}
[1853]41
[4855]42
[3564]43/**
[4836]44 *  Constructor for loading a Terrain out of a file
45 * @param fileName The file to load data from.
[4597]46
[3566]47   this either loads out of an OBJ-file, or loads a heightmap if no .obj-extension is found.
48*/
[4597]49Terrain::Terrain(const char* fileName)
[3566]50{
51  this->init();
52
[5415]53  if (!strstr(fileName, ".obj") || !strstr(fileName, ".OBJ") )
[3566]54    {
[5308]55      this->loadModel(fileName);
[3566]56    }
57  else
58    {
59      // load the hightMap here.
60    }
61}
62
63/**
[4836]64 *  a Constructor for the Debug-Worlds
[5308]65 */
[3564]66Terrain::Terrain(DebugTerrain debugTerrain)
67{
68  this->init();
69  this->buildDebugTerrain(debugTerrain);
70}
71
[3245]72/**
[4836]73 *  standard deconstructor
[1853]74
[3245]75*/
[4746]76Terrain::~Terrain ()
[3543]77{
[3564]78  if (objectList)
79    glDeleteLists(this->objectList, 1);
[4889]80  if( this->ssp)
81    delete ssp;
[5465]82  if (this->vegetation)
83  {
84    ResourceManager::getInstance()->unload(this->vegetation);
85  }
[3543]86}
[1853]87
[3245]88
[4746]89void Terrain::init()
[3559]90{
[4320]91  this->setClassID(CL_TERRAIN, "Terrain");
[4597]92
[4136]93  this->objectList = 0;
[5308]94  this->ssp = NULL;
[5465]95  this->vegetation = NULL;
[3559]96}
97
[4924]98
[4607]99void Terrain::loadParams(const TiXmlElement* root)
100{
101  static_cast<WorldEntity*>(this)->loadParams(root);
[3559]102
[5465]103  LoadParam<Terrain>(root, "vegetation", this, &Terrain::loadVegetation)
104      .describe("the fileName of the vegetation, that should be loaded onto this terrain. (must be relative to the data-dir)") ;
105
[4607]106  //LoadParam<Terrain>(root, "DebugTerrain",  );
107}
108
[5465]109void Terrain::loadVegetation(const char* vegetationFile)
110{
111  if (this->vegetation)
112    ResourceManager::getInstance()->unload(this->vegetation, RP_LEVEL);
113  if (vegetationFile != NULL)
114  {
115    PRINTF(4)("fetching %s\n", vegetationFile);
116      this->vegetation = (Model*)ResourceManager::getInstance()->load(vegetationFile, OBJ, RP_CAMPAIGN);
117  }
118  else
119    this->vegetation = NULL;
120}
121
122
123
[3559]124void Terrain::draw ()
[4597]125{
[3559]126  glMatrixMode(GL_MODELVIEW);
127  glPushMatrix();
[4597]128
[3559]129  /* translate */
[4597]130  glTranslatef (this->getAbsCoor ().x,
131                this->getAbsCoor ().y,
132                this->getAbsCoor ().z);
[3559]133  /* rotate */
[4998]134  Vector tmpRot = this->getAbsDir().getSpacialAxis();
135  glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
[3559]136
[4136]137  if (this->objectList)
138    glCallList(this->objectList);
139  else if (this->model)
140    this->model->draw();
[5465]141  if (this->vegetation)
142    this->vegetation->draw();
[3559]143  glPopMatrix();
[4904]144
[4924]145  /* THIS IS ONLY FOR DEBUGGING INFORMATION */
[5308]146  if (this->ssp != NULL)
147    this->ssp->drawQuadtree();
[3559]148}
[3564]149
150
151void Terrain::buildDebugTerrain(DebugTerrain debugTerrain)
152{
153  // if the terrain is the Terrain of Dave
154  if (debugTerrain == TERRAIN_DAVE)
155    {
156      objectList = glGenLists(1);
157      glNewList (objectList, GL_COMPILE);
[4597]158
[3564]159      glColor3f(1.0,0,0);
[4597]160
[3564]161      int sizeX = 100;
162      int sizeZ = 80;
163      float length = 1000;
164      float width = 200;
165      float widthX = float (length /sizeX);
166      float widthZ = float (width /sizeZ);
[4597]167
[3564]168      float height [sizeX][sizeZ];
169      Vector normal_vectors[sizeX][sizeZ];
[4597]170
171
[3564]172      for ( int i = 0; i<sizeX-1; i+=1)
[4597]173        for (int j = 0; j<sizeZ-1;j+=1)
174          //height[i][j] = rand()/20046 + (j-25)*(j-25)/30;
[3564]175#ifdef __WIN32__
[4597]176          height[i][j]=(sin((float)j/3)*rand()*i/182400)*.5;
[3564]177#else
178      height[i][j]=(sin((float)j/3)*rand()*(long)i/6282450500.0)*.5;
179#endif
[4597]180
[3564]181      //Die Huegel ein wenig glaetten
182      for (int h=1; h<2;h++)
[4597]183        for (int i=1;i<sizeX-2 ;i+=1 )
184          for(int j=1;j<sizeZ-2;j+=1)
185            height[i][j]=(height[i+1][j]+height[i][j+1]+height[i-1][j]+height[i][j-1])/4;
186
[3564]187      //Berechnung von normalen Vektoren
188      for(int i=1;i<sizeX-2;i+=1)
[4597]189        for(int j=1;j<sizeZ-2 ;j+=1)
190          {
191            Vector v1 = Vector (widthX*(1),      height[i][j],      widthZ*(j) );
192            Vector v2 = Vector (widthX*(i-1),    height[i-1][j],    widthZ*(j));
193            Vector v3 = Vector (widthX*(i),      height[i][j+1],    widthZ*(j+1));
194            Vector v4 = Vector (widthX*(i+1),    height[i+1][j],    widthZ*(j));
195            Vector v5 = Vector (widthX*(i),      height[i][j-1],    widthZ*(j-1));
196
197            Vector c1 = v2 - v1;
198            Vector c2 = v3 - v1;
199            Vector c3=  v4 - v1;
200            Vector c4 = v5 - v1;
201            Vector zero = Vector (0,0,0);
202            normal_vectors[i][j]=c1.cross(v3-v5)+c2.cross(v4-v2)+c3.cross(v5-v3)+c4.cross(v2-v4);
203            normal_vectors[i][j].normalize();
204          }
205
[3564]206      glBegin(GL_QUADS);
207      int snowheight=3;
208      for ( int i = 0; i<sizeX; i+=1)
[4597]209        for (int j = 0; j<sizeZ;j+=1)
210          {
211            Vector v1 = Vector (widthX*(i),      height[i][j]-20,       widthZ*(j)  -width/2);
212            Vector v2 = Vector (widthX*(i+1),    height[i+1][j]-20,     widthZ*(j)  -width/2);
213            Vector v3 = Vector (widthX*(i+1),    height[i+1][j+1]-20,   widthZ*(j+1)-width/2);
214            Vector v4 = Vector (widthX*(i),      height[i][j+1]-20,     widthZ*(j+1)-width/2);
215            float a[3];
216            if(height[i][j]<snowheight){
217              a[0]=0;
218              a[1]=1.0-height[i][j]/10-.3;
219              a[2]=0;
220              glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
221            }
222            else{
223              a[0]=1.0;
224              a[1]=1.0;
225              a[2]=1.0;
226              glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
227
228            }
229            glNormal3f(normal_vectors[i][j].x, normal_vectors[i][j].y, normal_vectors[i][j].z);
230            glVertex3f(v1.x, v1.y, v1.z);
231            if(height[i+1][j]<snowheight){
232              a[0]=0;
233              a[1] =1.0-height[i+1][j]/10-.3;
234              a[2]=0;
235              glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
236            }
237            else{
238              a[0]=1.0;
239              a[1]=1.0;
240              a[2]=1.0;
241              glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
242
243            }
244            glNormal3f(normal_vectors[i+1][j].x, normal_vectors[i+1][j].y, normal_vectors[i+1][j].z);
245            glVertex3f(v2.x, v2.y, v2.z);
246            if(height[i+1][j+1]<snowheight){
247              a[0]=0;
248              a[1] =1.0-height[i+1][j+1]/10-.3;
249              a[2]=0;
250              glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
251            }
252            else{
253              a[0]=1.0;
254              a[1]=1.0;
255              a[2]=1.0;
256              glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
257
258
259            }
260            glNormal3f(normal_vectors[i+1][j+1].x, normal_vectors[i+1][j+1].y, normal_vectors[i+1][j+1].z);
261            glVertex3f(v3.x, v3.y, v3.z);
262            if(height[i][j+1]<snowheight){
263              a[0]=0;
264              a[1] =1.0-height[i+1][j+1]/10-.3;
265              a[2]=0;
266              glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
267            }
268            else{
269              a[0]=1.0;
270              a[1]=1.0;
271              a[2]=1.0;
272              glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
273            }
274            glNormal3f(normal_vectors[i][j+1].x, normal_vectors[i][j+1].y, normal_vectors[i][j+1].z);
275            glVertex3f(v4.x, v4.y, v4.z);
276
277          }
[3564]278      glEnd();
279      glEndList();
280    }
281
282  if (debugTerrain == TERRAIN_BENSCH)
283    {
284      /*
[4597]285        this->model = (OBJModel*) new Model();
[3564]286      this->model->setName("CUBE");
287      this->model->addVertex (-0.5, -0.5, 0.5);
288      this->model->addVertex (0.5, -0.5, 0.5);
289      this->model->addVertex (-0.5, 0.5, 0.5);
290      this->model->addVertex (0.5, 0.5, 0.5);
291      this->model->addVertex (-0.5, 0.5, -0.5);
292      this->model->addVertex (0.5, 0.5, -0.5);
293      this->model->addVertex (-0.5, -0.5, -0.5);
294      this->model->addVertex (0.5, -0.5, -0.5);
[4597]295
[3564]296      this->model->addVertexTexture (0.0, 0.0);
297      this->model->addVertexTexture (1.0, 0.0);
298      this->model->addVertexTexture (0.0, 1.0);
299      this->model->addVertexTexture (1.0, 1.0);
300      this->model->addVertexTexture (0.0, 2.0);
301      this->model->addVertexTexture (1.0, 2.0);
302      this->model->addVertexTexture (0.0, 3.0);
303      this->model->addVertexTexture (1.0, 3.0);
304      this->model->addVertexTexture (0.0, 4.0);
305      this->model->addVertexTexture (1.0, 4.0);
306      this->model->addVertexTexture (2.0, 0.0);
307      this->model->addVertexTexture (2.0, 1.0);
308      this->model->addVertexTexture (-1.0, 0.0);
309      this->model->addVertexTexture (-1.0, 1.0);
310
311      this->model->finalize();
312      */
313    }
314}
Note: See TracBrowser for help on using the repository browser.