Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/avi_play/src/world_entities/terrain.cc @ 6483

Last change on this file since 6483 was 5750, checked in by bensch, 19 years ago

orxonox/trunk: merged the WorldEntities into the Trunk.
Merged with command:
svn merge branches/world_entities/ trunk/ -r5516:HEAD

conflics from world_entities changed in favor of branches/world_entity
all other conflict in favor of the trunk

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