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
RevLine 
[4597]1/*
[1853]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.
[1855]10
11   ### File Specific:
[3565]12   main-programmer: Benjamin Grauer
[1855]13   co-programmer: ...
[1853]14*/
[5357]15#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY
[1853]16
17
[3559]18#include "terrain.h"
[4607]19
[5356]20#include "load_param.h"
[4607]21#include "factory.h"
[4842]22#include "spatial_separation.h"
23
[5465]24#include "resource_manager.h"
[6249]25#include "vertex_array_model.h"
[6103]26#include "material.h"
27#include "height_map.h"
[5511]28#include "glincl.h"
29
[6103]30
[1856]31using namespace std;
[6227]32HeightMap * hm;
[1853]33
[5750]34CREATE_FACTORY(Terrain, CL_TERRAIN);
[1856]35
[3245]36/**
[4836]37 *  standard constructor
[5357]38 */
[4607]39Terrain::Terrain (const TiXmlElement* root)
[3365]40{
[6103]41
[3564]42  this->init();
[4844]43
[6268]44  this->tmp_mat = new Material(); 
[6227]45  tmp_mat->setTransparency(1.0);
[6329]46  tmp_mat->setIllum(0.2);
[6227]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);
[6329]53  tmp_mat->setShininess(.5);
[6227]54  tmp_mat->setTransparency(1.0);
55
56  tmp_mat->diffuseTexture = NULL;
57  tmp_mat->ambientTexture = NULL;
[6329]58  tmp_mat->specularTexture = NULL;
[6268]59 
[6472]60    const  char* texture_name = "pictures/ground.tga";
[6329]61  tmp_mat->setDiffuseMap(texture_name);
62  tmp_mat->setAmbientMap(texture_name);
63   tmp_mat->setSpecularMap(texture_name); 
[6103]64  // this->loadParams(root);
65
66 char* heightmapName = ResourceManager::getFullName("pictures/heightmapHello.bmp");
[6227]67 char* colourmapName = ResourceManager::getFullName("pictures/heightmapHelloCM.bmp");
[6268]68 hm = new HeightMap(heightmapName, colourmapName);
[6472]69 hm->scale(Vector(23.0f,0.7f,23.0f));
70 hm->shift(Vector(-1000.0,-90.0,-4000.0));
[6268]71 hm->load();
72 this->model=hm;
[6103]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(); */
[6268]133   
[6103]134 /*if (this->model != NULL)
135    this->ssp = new SpatialSeparation((AbstractModel*)this->model, 10.0f);*/
[3365]136}
[1853]137
[4855]138
[3564]139/**
[4836]140 *  Constructor for loading a Terrain out of a file
141 * @param fileName The file to load data from.
[4597]142
[3566]143   this either loads out of an OBJ-file, or loads a heightmap if no .obj-extension is found.
144*/
[4597]145Terrain::Terrain(const char* fileName)
[3566]146{
147  this->init();
148
[5415]149  if (!strstr(fileName, ".obj") || !strstr(fileName, ".OBJ") )
[3566]150    {
[6103]151      this->model = NULL;
152        //this->loadModel(fileName);
[3566]153    }
154  else
155    {
156      // load the hightMap here.
157    }
158}
159
160/**
[4836]161 *  a Constructor for the Debug-Worlds
[5308]162 */
[3564]163Terrain::Terrain(DebugTerrain debugTerrain)
164{
165  this->init();
166  this->buildDebugTerrain(debugTerrain);
167}
168
[3245]169/**
[4836]170 *  standard deconstructor
[1853]171
[3245]172*/
[4746]173Terrain::~Terrain ()
[3543]174{
[3564]175  if (objectList)
176    glDeleteLists(this->objectList, 1);
[4889]177  if( this->ssp)
178    delete ssp;
[5465]179  if (this->vegetation)
180  {
181    ResourceManager::getInstance()->unload(this->vegetation);
182  }
[3543]183}
[1853]184
[3245]185
[4746]186void Terrain::init()
[3559]187{
[4320]188  this->setClassID(CL_TERRAIN, "Terrain");
[4597]189
[4136]190  this->objectList = 0;
[5308]191  this->ssp = NULL;
[5465]192  this->vegetation = NULL;
[3559]193}
194
[4924]195
[4607]196void Terrain::loadParams(const TiXmlElement* root)
197{
198  static_cast<WorldEntity*>(this)->loadParams(root);
[3559]199
[5671]200  LoadParam(root, "vegetation", this, Terrain, loadVegetation)
[5465]201      .describe("the fileName of the vegetation, that should be loaded onto this terrain. (must be relative to the data-dir)") ;
[4607]202}
203
[5465]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
[5500]219void Terrain::draw () const
[4597]220{
[3559]221  glMatrixMode(GL_MODELVIEW);
222  glPushMatrix();
[4597]223
[6329]224 
[3559]225  /* translate */
[6329]226 glTranslatef (this->getAbsCoor ().x ,
227               this->getAbsCoor ().y ,
228               this->getAbsCoor ().z)   ;
229 
[3559]230  /* rotate */
[6329]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();
[6472]237   
[6329]238 hm->draw();
[6227]239 /* if (this->objectList)
[4136]240    glCallList(this->objectList);
[6103]241  else if (this->model)
242    this->model->draw();
[5465]243  if (this->vegetation)
244    this->vegetation->draw();
[6227]245*/
[3559]246  glPopMatrix();
[4904]247
[4924]248  /* THIS IS ONLY FOR DEBUGGING INFORMATION */
[6103]249 // if (this->ssp != NULL)
250 //   this->ssp->drawQuadtree();
[3559]251}
[3564]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);
[4597]261
[3564]262      glColor3f(1.0,0,0);
[4597]263
[3564]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);
[4597]270
[3564]271      float height [sizeX][sizeZ];
272      Vector normal_vectors[sizeX][sizeZ];
[4597]273
274
[3564]275      for ( int i = 0; i<sizeX-1; i+=1)
[4597]276        for (int j = 0; j<sizeZ-1;j+=1)
277          //height[i][j] = rand()/20046 + (j-25)*(j-25)/30;
[3564]278#ifdef __WIN32__
[4597]279          height[i][j]=(sin((float)j/3)*rand()*i/182400)*.5;
[3564]280#else
281      height[i][j]=(sin((float)j/3)*rand()*(long)i/6282450500.0)*.5;
282#endif
[4597]283
[3564]284      //Die Huegel ein wenig glaetten
285      for (int h=1; h<2;h++)
[4597]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
[3564]290      //Berechnung von normalen Vektoren
291      for(int i=1;i<sizeX-2;i+=1)
[4597]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
[3564]309      glBegin(GL_QUADS);
310      int snowheight=3;
311      for ( int i = 0; i<sizeX; i+=1)
[4597]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          }
[3564]381      glEnd();
382      glEndList();
383    }
384
385  if (debugTerrain == TERRAIN_BENSCH)
386    {
387      /*
[4597]388        this->model = (OBJModel*) new Model();
[3564]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);
[4597]398
[3564]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.