Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6451 was 6329, checked in by bottac, 19 years ago

Fixed some bugs.

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/ground1.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(43.0f,1.10f,43.0f));
70 hm->shift(Vector(-1000.0,-1500.0,-1000.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 hm->draw();
238 /* if (this->objectList)
239    glCallList(this->objectList);
240  else if (this->model)
241    this->model->draw();
242  if (this->vegetation)
243    this->vegetation->draw();
244*/
245  glPopMatrix();
246
247  /* THIS IS ONLY FOR DEBUGGING INFORMATION */
248 // if (this->ssp != NULL)
249 //   this->ssp->drawQuadtree();
250}
251
252
253void Terrain::buildDebugTerrain(DebugTerrain debugTerrain)
254{
255  // if the terrain is the Terrain of Dave
256  if (debugTerrain == TERRAIN_DAVE)
257    {
258      objectList = glGenLists(1);
259      glNewList (objectList, GL_COMPILE);
260
261      glColor3f(1.0,0,0);
262
263      int sizeX = 100;
264      int sizeZ = 80;
265      float length = 1000;
266      float width = 200;
267      float widthX = float (length /sizeX);
268      float widthZ = float (width /sizeZ);
269
270      float height [sizeX][sizeZ];
271      Vector normal_vectors[sizeX][sizeZ];
272
273
274      for ( int i = 0; i<sizeX-1; i+=1)
275        for (int j = 0; j<sizeZ-1;j+=1)
276          //height[i][j] = rand()/20046 + (j-25)*(j-25)/30;
277#ifdef __WIN32__
278          height[i][j]=(sin((float)j/3)*rand()*i/182400)*.5;
279#else
280      height[i][j]=(sin((float)j/3)*rand()*(long)i/6282450500.0)*.5;
281#endif
282
283      //Die Huegel ein wenig glaetten
284      for (int h=1; h<2;h++)
285        for (int i=1;i<sizeX-2 ;i+=1 )
286          for(int j=1;j<sizeZ-2;j+=1)
287            height[i][j]=(height[i+1][j]+height[i][j+1]+height[i-1][j]+height[i][j-1])/4;
288
289      //Berechnung von normalen Vektoren
290      for(int i=1;i<sizeX-2;i+=1)
291        for(int j=1;j<sizeZ-2 ;j+=1)
292          {
293            Vector v1 = Vector (widthX*(1),      height[i][j],      widthZ*(j) );
294            Vector v2 = Vector (widthX*(i-1),    height[i-1][j],    widthZ*(j));
295            Vector v3 = Vector (widthX*(i),      height[i][j+1],    widthZ*(j+1));
296            Vector v4 = Vector (widthX*(i+1),    height[i+1][j],    widthZ*(j));
297            Vector v5 = Vector (widthX*(i),      height[i][j-1],    widthZ*(j-1));
298
299            Vector c1 = v2 - v1;
300            Vector c2 = v3 - v1;
301            Vector c3=  v4 - v1;
302            Vector c4 = v5 - v1;
303            Vector zero = Vector (0,0,0);
304            normal_vectors[i][j]=c1.cross(v3-v5)+c2.cross(v4-v2)+c3.cross(v5-v3)+c4.cross(v2-v4);
305            normal_vectors[i][j].normalize();
306          }
307
308      glBegin(GL_QUADS);
309      int snowheight=3;
310      for ( int i = 0; i<sizeX; i+=1)
311        for (int j = 0; j<sizeZ;j+=1)
312          {
313            Vector v1 = Vector (widthX*(i),      height[i][j]-20,       widthZ*(j)  -width/2);
314            Vector v2 = Vector (widthX*(i+1),    height[i+1][j]-20,     widthZ*(j)  -width/2);
315            Vector v3 = Vector (widthX*(i+1),    height[i+1][j+1]-20,   widthZ*(j+1)-width/2);
316            Vector v4 = Vector (widthX*(i),      height[i][j+1]-20,     widthZ*(j+1)-width/2);
317            float a[3];
318            if(height[i][j]<snowheight){
319              a[0]=0;
320              a[1]=1.0-height[i][j]/10-.3;
321              a[2]=0;
322              glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
323            }
324            else{
325              a[0]=1.0;
326              a[1]=1.0;
327              a[2]=1.0;
328              glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
329
330            }
331            glNormal3f(normal_vectors[i][j].x, normal_vectors[i][j].y, normal_vectors[i][j].z);
332            glVertex3f(v1.x, v1.y, v1.z);
333            if(height[i+1][j]<snowheight){
334              a[0]=0;
335              a[1] =1.0-height[i+1][j]/10-.3;
336              a[2]=0;
337              glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
338            }
339            else{
340              a[0]=1.0;
341              a[1]=1.0;
342              a[2]=1.0;
343              glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
344
345            }
346            glNormal3f(normal_vectors[i+1][j].x, normal_vectors[i+1][j].y, normal_vectors[i+1][j].z);
347            glVertex3f(v2.x, v2.y, v2.z);
348            if(height[i+1][j+1]<snowheight){
349              a[0]=0;
350              a[1] =1.0-height[i+1][j+1]/10-.3;
351              a[2]=0;
352              glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
353            }
354            else{
355              a[0]=1.0;
356              a[1]=1.0;
357              a[2]=1.0;
358              glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
359
360
361            }
362            glNormal3f(normal_vectors[i+1][j+1].x, normal_vectors[i+1][j+1].y, normal_vectors[i+1][j+1].z);
363            glVertex3f(v3.x, v3.y, v3.z);
364            if(height[i][j+1]<snowheight){
365              a[0]=0;
366              a[1] =1.0-height[i+1][j+1]/10-.3;
367              a[2]=0;
368              glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
369            }
370            else{
371              a[0]=1.0;
372              a[1]=1.0;
373              a[2]=1.0;
374              glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
375            }
376            glNormal3f(normal_vectors[i][j+1].x, normal_vectors[i][j+1].y, normal_vectors[i][j+1].z);
377            glVertex3f(v4.x, v4.y, v4.z);
378
379          }
380      glEnd();
381      glEndList();
382    }
383
384  if (debugTerrain == TERRAIN_BENSCH)
385    {
386      /*
387        this->model = (OBJModel*) new Model();
388      this->model->setName("CUBE");
389      this->model->addVertex (-0.5, -0.5, 0.5);
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
398      this->model->addVertexTexture (0.0, 0.0);
399      this->model->addVertexTexture (1.0, 0.0);
400      this->model->addVertexTexture (0.0, 1.0);
401      this->model->addVertexTexture (1.0, 1.0);
402      this->model->addVertexTexture (0.0, 2.0);
403      this->model->addVertexTexture (1.0, 2.0);
404      this->model->addVertexTexture (0.0, 3.0);
405      this->model->addVertexTexture (1.0, 3.0);
406      this->model->addVertexTexture (0.0, 4.0);
407      this->model->addVertexTexture (1.0, 4.0);
408      this->model->addVertexTexture (2.0, 0.0);
409      this->model->addVertexTexture (2.0, 1.0);
410      this->model->addVertexTexture (-1.0, 0.0);
411      this->model->addVertexTexture (-1.0, 1.0);
412
413      this->model->finalize();
414      */
415    }
416}
Note: See TracBrowser for help on using the repository browser.