Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: some minor cleanup, of the mess i made with AutoMake-sh

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