Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/height_map_merge/src/world_entities/terrain.cc @ 6603

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

Improved texturemapping

File size: 12.3 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"
[5511]25#include "model.h"
[6341]26#include "network_game_manager.h"
[5465]27
[6495]28#include "height_map.h"
29#include "material.h"
[6341]30
[5511]31#include "glincl.h"
32
[1856]33using namespace std;
[1853]34
[5750]35CREATE_FACTORY(Terrain, CL_TERRAIN);
[1856]36
[3245]37/**
[4836]38 *  standard constructor
[5357]39 */
[4607]40Terrain::Terrain (const TiXmlElement* root)
[3365]41{
[3564]42  this->init();
[4607]43  this->loadParams(root);
[4844]44
[6579]45   this->heightMapMaterial = new Material(); 
46   heightMapMaterial->setTransparency(1.0);
47   heightMapMaterial->setIllum(0.2);
48
49   heightMapMaterial->setDiffuse(1.0,1.0,1.0);
50   heightMapMaterial->setAmbient(1.0,1.0,1.0 );
51   heightMapMaterial->setSpecular(1.0,1.0,1.0);
52   heightMapMaterial->setShininess(.5);
53   heightMapMaterial->setTransparency(1.0);
54
55   heightMapMaterial->diffuseTexture = NULL;
56   heightMapMaterial->ambientTexture = NULL;
57   heightMapMaterial->specularTexture = NULL;
58 
59   const  char* texture_name = "pictures/ground.tga";
60   heightMapMaterial->setDiffuseMap(texture_name);
61   heightMapMaterial->setAmbientMap(texture_name);
62   heightMapMaterial->setSpecularMap(texture_name);     
63   
[5308]64//  if (this->model != NULL)
[6022]65    //this->ssp = new SpatialSeparation((Model*)this->model, 10.0f);
[3365]66}
[1853]67
[4855]68
[3564]69/**
[4836]70 *  Constructor for loading a Terrain out of a file
71 * @param fileName The file to load data from.
[4597]72
[3566]73   this either loads out of an OBJ-file, or loads a heightmap if no .obj-extension is found.
74*/
[4597]75Terrain::Terrain(const char* fileName)
[3566]76{
77  this->init();
78
[5415]79  if (!strstr(fileName, ".obj") || !strstr(fileName, ".OBJ") )
[3566]80    {
[5308]81      this->loadModel(fileName);
[3566]82    }
83  else
84    {
85      // load the hightMap here.
86    }
87}
88
89/**
[4836]90 *  a Constructor for the Debug-Worlds
[5308]91 */
[3564]92Terrain::Terrain(DebugTerrain debugTerrain)
93{
94  this->init();
95  this->buildDebugTerrain(debugTerrain);
96}
97
[3245]98/**
[4836]99 *  standard deconstructor
[1853]100
[3245]101*/
[4746]102Terrain::~Terrain ()
[3543]103{
[3564]104  if (objectList)
105    glDeleteLists(this->objectList, 1);
[4889]106  if( this->ssp)
107    delete ssp;
[5465]108  if (this->vegetation)
109  {
110    ResourceManager::getInstance()->unload(this->vegetation);
111  }
[3543]112}
[1853]113
[3245]114
[4746]115void Terrain::init()
[3559]116{
[4320]117  this->setClassID(CL_TERRAIN, "Terrain");
[6142]118  this->toList(OM_ENVIRON_NOTICK);
[4597]119
[4136]120  this->objectList = 0;
[5308]121  this->ssp = NULL;
[5465]122  this->vegetation = NULL;
[6495]123
124  this->heightMap = NULL;
125  this->heightMapMaterial = NULL;
[3559]126}
127
[4924]128
[4607]129void Terrain::loadParams(const TiXmlElement* root)
130{
[6495]131  WorldEntity::loadParams(root);
[3559]132
[5671]133  LoadParam(root, "vegetation", this, Terrain, loadVegetation)
[5465]134      .describe("the fileName of the vegetation, that should be loaded onto this terrain. (must be relative to the data-dir)") ;
[6495]135
136
137  LoadParam(root, "height-map", this, Terrain, loadHeightMap)
138      .describe("The HeightMap, splitted into two strings seperated by ','. 1: HeighMap, 2: ColorMap");
139
140
141  LoadParam(root, "heigt-texture", this, Terrain, loadTexture)
142      .describe("The name of the Texture for this heightMap");
[4607]143}
144
[6495]145void Terrain::loadHeightMap(const char* heightMapFile, const char* colorMap)
146{
147  if (this->heightMap != NULL)
148    delete this->heightMap;
149  this->heightMap = NULL;
150
[6496]151  char* hmName = ResourceManager::getFullName(heightMapFile);
152  char* hmColorName = ResourceManager::getFullName(colorMap);
[6495]153
154
[6496]155  this->heightMap = new HeightMap(hmName, hmColorName);
[6579]156  heightMap->scale(Vector(23.0f,3.5f,23.0f));
157  heightMap->shift(Vector(-1000.0,-90.0,-4000.0));
158  heightMap->load();
[6496]159  delete[] hmName;
160  delete[] hmColorName;
161
[6495]162}
163
164
165void Terrain::loadTexture(const char* textureName)
166{
167  if (this->heightMapMaterial != NULL)
168    delete this->heightMapMaterial;
[6603]169
170
171   this->heightMapMaterial = new Material(); 
172   heightMapMaterial->setTransparency(1.0);
173   heightMapMaterial->setIllum(0.2);
174
175   heightMapMaterial->setDiffuse(1.0,1.0,1.0);
176   heightMapMaterial->setAmbient(1.0,1.0,1.0 );
177   heightMapMaterial->setSpecular(1.0,1.0,1.0);
178   heightMapMaterial->setShininess(.5);
179   heightMapMaterial->setTransparency(1.0);
180
181   heightMapMaterial->diffuseTexture = NULL;
182   heightMapMaterial->ambientTexture = NULL;
183   heightMapMaterial->specularTexture = NULL;
184 
185   
186   heightMapMaterial->setDiffuseMap(textureName);
187   heightMapMaterial->setAmbientMap(textureName);
188   heightMapMaterial->setSpecularMap(textureName);     
189 
190
191
[6495]192}
193
194
195
[5465]196void Terrain::loadVegetation(const char* vegetationFile)
197{
[6341]198  PRINTF(0)("loadVegetation: %s\n", vegetationFile);
[5465]199  if (this->vegetation)
200    ResourceManager::getInstance()->unload(this->vegetation, RP_LEVEL);
201  if (vegetationFile != NULL)
202  {
203    PRINTF(4)("fetching %s\n", vegetationFile);
[6341]204    this->vegetation = (Model*)ResourceManager::getInstance()->load(vegetationFile, OBJ, RP_CAMPAIGN);
[5465]205  }
206  else
207    this->vegetation = NULL;
208}
209
210
211
[6495]212
213
[5500]214void Terrain::draw () const
[4597]215{
[3559]216  glMatrixMode(GL_MODELVIEW);
217  glPushMatrix();
[4597]218
[3559]219  /* translate */
[4597]220  glTranslatef (this->getAbsCoor ().x,
221                this->getAbsCoor ().y,
222                this->getAbsCoor ().z);
[3559]223  /* rotate */
[4998]224  Vector tmpRot = this->getAbsDir().getSpacialAxis();
225  glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
[3559]226
[4136]227  if (this->objectList)
228    glCallList(this->objectList);
[5994]229  else if (this->getModel())
230    this->getModel()->draw();
[5465]231  if (this->vegetation)
232    this->vegetation->draw();
[6496]233
[6579]234  if(this->heightMap)
235        {
236        this->heightMapMaterial->select();
[6603]237        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
238        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
[6579]239   
[6603]240         glEnable(GL_LIGHTING);
[6579]241        glColorMaterial ( GL_FRONT_AND_BACK, GL_EMISSION ) ;
242        glEnable (GL_COLOR_MATERIAL) ;
243        this->heightMap->draw();
244        }
[3559]245  glPopMatrix();
[4904]246
[6496]247
248
[4924]249  /* THIS IS ONLY FOR DEBUGGING INFORMATION */
[5308]250  if (this->ssp != NULL)
251    this->ssp->drawQuadtree();
[3559]252}
[3564]253
254
255void Terrain::buildDebugTerrain(DebugTerrain debugTerrain)
256{
257  // if the terrain is the Terrain of Dave
258  if (debugTerrain == TERRAIN_DAVE)
259    {
260      objectList = glGenLists(1);
261      glNewList (objectList, GL_COMPILE);
[4597]262
[3564]263      glColor3f(1.0,0,0);
[4597]264
[3564]265      int sizeX = 100;
266      int sizeZ = 80;
267      float length = 1000;
268      float width = 200;
269      float widthX = float (length /sizeX);
270      float widthZ = float (width /sizeZ);
[4597]271
[3564]272      float height [sizeX][sizeZ];
273      Vector normal_vectors[sizeX][sizeZ];
[4597]274
275
[3564]276      for ( int i = 0; i<sizeX-1; i+=1)
[4597]277        for (int j = 0; j<sizeZ-1;j+=1)
278          //height[i][j] = rand()/20046 + (j-25)*(j-25)/30;
[3564]279#ifdef __WIN32__
[4597]280          height[i][j]=(sin((float)j/3)*rand()*i/182400)*.5;
[3564]281#else
282      height[i][j]=(sin((float)j/3)*rand()*(long)i/6282450500.0)*.5;
283#endif
[4597]284
[3564]285      //Die Huegel ein wenig glaetten
286      for (int h=1; h<2;h++)
[4597]287        for (int i=1;i<sizeX-2 ;i+=1 )
288          for(int j=1;j<sizeZ-2;j+=1)
289            height[i][j]=(height[i+1][j]+height[i][j+1]+height[i-1][j]+height[i][j-1])/4;
290
[3564]291      //Berechnung von normalen Vektoren
292      for(int i=1;i<sizeX-2;i+=1)
[4597]293        for(int j=1;j<sizeZ-2 ;j+=1)
294          {
295            Vector v1 = Vector (widthX*(1),      height[i][j],      widthZ*(j) );
296            Vector v2 = Vector (widthX*(i-1),    height[i-1][j],    widthZ*(j));
297            Vector v3 = Vector (widthX*(i),      height[i][j+1],    widthZ*(j+1));
298            Vector v4 = Vector (widthX*(i+1),    height[i+1][j],    widthZ*(j));
299            Vector v5 = Vector (widthX*(i),      height[i][j-1],    widthZ*(j-1));
300
301            Vector c1 = v2 - v1;
302            Vector c2 = v3 - v1;
303            Vector c3=  v4 - v1;
304            Vector c4 = v5 - v1;
305            Vector zero = Vector (0,0,0);
306            normal_vectors[i][j]=c1.cross(v3-v5)+c2.cross(v4-v2)+c3.cross(v5-v3)+c4.cross(v2-v4);
307            normal_vectors[i][j].normalize();
308          }
309
[3564]310      glBegin(GL_QUADS);
311      int snowheight=3;
312      for ( int i = 0; i<sizeX; i+=1)
[4597]313        for (int j = 0; j<sizeZ;j+=1)
314          {
315            Vector v1 = Vector (widthX*(i),      height[i][j]-20,       widthZ*(j)  -width/2);
316            Vector v2 = Vector (widthX*(i+1),    height[i+1][j]-20,     widthZ*(j)  -width/2);
317            Vector v3 = Vector (widthX*(i+1),    height[i+1][j+1]-20,   widthZ*(j+1)-width/2);
318            Vector v4 = Vector (widthX*(i),      height[i][j+1]-20,     widthZ*(j+1)-width/2);
319            float a[3];
320            if(height[i][j]<snowheight){
321              a[0]=0;
322              a[1]=1.0-height[i][j]/10-.3;
323              a[2]=0;
324              glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
325            }
326            else{
327              a[0]=1.0;
328              a[1]=1.0;
329              a[2]=1.0;
330              glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
331
332            }
333            glNormal3f(normal_vectors[i][j].x, normal_vectors[i][j].y, normal_vectors[i][j].z);
334            glVertex3f(v1.x, v1.y, v1.z);
335            if(height[i+1][j]<snowheight){
336              a[0]=0;
337              a[1] =1.0-height[i+1][j]/10-.3;
338              a[2]=0;
339              glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
340            }
341            else{
342              a[0]=1.0;
343              a[1]=1.0;
344              a[2]=1.0;
345              glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
346
347            }
348            glNormal3f(normal_vectors[i+1][j].x, normal_vectors[i+1][j].y, normal_vectors[i+1][j].z);
349            glVertex3f(v2.x, v2.y, v2.z);
350            if(height[i+1][j+1]<snowheight){
351              a[0]=0;
352              a[1] =1.0-height[i+1][j+1]/10-.3;
353              a[2]=0;
354              glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
355            }
356            else{
357              a[0]=1.0;
358              a[1]=1.0;
359              a[2]=1.0;
360              glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
361
362
363            }
364            glNormal3f(normal_vectors[i+1][j+1].x, normal_vectors[i+1][j+1].y, normal_vectors[i+1][j+1].z);
365            glVertex3f(v3.x, v3.y, v3.z);
366            if(height[i][j+1]<snowheight){
367              a[0]=0;
368              a[1] =1.0-height[i+1][j+1]/10-.3;
369              a[2]=0;
370              glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
371            }
372            else{
373              a[0]=1.0;
374              a[1]=1.0;
375              a[2]=1.0;
376              glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
377            }
378            glNormal3f(normal_vectors[i][j+1].x, normal_vectors[i][j+1].y, normal_vectors[i][j+1].z);
379            glVertex3f(v4.x, v4.y, v4.z);
380
381          }
[3564]382      glEnd();
383      glEndList();
384    }
385
386  if (debugTerrain == TERRAIN_BENSCH)
387    {
388      /*
[4597]389        this->model = (OBJModel*) new Model();
[3564]390      this->model->setName("CUBE");
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      this->model->addVertex (0.5, -0.5, -0.5);
[4597]399
[3564]400      this->model->addVertexTexture (0.0, 0.0);
401      this->model->addVertexTexture (1.0, 0.0);
402      this->model->addVertexTexture (0.0, 1.0);
403      this->model->addVertexTexture (1.0, 1.0);
404      this->model->addVertexTexture (0.0, 2.0);
405      this->model->addVertexTexture (1.0, 2.0);
406      this->model->addVertexTexture (0.0, 3.0);
407      this->model->addVertexTexture (1.0, 3.0);
408      this->model->addVertexTexture (0.0, 4.0);
409      this->model->addVertexTexture (1.0, 4.0);
410      this->model->addVertexTexture (2.0, 0.0);
411      this->model->addVertexTexture (2.0, 1.0);
412      this->model->addVertexTexture (-1.0, 0.0);
413      this->model->addVertexTexture (-1.0, 1.0);
414
415      this->model->finalize();
416      */
417    }
418}
[6341]419
420int Terrain::writeBytes( const byte * data, int length, int sender )
421{
422  setRequestedSync( false );
423  setIsOutOfSync( false );
424
425  SYNCHELP_READ_BEGIN();
426  SYNCHELP_READ_FKT( WorldEntity::writeState );
427
428  return SYNCHELP_READ_N;
429}
430
431int Terrain::readBytes( byte * data, int maxLength, int * reciever )
432{
433  if ( isOutOfSync() && !requestedSync() && this->getHostID()!=this->getOwner() )
434  {
435    (NetworkGameManager::getInstance())->sync( this->getUniqueID(), this->getOwner() );
436    setRequestedSync( true );
437  }
438
439  int rec = this->getRequestSync();
440  if ( rec > 0 )
441  {
442    *reciever = rec;
443
444    return WorldEntity::readState( data, maxLength );
445
446  }
447
448  *reciever = 0;
449  return 0;
450}
451
452void Terrain::writeDebug( ) const
453{
454}
455
456void Terrain::readDebug( ) const
457{
458}
Note: See TracBrowser for help on using the repository browser.