Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/height_map/src/world_entities/terrain.cc @ 5976

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