Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/network/src/world_entities/terrain.cc @ 5577

Last change on this file since 5577 was 5500, checked in by bensch, 19 years ago

orxonox/trunk: all the WorldEntities now have a const function to draw themselves

File size: 8.8 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
26using namespace std;
27
28CREATE_FACTORY(Terrain);
29
30/**
31 *  standard constructor
32 */
33Terrain::Terrain (const TiXmlElement* root)
34{
35  this->init();
36  this->loadParams(root);
37
38//  if (this->model != NULL)
39    //this->ssp = new SpatialSeparation((AbstractModel*)this->model, 10.0f);
40}
41
42
43/**
44 *  Constructor for loading a Terrain out of a file
45 * @param fileName The file to load data from.
46
47   this either loads out of an OBJ-file, or loads a heightmap if no .obj-extension is found.
48*/
49Terrain::Terrain(const char* fileName)
50{
51  this->init();
52
53  if (!strstr(fileName, ".obj") || !strstr(fileName, ".OBJ") )
54    {
55      this->loadModel(fileName);
56    }
57  else
58    {
59      // load the hightMap here.
60    }
61}
62
63/**
64 *  a Constructor for the Debug-Worlds
65 */
66Terrain::Terrain(DebugTerrain debugTerrain)
67{
68  this->init();
69  this->buildDebugTerrain(debugTerrain);
70}
71
72/**
73 *  standard deconstructor
74
75*/
76Terrain::~Terrain ()
77{
78  if (objectList)
79    glDeleteLists(this->objectList, 1);
80  if( this->ssp)
81    delete ssp;
82  if (this->vegetation)
83  {
84    ResourceManager::getInstance()->unload(this->vegetation);
85  }
86}
87
88
89void Terrain::init()
90{
91  this->setClassID(CL_TERRAIN, "Terrain");
92
93  this->objectList = 0;
94  this->ssp = NULL;
95  this->vegetation = NULL;
96}
97
98
99void Terrain::loadParams(const TiXmlElement* root)
100{
101  static_cast<WorldEntity*>(this)->loadParams(root);
102
103  LoadParam<Terrain>(root, "vegetation", this, &Terrain::loadVegetation)
104      .describe("the fileName of the vegetation, that should be loaded onto this terrain. (must be relative to the data-dir)") ;
105
106  //LoadParam<Terrain>(root, "DebugTerrain",  );
107}
108
109void Terrain::loadVegetation(const char* vegetationFile)
110{
111  if (this->vegetation)
112    ResourceManager::getInstance()->unload(this->vegetation, RP_LEVEL);
113  if (vegetationFile != NULL)
114  {
115    PRINTF(4)("fetching %s\n", vegetationFile);
116      this->vegetation = (Model*)ResourceManager::getInstance()->load(vegetationFile, OBJ, RP_CAMPAIGN);
117  }
118  else
119    this->vegetation = NULL;
120}
121
122
123
124void Terrain::draw () const
125{
126  glMatrixMode(GL_MODELVIEW);
127  glPushMatrix();
128
129  /* translate */
130  glTranslatef (this->getAbsCoor ().x,
131                this->getAbsCoor ().y,
132                this->getAbsCoor ().z);
133  /* rotate */
134  Vector tmpRot = this->getAbsDir().getSpacialAxis();
135  glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
136
137  if (this->objectList)
138    glCallList(this->objectList);
139  else if (this->model)
140    this->model->draw();
141  if (this->vegetation)
142    this->vegetation->draw();
143  glPopMatrix();
144
145  /* THIS IS ONLY FOR DEBUGGING INFORMATION */
146  if (this->ssp != NULL)
147    this->ssp->drawQuadtree();
148}
149
150
151void Terrain::buildDebugTerrain(DebugTerrain debugTerrain)
152{
153  // if the terrain is the Terrain of Dave
154  if (debugTerrain == TERRAIN_DAVE)
155    {
156      objectList = glGenLists(1);
157      glNewList (objectList, GL_COMPILE);
158
159      glColor3f(1.0,0,0);
160
161      int sizeX = 100;
162      int sizeZ = 80;
163      float length = 1000;
164      float width = 200;
165      float widthX = float (length /sizeX);
166      float widthZ = float (width /sizeZ);
167
168      float height [sizeX][sizeZ];
169      Vector normal_vectors[sizeX][sizeZ];
170
171
172      for ( int i = 0; i<sizeX-1; i+=1)
173        for (int j = 0; j<sizeZ-1;j+=1)
174          //height[i][j] = rand()/20046 + (j-25)*(j-25)/30;
175#ifdef __WIN32__
176          height[i][j]=(sin((float)j/3)*rand()*i/182400)*.5;
177#else
178      height[i][j]=(sin((float)j/3)*rand()*(long)i/6282450500.0)*.5;
179#endif
180
181      //Die Huegel ein wenig glaetten
182      for (int h=1; h<2;h++)
183        for (int i=1;i<sizeX-2 ;i+=1 )
184          for(int j=1;j<sizeZ-2;j+=1)
185            height[i][j]=(height[i+1][j]+height[i][j+1]+height[i-1][j]+height[i][j-1])/4;
186
187      //Berechnung von normalen Vektoren
188      for(int i=1;i<sizeX-2;i+=1)
189        for(int j=1;j<sizeZ-2 ;j+=1)
190          {
191            Vector v1 = Vector (widthX*(1),      height[i][j],      widthZ*(j) );
192            Vector v2 = Vector (widthX*(i-1),    height[i-1][j],    widthZ*(j));
193            Vector v3 = Vector (widthX*(i),      height[i][j+1],    widthZ*(j+1));
194            Vector v4 = Vector (widthX*(i+1),    height[i+1][j],    widthZ*(j));
195            Vector v5 = Vector (widthX*(i),      height[i][j-1],    widthZ*(j-1));
196
197            Vector c1 = v2 - v1;
198            Vector c2 = v3 - v1;
199            Vector c3=  v4 - v1;
200            Vector c4 = v5 - v1;
201            Vector zero = Vector (0,0,0);
202            normal_vectors[i][j]=c1.cross(v3-v5)+c2.cross(v4-v2)+c3.cross(v5-v3)+c4.cross(v2-v4);
203            normal_vectors[i][j].normalize();
204          }
205
206      glBegin(GL_QUADS);
207      int snowheight=3;
208      for ( int i = 0; i<sizeX; i+=1)
209        for (int j = 0; j<sizeZ;j+=1)
210          {
211            Vector v1 = Vector (widthX*(i),      height[i][j]-20,       widthZ*(j)  -width/2);
212            Vector v2 = Vector (widthX*(i+1),    height[i+1][j]-20,     widthZ*(j)  -width/2);
213            Vector v3 = Vector (widthX*(i+1),    height[i+1][j+1]-20,   widthZ*(j+1)-width/2);
214            Vector v4 = Vector (widthX*(i),      height[i][j+1]-20,     widthZ*(j+1)-width/2);
215            float a[3];
216            if(height[i][j]<snowheight){
217              a[0]=0;
218              a[1]=1.0-height[i][j]/10-.3;
219              a[2]=0;
220              glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
221            }
222            else{
223              a[0]=1.0;
224              a[1]=1.0;
225              a[2]=1.0;
226              glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
227
228            }
229            glNormal3f(normal_vectors[i][j].x, normal_vectors[i][j].y, normal_vectors[i][j].z);
230            glVertex3f(v1.x, v1.y, v1.z);
231            if(height[i+1][j]<snowheight){
232              a[0]=0;
233              a[1] =1.0-height[i+1][j]/10-.3;
234              a[2]=0;
235              glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
236            }
237            else{
238              a[0]=1.0;
239              a[1]=1.0;
240              a[2]=1.0;
241              glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
242
243            }
244            glNormal3f(normal_vectors[i+1][j].x, normal_vectors[i+1][j].y, normal_vectors[i+1][j].z);
245            glVertex3f(v2.x, v2.y, v2.z);
246            if(height[i+1][j+1]<snowheight){
247              a[0]=0;
248              a[1] =1.0-height[i+1][j+1]/10-.3;
249              a[2]=0;
250              glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
251            }
252            else{
253              a[0]=1.0;
254              a[1]=1.0;
255              a[2]=1.0;
256              glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
257
258
259            }
260            glNormal3f(normal_vectors[i+1][j+1].x, normal_vectors[i+1][j+1].y, normal_vectors[i+1][j+1].z);
261            glVertex3f(v3.x, v3.y, v3.z);
262            if(height[i][j+1]<snowheight){
263              a[0]=0;
264              a[1] =1.0-height[i+1][j+1]/10-.3;
265              a[2]=0;
266              glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
267            }
268            else{
269              a[0]=1.0;
270              a[1]=1.0;
271              a[2]=1.0;
272              glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
273            }
274            glNormal3f(normal_vectors[i][j+1].x, normal_vectors[i][j+1].y, normal_vectors[i][j+1].z);
275            glVertex3f(v4.x, v4.y, v4.z);
276
277          }
278      glEnd();
279      glEndList();
280    }
281
282  if (debugTerrain == TERRAIN_BENSCH)
283    {
284      /*
285        this->model = (OBJModel*) new Model();
286      this->model->setName("CUBE");
287      this->model->addVertex (-0.5, -0.5, 0.5);
288      this->model->addVertex (0.5, -0.5, 0.5);
289      this->model->addVertex (-0.5, 0.5, 0.5);
290      this->model->addVertex (0.5, 0.5, 0.5);
291      this->model->addVertex (-0.5, 0.5, -0.5);
292      this->model->addVertex (0.5, 0.5, -0.5);
293      this->model->addVertex (-0.5, -0.5, -0.5);
294      this->model->addVertex (0.5, -0.5, -0.5);
295
296      this->model->addVertexTexture (0.0, 0.0);
297      this->model->addVertexTexture (1.0, 0.0);
298      this->model->addVertexTexture (0.0, 1.0);
299      this->model->addVertexTexture (1.0, 1.0);
300      this->model->addVertexTexture (0.0, 2.0);
301      this->model->addVertexTexture (1.0, 2.0);
302      this->model->addVertexTexture (0.0, 3.0);
303      this->model->addVertexTexture (1.0, 3.0);
304      this->model->addVertexTexture (0.0, 4.0);
305      this->model->addVertexTexture (1.0, 4.0);
306      this->model->addVertexTexture (2.0, 0.0);
307      this->model->addVertexTexture (2.0, 1.0);
308      this->model->addVertexTexture (-1.0, 0.0);
309      this->model->addVertexTexture (-1.0, 1.0);
310
311      this->model->finalize();
312      */
313    }
314}
Note: See TracBrowser for help on using the repository browser.