Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: merge the ObjectManager to the trunk
merged with command:
svn merge -r6082:HEAD objectmanager/ ../trunk/

conflicts resolution was easy this time :)
but specially merged the world to network_world

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