Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/heightMap/src/world_entities/terrain.cc @ 4021

Last change on this file since 4021 was 4021, checked in by nico, 19 years ago

branches/heightmap/: created new empty files for the heightmap class.

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