Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: saver Weapon-Projectile-generation and Stuff

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