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 | |
---|
26 | using namespace std; |
---|
27 | |
---|
28 | |
---|
29 | /** |
---|
30 | \brief standard constructor |
---|
31 | |
---|
32 | */ |
---|
33 | Terrain::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 | */ |
---|
44 | Terrain::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 | */ |
---|
71 | Terrain::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 | */ |
---|
92 | Terrain::Terrain(DebugTerrain debugTerrain) |
---|
93 | { |
---|
94 | this->init(); |
---|
95 | this->buildDebugTerrain(debugTerrain); |
---|
96 | } |
---|
97 | |
---|
98 | /** |
---|
99 | \brief standard deconstructor |
---|
100 | |
---|
101 | */ |
---|
102 | Terrain::~Terrain () |
---|
103 | { |
---|
104 | if (objectList) |
---|
105 | glDeleteLists(this->objectList, 1); |
---|
106 | } |
---|
107 | |
---|
108 | |
---|
109 | void Terrain::init(void) |
---|
110 | { |
---|
111 | this->setClassName ("Terrain"); |
---|
112 | |
---|
113 | this->objectList = 0; |
---|
114 | } |
---|
115 | |
---|
116 | |
---|
117 | |
---|
118 | void 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 (this->objectList) |
---|
133 | glCallList(this->objectList); |
---|
134 | else if (this->model) |
---|
135 | this->model->draw(); |
---|
136 | glPopMatrix(); |
---|
137 | } |
---|
138 | |
---|
139 | |
---|
140 | void Terrain::buildDebugTerrain(DebugTerrain debugTerrain) |
---|
141 | { |
---|
142 | // if the terrain is the Terrain of Dave |
---|
143 | if (debugTerrain == TERRAIN_DAVE) |
---|
144 | { |
---|
145 | objectList = glGenLists(1); |
---|
146 | glNewList (objectList, GL_COMPILE); |
---|
147 | |
---|
148 | glColor3f(1.0,0,0); |
---|
149 | |
---|
150 | int sizeX = 100; |
---|
151 | int sizeZ = 80; |
---|
152 | float length = 1000; |
---|
153 | float width = 200; |
---|
154 | float widthX = float (length /sizeX); |
---|
155 | float widthZ = float (width /sizeZ); |
---|
156 | |
---|
157 | float height [sizeX][sizeZ]; |
---|
158 | Vector normal_vectors[sizeX][sizeZ]; |
---|
159 | |
---|
160 | |
---|
161 | for ( int i = 0; i<sizeX-1; i+=1) |
---|
162 | for (int j = 0; j<sizeZ-1;j+=1) |
---|
163 | //height[i][j] = rand()/20046 + (j-25)*(j-25)/30; |
---|
164 | #ifdef __WIN32__ |
---|
165 | height[i][j]=(sin((float)j/3)*rand()*i/182400)*.5; |
---|
166 | #else |
---|
167 | height[i][j]=(sin((float)j/3)*rand()*(long)i/6282450500.0)*.5; |
---|
168 | #endif |
---|
169 | |
---|
170 | //Die Huegel ein wenig glaetten |
---|
171 | for (int h=1; h<2;h++) |
---|
172 | for (int i=1;i<sizeX-2 ;i+=1 ) |
---|
173 | for(int j=1;j<sizeZ-2;j+=1) |
---|
174 | height[i][j]=(height[i+1][j]+height[i][j+1]+height[i-1][j]+height[i][j-1])/4; |
---|
175 | |
---|
176 | //Berechnung von normalen Vektoren |
---|
177 | for(int i=1;i<sizeX-2;i+=1) |
---|
178 | for(int j=1;j<sizeZ-2 ;j+=1) |
---|
179 | { |
---|
180 | Vector v1 = Vector (widthX*(1), height[i][j], widthZ*(j) ); |
---|
181 | Vector v2 = Vector (widthX*(i-1), height[i-1][j], widthZ*(j)); |
---|
182 | Vector v3 = Vector (widthX*(i), height[i][j+1], widthZ*(j+1)); |
---|
183 | Vector v4 = Vector (widthX*(i+1), height[i+1][j], widthZ*(j)); |
---|
184 | Vector v5 = Vector (widthX*(i), height[i][j-1], widthZ*(j-1)); |
---|
185 | |
---|
186 | Vector c1 = v2 - v1; |
---|
187 | Vector c2 = v3 - v1; |
---|
188 | Vector c3= v4 - v1; |
---|
189 | Vector c4 = v5 - v1; |
---|
190 | Vector zero = Vector (0,0,0); |
---|
191 | normal_vectors[i][j]=c1.cross(v3-v5)+c2.cross(v4-v2)+c3.cross(v5-v3)+c4.cross(v2-v4); |
---|
192 | normal_vectors[i][j].normalize(); |
---|
193 | } |
---|
194 | |
---|
195 | glBegin(GL_QUADS); |
---|
196 | int snowheight=3; |
---|
197 | for ( int i = 0; i<sizeX; i+=1) |
---|
198 | for (int j = 0; j<sizeZ;j+=1) |
---|
199 | { |
---|
200 | Vector v1 = Vector (widthX*(i), height[i][j]-20, widthZ*(j) -width/2); |
---|
201 | Vector v2 = Vector (widthX*(i+1), height[i+1][j]-20, widthZ*(j) -width/2); |
---|
202 | Vector v3 = Vector (widthX*(i+1), height[i+1][j+1]-20, widthZ*(j+1)-width/2); |
---|
203 | Vector v4 = Vector (widthX*(i), height[i][j+1]-20, widthZ*(j+1)-width/2); |
---|
204 | float a[3]; |
---|
205 | if(height[i][j]<snowheight){ |
---|
206 | a[0]=0; |
---|
207 | a[1]=1.0-height[i][j]/10-.3; |
---|
208 | a[2]=0; |
---|
209 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
210 | } |
---|
211 | else{ |
---|
212 | a[0]=1.0; |
---|
213 | a[1]=1.0; |
---|
214 | a[2]=1.0; |
---|
215 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
216 | |
---|
217 | } |
---|
218 | glNormal3f(normal_vectors[i][j].x, normal_vectors[i][j].y, normal_vectors[i][j].z); |
---|
219 | glVertex3f(v1.x, v1.y, v1.z); |
---|
220 | if(height[i+1][j]<snowheight){ |
---|
221 | a[0]=0; |
---|
222 | a[1] =1.0-height[i+1][j]/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 | } |
---|
233 | glNormal3f(normal_vectors[i+1][j].x, normal_vectors[i+1][j].y, normal_vectors[i+1][j].z); |
---|
234 | glVertex3f(v2.x, v2.y, v2.z); |
---|
235 | if(height[i+1][j+1]<snowheight){ |
---|
236 | a[0]=0; |
---|
237 | a[1] =1.0-height[i+1][j+1]/10-.3; |
---|
238 | a[2]=0; |
---|
239 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
240 | } |
---|
241 | else{ |
---|
242 | a[0]=1.0; |
---|
243 | a[1]=1.0; |
---|
244 | a[2]=1.0; |
---|
245 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
246 | |
---|
247 | |
---|
248 | } |
---|
249 | glNormal3f(normal_vectors[i+1][j+1].x, normal_vectors[i+1][j+1].y, normal_vectors[i+1][j+1].z); |
---|
250 | glVertex3f(v3.x, v3.y, v3.z); |
---|
251 | if(height[i][j+1]<snowheight){ |
---|
252 | a[0]=0; |
---|
253 | a[1] =1.0-height[i+1][j+1]/10-.3; |
---|
254 | a[2]=0; |
---|
255 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
256 | } |
---|
257 | else{ |
---|
258 | a[0]=1.0; |
---|
259 | a[1]=1.0; |
---|
260 | a[2]=1.0; |
---|
261 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
262 | } |
---|
263 | glNormal3f(normal_vectors[i][j+1].x, normal_vectors[i][j+1].y, normal_vectors[i][j+1].z); |
---|
264 | glVertex3f(v4.x, v4.y, v4.z); |
---|
265 | |
---|
266 | } |
---|
267 | glEnd(); |
---|
268 | glEndList(); |
---|
269 | } |
---|
270 | |
---|
271 | if (debugTerrain == TERRAIN_BENSCH) |
---|
272 | { |
---|
273 | /* |
---|
274 | this->model = (OBJModel*) new Model(); |
---|
275 | this->model->setName("CUBE"); |
---|
276 | this->model->addVertex (-0.5, -0.5, 0.5); |
---|
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 | |
---|
285 | this->model->addVertexTexture (0.0, 0.0); |
---|
286 | this->model->addVertexTexture (1.0, 0.0); |
---|
287 | this->model->addVertexTexture (0.0, 1.0); |
---|
288 | this->model->addVertexTexture (1.0, 1.0); |
---|
289 | this->model->addVertexTexture (0.0, 2.0); |
---|
290 | this->model->addVertexTexture (1.0, 2.0); |
---|
291 | this->model->addVertexTexture (0.0, 3.0); |
---|
292 | this->model->addVertexTexture (1.0, 3.0); |
---|
293 | this->model->addVertexTexture (0.0, 4.0); |
---|
294 | this->model->addVertexTexture (1.0, 4.0); |
---|
295 | this->model->addVertexTexture (2.0, 0.0); |
---|
296 | this->model->addVertexTexture (2.0, 1.0); |
---|
297 | this->model->addVertexTexture (-1.0, 0.0); |
---|
298 | this->model->addVertexTexture (-1.0, 1.0); |
---|
299 | |
---|
300 | this->model->finalize(); |
---|
301 | */ |
---|
302 | } |
---|
303 | |
---|
304 | } |
---|