Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: updated the terrain class to more functionality
view goes farther 2000 instead of 250
model gets deleted by world_entity instead of childs

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