Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6675 was 6529, checked in by bottac, 19 years ago

Some bugs fixed

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