Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: setClassID implemented in all files

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