Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/world_entities/terrain.cc @ 4846

Last change on this file since 4846 was 4846, checked in by patrick, 19 years ago

orxonox/trunk: flushing work state to repos - some smaller changes in the comments and deleting SpatialSeparation object

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