Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6177 was 6103, checked in by bensch, 19 years ago

heightmap: copied old terrain inside

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