Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 4161 was 4090, checked in by nico, 20 years ago

branches/heightMap: some makefile and include stuff fixed

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