Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 4684 was 4607, checked in by bensch, 19 years ago

orxonox/trunk: loading Terrain via XML

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