Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6227 was 6227, checked in by bottac, 18 years ago

getHeight implemented
support for 'colour maps'

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