X-Git-Url: https://git.jsancho.org/?a=blobdiff_plain;ds=sidebyside;f=Source%2FTerrain.cpp;h=41a2181cacefe40556cec8b44fe5ca731f375733;hb=20e924d;hp=58a31f919f8d543ce518684a93abe53a9e58db2f;hpb=24004d6ab1e68faaf85ece11b566449997da5013;p=lugaru.git
diff --git a/Source/Terrain.cpp b/Source/Terrain.cpp
index 58a31f9..41a2181 100644
--- a/Source/Terrain.cpp
+++ b/Source/Terrain.cpp
@@ -1,27 +1,28 @@
/*
Copyright (C) 2003, 2010 - Wolfire Games
+Copyright (C) 2010-2016 - Lugaru contributors (see AUTHORS file)
This file is part of Lugaru.
-Lugaru is free software; you can redistribute it and/or
-modify it under the terms of the GNU General Public License
-as published by the Free Software Foundation; either version 2
-of the License, or (at your option) any later version.
+Lugaru is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
-This program is distributed in the hope that it will be useful,
+Lugaru is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
-
-See the GNU General Public License for more details.
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
You should have received a copy of the GNU General Public License
-along with this program; if not, write to the Free Software
-Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+along with Lugaru. If not, see .
*/
#include "Game.h"
#include "Terrain.h"
#include "Objects.h"
+#include "Utils/Folders.h"
+
extern XYZ viewer;
extern float viewdistance;
extern float fadestart;
@@ -36,7 +37,6 @@ extern bool decals;
extern float blurness;
extern float targetblurness;
extern Objects objects;
-extern TGAImageRec texture;
extern bool visibleloading;
extern bool skyboxtexture;
extern int tutoriallevel;
@@ -52,9 +52,7 @@ int Terrain::lineTerrain(XYZ p1, XYZ p2, XYZ *p)
static int firstintersecting;
static XYZ point;
static int startx, starty;
- static float slope;
static int endx, endy;
- static int numtris = (size - 1) * (size - 1) * 2;
static float highest, lowest;
firstintersecting = -1;
@@ -82,20 +80,28 @@ int Terrain::lineTerrain(XYZ p1, XYZ p2, XYZ *p)
starty = i;
}
- if (startx < 0)startx = 0;
- if (starty < 0)starty = 0;
- if (endx > size - 1)endx = size - 1;
- if (endy > size - 1)endy = size - 1;
+ if (startx < 0)
+ startx = 0;
+ if (starty < 0)
+ starty = 0;
+ if (endx > size - 1)
+ endx = size - 1;
+ if (endy > size - 1)
+ endy = size - 1;
for (i = startx; i <= endx; i++) {
for (j = starty; j <= endy; j++) {
highest = -1000;
lowest = 1000;
for (k = 0; k < 2; k++) {
- if (heightmap[i + k][j] > highest)highest = heightmap[i + k][j];
- if (heightmap[i + k][j] < lowest)lowest = heightmap[i + k][j];
- if (heightmap[i + k][j + 1] > highest)highest = heightmap[i + k][j + 1];
- if (heightmap[i + k][j + 1] < lowest)lowest = heightmap[i + k][j + 1];
+ if (heightmap[i + k][j] > highest)
+ highest = heightmap[i + k][j];
+ if (heightmap[i + k][j] < lowest)
+ lowest = heightmap[i + k][j];
+ if (heightmap[i + k][j + 1] > highest)
+ highest = heightmap[i + k][j + 1];
+ if (heightmap[i + k][j + 1] < lowest)
+ lowest = heightmap[i + k][j + 1];
}
if ((p1.y <= highest || p2.y <= highest) && (p1.y >= lowest || p2.y >= lowest)) {
triangles[0].x = i;
@@ -164,7 +170,8 @@ void Terrain::UpdateTransparency(int whichx, int whichy)
vertex.z = j * scale;
vertex.y = heightmap[i][j] * scale;
distance = distsq(&viewer, &vertex);
- if (distance > viewdistsquared)distance = viewdistsquared;
+ if (distance > viewdistsquared)
+ distance = viewdistsquared;
colors[i][j][3] = (viewdistsquared - (distance - (viewdistsquared * fadestart)) * (1 / (1 - fadestart))) / viewdistsquared;
}
}
@@ -192,9 +199,7 @@ void Terrain::UpdateTransparency(int whichx, int whichy)
void Terrain::UpdateTransparencyother(int whichx, int whichy)
{
- static XYZ vertex;
static int i, j, a, b, c, d, patch_size, stepsize;
- static float distance;
patch_size = size / subdivision;
@@ -242,7 +247,8 @@ void Terrain::UpdateTransparencyotherother(int whichx, int whichy)
vertex.z = j * scale;
vertex.y = heightmap[i][j] * scale;
distance = distsq(&viewer, &vertex);
- if (distance > viewdistsquared)distance = viewdistsquared;
+ if (distance > viewdistsquared)
+ distance = viewdistsquared;
colors[i][j][3] = (viewdistsquared - (distance - (viewdistsquared * fadestart)) * (1 / (1 - fadestart))) / viewdistsquared;
}
}
@@ -350,12 +356,15 @@ void Terrain::UpdateVertexArray(int whichx, int whichy)
minypatch[whichx][whichy] = 10000;
for (a = 0; a < size / subdivision; a++) {
for (b = 0; b < size / subdivision; b++) {
- if (heightmap[(size / subdivision)*whichx + a][(size / subdivision)*whichy + b]*scale > maxypatch[whichx][whichy]) maxypatch[whichx][whichy] = heightmap[(size / subdivision) * whichx + a][(size / subdivision) * whichy + b] * scale;
- if (heightmap[(size / subdivision)*whichx + a][(size / subdivision)*whichy + b]*scale < minypatch[whichx][whichy]) minypatch[whichx][whichy] = heightmap[(size / subdivision) * whichx + a][(size / subdivision) * whichy + b] * scale;
+ if (heightmap[(size / subdivision)*whichx + a][(size / subdivision)*whichy + b]*scale > maxypatch[whichx][whichy])
+ maxypatch[whichx][whichy] = heightmap[(size / subdivision) * whichx + a][(size / subdivision) * whichy + b] * scale;
+ if (heightmap[(size / subdivision)*whichx + a][(size / subdivision)*whichy + b]*scale < minypatch[whichx][whichy])
+ minypatch[whichx][whichy] = heightmap[(size / subdivision) * whichx + a][(size / subdivision) * whichy + b] * scale;
}
}
heightypatch[whichx][whichy] = (maxypatch[whichx][whichy] - minypatch[whichx][whichy]);
- if (heightypatch[whichx][whichy] < size / subdivision * scale)heightypatch[whichx][whichy] = size / subdivision * scale;
+ if (heightypatch[whichx][whichy] < size / subdivision * scale)
+ heightypatch[whichx][whichy] = size / subdivision * scale;
avgypatch[whichx][whichy] = (minypatch[whichx][whichy] + maxypatch[whichx][whichy]) / 2;
for (i = whichx * size / subdivision; i < (whichx + 1)*size / subdivision - 1; i++) {
@@ -391,20 +400,18 @@ void Terrain::UpdateVertexArray(int whichx, int whichy)
bool Terrain::load(const char *fileName)
{
- static long i, j;
+ static long i, j;
static long x, y;
static float patch_size;
float temptexdetail = texdetail;
- //LoadTGA( fileName );
- // Fixing filename so that it works with its own os
- char * FixedFN = ConvertFileName(fileName);
+ ImageRec texture;
- unsigned char fileNamep[256];
- CopyCStringToPascal(FixedFN, fileNamep);
//Load Image
- upload_image( fileNamep , 0);
+ if (!load_image(Folders::getResourcePath(fileName).c_str(), texture)) {
+ return false;
+ }
//Is it valid?
if (texture.bpp > 24) {
@@ -419,39 +426,21 @@ bool Terrain::load(const char *fileName)
}
}
texture.bpp = 24;
- if (visibleloading) Game::LoadingScreen();
+ if (visibleloading)
+ Game::LoadingScreen();
texdetail = temptexdetail;
- size = 128;
- if (1 == 1) {
- /*if ( texture.bpp == 24 )
- type = GL_RGB;
- else
- type = GL_RGBA;
-
- glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
-
- if(!terraintexture)glGenTextures( 1, &terraintexture );
- glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
-
- glBindTexture( GL_TEXTURE_2D, terraintexture);
- //glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST );
- glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
+ size = texture.sizeX;
- gluBuild2DMipmaps( GL_TEXTURE_2D, type, texture.sizeX, texture.sizeY, type, GL_UNSIGNED_BYTE, texture.data );
- */
-
- size = texture.sizeX;
-
- for (i = 0; i < size; i++) {
- for (j = 0; j < size; j++) {
- heightmap[size - 1 - i][j] = (float)((texture.data[(i + (j * size)) * texture.bpp / 8])) / 5;
- }
+ for (i = 0; i < size; i++) {
+ for (j = 0; j < size; j++) {
+ heightmap[size - 1 - i][j] = (float)((texture.data[(i + (j * size)) * texture.bpp / 8])) / 5;
}
-
}
- if (visibleloading) Game::LoadingScreen();
+
+ if (visibleloading)
+ Game::LoadingScreen();
float slopeness;
@@ -460,7 +449,8 @@ bool Terrain::load(const char *fileName)
textureness[i][j] = -1;
}
}
- if (visibleloading) Game::LoadingScreen();
+ if (visibleloading)
+ Game::LoadingScreen();
for (i = 0; i < size; i++) {
@@ -476,7 +466,8 @@ bool Terrain::load(const char *fileName)
slopeness = heightmap[i][j] - heightmap[i][j - 1];
}
opacityother[i][j] = slopeness * slopeness * 2;
- if (opacityother[i][j] > 1)opacityother[i][j] = 1;
+ if (opacityother[i][j] > 1)
+ opacityother[i][j] = 1;
opacityother[i][j] -= (float)abs(Random() % 100) / 300;
}
if (environment == desertenvironment) {
@@ -484,7 +475,8 @@ bool Terrain::load(const char *fileName)
slopeness = heightmap[i][j] - heightmap[i][j - 1];
}
opacityother[i][j] = slopeness * slopeness * 2;
- if (opacityother[i][j] > 1)opacityother[i][j] = 1;
+ if (opacityother[i][j] > 1)
+ opacityother[i][j] = 1;
opacityother[i][j] -= (float)abs(Random() % 100) / 300;
}
if (environment == grassyenvironment) {
@@ -501,12 +493,14 @@ bool Terrain::load(const char *fileName)
slopeness = heightmap[i][j] - heightmap[i][j + 1];
}
opacityother[i][j] = slopeness * slopeness * 10;
- if (opacityother[i][j] > 1)opacityother[i][j] = 1;
+ if (opacityother[i][j] > 1)
+ opacityother[i][j] = 1;
opacityother[i][j] -= (float)abs(Random() % 100) / 100;
}
}
}
- if (visibleloading) Game::LoadingScreen();
+ if (visibleloading)
+ Game::LoadingScreen();
for (i = 0; i < size; i++) {
for (j = 0; j < size; j++) {
@@ -518,64 +512,60 @@ bool Terrain::load(const char *fileName)
}
}
}
- if (visibleloading) Game::LoadingScreen();
-
- /*float total;
- int todivide;
- //Smooth opacityother
- for(i=0;i 0) {
i--;
- if (opacityother[x][y] && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allfirst)textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] = mixed;
- if (opacityother[x][y] != 1 && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allsecond)textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] = mixed;
- if (opacityother[i][j] && textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] == allfirst)textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] = mixed;
- if (opacityother[i][j] != 1 && textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] == allsecond)textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] = mixed;
+ if (opacityother[x][y] && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allfirst)
+ textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] = mixed;
+ if (opacityother[x][y] != 1 && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allsecond)
+ textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] = mixed;
+ if (opacityother[i][j] && textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] == allfirst)
+ textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] = mixed;
+ if (opacityother[i][j] != 1 && textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] == allsecond)
+ textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] = mixed;
if (j > 0) {
j--;
- if (opacityother[x][y] && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allfirst)textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] = mixed;
- if (opacityother[x][y] != 1 && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allsecond)textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] = mixed;
- if (opacityother[i][j] && textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] == allfirst)textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] = mixed;
- if (opacityother[i][j] != 1 && textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] == allsecond)textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] = mixed;
+ if (opacityother[x][y] && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allfirst)
+ textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] = mixed;
+ if (opacityother[x][y] != 1 && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allsecond)
+ textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] = mixed;
+ if (opacityother[i][j] && textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] == allfirst)
+ textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] = mixed;
+ if (opacityother[i][j] != 1 && textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] == allsecond)
+ textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] = mixed;
j++;
}
if (j < size - 1) {
j++;
- if (opacityother[x][y] && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allfirst)textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] = mixed;
- if (opacityother[x][y] != 1 && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allsecond)textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] = mixed;
- if (opacityother[i][j] && textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] == allfirst)textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] = mixed;
- if (opacityother[i][j] != 1 && textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] == allsecond)textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] = mixed;
+ if (opacityother[x][y] && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allfirst)
+ textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] = mixed;
+ if (opacityother[x][y] != 1 && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allsecond)
+ textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] = mixed;
+ if (opacityother[i][j] && textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] == allfirst)
+ textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] = mixed;
+ if (opacityother[i][j] != 1 && textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] == allsecond)
+ textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] = mixed;
j--;
}
i++;
@@ -583,26 +573,38 @@ bool Terrain::load(const char *fileName)
if (i < size - 1) {
i++;
- if (opacityother[x][y] && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allfirst)textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] = mixed;
- if (opacityother[x][y] != 1 && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allsecond)textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] = mixed;
- if (opacityother[i][j] && textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] == allfirst)textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] = mixed;
- if (opacityother[i][j] != 1 && textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] == allsecond)textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] = mixed;
+ if (opacityother[x][y] && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allfirst)
+ textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] = mixed;
+ if (opacityother[x][y] != 1 && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allsecond)
+ textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] = mixed;
+ if (opacityother[i][j] && textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] == allfirst)
+ textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] = mixed;
+ if (opacityother[i][j] != 1 && textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] == allsecond)
+ textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] = mixed;
if (j > 0) {
j--;
- if (opacityother[x][y] && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allfirst)textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] = mixed;
- if (opacityother[x][y] != 1 && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allsecond)textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] = mixed;
- if (opacityother[i][j] && textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] == allfirst)textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] = mixed;
- if (opacityother[i][j] != 1 && textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] == allsecond)textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] = mixed;
+ if (opacityother[x][y] && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allfirst)
+ textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] = mixed;
+ if (opacityother[x][y] != 1 && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allsecond)
+ textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] = mixed;
+ if (opacityother[i][j] && textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] == allfirst)
+ textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] = mixed;
+ if (opacityother[i][j] != 1 && textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] == allsecond)
+ textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] = mixed;
j++;
}
if (j < size - 1) {
j++;
- if (opacityother[x][y] && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allfirst)textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] = mixed;
- if (opacityother[x][y] != 1 && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allsecond)textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] = mixed;
- if (opacityother[i][j] && textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] == allfirst)textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] = mixed;
- if (opacityother[i][j] != 1 && textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] == allsecond)textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] = mixed;
+ if (opacityother[x][y] && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allfirst)
+ textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] = mixed;
+ if (opacityother[x][y] != 1 && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allsecond)
+ textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] = mixed;
+ if (opacityother[i][j] && textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] == allfirst)
+ textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] = mixed;
+ if (opacityother[i][j] != 1 && textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] == allsecond)
+ textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] = mixed;
j--;
}
i--;
@@ -610,38 +612,40 @@ bool Terrain::load(const char *fileName)
if (j > 0) {
j--;
- if (opacityother[x][y] && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allfirst)textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] = mixed;
- if (opacityother[x][y] != 1 && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allsecond)textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] = mixed;
- if (opacityother[i][j] && textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] == allfirst)textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] = mixed;
- if (opacityother[i][j] != 1 && textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] == allsecond)textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] = mixed;
+ if (opacityother[x][y] && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allfirst)
+ textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] = mixed;
+ if (opacityother[x][y] != 1 && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allsecond)
+ textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] = mixed;
+ if (opacityother[i][j] && textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] == allfirst)
+ textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] = mixed;
+ if (opacityother[i][j] != 1 && textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] == allsecond)
+ textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] = mixed;
j++;
}
if (j < size - 1) {
j++;
- if (opacityother[x][y] && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allfirst)textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] = mixed;
- if (opacityother[x][y] != 1 && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allsecond)textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] = mixed;
- if (opacityother[i][j] && textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] == allfirst)textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] = mixed;
- if (opacityother[i][j] != 1 && textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] == allsecond)textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] = mixed;
+ if (opacityother[x][y] && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allfirst)
+ textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] = mixed;
+ if (opacityother[x][y] != 1 && textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] == allsecond)
+ textureness[(int)(i * subdivision / size)][(int)(j * subdivision / size)] = mixed;
+ if (opacityother[i][j] && textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] == allfirst)
+ textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] = mixed;
+ if (opacityother[i][j] != 1 && textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] == allsecond)
+ textureness[(int)(x * subdivision / size)][(int)(y * subdivision / size)] = mixed;
j--;
}
}
}
- if (visibleloading) Game::LoadingScreen();
+ if (visibleloading)
+ Game::LoadingScreen();
patch_size = size / subdivision;
patch_elements = (patch_size) * (patch_size) * 54;
CalculateNormals();
- /*DoShadows();
-
- for(i=0;i= 1)glDisable(GL_BLEND);
+ if (opacity >= 1)
+ glDisable(GL_BLEND);
if (opacity < 1) {
glEnable(GL_BLEND);
UpdateTransparency(whichx, whichy);
@@ -802,14 +806,14 @@ void Terrain::drawpatchotherother(int whichx, int whichy, float opacity)
float Terrain::getHeight(float pointx, float pointz)
{
- static float height1, height2;
static int tilex, tiley;
- static XYZ startpoint, endpoint, intersect, triangle[3], average;
+ static XYZ startpoint, endpoint, intersect, triangle[3];
pointx /= scale;
pointz /= scale;
- if (pointx >= size - 1 || pointz >= size - 1 || pointx <= 0 || pointz <= 0)return 0;
+ if (pointx >= size - 1 || pointz >= size - 1 || pointx <= 0 || pointz <= 0)
+ return 0;
startpoint.x = pointx;
startpoint.y = -1000;
@@ -848,81 +852,8 @@ float Terrain::getHeight(float pointx, float pointz)
LineFacetd(&startpoint, &endpoint, &triangle[0], &triangle[1], &triangle[2], &intersect);
}
return intersect.y * scale + getOpacity(pointx * scale, pointz * scale) / 8;
-
- //height1=heightmap[tilex][tiley]*(1-(pointx-tilex))+heightmap[tilex+1][tiley]*(pointx-tilex);
- //height2=heightmap[tilex][tiley+1]*(1-(pointx-tilex))+heightmap[tilex+1][tiley+1]*(pointx-tilex);
-
- //return height1*(1-(pointz-tiley))*scale+height2*(pointz-tiley)*scale;
-}
-
-float Terrain::getHeightExtrude(float pointx, float pointz, float point2x, float point2z)
-{
- static float height1, height2;
- static int tilex, tiley;
- static XYZ startpoint, endpoint, intersect, triangle[3], average;
-
- pointx /= scale;
- pointz /= scale;
- point2x /= scale;
- point2z /= scale;
-
- if (pointx >= size - 1 || pointz >= size - 1 || pointx <= 0 || pointz <= 0)return 0;
- if (point2x >= size - 1 || point2z >= size - 1 || point2x <= 0 || point2z <= 0)return 0;
-
- startpoint.x = point2x;
- startpoint.y = -1000;
- startpoint.z = point2z;
-
- endpoint = startpoint;
- endpoint.y = 1000;
-
- tilex = pointx;
- tiley = pointz;
-
- triangle[0].x = tilex;
- triangle[0].z = tiley;
- triangle[0].y = heightmap[tilex][tiley];
-
- triangle[1].x = tilex + 1;
- triangle[1].z = tiley;
- triangle[1].y = heightmap[tilex + 1][tiley];
-
- triangle[2].x = tilex;
- triangle[2].z = tiley + 1;
- triangle[2].y = heightmap[tilex][tiley + 1];
-
- XYZ mid;
-
- mid = (triangle[0] + triangle[1] + triangle[2]) / 2;
-
- triangle[0] = mid + (triangle[0] - mid) * 10;
- triangle[1] = mid + (triangle[0] - mid) * 10;
- triangle[2] = mid + (triangle[0] - mid) * 10;
-
- /*
- if(!LineFacetd(&startpoint,&endpoint,&triangle[0],&triangle[1],&triangle[2],&intersect)){
- triangle[0].x=tilex+1;
- triangle[0].z=tiley;
- triangle[0].y=heightmap[tilex+1][tiley];
-
- triangle[1].x=tilex+1;
- triangle[1].z=tiley+1;
- triangle[1].y=heightmap[tilex+1][tiley+1];
-
- triangle[2].x=tilex;
- triangle[2].z=tiley+1;
- triangle[2].y=heightmap[tilex][tiley+1];
- LineFacetd(&startpoint,&endpoint,&triangle[0],&triangle[1],&triangle[2],&intersect);
- }*/
- return intersect.y * scale + getOpacity(pointx * scale, pointz * scale) / 8;
-
- //height1=heightmap[tilex][tiley]*(1-(pointx-tilex))+heightmap[tilex+1][tiley]*(pointx-tilex);
- //height2=heightmap[tilex][tiley+1]*(1-(pointx-tilex))+heightmap[tilex+1][tiley+1]*(pointx-tilex);
-
- //return height1*(1-(pointz-tiley))*scale+height2*(pointz-tiley)*scale;
}
-
float Terrain::getOpacity(float pointx, float pointz)
{
static float height1, height2;
@@ -931,7 +862,8 @@ float Terrain::getOpacity(float pointx, float pointz)
pointx /= scale;
pointz /= scale;
- if (pointx >= size - 1 || pointz >= size - 1 || pointx <= 0 || pointz <= 0)return 0;
+ if (pointx >= size - 1 || pointz >= size - 1 || pointx <= 0 || pointz <= 0)
+ return 0;
tilex = pointx;
tiley = pointz;
@@ -951,7 +883,8 @@ XYZ Terrain::getNormal(float pointx, float pointz)
pointz /= scale;
height1 = 0;
- if (pointx >= size - 1 || pointz >= size - 1 || pointx <= 0 || pointz <= 0)return height1;
+ if (pointx >= size - 1 || pointz >= size - 1 || pointx <= 0 || pointz <= 0)
+ return height1;
tilex = pointx;
tiley = pointz;
@@ -971,7 +904,8 @@ XYZ Terrain::getLighting(float pointx, float pointz)
pointz /= scale;
height1 = 0;
- if (pointx >= size - 1 || pointz >= size - 1 || pointx <= 0 || pointz <= 0)return height1;
+ if (pointx >= size - 1 || pointz >= size - 1 || pointx <= 0 || pointz <= 0)
+ return height1;
tilex = pointx;
tiley = pointz;
@@ -1002,14 +936,18 @@ void Terrain::draw(int layer)
//Only nearby blocks
beginx = (viewer.x - viewdistance) / (patch_size) - 1;
- if (beginx < 0)beginx = 0;
+ if (beginx < 0)
+ beginx = 0;
beginz = (viewer.z - viewdistance) / (patch_size) - 1;
- if (beginz < 0)beginz = 0;
+ if (beginz < 0)
+ beginz = 0;
endx = (viewer.x + viewdistance) / (patch_size) + 1;
- if (endx > subdivision)endx = subdivision;
+ if (endx > subdivision)
+ endx = subdivision;
endz = (viewer.z + viewdistance) / (patch_size) + 1;
- if (endz > subdivision)endz = subdivision;
+ if (endz > subdivision)
+ endz = subdivision;
if (!layer) {
for (i = beginx; i < endx; i++) {
@@ -1036,7 +974,7 @@ void Terrain::draw(int layer)
if (opacity == 1 && j != subdivision && i != subdivision)
if (distance[i + 1][j + 1] > viewdistsquared * fadestart - viewdistsquared)
opacity = 0;
- glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
+ glMatrixMode(GL_MODELVIEW);
glPushMatrix();
if (frustum.CubeInFrustum(i * patch_size + patch_size * .5, avgypatch[i][j], j * patch_size + patch_size * .5, heightypatch[i][j] / 2)) {
if (environment == desertenvironment && distance[i][j] > viewdistsquared / 4)
@@ -1061,11 +999,10 @@ void Terrain::draw(int layer)
void Terrain::drawdecals()
{
if (decals) {
- static int i, j;
+ static int i;
static float distancemult;
static int lasttype;
- static float patch_size = size / subdivision * scale;
static float viewdistsquared;
static bool blend;
@@ -1079,7 +1016,8 @@ void Terrain::drawdecals()
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDepthMask(0);
for (i = 0; i < numdecals; i++) {
- if (decaltype[i] == blooddecalfast && decalalivetime[i] < 2)decalalivetime[i] = 2;
+ if (decaltype[i] == blooddecalfast && decalalivetime[i] < 2)
+ decalalivetime[i] = 2;
if ((decaltype[i] == shadowdecal || decaltype[i] == shadowdecalpermanent) && decaltype[i] != lasttype) {
shadowtexture.bind();
if (!blend) {
@@ -1122,38 +1060,46 @@ void Terrain::drawdecals()
}
if (decaltype[i] == shadowdecal || decaltype[i] == shadowdecalpermanent) {
distancemult = (viewdistsquared - (distsq(&viewer, &decalposition[i]) - (viewdistsquared * fadestart)) * (1 / (1 - fadestart))) / viewdistsquared;
- if (distancemult >= 1)glColor4f(1, 1, 1, decalopacity[i]);
- if (distancemult < 1)glColor4f(1, 1, 1, decalopacity[i]*distancemult);
+ if (distancemult >= 1)
+ glColor4f(1, 1, 1, decalopacity[i]);
+ if (distancemult < 1)
+ glColor4f(1, 1, 1, decalopacity[i]*distancemult);
}
if (decaltype[i] == footprintdecal || decaltype[i] == bodyprintdecal) {
distancemult = (viewdistsquared - (distsq(&viewer, &decalposition[i]) - (viewdistsquared * fadestart)) * (1 / (1 - fadestart))) / viewdistsquared;
if (distancemult >= 1) {
glColor4f(1, 1, 1, decalopacity[i]);
- if (decalalivetime[i] > 3)glColor4f(1, 1, 1, decalopacity[i] * (5 - decalalivetime[i]) / 2);
+ if (decalalivetime[i] > 3)
+ glColor4f(1, 1, 1, decalopacity[i] * (5 - decalalivetime[i]) / 2);
}
if (distancemult < 1) {
glColor4f(1, 1, 1, decalopacity[i]*distancemult);
- if (decalalivetime[i] > 3)glColor4f(1, 1, 1, decalopacity[i] * (5 - decalalivetime[i]) / 2 * distancemult);
+ if (decalalivetime[i] > 3)
+ glColor4f(1, 1, 1, decalopacity[i] * (5 - decalalivetime[i]) / 2 * distancemult);
}
}
if ((decaltype[i] == blooddecal || decaltype[i] == blooddecalfast || decaltype[i] == blooddecalslow)) {
distancemult = (viewdistsquared - (distsq(&viewer, &decalposition[i]) - (viewdistsquared * fadestart)) * (1 / (1 - fadestart))) / viewdistsquared;
if (distancemult >= 1) {
glColor4f(decalbrightness[i], decalbrightness[i], decalbrightness[i], decalopacity[i]);
- if (decalalivetime[i] < 4)glColor4f(decalbrightness[i], decalbrightness[i], decalbrightness[i], decalopacity[i]*decalalivetime[i]*.25);
- if (decalalivetime[i] > 58)glColor4f(decalbrightness[i], decalbrightness[i], decalbrightness[i], decalopacity[i] * (60 - decalalivetime[i]) / 2);
+ if (decalalivetime[i] < 4)
+ glColor4f(decalbrightness[i], decalbrightness[i], decalbrightness[i], decalopacity[i]*decalalivetime[i]*.25);
+ if (decalalivetime[i] > 58)
+ glColor4f(decalbrightness[i], decalbrightness[i], decalbrightness[i], decalopacity[i] * (60 - decalalivetime[i]) / 2);
}
if (distancemult < 1) {
glColor4f(decalbrightness[i], decalbrightness[i], decalbrightness[i], decalopacity[i]*distancemult);
- if (decalalivetime[i] < 4)glColor4f(decalbrightness[i], decalbrightness[i], decalbrightness[i], decalopacity[i]*decalalivetime[i]*distancemult * .25);
- if (decalalivetime[i] > 58)glColor4f(decalbrightness[i], decalbrightness[i], decalbrightness[i], decalopacity[i] * (60 - decalalivetime[i]) / 2 * distancemult);
+ if (decalalivetime[i] < 4)
+ glColor4f(decalbrightness[i], decalbrightness[i], decalbrightness[i], decalopacity[i]*decalalivetime[i]*distancemult * .25);
+ if (decalalivetime[i] > 58)
+ glColor4f(decalbrightness[i], decalbrightness[i], decalbrightness[i], decalopacity[i] * (60 - decalalivetime[i]) / 2 * distancemult);
}
}
lasttype = decaltype[i];
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
- glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
+ glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glBegin(GL_TRIANGLES);
for (int j = 0; j < 3; j++) {
@@ -1165,12 +1111,18 @@ void Terrain::drawdecals()
}
for (i = numdecals - 1; i >= 0; i--) {
decalalivetime[i] += multiplier;
- if (decaltype[i] == blooddecalslow)decalalivetime[i] -= multiplier * 2 / 3;
- if (decaltype[i] == blooddecalfast)decalalivetime[i] += multiplier * 4;
- if (decaltype[i] == shadowdecal)DeleteDecal(i);
- if (decaltype[i] == footprintdecal && decalalivetime[i] >= 5)DeleteDecal(i);
- if (decaltype[i] == bodyprintdecal && decalalivetime[i] >= 5)DeleteDecal(i);
- if ((decaltype[i] == blooddecal || decaltype[i] == blooddecalfast || decaltype[i] == blooddecalslow) && decalalivetime[i] >= 60)DeleteDecal(i);
+ if (decaltype[i] == blooddecalslow)
+ decalalivetime[i] -= multiplier * 2 / 3;
+ if (decaltype[i] == blooddecalfast)
+ decalalivetime[i] += multiplier * 4;
+ if (decaltype[i] == shadowdecal)
+ DeleteDecal(i);
+ if (decaltype[i] == footprintdecal && decalalivetime[i] >= 5)
+ DeleteDecal(i);
+ if (decaltype[i] == bodyprintdecal && decalalivetime[i] >= 5)
+ DeleteDecal(i);
+ if ((decaltype[i] == blooddecal || decaltype[i] == blooddecalfast || decaltype[i] == blooddecalslow) && decalalivetime[i] >= 60)
+ DeleteDecal(i);
}
glAlphaFunc(GL_GREATER, 0.0001);
}
@@ -1250,11 +1202,6 @@ void Terrain::MakeDecal(int type, XYZ where, float size, float opacity, float ro
patchy[2] = (where.z + size) / scale;
patchy[3] = (where.z + size) / scale;
- /*if(patchx[1]0&&patchy[1]>0)
- if(patchx[2]0&&patchy[2]>0)
- if(patchx[3]0&&patchy[3]>0)
- if(patchx[0]0&&patchy[0]>0){
- */
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])) {
MakeDecalLock(type, where, patchx[0], patchy[0], size, opacity, rotation);
}
@@ -1282,13 +1229,15 @@ void Terrain::MakeDecalLock(int type, XYZ where, int whichx, int whichy, float s
rot = getLighting(where.x, where.z);
decalbrightness[numdecals] = (rot.x + rot.y + rot.z) / 3;
- if (decalbrightness[numdecals] < .4)decalbrightness[numdecals] = .4;
+ if (decalbrightness[numdecals] < .4)
+ decalbrightness[numdecals] = .4;
if (environment == grassyenvironment) {
decalbrightness[numdecals] *= .6;
}
- if (decalbrightness[numdecals] > 1)decalbrightness[numdecals] = 1;
+ if (decalbrightness[numdecals] > 1)
+ decalbrightness[numdecals] = 1;
decalbright = decalbrightness[numdecals];
decalposition[numdecals] = where;
@@ -1344,7 +1293,8 @@ void Terrain::MakeDecalLock(int type, XYZ where, int whichx, int whichy, float s
if (!(decaltexcoords[numdecals][0][1] < 0 && decaltexcoords[numdecals][1][1] < 0 && decaltexcoords[numdecals][2][1] < 0))
if (!(decaltexcoords[numdecals][0][0] > 1 && decaltexcoords[numdecals][1][0] > 1 && decaltexcoords[numdecals][2][0] > 1))
if (!(decaltexcoords[numdecals][0][1] > 1 && decaltexcoords[numdecals][1][1] > 1 && decaltexcoords[numdecals][2][1] > 1))
- if (numdecals < max_decals - 1)numdecals++;
+ if (numdecals < max_decals - 1)
+ numdecals++;
decalbrightness[numdecals] = decalbright;
@@ -1401,88 +1351,8 @@ void Terrain::MakeDecalLock(int type, XYZ where, int whichx, int whichy, float s
if (!(decaltexcoords[numdecals][0][1] < 0 && decaltexcoords[numdecals][1][1] < 0 && decaltexcoords[numdecals][2][1] < 0))
if (!(decaltexcoords[numdecals][0][0] > 1 && decaltexcoords[numdecals][1][0] > 1 && decaltexcoords[numdecals][2][0] > 1))
if (!(decaltexcoords[numdecals][0][1] > 1 && decaltexcoords[numdecals][1][1] > 1 && decaltexcoords[numdecals][2][1] > 1))
- if (numdecals < max_decals - 1)numdecals++;
- }
-}
-
-void Terrain::DoLighting()
-{
- static int i, j, k, todivide;
- static float brightness, total;
- static XYZ blank, terrainpoint, lightloc;
- lightloc = light.location;
- Normalise(&lightloc);
- //Calculate shadows
- for (i = 0; i < size; i++) {
- for (j = 0; j < size; j++) {
- terrainpoint.x = (float)i * scale;
- terrainpoint.z = (float)j * scale;
- terrainpoint.y = heightmap[i][j] * scale + .1;
- /*brightness=0;
- if(lineTerrain(lightlocation*10+terrainpoint,terrainpoint,&blank)==-1)
- */
- brightness = dotproduct(&lightloc, &normals[i][j]);
-
- if (brightness > 1)brightness = 1;
- if (brightness < 0)brightness = 0;
-
- colors[i][j][0] = light.color[0] * brightness + light.ambient[0];
- colors[i][j][1] = light.color[1] * brightness + light.ambient[1];
- colors[i][j][2] = light.color[2] * brightness + light.ambient[2];
-
- if (colors[i][j][0] > 1)colors[i][j][0] = 1;
- if (colors[i][j][1] > 1)colors[i][j][1] = 1;
- if (colors[i][j][2] > 1)colors[i][j][2] = 1;
- if (colors[i][j][0] < 0)colors[i][j][0] = 0;
- if (colors[i][j][1] < 0)colors[i][j][1] = 0;
- if (colors[i][j][2] < 0)colors[i][j][2] = 0;
- }
- }
-
- //Smooth shadows
- for (i = 0; i < size; i++) {
- for (j = 0; j < size; j++) {
- for (k = 0; k < 3; k++) {
- total = 0;
- todivide = 0;
- if (i != 0) {
- total += colors[j][i - 1][k];
- todivide++;
- }
- if (i != size - 1) {
- total += colors[j][i + 1][k];
- todivide++;
- }
- if (j != 0) {
- total += colors[j - 1][i][k];
- todivide++;
- }
- if (j != size - 1) {
- total += colors[j + 1][i][k];
- todivide++;
- }
- if (i != 0 && j != 0) {
- total += colors[j - 1][i - 1][k];
- todivide++;
- }
- if (i != size - 1 && j != 0) {
- total += colors[j - 1][i + 1][k];
- todivide++;
- }
- if (j != size - 1 && i != size - 1) {
- total += colors[j + 1][i + 1][k];
- todivide++;
- }
- if (j != size - 1 && i != 0) {
- total += colors[j + 1][i - 1][k];
- todivide++;
- }
- total += colors[j][i][k];
- todivide++;
-
- colors[j][i][k] = total / todivide;
- }
- }
+ if (numdecals < max_decals - 1)
+ numdecals++;
}
}
@@ -1524,28 +1394,33 @@ void Terrain::DoShadows()
}
}
}
- if (visibleloading) Game::LoadingScreen();
+ if (visibleloading)
+ Game::LoadingScreen();
}
brightness = dotproduct(&lightloc, &normals[i][j]);
- if (shadowed)brightness *= 1 - shadowed;
+ if (shadowed)
+ brightness *= 1 - shadowed;
- if (brightness > 1)brightness = 1;
- if (brightness < 0)brightness = 0;
+ if (brightness > 1)
+ brightness = 1;
+ if (brightness < 0)
+ brightness = 0;
colors[i][j][0] = light.color[0] * brightness + light.ambient[0];
colors[i][j][1] = light.color[1] * brightness + light.ambient[1];
colors[i][j][2] = light.color[2] * brightness + light.ambient[2];
- if (colors[i][j][0] > 1)colors[i][j][0] = 1;
- if (colors[i][j][1] > 1)colors[i][j][1] = 1;
- if (colors[i][j][2] > 1)colors[i][j][2] = 1;
- if (colors[i][j][0] < 0)colors[i][j][0] = 0;
- if (colors[i][j][1] < 0)colors[i][j][1] = 0;
- if (colors[i][j][2] < 0)colors[i][j][2] = 0;
+ if (colors[i][j][0] > 1) colors[i][j][0] = 1;
+ if (colors[i][j][1] > 1) colors[i][j][1] = 1;
+ if (colors[i][j][2] > 1) colors[i][j][2] = 1;
+ if (colors[i][j][0] < 0) colors[i][j][0] = 0;
+ if (colors[i][j][1] < 0) colors[i][j][1] = 0;
+ if (colors[i][j][2] < 0) colors[i][j][2] = 0;
}
}
- if (visibleloading) Game::LoadingScreen();
+ if (visibleloading)
+ Game::LoadingScreen();
//Smooth shadows
for (i = 0; i < size; i++) {