X-Git-Url: https://git.jsancho.org/?a=blobdiff_plain;f=Source%2FEnvironment%2FTerrain.cpp;h=d3a6f001346f14b8a7dbdbe5668665267e188de5;hb=8b6e8f3ad7390309795eb35c0959264cb7924402;hp=6218c307bf2af99666f610183557282c0245f19b;hpb=828b5b9ec7818c7047e96aebeb67f79837cbc793;p=lugaru.git diff --git a/Source/Environment/Terrain.cpp b/Source/Environment/Terrain.cpp index 6218c30..d3a6f00 100644 --- a/Source/Environment/Terrain.cpp +++ b/Source/Environment/Terrain.cpp @@ -22,8 +22,8 @@ along with Lugaru. If not, see . #include "Game.hpp" #include "Objects/Object.hpp" -#include "Utils/Folders.hpp" #include "Tutorial.hpp" +#include "Utils/Folders.hpp" extern XYZ viewer; extern float viewdistance; @@ -42,7 +42,7 @@ extern bool skyboxtexture; //Functions -int Terrain::lineTerrain(XYZ p1, XYZ p2, XYZ *p) +int Terrain::lineTerrain(XYZ p1, XYZ p2, XYZ* p) { static int i, j, k; static float distance; @@ -277,7 +277,6 @@ void Terrain::UpdateVertexArray(int whichx, int whichy) { static int i, j, a, b, c, patch_size, stepsize; - numtris[whichx][whichy] = 0; patch_size = size / subdivision; @@ -355,9 +354,9 @@ 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]) + 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]) + 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; } } @@ -366,8 +365,8 @@ void Terrain::UpdateVertexArray(int whichx, int whichy) 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++) { - for (j = whichy * size / subdivision; j < (whichy + 1)*size / subdivision - 1; j++) { + for (i = whichx * size / subdivision; i < (whichx + 1) * size / subdivision - 1; i++) { + for (j = whichy * size / subdivision; j < (whichy + 1) * size / subdivision - 1; j++) { triangles[(i * (size - 1) * 2) + (j * 2)][0].x = i * scale; triangles[(i * (size - 1) * 2) + (j * 2)][0].y = heightmap[i][j] * scale; triangles[(i * (size - 1) * 2) + (j * 2)][0].z = j * scale; @@ -393,10 +392,8 @@ void Terrain::UpdateVertexArray(int whichx, int whichy) triangles[(i * (size - 1) * 2) + (j * 2) + 1][2].z = j * scale + 1 * scale; } } - } - bool Terrain::load(const std::string& fileName) { static long i, j; @@ -448,7 +445,6 @@ bool Terrain::load(const std::string& fileName) } Game::LoadingScreen(); - for (i = 0; i < size; i++) { for (j = 0; j < size; j++) { heightmap[i][j] *= .5; @@ -628,14 +624,13 @@ bool Terrain::load(const std::string& fileName) 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--; - } } } Game::LoadingScreen(); patch_size = size / subdivision; - patch_elements = (patch_size) * (patch_size) * 54; + patch_elements = patch_size * patch_size * 54; CalculateNormals(); return true; @@ -682,7 +677,6 @@ void Terrain::CalculateNormals() normals[i][j + 1] = normals[i][j + 1] + facenormal; normals[i + 1][j] = normals[i + 1][j] + facenormal; - a.x = i + 1; a.y = heightmap[i + 1][j]; a.z = j; @@ -800,7 +794,6 @@ void Terrain::drawpatchotherother(int whichx, int whichy) glMatrixMode(GL_MODELVIEW); } - float Terrain::getHeight(float pointx, float pointz) { static int tilex, tiley; @@ -932,17 +925,17 @@ void Terrain::draw(int layer) viewdistsquared = viewdistance * viewdistance; //Only nearby blocks - beginx = (viewer.x - viewdistance) / (patch_size) - 1; + beginx = ((viewer.x - viewdistance) / patch_size) - 1; if (beginx < 0) beginx = 0; - beginz = (viewer.z - viewdistance) / (patch_size) - 1; + beginz = ((viewer.z - viewdistance) / patch_size) - 1; if (beginz < 0) beginz = 0; - endx = (viewer.x + viewdistance) / (patch_size) + 1; + endx = ((viewer.x + viewdistance) / patch_size) + 1; if (endx > subdivision) endx = subdivision; - endz = (viewer.z + viewdistance) / (patch_size) + 1; + endz = ((viewer.z + viewdistance) / patch_size) + 1; if (endz > subdivision) endz = subdivision; @@ -975,9 +968,9 @@ void Terrain::draw(int layer) 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) - glTexEnvf( GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, blurness); + glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, blurness); else if (environment == desertenvironment) - glTexEnvf( GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, 0 ); + glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, 0); if (!layer && textureness[i][j] != allsecond) drawpatch(i, j, opacity); if (layer == 1 && textureness[i][j] != allfirst) @@ -990,7 +983,7 @@ void Terrain::draw(int layer) } } if (environment == desertenvironment) - glTexEnvf( GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, 0 ); + glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, 0); } void Terrain::drawdecals() @@ -1061,7 +1054,7 @@ void Terrain::drawdecals() if (distancemult >= 1) glColor4f(1, 1, 1, decals[i].opacity); if (distancemult < 1) - glColor4f(1, 1, 1, decals[i].opacity*distancemult); + glColor4f(1, 1, 1, decals[i].opacity * distancemult); } if (decals[i].type == footprintdecal || decals[i].type == bodyprintdecal) { distancemult = (viewdistsquared - (distsq(&viewer, &decals[i].position) - (viewdistsquared * fadestart)) * (1 / (1 - fadestart))) / viewdistsquared; @@ -1071,7 +1064,7 @@ void Terrain::drawdecals() glColor4f(1, 1, 1, decals[i].opacity * (5 - decals[i].alivetime) / 2); } if (distancemult < 1) { - glColor4f(1, 1, 1, decals[i].opacity*distancemult); + glColor4f(1, 1, 1, decals[i].opacity * distancemult); if (decals[i].alivetime > 3) glColor4f(1, 1, 1, decals[i].opacity * (5 - decals[i].alivetime) / 2 * distancemult); } @@ -1081,14 +1074,14 @@ void Terrain::drawdecals() if (distancemult >= 1) { glColor4f(decals[i].brightness, decals[i].brightness, decals[i].brightness, decals[i].opacity); if (decals[i].alivetime < 4) - glColor4f(decals[i].brightness, decals[i].brightness, decals[i].brightness, decals[i].opacity*decals[i].alivetime*.25); + glColor4f(decals[i].brightness, decals[i].brightness, decals[i].brightness, decals[i].opacity * decals[i].alivetime * .25); if (decals[i].alivetime > 58) glColor4f(decals[i].brightness, decals[i].brightness, decals[i].brightness, decals[i].opacity * (60 - decals[i].alivetime) / 2); } if (distancemult < 1) { - glColor4f(decals[i].brightness, decals[i].brightness, decals[i].brightness, decals[i].opacity*distancemult); + glColor4f(decals[i].brightness, decals[i].brightness, decals[i].brightness, decals[i].opacity * distancemult); if (decals[i].alivetime < 4) - glColor4f(decals[i].brightness, decals[i].brightness, decals[i].brightness, decals[i].opacity*decals[i].alivetime*distancemult * .25); + glColor4f(decals[i].brightness, decals[i].brightness, decals[i].brightness, decals[i].opacity * decals[i].alivetime * distancemult * .25); if (decals[i].alivetime > 58) glColor4f(decals[i].brightness, decals[i].brightness, decals[i].brightness, decals[i].opacity * (60 - decals[i].alivetime) / 2 * distancemult); } @@ -1141,7 +1134,7 @@ void Terrain::AddObject(XYZ where, float radius, int id) if (id >= 0 && id < 10000) { for (int i = 0; i < subdivision; i++) { for (int j = 0; j < subdivision; j++) { - if (patchobjectnum[i][j] < 300 - 1) { + if (patchobjects[i][j].size() < 300 - 1) { points[0].x = (size / subdivision) * i; points[0].z = (size / subdivision) * j; points[0].y = heightmap[(int)points[0].x][(int)points[0].z]; @@ -1151,8 +1144,7 @@ void Terrain::AddObject(XYZ where, float radius, int id) points[0] *= scale; points[1] *= scale; if (where.x + radius > points[0].x && where.x - radius < points[1].x && where.z + radius > points[0].z && where.z - radius < points[1].z) { - patchobjects[i][j][patchobjectnum[i][j]] = id; - patchobjectnum[i][j]++; + patchobjects[i][j].push_back(id); } } } @@ -1240,8 +1232,6 @@ void Terrain::MakeDecalLock(decal_type type, XYZ where, int whichx, int whichy, void Terrain::DoShadows() { - static int i, j, k, l, todivide; - static float brightness, total; static XYZ testpoint, testpoint2, terrainpoint, lightloc, col; lightloc = light.location; if (!skyboxtexture) { @@ -1256,18 +1246,18 @@ void Terrain::DoShadows() float shadowed; 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; + for (short int i = 0; i < size; i++) { + for (short int j = 0; j < size; j++) { + terrainpoint.x = (float)i * scale; + terrainpoint.z = (float)j * scale; terrainpoint.y = heightmap[i][j] * scale; shadowed = 0; - patchx = (float)(i) * subdivision / size; - patchz = (float)(j) * subdivision / size; - if (patchobjectnum[patchx][patchz]) { - for (k = 0; k < patchobjectnum[patchx][patchz]; k++) { - l = patchobjects[patchx][patchz][k]; + patchx = (float)i * subdivision / size; + patchz = (float)j * subdivision / size; + if (patchobjects[patchx][patchz].size()) { + for (unsigned int k = 0; k < patchobjects[patchx][patchz].size(); k++) { + unsigned int l = patchobjects[patchx][patchz][k]; if (Object::objects[l]->type != treetrunktype) { testpoint = terrainpoint; testpoint2 = terrainpoint + lightloc * 50 * (1 - shadowed); @@ -1278,7 +1268,7 @@ void Terrain::DoShadows() } Game::LoadingScreen(); } - brightness = dotproduct(&lightloc, &normals[i][j]); + float brightness = dotproduct(&lightloc, &normals[i][j]); if (shadowed) brightness *= 1 - shadowed; @@ -1291,23 +1281,29 @@ void Terrain::DoShadows() 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; } } Game::LoadingScreen(); //Smooth shadows - for (i = 0; i < size; i++) { - for (j = 0; j < size; j++) { - for (k = 0; k < 3; k++) { - total = 0; - todivide = 0; + for (short int i = 0; i < size; i++) { + for (short int j = 0; j < size; j++) { + for (short int k = 0; k < 3; k++) { + float total = 0; + unsigned int todivide = 0; if (i != 0) { total += colors[j][i - 1][k]; todivide++; @@ -1348,8 +1344,8 @@ void Terrain::DoShadows() } } - for (i = 0; i < subdivision; i++) { - for (j = 0; j < subdivision; j++) { + for (unsigned int i = 0; i < subdivision; i++) { + for (unsigned int j = 0; j < subdivision; j++) { UpdateVertexArray(i, j); } } @@ -1359,9 +1355,6 @@ Terrain::Terrain() { size = 0; - memset(patchobjectnum, 0, sizeof(patchobjectnum)); - memset(patchobjects, 0, sizeof(patchobjects)); - scale = 1.0f; type = 0; memset(heightmap, 0, sizeof(heightmap));