Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/height_map1/src/world_entities/terrain.cc @ 5940

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