Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

changes in height_map.cc

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