2 Copyright (C) 2003, 2010 - Wolfire Games
4 This file is part of Lugaru.
6 Lugaru 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 of the License, or
9 (at your option) any later version.
11 Lugaru is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with Lugaru. If not, see <http://www.gnu.org/licenses/>.
24 extern float viewdistance;
25 extern float fadestart;
26 extern int environment;
27 extern float texscale;
29 extern float multiplier;
30 extern FRUSTUM frustum;
31 extern float texdetail;
34 extern float blurness;
35 extern float targetblurness;
36 extern Objects objects;
37 extern TGAImageRec texture;
38 extern bool visibleloading;
39 extern bool skyboxtexture;
40 extern int tutoriallevel;
44 int Terrain::lineTerrain(XYZ p1, XYZ p2, XYZ *p)
47 static float distance;
48 static float olddistance;
49 static int intersecting;
50 static int firstintersecting;
52 static int startx, starty;
53 static int endx, endy;
54 static float highest, lowest;
56 firstintersecting = -1;
90 for (i = startx; i <= endx; i++) {
91 for (j = starty; j <= endy; j++) {
94 for (k = 0; k < 2; k++) {
95 if (heightmap[i + k][j] > highest)
96 highest = heightmap[i + k][j];
97 if (heightmap[i + k][j] < lowest)
98 lowest = heightmap[i + k][j];
99 if (heightmap[i + k][j + 1] > highest)
100 highest = heightmap[i + k][j + 1];
101 if (heightmap[i + k][j + 1] < lowest)
102 lowest = heightmap[i + k][j + 1];
104 if ((p1.y <= highest || p2.y <= highest) && (p1.y >= lowest || p2.y >= lowest)) {
106 triangles[0].y = heightmap[i][j];
110 triangles[1].y = heightmap[i][j + 1];
111 triangles[1].z = j + 1;
113 triangles[2].x = i + 1;
114 triangles[2].y = heightmap[i + 1][j];
117 intersecting = LineFacet(p1, p2, triangles[0], triangles[1], triangles[2], &point);
118 distance = distsq(&p1, &point);
119 if ((distance < olddistance || firstintersecting == -1) && intersecting == 1) {
120 olddistance = distance;
121 firstintersecting = 1;
125 triangles[0].x = i + 1;
126 triangles[0].y = heightmap[i + 1][j];
130 triangles[1].y = heightmap[i][j + 1];
131 triangles[1].z = j + 1;
133 triangles[2].x = i + 1;
134 triangles[2].y = heightmap[i + 1][j + 1];
135 triangles[2].z = j + 1;
137 intersecting = LineFacet(p1, p2, triangles[0], triangles[1], triangles[2], &point);
138 distance = distsq(&p1, &point);
139 if ((distance < olddistance || firstintersecting == -1) && intersecting == 1) {
140 olddistance = distance;
141 firstintersecting = 1;
147 return firstintersecting;
150 void Terrain::UpdateTransparency(int whichx, int whichy)
153 static int i, j, a, b, c, d, patch_size, stepsize;
154 static float distance;
156 static float viewdistsquared;
158 viewdistsquared = viewdistance * viewdistance;
159 patch_size = size / subdivision;
162 c = whichx * patch_elements + whichy * patch_elements * subdivision;
164 for (i = patch_size * whichx; i < patch_size * (whichx + 1) + 1; i += stepsize) {
165 for (j = patch_size * whichy; j < patch_size * (whichy + 1) + 1; j += stepsize) {
166 if (i < size && j < size) {
167 vertex.x = i * scale;
168 vertex.z = j * scale;
169 vertex.y = heightmap[i][j] * scale;
170 distance = distsq(&viewer, &vertex);
171 if (distance > viewdistsquared)
172 distance = viewdistsquared;
173 colors[i][j][3] = (viewdistsquared - (distance - (viewdistsquared * fadestart)) * (1 / (1 - fadestart))) / viewdistsquared;
178 for (i = patch_size * whichx; i < patch_size * (whichx + 1); i += stepsize) {
179 for (j = patch_size * whichy; j < patch_size * (whichy + 1); j += stepsize) {
180 a = (i - (patch_size * whichx)) / stepsize;
181 b = (j - (patch_size * whichy)) / stepsize;
182 d = (a * 54) + (b * 54 * patch_size / stepsize);
183 vArray[d + c + 6] = colors[i][j][3];
185 vArray[d + c + 15] = colors[i][j + stepsize][3];
187 vArray[d + c + 24] = colors[i + stepsize][j][3];
189 vArray[d + c + 33] = colors[i + stepsize][j][3];
191 vArray[d + c + 42] = colors[i][j + stepsize][3];
193 vArray[d + c + 51] = colors[i + stepsize][j + stepsize][3];
198 void Terrain::UpdateTransparencyother(int whichx, int whichy)
201 static int i, j, a, b, c, d, patch_size, stepsize;
203 patch_size = size / subdivision;
206 c = whichx * patch_elements + whichy * patch_elements * subdivision;
208 for (i = patch_size * whichx; i < patch_size * (whichx + 1); i += stepsize) {
209 for (j = patch_size * whichy; j < patch_size * (whichy + 1); j += stepsize) {
210 a = (i - (patch_size * whichx)) / stepsize;
211 b = (j - (patch_size * whichy)) / stepsize;
212 d = (a * 54) + (b * 54 * patch_size / stepsize);
213 vArray[d + c + 6] = colors[i][j][3] * opacityother[i][j];
215 vArray[d + c + 15] = colors[i][j + stepsize][3] * opacityother[i][j + stepsize];
217 vArray[d + c + 24] = colors[i + stepsize][j][3] * opacityother[i + stepsize][j];
219 vArray[d + c + 33] = colors[i + stepsize][j][3] * opacityother[i + stepsize][j];
221 vArray[d + c + 42] = colors[i][j + stepsize][3] * opacityother[i][j + stepsize];
223 vArray[d + c + 51] = colors[i + stepsize][j + stepsize][3] * opacityother[i + stepsize][j + stepsize];
228 void Terrain::UpdateTransparencyotherother(int whichx, int whichy)
231 static int i, j, a, b, c, d, patch_size, stepsize;
232 static float distance;
234 static float viewdistsquared;
236 viewdistsquared = viewdistance * viewdistance;
237 patch_size = size / subdivision;
240 c = whichx * patch_elements + whichy * patch_elements * subdivision;
242 for (i = patch_size * whichx; i < patch_size * (whichx + 1) + 1; i += stepsize) {
243 for (j = patch_size * whichy; j < patch_size * (whichy + 1) + 1; j += stepsize) {
244 if (i < size && j < size) {
245 vertex.x = i * scale;
246 vertex.z = j * scale;
247 vertex.y = heightmap[i][j] * scale;
248 distance = distsq(&viewer, &vertex);
249 if (distance > viewdistsquared)
250 distance = viewdistsquared;
251 colors[i][j][3] = (viewdistsquared - (distance - (viewdistsquared * fadestart)) * (1 / (1 - fadestart))) / viewdistsquared;
256 for (i = patch_size * whichx; i < patch_size * (whichx + 1); i += stepsize) {
257 for (j = patch_size * whichy; j < patch_size * (whichy + 1); j += stepsize) {
258 a = (i - (patch_size * whichx)) / stepsize;
259 b = (j - (patch_size * whichy)) / stepsize;
260 d = (a * 54) + (b * 54 * patch_size / stepsize);
261 vArray[d + c + 6] = colors[i][j][3];
263 vArray[d + c + 15] = colors[i][j + stepsize][3];
265 vArray[d + c + 24] = colors[i + stepsize][j][3];
267 vArray[d + c + 33] = colors[i + stepsize][j][3];
269 vArray[d + c + 42] = colors[i][j + stepsize][3];
271 vArray[d + c + 51] = colors[i + stepsize][j + stepsize][3];
276 void Terrain::UpdateVertexArray(int whichx, int whichy)
278 static int i, j, a, b, c, patch_size, stepsize;
281 numtris[whichx][whichy] = 0;
283 patch_size = size / subdivision;
286 c = whichx * patch_elements + whichy * patch_elements * subdivision;
287 for (i = patch_size * whichx; i < patch_size * (whichx + 1); i += stepsize) {
288 for (j = patch_size * whichy; j < patch_size * (whichy + 1); j += stepsize) {
289 a = (i - ((float)size / subdivision * (float)whichx)) / stepsize;
290 b = (j - ((float)size / subdivision * (float)whichy)) / stepsize;
291 vArray[(a * 54) + (b * 54 * patch_size / stepsize) + c + 0] = i * scale;
292 vArray[(a * 54) + (b * 54 * patch_size / stepsize) + c + 1] = heightmap[i][j] * scale;
293 vArray[(a * 54) + (b * 54 * patch_size / stepsize) + c + 2] = j * scale;
294 vArray[(a * 54) + (b * 54 * patch_size / stepsize) + c + 3] = colors[i][j][0];
295 vArray[(a * 54) + (b * 54 * patch_size / stepsize) + c + 4] = colors[i][j][1];
296 vArray[(a * 54) + (b * 54 * patch_size / stepsize) + c + 5] = colors[i][j][2];
297 vArray[(a * 54) + (b * 54 * patch_size / stepsize) + c + 6] = colors[i][j][3];
298 vArray[(a * 54) + (b * 54 * patch_size / stepsize) + c + 7] = i * scale * texscale + texoffsetx[i][j];
299 vArray[(a * 54) + (b * 54 * patch_size / stepsize) + c + 8] = j * scale * texscale + texoffsety[i][j];
301 vArray[(a * 54) + (b * 54 * patch_size / stepsize) + c + 9] = i * scale;
302 vArray[(a * 54) + (b * 54 * patch_size / stepsize) + c + 10] = heightmap[i][j + stepsize] * scale;
303 vArray[(a * 54) + (b * 54 * patch_size / stepsize) + c + 11] = j * scale + stepsize * scale;
304 vArray[(a * 54) + (b * 54 * patch_size / stepsize) + c + 12] = colors[i][j + stepsize][0];
305 vArray[(a * 54) + (b * 54 * patch_size / stepsize) + c + 13] = colors[i][j + stepsize][1];
306 vArray[(a * 54) + (b * 54 * patch_size / stepsize) + c + 14] = colors[i][j + stepsize][2];
307 vArray[(a * 54) + (b * 54 * patch_size / stepsize) + c + 15] = colors[i][j + stepsize][3];
308 vArray[(a * 54) + (b * 54 * patch_size / stepsize) + c + 16] = i * scale * texscale + texoffsetx[i][j + stepsize];
309 vArray[(a * 54) + (b * 54 * patch_size / stepsize) + c + 17] = j * scale * texscale + stepsize * scale * texscale + texoffsety[i][j + stepsize];
311 vArray[(a * 54) + (b * 54 * patch_size / stepsize) + c + 18] = i * scale + stepsize * scale;
312 vArray[(a * 54) + (b * 54 * patch_size / stepsize) + c + 19] = heightmap[i + stepsize][j] * scale;
313 vArray[(a * 54) + (b * 54 * patch_size / stepsize) + c + 20] = j * scale;
314 vArray[(a * 54) + (b * 54 * patch_size / stepsize) + c + 21] = colors[i + stepsize][j][0];
315 vArray[(a * 54) + (b * 54 * patch_size / stepsize) + c + 22] = colors[i + stepsize][j][1];
316 vArray[(a * 54) + (b * 54 * patch_size / stepsize) + c + 23] = colors[i + stepsize][j][2];
317 vArray[(a * 54) + (b * 54 * patch_size / stepsize) + c + 24] = colors[i + stepsize][j][3];
318 vArray[(a * 54) + (b * 54 * patch_size / stepsize) + c + 25] = i * scale * texscale + stepsize * scale * texscale + texoffsetx[i + stepsize][j];
319 vArray[(a * 54) + (b * 54 * patch_size / stepsize) + c + 26] = j * scale * texscale + texoffsety[i + stepsize][j];
321 vArray[(a * 54) + (b * 54 * patch_size / stepsize) + c + 27] = i * scale + stepsize * scale;
322 vArray[(a * 54) + (b * 54 * patch_size / stepsize) + c + 28] = heightmap[i + stepsize][j] * scale;
323 vArray[(a * 54) + (b * 54 * patch_size / stepsize) + c + 29] = j * scale;
324 vArray[(a * 54) + (b * 54 * patch_size / stepsize) + c + 30] = colors[i + stepsize][j][0];
325 vArray[(a * 54) + (b * 54 * patch_size / stepsize) + c + 31] = colors[i + stepsize][j][1];
326 vArray[(a * 54) + (b * 54 * patch_size / stepsize) + c + 32] = colors[i + stepsize][j][2];
327 vArray[(a * 54) + (b * 54 * patch_size / stepsize) + c + 33] = colors[i + stepsize][j][3];
328 vArray[(a * 54) + (b * 54 * patch_size / stepsize) + c + 34] = i * scale * texscale + stepsize * scale * texscale + texoffsetx[i + stepsize][j];
329 vArray[(a * 54) + (b * 54 * patch_size / stepsize) + c + 35] = j * scale * texscale + texoffsety[i + stepsize][j];
331 vArray[(a * 54) + (b * 54 * patch_size / stepsize) + c + 36] = i * scale;
332 vArray[(a * 54) + (b * 54 * patch_size / stepsize) + c + 37] = heightmap[i][j + stepsize] * scale;
333 vArray[(a * 54) + (b * 54 * patch_size / stepsize) + c + 38] = j * scale + stepsize * scale;
334 vArray[(a * 54) + (b * 54 * patch_size / stepsize) + c + 39] = colors[i][j + stepsize][0];
335 vArray[(a * 54) + (b * 54 * patch_size / stepsize) + c + 40] = colors[i][j + stepsize][1];
336 vArray[(a * 54) + (b * 54 * patch_size / stepsize) + c + 41] = colors[i][j + stepsize][2];
337 vArray[(a * 54) + (b * 54 * patch_size / stepsize) + c + 42] = colors[i][j + stepsize][3];
338 vArray[(a * 54) + (b * 54 * patch_size / stepsize) + c + 43] = i * scale * texscale + texoffsetx[i][j + stepsize];
339 vArray[(a * 54) + (b * 54 * patch_size / stepsize) + c + 44] = j * scale * texscale + stepsize * scale * texscale + texoffsety[i][j + stepsize];
341 vArray[(a * 54) + (b * 54 * patch_size / stepsize) + c + 45] = i * scale + stepsize * scale;
342 vArray[(a * 54) + (b * 54 * patch_size / stepsize) + c + 46] = heightmap[i + stepsize][j + stepsize] * scale;
343 vArray[(a * 54) + (b * 54 * patch_size / stepsize) + c + 47] = j * scale + stepsize * scale;
344 vArray[(a * 54) + (b * 54 * patch_size / stepsize) + c + 48] = colors[i + stepsize][j + stepsize][0];
345 vArray[(a * 54) + (b * 54 * patch_size / stepsize) + c + 49] = colors[i + stepsize][j + stepsize][1];
346 vArray[(a * 54) + (b * 54 * patch_size / stepsize) + c + 50] = colors[i + stepsize][j + stepsize][2];
347 vArray[(a * 54) + (b * 54 * patch_size / stepsize) + c + 51] = colors[i + stepsize][j + stepsize][3];
348 vArray[(a * 54) + (b * 54 * patch_size / stepsize) + c + 52] = i * scale * texscale + stepsize * scale * texscale + texoffsetx[i + stepsize][j + stepsize];
349 vArray[(a * 54) + (b * 54 * patch_size / stepsize) + c + 53] = j * scale * texscale + stepsize * scale * texscale + texoffsety[i + stepsize][j + stepsize];
350 numtris[whichx][whichy] += 2;
354 maxypatch[whichx][whichy] = -10000;
355 minypatch[whichx][whichy] = 10000;
356 for (a = 0; a < size / subdivision; a++) {
357 for (b = 0; b < size / subdivision; b++) {
358 if (heightmap[(size / subdivision)*whichx + a][(size / subdivision)*whichy + b]*scale > maxypatch[whichx][whichy])
359 maxypatch[whichx][whichy] = heightmap[(size / subdivision) * whichx + a][(size / subdivision) * whichy + b] * scale;
360 if (heightmap[(size / subdivision)*whichx + a][(size / subdivision)*whichy + b]*scale < minypatch[whichx][whichy])
361 minypatch[whichx][whichy] = heightmap[(size / subdivision) * whichx + a][(size / subdivision) * whichy + b] * scale;
364 heightypatch[whichx][whichy] = (maxypatch[whichx][whichy] - minypatch[whichx][whichy]);
365 if (heightypatch[whichx][whichy] < size / subdivision * scale)
366 heightypatch[whichx][whichy] = size / subdivision * scale;
367 avgypatch[whichx][whichy] = (minypatch[whichx][whichy] + maxypatch[whichx][whichy]) / 2;
369 for (i = whichx * size / subdivision; i < (whichx + 1)*size / subdivision - 1; i++) {
370 for (j = whichy * size / subdivision; j < (whichy + 1)*size / subdivision - 1; j++) {
371 triangles[(i * (size - 1) * 2) + (j * 2)][0].x = i * scale;
372 triangles[(i * (size - 1) * 2) + (j * 2)][0].y = heightmap[i][j] * scale;
373 triangles[(i * (size - 1) * 2) + (j * 2)][0].z = j * scale;
375 triangles[(i * (size - 1) * 2) + (j * 2)][1].x = i * scale;
376 triangles[(i * (size - 1) * 2) + (j * 2)][1].y = heightmap[i][j + 1] * scale;
377 triangles[(i * (size - 1) * 2) + (j * 2)][1].z = j * scale + scale;
379 triangles[(i * (size - 1) * 2) + (j * 2)][2].x = i * scale + 1 * scale;
380 triangles[(i * (size - 1) * 2) + (j * 2)][2].y = heightmap[i + 1][j] * scale;
381 triangles[(i * (size - 1) * 2) + (j * 2)][2].z = j * scale;
383 triangles[(i * (size - 1) * 2) + (j * 2) + 1][0].x = i * scale + 1 * scale;
384 triangles[(i * (size - 1) * 2) + (j * 2) + 1][0].y = heightmap[i + 1][j] * scale;
385 triangles[(i * (size - 1) * 2) + (j * 2) + 1][0].z = j * scale;
387 triangles[(i * (size - 1) * 2) + (j * 2) + 1][1].x = i * scale;
388 triangles[(i * (size - 1) * 2) + (j * 2) + 1][1].y = heightmap[i][j + 1] * scale;
389 triangles[(i * (size - 1) * 2) + (j * 2) + 1][1].z = j * scale + 1 * scale;
391 triangles[(i * (size - 1) * 2) + (j * 2) + 1][2].x = i * scale + 1 * scale;
392 triangles[(i * (size - 1) * 2) + (j * 2) + 1][2].y = heightmap[i + 1][j + 1] * scale;
393 triangles[(i * (size - 1) * 2) + (j * 2) + 1][2].z = j * scale + 1 * scale;
400 bool Terrain::load(const char *fileName)
404 static float patch_size;
406 float temptexdetail = texdetail;
407 //LoadTGA( fileName );
409 // Fixing filename so that it works with its own os
410 char * FixedFN = ConvertFileName(fileName);
412 unsigned char fileNamep[256];
413 CopyCStringToPascal(FixedFN, fileNamep);
415 upload_image( fileNamep , 0);
418 if (texture.bpp > 24) {
419 int bytesPerPixel = texture.bpp / 8;
422 for (i = 0; i < (long)(texture.sizeY * texture.sizeX * bytesPerPixel); i++) {
424 texture.data[tempnum] = texture.data[i];
431 Game::LoadingScreen();
433 texdetail = temptexdetail;
435 size = texture.sizeX;
437 for (i = 0; i < size; i++) {
438 for (j = 0; j < size; j++) {
439 heightmap[size - 1 - i][j] = (float)((texture.data[(i + (j * size)) * texture.bpp / 8])) / 5;
444 Game::LoadingScreen();
448 for (i = 0; i < subdivision; i++) {
449 for (j = 0; j < subdivision; j++) {
450 textureness[i][j] = -1;
454 Game::LoadingScreen();
457 for (i = 0; i < size; i++) {
458 for (j = 0; j < size; j++) {
459 heightmap[i][j] *= .5;
461 texoffsetx[i][j] = (float)abs(Random() % 100) / 1200 / scale * 3;
462 texoffsety[i][j] = (float)abs(Random() % 100) / 1200 / scale * 3;
465 if (environment == snowyenvironment) {
466 if (j != 0 && heightmap[i][j] - heightmap[i][j - 1] > slopeness) {
467 slopeness = heightmap[i][j] - heightmap[i][j - 1];
469 opacityother[i][j] = slopeness * slopeness * 2;
470 if (opacityother[i][j] > 1)
471 opacityother[i][j] = 1;
472 opacityother[i][j] -= (float)abs(Random() % 100) / 300;
474 if (environment == desertenvironment) {
475 if (j != 0 && heightmap[i][j] - heightmap[i][j - 1] > slopeness) {
476 slopeness = heightmap[i][j] - heightmap[i][j - 1];
478 opacityother[i][j] = slopeness * slopeness * 2;
479 if (opacityother[i][j] > 1)
480 opacityother[i][j] = 1;
481 opacityother[i][j] -= (float)abs(Random() % 100) / 300;
483 if (environment == grassyenvironment) {
484 if (i != 0 && heightmap[i][j] - heightmap[i - 1][j] > slopeness) {
485 slopeness = heightmap[i][j] - heightmap[i - 1][j];
487 if (j != 0 && heightmap[i][j] - heightmap[i][j - 1] > slopeness) {
488 slopeness = heightmap[i][j] - heightmap[i][j - 1];
490 if (i < size - 1 && heightmap[i][j] - heightmap[i + 1][j] > slopeness) {
491 slopeness = heightmap[i][j] - heightmap[i + 1][j];
493 if (j < size - 1 && heightmap[i][j] - heightmap[i][j + 1] > slopeness) {
494 slopeness = heightmap[i][j] - heightmap[i][j + 1];
496 opacityother[i][j] = slopeness * slopeness * 10;
497 if (opacityother[i][j] > 1)
498 opacityother[i][j] = 1;
499 opacityother[i][j] -= (float)abs(Random() % 100) / 100;
504 Game::LoadingScreen();
506 for (i = 0; i < size; i++) {
507 for (j = 0; j < size; j++) {
508 if (environment == snowyenvironment) {
509 heightmap[i][j] -= opacityother[i][j];
511 if (environment == desertenvironment) {
512 heightmap[i][j] -= opacityother[i][j];
517 Game::LoadingScreen();
521 //Smooth opacityother
526 if(i!=0){total+=opacityother[j][i-1]; todivide++;}
527 if(i!=size-1){total+=opacityother[j][i+1]; todivide++;}
528 if(j!=0){total+=opacityother[j-1][i]; todivide++;}
529 if(j!=size-1){total+=opacityother[j+1][i]; todivide++;}
530 if(i!=0&&j!=0){total+=opacityother[j-1][i-1]; todivide++;}
531 if(i!=size-1&&j!=0){total+=opacityother[j-1][i+1]; todivide++;}
532 if(j!=size-1&&i!=size-1){total+=opacityother[j+1][i+1]; todivide++;}
533 if(j!=size-1&&i!=0){total+=opacityother[j+1][i-1]; todivide++;}
534 total+=opacityother[j][i]; todivide++;
536 opacityother[j][i]=total/(float)todivide;
541 for (i = 0; i < size; i++) {
542 for (j = 0; j < size; j++) {
543 if (opacityother[i][j] < .1)
544 opacityother[i][j] = 0;
545 if (textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == -1) {
546 if (!opacityother[i][j])
547 textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] = allfirst;
548 if (opacityother[i][j] == 1)
549 textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] = allsecond;
551 if (opacityother[i][j] && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allfirst)
552 textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] = mixed;
553 if (opacityother[i][j] != 1 && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allsecond)
554 textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] = mixed;
560 if (opacityother[x][y] && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allfirst)
561 textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] = mixed;
562 if (opacityother[x][y] != 1 && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allsecond)
563 textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] = mixed;
564 if (opacityother[i][j] && textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] == allfirst)
565 textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] = mixed;
566 if (opacityother[i][j] != 1 && textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] == allsecond)
567 textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] = mixed;
571 if (opacityother[x][y] && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allfirst)
572 textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] = mixed;
573 if (opacityother[x][y] != 1 && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allsecond)
574 textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] = mixed;
575 if (opacityother[i][j] && textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] == allfirst)
576 textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] = mixed;
577 if (opacityother[i][j] != 1 && textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] == allsecond)
578 textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] = mixed;
584 if (opacityother[x][y] && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allfirst)
585 textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] = mixed;
586 if (opacityother[x][y] != 1 && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allsecond)
587 textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] = mixed;
588 if (opacityother[i][j] && textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] == allfirst)
589 textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] = mixed;
590 if (opacityother[i][j] != 1 && textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] == allsecond)
591 textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] = mixed;
599 if (opacityother[x][y] && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allfirst)
600 textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] = mixed;
601 if (opacityother[x][y] != 1 && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allsecond)
602 textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] = mixed;
603 if (opacityother[i][j] && textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] == allfirst)
604 textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] = mixed;
605 if (opacityother[i][j] != 1 && textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] == allsecond)
606 textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] = mixed;
610 if (opacityother[x][y] && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allfirst)
611 textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] = mixed;
612 if (opacityother[x][y] != 1 && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allsecond)
613 textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] = mixed;
614 if (opacityother[i][j] && textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] == allfirst)
615 textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] = mixed;
616 if (opacityother[i][j] != 1 && textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] == allsecond)
617 textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] = mixed;
623 if (opacityother[x][y] && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allfirst)
624 textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] = mixed;
625 if (opacityother[x][y] != 1 && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allsecond)
626 textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] = mixed;
627 if (opacityother[i][j] && textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] == allfirst)
628 textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] = mixed;
629 if (opacityother[i][j] != 1 && textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] == allsecond)
630 textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] = mixed;
638 if (opacityother[x][y] && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allfirst)
639 textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] = mixed;
640 if (opacityother[x][y] != 1 && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allsecond)
641 textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] = mixed;
642 if (opacityother[i][j] && textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] == allfirst)
643 textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] = mixed;
644 if (opacityother[i][j] != 1 && textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] == allsecond)
645 textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] = mixed;
651 if (opacityother[x][y] && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allfirst)
652 textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] = mixed;
653 if (opacityother[x][y] != 1 && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allsecond)
654 textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] = mixed;
655 if (opacityother[i][j] && textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] == allfirst)
656 textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] = mixed;
657 if (opacityother[i][j] != 1 && textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] == allsecond)
658 textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] = mixed;
665 Game::LoadingScreen();
667 patch_size = size / subdivision;
668 patch_elements = (patch_size) * (patch_size) * 54;
672 for(i=0;i<subdivision;i++){
673 for(j=0;j<subdivision;j++){
674 UpdateVertexArray(i,j);
681 void Terrain::CalculateNormals()
684 static XYZ facenormal;
685 static XYZ p, q, a, b, c;
687 for (i = 0; i < size; i++) {
688 for (j = 0; j < size; j++) {
695 for (i = 0; i < size - 1; i++) {
696 for (j = 0; j < size - 1; j++) {
698 a.y = heightmap[i][j];
701 b.y = heightmap[i][j + 1];
704 c.y = heightmap[i + 1][j];
714 CrossProduct(&p, &q, &facenormal);
716 facenormals[i][j] = facenormal;
718 normals[i][j] = normals[i][j] + facenormal;
719 normals[i][j + 1] = normals[i][j + 1] + facenormal;
720 normals[i + 1][j] = normals[i + 1][j] + facenormal;
724 a.y = heightmap[i + 1][j];
727 b.y = heightmap[i][j + 1];
730 c.y = heightmap[i + 1][j + 1];
740 CrossProduct(&p, &q, &facenormal);
742 normals[i + 1][j + 1] = normals[i + 1][j + 1] + facenormal;
743 normals[i][j + 1] = normals[i][j + 1] + facenormal;
744 normals[i + 1][j] = normals[i + 1][j] + facenormal;
746 Normalise(&facenormals[i][j]);
750 for (i = 0; i < size; i++) {
751 for (j = 0; j < size; j++) {
752 Normalise(&normals[i][j]);
757 void Terrain::drawpatch(int whichx, int whichy, float opacity)
763 UpdateTransparency(whichx, whichy);
765 glColor4f(1, 1, 1, 1);
766 //Set up vertex array
767 glEnableClientState(GL_VERTEX_ARRAY);
768 glEnableClientState(GL_COLOR_ARRAY);
769 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
770 glVertexPointer(3, GL_FLOAT, 9 * sizeof(GLfloat), &vArray[0 + whichx * patch_elements + whichy * patch_elements * subdivision]);
771 glColorPointer(4, GL_FLOAT, 9 * sizeof(GLfloat), &vArray[3 + whichx * patch_elements + whichy * patch_elements * subdivision]);
772 glTexCoordPointer(2, GL_FLOAT, 9 * sizeof(GLfloat), &vArray[7 + whichx * patch_elements + whichy * patch_elements * subdivision]);
775 glDrawArrays(GL_TRIANGLES, 0, numtris[whichx][whichy] * 3);
777 glDisableClientState(GL_VERTEX_ARRAY);
778 glDisableClientState(GL_COLOR_ARRAY);
779 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
782 void Terrain::drawpatchother(int whichx, int whichy, float opacity)
786 UpdateTransparency(whichx, whichy);
788 UpdateTransparencyother(whichx, whichy);
789 glColor4f(1, 1, 1, 1);
790 //Set up vertex array
791 glEnableClientState(GL_VERTEX_ARRAY);
792 glEnableClientState(GL_COLOR_ARRAY);
793 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
794 glVertexPointer(3, GL_FLOAT, 9 * sizeof(GLfloat), &vArray[0 + whichx * patch_elements + whichy * patch_elements * subdivision]);
795 glColorPointer(4, GL_FLOAT, 9 * sizeof(GLfloat), &vArray[3 + whichx * patch_elements + whichy * patch_elements * subdivision]);
796 glTexCoordPointer(2, GL_FLOAT, 9 * sizeof(GLfloat), &vArray[7 + whichx * patch_elements + whichy * patch_elements * subdivision]);
799 glDrawArrays(GL_TRIANGLES, 0, numtris[whichx][whichy] * 3);
801 glDisableClientState(GL_VERTEX_ARRAY);
802 glDisableClientState(GL_COLOR_ARRAY);
803 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
806 void Terrain::drawpatchotherother(int whichx, int whichy, float opacity)
809 UpdateTransparencyotherother(whichx, whichy);
811 glMatrixMode(GL_TEXTURE);
815 glColor4f(1, 1, 1, 1);
817 //Set up vertex array
818 glEnableClientState(GL_VERTEX_ARRAY);
819 glEnableClientState(GL_COLOR_ARRAY);
820 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
821 glVertexPointer(3, GL_FLOAT, 9 * sizeof(GLfloat), &vArray[0 + whichx * patch_elements + whichy * patch_elements * subdivision]);
822 glColorPointer(4, GL_FLOAT, 9 * sizeof(GLfloat), &vArray[3 + whichx * patch_elements + whichy * patch_elements * subdivision]);
823 glTexCoordPointer(2, GL_FLOAT, 9 * sizeof(GLfloat), &vArray[7 + whichx * patch_elements + whichy * patch_elements * subdivision]);
826 glDrawArrays(GL_TRIANGLES, 0, numtris[whichx][whichy] * 3);
828 glDisableClientState(GL_VERTEX_ARRAY);
829 glDisableClientState(GL_COLOR_ARRAY);
830 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
833 glMatrixMode(GL_MODELVIEW);
837 float Terrain::getHeight(float pointx, float pointz)
839 static int tilex, tiley;
840 static XYZ startpoint, endpoint, intersect, triangle[3], average;
845 if (pointx >= size - 1 || pointz >= size - 1 || pointx <= 0 || pointz <= 0)
848 startpoint.x = pointx;
849 startpoint.y = -1000;
850 startpoint.z = pointz;
852 endpoint = startpoint;
858 triangle[0].x = tilex;
859 triangle[0].z = tiley;
860 triangle[0].y = heightmap[tilex][tiley];
862 triangle[1].x = tilex + 1;
863 triangle[1].z = tiley;
864 triangle[1].y = heightmap[tilex + 1][tiley];
866 triangle[2].x = tilex;
867 triangle[2].z = tiley + 1;
868 triangle[2].y = heightmap[tilex][tiley + 1];
870 if (!LineFacetd(&startpoint, &endpoint, &triangle[0], &triangle[1], &triangle[2], &intersect)) {
871 triangle[0].x = tilex + 1;
872 triangle[0].z = tiley;
873 triangle[0].y = heightmap[tilex + 1][tiley];
875 triangle[1].x = tilex + 1;
876 triangle[1].z = tiley + 1;
877 triangle[1].y = heightmap[tilex + 1][tiley + 1];
879 triangle[2].x = tilex;
880 triangle[2].z = tiley + 1;
881 triangle[2].y = heightmap[tilex][tiley + 1];
882 LineFacetd(&startpoint, &endpoint, &triangle[0], &triangle[1], &triangle[2], &intersect);
884 return intersect.y * scale + getOpacity(pointx * scale, pointz * scale) / 8;
886 //height1=heightmap[tilex][tiley]*(1-(pointx-tilex))+heightmap[tilex+1][tiley]*(pointx-tilex);
887 //height2=heightmap[tilex][tiley+1]*(1-(pointx-tilex))+heightmap[tilex+1][tiley+1]*(pointx-tilex);
889 //return height1*(1-(pointz-tiley))*scale+height2*(pointz-tiley)*scale;
892 float Terrain::getOpacity(float pointx, float pointz)
894 static float height1, height2;
895 static int tilex, tiley;
900 if (pointx >= size - 1 || pointz >= size - 1 || pointx <= 0 || pointz <= 0)
906 height1 = opacityother[tilex][tiley] * (1 - (pointx - tilex)) + opacityother[tilex + 1][tiley] * (pointx - tilex);
907 height2 = opacityother[tilex][tiley + 1] * (1 - (pointx - tilex)) + opacityother[tilex + 1][tiley + 1] * (pointx - tilex);
909 return height1 * (1 - (pointz - tiley)) + height2 * (pointz - tiley);
912 XYZ Terrain::getNormal(float pointx, float pointz)
914 static XYZ height1, height2, total;
915 static int tilex, tiley;
921 if (pointx >= size - 1 || pointz >= size - 1 || pointx <= 0 || pointz <= 0)
926 height1 = normals[tilex][tiley] * (1 - (pointx - tilex)) + normals[tilex + 1][tiley] * (pointx - tilex);
927 height2 = normals[tilex][tiley + 1] * (1 - (pointx - tilex)) + normals[tilex + 1][tiley + 1] * (pointx - tilex);
928 total = height1 * (1 - (pointz - tiley)) + height2 * (pointz - tiley);
933 XYZ Terrain::getLighting(float pointx, float pointz)
935 static XYZ height1, height2;
936 static int tilex, tiley;
942 if (pointx >= size - 1 || pointz >= size - 1 || pointx <= 0 || pointz <= 0)
947 height1.x = colors[tilex][tiley][0] * (1 - (pointx - tilex)) + colors[tilex + 1][tiley][0] * (pointx - tilex);
948 height1.y = colors[tilex][tiley][1] * (1 - (pointx - tilex)) + colors[tilex + 1][tiley][1] * (pointx - tilex);
949 height1.z = colors[tilex][tiley][2] * (1 - (pointx - tilex)) + colors[tilex + 1][tiley][2] * (pointx - tilex);
950 height2.x = colors[tilex][tiley + 1][0] * (1 - (pointx - tilex)) + colors[tilex + 1][tiley + 1][0] * (pointx - tilex);
951 height2.y = colors[tilex][tiley + 1][1] * (1 - (pointx - tilex)) + colors[tilex + 1][tiley + 1][1] * (pointx - tilex);
952 height2.z = colors[tilex][tiley + 1][2] * (1 - (pointx - tilex)) + colors[tilex + 1][tiley + 1][2] * (pointx - tilex);
954 return height1 * (1 - (pointz - tiley)) + height2 * (pointz - tiley);
957 void Terrain::draw(int layer)
960 static float opacity;
961 static XYZ terrainpoint;
962 static float distance[subdivision][subdivision];
964 static int beginx, endx;
965 static int beginz, endz;
967 static float patch_size = size / subdivision * scale;
968 static float viewdistsquared;
970 viewdistsquared = viewdistance * viewdistance;
973 beginx = (viewer.x - viewdistance) / (patch_size) - 1;
976 beginz = (viewer.z - viewdistance) / (patch_size) - 1;
980 endx = (viewer.x + viewdistance) / (patch_size) + 1;
981 if (endx > subdivision)
983 endz = (viewer.z + viewdistance) / (patch_size) + 1;
984 if (endz > subdivision)
988 for (i = beginx; i < endx; i++) {
989 for (j = beginz; j < endz; j++) {
990 terrainpoint.x = i * patch_size + (patch_size) / 2;
991 terrainpoint.y = viewer.y; //heightmap[i][j]*scale;
992 terrainpoint.z = j * patch_size + (patch_size) / 2;
993 distance[i][j] = distsq(&viewer, &terrainpoint);
997 for (i = beginx; i < endx; i++) {
998 for (j = beginz; j < endz; j++) {
999 if (distance[i][j] < (viewdistance + patch_size) * (viewdistance + patch_size)) {
1001 if (distance[i][j] > viewdistsquared * fadestart - viewdistsquared)
1003 if (opacity == 1 && i != subdivision)
1004 if (distance[i + 1][j] > viewdistsquared * fadestart - viewdistsquared)
1006 if (opacity == 1 && j != subdivision)
1007 if (distance[i][j + 1] > viewdistsquared * fadestart - viewdistsquared)
1009 if (opacity == 1 && j != subdivision && i != subdivision)
1010 if (distance[i + 1][j + 1] > viewdistsquared * fadestart - viewdistsquared)
1012 glMatrixMode(GL_MODELVIEW);
1014 if (frustum.CubeInFrustum(i * patch_size + patch_size * .5, avgypatch[i][j], j * patch_size + patch_size * .5, heightypatch[i][j] / 2)) {
1015 if (environment == desertenvironment && distance[i][j] > viewdistsquared / 4)
1016 glTexEnvf( GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, blurness);
1017 else if (environment == desertenvironment)
1018 glTexEnvf( GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, 0 );
1019 if (!layer && textureness[i][j] != allsecond)
1020 drawpatch(i, j, opacity);
1021 if (layer == 1 && textureness[i][j] != allfirst)
1022 drawpatchother(i, j, opacity);
1023 if (layer == 2 && textureness[i][j] != allfirst)
1024 drawpatchotherother(i, j, opacity);
1030 if (environment == desertenvironment)
1031 glTexEnvf( GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, 0 );
1034 void Terrain::drawdecals()
1038 static float distancemult;
1039 static int lasttype;
1041 static float viewdistsquared;
1044 viewdistsquared = viewdistance * viewdistance;
1049 glDisable(GL_LIGHTING);
1050 glDisable(GL_CULL_FACE);
1051 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1053 for (i = 0; i < numdecals; i++) {
1054 if (decaltype[i] == blooddecalfast && decalalivetime[i] < 2)
1055 decalalivetime[i] = 2;
1056 if ((decaltype[i] == shadowdecal || decaltype[i] == shadowdecalpermanent) && decaltype[i] != lasttype) {
1057 shadowtexture.bind();
1060 glAlphaFunc(GL_GREATER, 0.0001);
1061 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1064 if (decaltype[i] == footprintdecal && decaltype[i] != lasttype) {
1065 footprinttexture.bind();
1068 glAlphaFunc(GL_GREATER, 0.0001);
1069 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1072 if (decaltype[i] == bodyprintdecal && decaltype[i] != lasttype) {
1073 bodyprinttexture.bind();
1076 glAlphaFunc(GL_GREATER, 0.0001);
1077 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1080 if ((decaltype[i] == blooddecal || decaltype[i] == blooddecalslow) && decaltype[i] != lasttype) {
1081 bloodtexture.bind();
1084 glAlphaFunc(GL_GREATER, 0.15);
1085 glBlendFunc(GL_ONE, GL_ZERO);
1088 if ((decaltype[i] == blooddecalfast) && decaltype[i] != lasttype) {
1089 bloodtexture2.bind();
1092 glAlphaFunc(GL_GREATER, 0.15);
1093 glBlendFunc(GL_ONE, GL_ZERO);
1096 if (decaltype[i] == shadowdecal || decaltype[i] == shadowdecalpermanent) {
1097 distancemult = (viewdistsquared - (distsq(&viewer, &decalposition[i]) - (viewdistsquared * fadestart)) * (1 / (1 - fadestart))) / viewdistsquared;
1098 if (distancemult >= 1)
1099 glColor4f(1, 1, 1, decalopacity[i]);
1100 if (distancemult < 1)
1101 glColor4f(1, 1, 1, decalopacity[i]*distancemult);
1103 if (decaltype[i] == footprintdecal || decaltype[i] == bodyprintdecal) {
1104 distancemult = (viewdistsquared - (distsq(&viewer, &decalposition[i]) - (viewdistsquared * fadestart)) * (1 / (1 - fadestart))) / viewdistsquared;
1105 if (distancemult >= 1) {
1106 glColor4f(1, 1, 1, decalopacity[i]);
1107 if (decalalivetime[i] > 3)
1108 glColor4f(1, 1, 1, decalopacity[i] * (5 - decalalivetime[i]) / 2);
1110 if (distancemult < 1) {
1111 glColor4f(1, 1, 1, decalopacity[i]*distancemult);
1112 if (decalalivetime[i] > 3)
1113 glColor4f(1, 1, 1, decalopacity[i] * (5 - decalalivetime[i]) / 2 * distancemult);
1116 if ((decaltype[i] == blooddecal || decaltype[i] == blooddecalfast || decaltype[i] == blooddecalslow)) {
1117 distancemult = (viewdistsquared - (distsq(&viewer, &decalposition[i]) - (viewdistsquared * fadestart)) * (1 / (1 - fadestart))) / viewdistsquared;
1118 if (distancemult >= 1) {
1119 glColor4f(decalbrightness[i], decalbrightness[i], decalbrightness[i], decalopacity[i]);
1120 if (decalalivetime[i] < 4)
1121 glColor4f(decalbrightness[i], decalbrightness[i], decalbrightness[i], decalopacity[i]*decalalivetime[i]*.25);
1122 if (decalalivetime[i] > 58)
1123 glColor4f(decalbrightness[i], decalbrightness[i], decalbrightness[i], decalopacity[i] * (60 - decalalivetime[i]) / 2);
1125 if (distancemult < 1) {
1126 glColor4f(decalbrightness[i], decalbrightness[i], decalbrightness[i], decalopacity[i]*distancemult);
1127 if (decalalivetime[i] < 4)
1128 glColor4f(decalbrightness[i], decalbrightness[i], decalbrightness[i], decalopacity[i]*decalalivetime[i]*distancemult * .25);
1129 if (decalalivetime[i] > 58)
1130 glColor4f(decalbrightness[i], decalbrightness[i], decalbrightness[i], decalopacity[i] * (60 - decalalivetime[i]) / 2 * distancemult);
1133 lasttype = decaltype[i];
1134 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
1135 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
1137 glMatrixMode(GL_MODELVIEW);
1139 glBegin(GL_TRIANGLES);
1140 for (int j = 0; j < 3; j++) {
1141 glTexCoord2f(decaltexcoords[i][j][0], decaltexcoords[i][j][1]);
1142 glVertex3f(decalvertex[i][j].x, decalvertex[i][j].y, decalvertex[i][j].z);
1147 for (i = numdecals - 1; i >= 0; i--) {
1148 decalalivetime[i] += multiplier;
1149 if (decaltype[i] == blooddecalslow)
1150 decalalivetime[i] -= multiplier * 2 / 3;
1151 if (decaltype[i] == blooddecalfast)
1152 decalalivetime[i] += multiplier * 4;
1153 if (decaltype[i] == shadowdecal)
1155 if (decaltype[i] == footprintdecal && decalalivetime[i] >= 5)
1157 if (decaltype[i] == bodyprintdecal && decalalivetime[i] >= 5)
1159 if ((decaltype[i] == blooddecal || decaltype[i] == blooddecalfast || decaltype[i] == blooddecalslow) && decalalivetime[i] >= 60)
1162 glAlphaFunc(GL_GREATER, 0.0001);
1166 void Terrain::AddObject(XYZ where, float radius, int id)
1171 if (id >= 0 && id < 10000)
1172 for (i = 0; i < subdivision; i++) {
1173 for (j = 0; j < subdivision; j++) {
1174 if (patchobjectnum[i][j] < 300 - 1) {
1176 points[0].x = (size / subdivision) * i;
1177 points[0].z = (size / subdivision) * j;
1178 points[0].y = heightmap[(int)points[0].x][(int)points[0].z];
1179 points[1].x = (size / subdivision) * (i + 1);
1180 points[1].z = (size / subdivision) * j;
1181 points[1].y = heightmap[(int)points[1].x][(int)points[1].z];
1182 points[2].x = (size / subdivision) * (i + 1);
1183 points[2].z = (size / subdivision) * (j + 1);
1184 points[2].y = heightmap[(int)points[2].x][(int)points[2].z];
1185 points[3].x = (size / subdivision) * i;
1186 points[3].z = (size / subdivision) * (j + 1);
1187 points[3].y = heightmap[(int)points[3].x][(int)points[3].z];
1192 if (!done && where.x + radius > points[0].x && where.x - radius < points[2].x && where.z + radius > points[0].z && where.z - radius < points[2].z) {
1193 patchobjects[i][j][patchobjectnum[i][j]] = id;
1194 patchobjectnum[i][j]++;
1202 void Terrain::DeleteDecal(int which)
1205 decaltype[which] = decaltype[numdecals - 1];
1206 decalposition[which] = decalposition[numdecals - 1];
1207 for (int i = 0; i < 3; i++) {
1208 decalvertex[which][i] = decalvertex[numdecals - 1][i];
1209 decaltexcoords[which][i][0] = decaltexcoords[numdecals - 1][i][0];
1210 decaltexcoords[which][i][1] = decaltexcoords[numdecals - 1][i][1];
1212 decalrotation[which] = decalrotation[numdecals - 1];
1213 decalalivetime[which] = decalalivetime[numdecals - 1];
1214 decalopacity[which] = decalopacity[numdecals - 1];
1215 decalbrightness[which] = decalbrightness[numdecals - 1];
1220 void Terrain::MakeDecal(int type, XYZ where, float size, float opacity, float rotation)
1223 if (opacity > 0 && size > 0) {
1224 static int patchx[4];
1225 static int patchy[4];
1227 decaltexcoords[numdecals][0][0] = 1;
1228 decaltexcoords[numdecals][0][1] = 0;
1230 patchx[0] = (where.x + size) / scale;
1231 patchx[1] = (where.x - size) / scale;
1232 patchx[2] = (where.x - size) / scale;
1233 patchx[3] = (where.x + size) / scale;
1235 patchy[0] = (where.z - size) / scale;
1236 patchy[1] = (where.z - size) / scale;
1237 patchy[2] = (where.z + size) / scale;
1238 patchy[3] = (where.z + size) / scale;
1240 /*if(patchx[1]<subdivision-1&&patchy[1]<subdivision-1&&patchx[1]>0&&patchy[1]>0)
1241 if(patchx[2]<subdivision-1&&patchy[2]<subdivision-1&&patchx[2]>0&&patchy[2]>0)
1242 if(patchx[3]<subdivision-1&&patchy[3]<subdivision-1&&patchx[3]>0&&patchy[3]>0)
1243 if(patchx[0]<subdivision-1&&patchy[0]<subdivision-1&&patchx[0]>0&&patchy[0]>0){
1245 if ((patchx[0] != patchx[1] || patchy[0] != patchy[1]) && (patchx[0] != patchx[2] || patchy[0] != patchy[2]) && (patchx[0] != patchx[3] || patchy[0] != patchy[3])) {
1246 MakeDecalLock(type, where, patchx[0], patchy[0], size, opacity, rotation);
1249 if ((patchx[1] != patchx[2] || patchy[1] != patchy[2]) && (patchx[1] != patchx[3] || patchy[1] != patchy[3])) {
1250 MakeDecalLock(type, where, patchx[1], patchy[1], size, opacity, rotation);
1253 if ((patchx[2] != patchx[3] || patchy[2] != patchy[3])) {
1254 MakeDecalLock(type, where, patchx[2], patchy[2], size, opacity, rotation);
1256 MakeDecalLock(type, where, patchx[3], patchy[3], size, opacity, rotation);
1262 void Terrain::MakeDecalLock(int type, XYZ where, int whichx, int whichy, float size, float opacity, float rotation)
1265 static float placex, placez;
1270 rot = getLighting(where.x, where.z);
1271 decalbrightness[numdecals] = (rot.x + rot.y + rot.z) / 3;
1272 if (decalbrightness[numdecals] < .4)
1273 decalbrightness[numdecals] = .4;
1275 if (environment == grassyenvironment) {
1276 decalbrightness[numdecals] *= .6;
1279 if (decalbrightness[numdecals] > 1)
1280 decalbrightness[numdecals] = 1;
1281 decalbright = decalbrightness[numdecals];
1283 decalposition[numdecals] = where;
1284 decaltype[numdecals] = type;
1285 decalopacity[numdecals] = opacity;
1286 decalrotation[numdecals] = rotation;
1287 decalalivetime[numdecals] = 0;
1289 placex = (float)whichx * scale + scale;
1290 placez = (float)whichy * scale;
1292 decaltexcoords[numdecals][0][0] = (placex - where.x) / size / 2 + .5;
1293 decaltexcoords[numdecals][0][1] = (placez - where.z) / size / 2 + .5;
1295 decalvertex[numdecals][0].x = placex;
1296 decalvertex[numdecals][0].z = placez;
1297 decalvertex[numdecals][0].y = heightmap[whichx + 1][whichy] * scale + .01;
1300 placex = (float)whichx * scale + scale;
1301 placez = (float)whichy * scale + scale;
1303 decaltexcoords[numdecals][1][0] = (placex - where.x) / size / 2 + .5;
1304 decaltexcoords[numdecals][1][1] = (placez - where.z) / size / 2 + .5;
1306 decalvertex[numdecals][1].x = placex;
1307 decalvertex[numdecals][1].z = placez;
1308 decalvertex[numdecals][1].y = heightmap[whichx + 1][whichy + 1] * scale + .01;
1311 placex = (float)whichx * scale;
1312 placez = (float)whichy * scale + scale;
1314 decaltexcoords[numdecals][2][0] = (placex - where.x) / size / 2 + .5;
1315 decaltexcoords[numdecals][2][1] = (placez - where.z) / size / 2 + .5;
1317 decalvertex[numdecals][2].x = placex;
1318 decalvertex[numdecals][2].z = placez;
1319 decalvertex[numdecals][2].y = heightmap[whichx][whichy + 1] * scale + .01;
1321 if (decalrotation[numdecals]) {
1322 for (int i = 0; i < 3; i++) {
1324 rot.x = decaltexcoords[numdecals][i][0] - .5;
1325 rot.z = decaltexcoords[numdecals][i][1] - .5;
1326 rot = DoRotation(rot, 0, -decalrotation[numdecals], 0);
1327 decaltexcoords[numdecals][i][0] = rot.x + .5;
1328 decaltexcoords[numdecals][i][1] = rot.z + .5;
1332 if (!(decaltexcoords[numdecals][0][0] < 0 && decaltexcoords[numdecals][1][0] < 0 && decaltexcoords[numdecals][2][0] < 0))
1333 if (!(decaltexcoords[numdecals][0][1] < 0 && decaltexcoords[numdecals][1][1] < 0 && decaltexcoords[numdecals][2][1] < 0))
1334 if (!(decaltexcoords[numdecals][0][0] > 1 && decaltexcoords[numdecals][1][0] > 1 && decaltexcoords[numdecals][2][0] > 1))
1335 if (!(decaltexcoords[numdecals][0][1] > 1 && decaltexcoords[numdecals][1][1] > 1 && decaltexcoords[numdecals][2][1] > 1))
1336 if (numdecals < max_decals - 1)
1339 decalbrightness[numdecals] = decalbright;
1341 decalposition[numdecals] = where;
1342 decaltype[numdecals] = type;
1343 decalopacity[numdecals] = opacity;
1344 decalrotation[numdecals] = rotation;
1345 decalalivetime[numdecals] = 0;
1347 placex = (float)whichx * scale + scale;
1348 placez = (float)whichy * scale;
1350 decaltexcoords[numdecals][0][0] = (placex - where.x) / size / 2 + .5;
1351 decaltexcoords[numdecals][0][1] = (placez - where.z) / size / 2 + .5;
1353 decalvertex[numdecals][0].x = placex;
1354 decalvertex[numdecals][0].z = placez;
1355 decalvertex[numdecals][0].y = heightmap[whichx + 1][whichy] * scale + .01;
1358 placex = (float)whichx * scale;
1359 placez = (float)whichy * scale;
1361 decaltexcoords[numdecals][1][0] = (placex - where.x) / size / 2 + .5;
1362 decaltexcoords[numdecals][1][1] = (placez - where.z) / size / 2 + .5;
1364 decalvertex[numdecals][1].x = placex;
1365 decalvertex[numdecals][1].z = placez;
1366 decalvertex[numdecals][1].y = heightmap[whichx][whichy] * scale + .01;
1369 placex = (float)whichx * scale;
1370 placez = (float)whichy * scale + scale;
1372 decaltexcoords[numdecals][2][0] = (placex - where.x) / size / 2 + .5;
1373 decaltexcoords[numdecals][2][1] = (placez - where.z) / size / 2 + .5;
1375 decalvertex[numdecals][2].x = placex;
1376 decalvertex[numdecals][2].z = placez;
1377 decalvertex[numdecals][2].y = heightmap[whichx][whichy + 1] * scale + .01;
1379 if (decalrotation[numdecals]) {
1380 for (int i = 0; i < 3; i++) {
1382 rot.x = decaltexcoords[numdecals][i][0] - .5;
1383 rot.z = decaltexcoords[numdecals][i][1] - .5;
1384 rot = DoRotation(rot, 0, -decalrotation[numdecals], 0);
1385 decaltexcoords[numdecals][i][0] = rot.x + .5;
1386 decaltexcoords[numdecals][i][1] = rot.z + .5;
1390 if (!(decaltexcoords[numdecals][0][0] < 0 && decaltexcoords[numdecals][1][0] < 0 && decaltexcoords[numdecals][2][0] < 0))
1391 if (!(decaltexcoords[numdecals][0][1] < 0 && decaltexcoords[numdecals][1][1] < 0 && decaltexcoords[numdecals][2][1] < 0))
1392 if (!(decaltexcoords[numdecals][0][0] > 1 && decaltexcoords[numdecals][1][0] > 1 && decaltexcoords[numdecals][2][0] > 1))
1393 if (!(decaltexcoords[numdecals][0][1] > 1 && decaltexcoords[numdecals][1][1] > 1 && decaltexcoords[numdecals][2][1] > 1))
1394 if (numdecals < max_decals - 1)
1399 void Terrain::DoShadows()
1401 static int i, j, k, l, todivide;
1402 static float brightness, total;
1403 static XYZ testpoint, testpoint2, terrainpoint, lightloc, col;
1404 lightloc = light.location;
1405 if (!skyboxtexture) {
1409 if (skyboxtexture && tutoriallevel) {
1415 Normalise(&lightloc);
1417 for (i = 0; i < size; i++) {
1418 for (j = 0; j < size; j++) {
1419 terrainpoint.x = (float)(i) * scale;
1420 terrainpoint.z = (float)(j) * scale;
1421 terrainpoint.y = heightmap[i][j] * scale;
1424 patchx = (float)(i) * subdivision / size;
1425 patchz = (float)(j) * subdivision / size;
1426 if (patchobjectnum[patchx][patchz]) {
1427 for (k = 0; k < patchobjectnum[patchx][patchz]; k++) {
1428 l = patchobjects[patchx][patchz][k];
1429 if (objects.type[l] != treetrunktype) {
1430 testpoint = terrainpoint;
1431 testpoint2 = terrainpoint + lightloc * 50 * (1 - shadowed);
1432 if (objects.model[l].LineCheck(&testpoint, &testpoint2, &col, &objects.position[l], &objects.yaw[l]) != -1) {
1433 shadowed = 1 - (findDistance(&terrainpoint, &col) / 50);
1438 Game::LoadingScreen();
1440 brightness = dotproduct(&lightloc, &normals[i][j]);
1442 brightness *= 1 - shadowed;
1449 colors[i][j][0] = light.color[0] * brightness + light.ambient[0];
1450 colors[i][j][1] = light.color[1] * brightness + light.ambient[1];
1451 colors[i][j][2] = light.color[2] * brightness + light.ambient[2];
1453 if (colors[i][j][0] > 1) colors[i][j][0] = 1;
1454 if (colors[i][j][1] > 1) colors[i][j][1] = 1;
1455 if (colors[i][j][2] > 1) colors[i][j][2] = 1;
1456 if (colors[i][j][0] < 0) colors[i][j][0] = 0;
1457 if (colors[i][j][1] < 0) colors[i][j][1] = 0;
1458 if (colors[i][j][2] < 0) colors[i][j][2] = 0;
1463 Game::LoadingScreen();
1466 for (i = 0; i < size; i++) {
1467 for (j = 0; j < size; j++) {
1468 for (k = 0; k < 3; k++) {
1472 total += colors[j][i - 1][k];
1475 if (i != size - 1) {
1476 total += colors[j][i + 1][k];
1480 total += colors[j - 1][i][k];
1483 if (j != size - 1) {
1484 total += colors[j + 1][i][k];
1487 if (i != 0 && j != 0) {
1488 total += colors[j - 1][i - 1][k];
1491 if (i != size - 1 && j != 0) {
1492 total += colors[j - 1][i + 1][k];
1495 if (j != size - 1 && i != size - 1) {
1496 total += colors[j + 1][i + 1][k];
1499 if (j != size - 1 && i != 0) {
1500 total += colors[j + 1][i - 1][k];
1503 total += colors[j][i][k];
1506 colors[j][i][k] = total / todivide;
1511 for (i = 0; i < subdivision; i++) {
1512 for (j = 0; j < subdivision; j++) {
1513 UpdateVertexArray(i, j);
1522 memset(patchobjectnum, 0, sizeof(patchobjectnum));
1523 memset(patchobjects, 0, sizeof(patchobjects));
1527 memset(heightmap, 0, sizeof(heightmap));
1528 memset(normals, 0, sizeof(normals));
1529 memset(facenormals, 0, sizeof(facenormals));
1530 memset(triangles, 0, sizeof(triangles));
1531 memset(colors, 0, sizeof(colors));
1532 memset(opacityother, 0, sizeof(opacityother));
1533 memset(texoffsetx, 0, sizeof(texoffsetx));
1534 memset(texoffsety, 0, sizeof(texoffsety));
1535 memset(numtris, 0, sizeof(numtris));
1536 memset(textureness, 0, sizeof(textureness));
1538 memset(vArray, 0, sizeof(vArray));
1540 memset(visible, 0, sizeof(visible));
1541 memset(avgypatch, 0, sizeof(avgypatch));
1542 memset(maxypatch, 0, sizeof(maxypatch));
1543 memset(minypatch, 0, sizeof(minypatch));
1544 memset(heightypatch, 0, sizeof(heightypatch));
1548 memset(decaltexcoords, 0, sizeof(decaltexcoords));
1549 memset(decalvertex, 0, sizeof(decalvertex));
1550 memset(decaltype, 0, sizeof(decaltype));
1551 memset(decalopacity, 0, sizeof(decalopacity));
1552 memset(decalrotation, 0, sizeof(decalrotation));
1553 memset(decalalivetime, 0, sizeof(decalalivetime));
1554 memset(decalbrightness, 0, sizeof(decalbrightness));
1555 memset(decalposition, 0, sizeof(decalposition));
1560 terraintexture.destroy();
1561 shadowtexture.destroy();
1562 bodyprinttexture.destroy();
1563 footprinttexture.destroy();
1564 bloodtexture.destroy();
1565 bloodtexture2.destroy();
1566 breaktexture.destroy();