Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Added Class Tile to HeightMap (allows a primitive LOD Implementation).

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