Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: more output info, some fixes in resourceManager

File size: 7.0 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      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->setClassName ("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 (objectList)
101    glCallList(objectList);
102  else if (model)
103    model->draw();
104  //  this->model->draw();
105  glPopMatrix();
106}
107
108
109void Terrain::buildDebugTerrain(DebugTerrain debugTerrain)
110{
111  // if the terrain is the Terrain of Dave
112  if (debugTerrain == TERRAIN_DAVE)
113    {
114      objectList = glGenLists(1);
115      glNewList (objectList, GL_COMPILE);
116     
117      glColor3f(1.0,0,0);
118     
119      int sizeX = 100;
120      int sizeZ = 80;
121      float length = 1000;
122      float width = 200;
123      float widthX = float (length /sizeX);
124      float widthZ = float (width /sizeZ);
125     
126      float height [sizeX][sizeZ];
127      Vector normal_vectors[sizeX][sizeZ];
128     
129     
130      for ( int i = 0; i<sizeX-1; i+=1)
131        for (int j = 0; j<sizeZ-1;j+=1)
132          //height[i][j] = rand()/20046 + (j-25)*(j-25)/30;
133#ifdef __WIN32__
134          height[i][j]=(sin((float)j/3)*rand()*i/182400)*.5;
135#else
136      height[i][j]=(sin((float)j/3)*rand()*(long)i/6282450500.0)*.5;
137#endif
138     
139      //Die Huegel ein wenig glaetten
140      for (int h=1; h<2;h++)
141        for (int i=1;i<sizeX-2 ;i+=1 )
142          for(int j=1;j<sizeZ-2;j+=1)
143            height[i][j]=(height[i+1][j]+height[i][j+1]+height[i-1][j]+height[i][j-1])/4;
144     
145      //Berechnung von normalen Vektoren
146      for(int i=1;i<sizeX-2;i+=1)
147        for(int j=1;j<sizeZ-2 ;j+=1)
148          {
149            Vector v1 = Vector (widthX*(1),      height[i][j],      widthZ*(j) );
150            Vector v2 = Vector (widthX*(i-1),    height[i-1][j],    widthZ*(j));
151            Vector v3 = Vector (widthX*(i),      height[i][j+1],    widthZ*(j+1));
152            Vector v4 = Vector (widthX*(i+1),    height[i+1][j],    widthZ*(j));
153            Vector v5 = Vector (widthX*(i),      height[i][j-1],    widthZ*(j-1));
154           
155            Vector c1 = v2 - v1;
156            Vector c2 = v3 - v1;
157            Vector c3=  v4 - v1;
158            Vector c4 = v5 - v1;
159            Vector zero = Vector (0,0,0);
160            normal_vectors[i][j]=c1.cross(v3-v5)+c2.cross(v4-v2)+c3.cross(v5-v3)+c4.cross(v2-v4);
161            normal_vectors[i][j].normalize();
162          }
163     
164      glBegin(GL_QUADS);
165      int snowheight=3;
166      for ( int i = 0; i<sizeX; i+=1)
167        for (int j = 0; j<sizeZ;j+=1)
168          {       
169            Vector v1 = Vector (widthX*(i),      height[i][j]-20,       widthZ*(j)  -width/2);
170            Vector v2 = Vector (widthX*(i+1),    height[i+1][j]-20,     widthZ*(j)  -width/2);
171            Vector v3 = Vector (widthX*(i+1),    height[i+1][j+1]-20,   widthZ*(j+1)-width/2);
172            Vector v4 = Vector (widthX*(i),      height[i][j+1]-20,     widthZ*(j+1)-width/2);
173            float a[3];
174            if(height[i][j]<snowheight){
175              a[0]=0;
176              a[1]=1.0-height[i][j]/10-.3;
177              a[2]=0;
178              glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
179            }
180            else{
181              a[0]=1.0;
182              a[1]=1.0;
183              a[2]=1.0;
184              glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
185             
186            }
187            glNormal3f(normal_vectors[i][j].x, normal_vectors[i][j].y, normal_vectors[i][j].z);
188            glVertex3f(v1.x, v1.y, v1.z);
189            if(height[i+1][j]<snowheight){
190              a[0]=0;
191              a[1] =1.0-height[i+1][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+1][j].x, normal_vectors[i+1][j].y, normal_vectors[i+1][j].z);
203            glVertex3f(v2.x, v2.y, v2.z);
204            if(height[i+1][j+1]<snowheight){
205              a[0]=0;
206              a[1] =1.0-height[i+1][j+1]/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            }
218            glNormal3f(normal_vectors[i+1][j+1].x, normal_vectors[i+1][j+1].y, normal_vectors[i+1][j+1].z);
219            glVertex3f(v3.x, v3.y, v3.z);
220            if(height[i][j+1]<snowheight){
221              a[0]=0;
222              a[1] =1.0-height[i+1][j+1]/10-.3;
223              a[2]=0;
224              glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
225            }
226            else{
227              a[0]=1.0;
228              a[1]=1.0;
229              a[2]=1.0;
230              glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
231            }
232            glNormal3f(normal_vectors[i][j+1].x, normal_vectors[i][j+1].y, normal_vectors[i][j+1].z);
233            glVertex3f(v4.x, v4.y, v4.z);
234           
235          }
236      glEnd();
237      glEndList();
238    }
239
240  if (debugTerrain == TERRAIN_BENSCH)
241    {
242      /*
243        this->model = (OBJModel*) new Model();
244      this->model->setName("CUBE");
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      this->model->addVertex (0.5, -0.5, -0.5);
253     
254      this->model->addVertexTexture (0.0, 0.0);
255      this->model->addVertexTexture (1.0, 0.0);
256      this->model->addVertexTexture (0.0, 1.0);
257      this->model->addVertexTexture (1.0, 1.0);
258      this->model->addVertexTexture (0.0, 2.0);
259      this->model->addVertexTexture (1.0, 2.0);
260      this->model->addVertexTexture (0.0, 3.0);
261      this->model->addVertexTexture (1.0, 3.0);
262      this->model->addVertexTexture (0.0, 4.0);
263      this->model->addVertexTexture (1.0, 4.0);
264      this->model->addVertexTexture (2.0, 0.0);
265      this->model->addVertexTexture (2.0, 1.0);
266      this->model->addVertexTexture (-1.0, 0.0);
267      this->model->addVertexTexture (-1.0, 1.0);
268
269      this->model->finalize();
270      */
271    }
272 
273}
Note: See TracBrowser for help on using the repository browser.