X-Git-Url: http://git.mutantstargoat.com?p=demo;a=blobdiff_plain;f=src%2Fterrain.cc;h=c4db7edadf066c8618866051da4349067f8f3721;hp=ff54f4603b6336af5b611f48a27f4443d7346858;hb=0d3454cbd5e803b8dea601110d403cd5563df9be;hpb=fd601d4218b63fdf92c5e4dfa32eac8adbda82fa diff --git a/src/terrain.cc b/src/terrain.cc index ff54f46..c4db7ed 100644 --- a/src/terrain.cc +++ b/src/terrain.cc @@ -2,7 +2,6 @@ #include "camera.h" #include "gfxapi.h" -#include "image.h" #include "mesh.h" #include "meshgen.h" #include "object.h" @@ -33,6 +32,7 @@ void Terrain::destroy() } struct GenData { + const Terrain *terrain; const TerrainParams *tp; float xoffs; float yoffs; @@ -41,6 +41,7 @@ struct GenData { bool Terrain::generate(const TerrainParams ¶ms) { tiles.clear(); + this->params = params; float txsz = params.xsz / params.xtiles; float tysz = params.ysz / params.ytiles; @@ -51,6 +52,7 @@ bool Terrain::generate(const TerrainParams ¶ms) tile.mesh = gfx_create_mesh(); GenData data; + data.terrain = this; data.tp = ¶ms; data.xoffs = (float)j / (float)params.xtiles; data.yoffs = (float)i / (float)params.ytiles; @@ -88,24 +90,44 @@ Scene *Terrain::get_visible(const Camera *camera) const return vis_scene; } -static float calc_height(float u, float v, void *ptr) +float Terrain::get_height(const Vec3 &pos) const { - if(!ptr) { - fprintf(stderr, "Terrain parameters not found.\n"); + float x = pos.x; + float y = pos.z; + + float half_width = params.xsz / 2.0; + float half_height = params.ysz / 2.0; + + if(x < -half_width || x > half_width) return 0; + if(y < -half_height || y > half_height) + return 0; + + float u = (x + half_width) / params.xsz; + float v = (y + half_height) / params.ysz; + + return get_height(u, v) * params.max_height; +} + +float Terrain::get_height(float u, float v) const +{ + float sn = gph::fbm(u * params.noise_freq, v * params.noise_freq, params.num_octaves); + sn = sn * 0.5 + 0.5; + + if(params.coarse_heightmap.pixels) { + Vec4 texel = params.coarse_heightmap.lookup_nearest(u, v); + sn *= texel.x; } + return sn; +} +static float calc_height(float u, float v, void *ptr) +{ GenData *data = (GenData*)ptr; const TerrainParams *tp = data->tp; u = u / tp->xtiles + data->xoffs; v = v / tp->ytiles + data->yoffs; - float sn = gph::fbm(u * tp->noise_freq, v * tp->noise_freq, tp->num_octaves); - - if(tp->coarse_heightmap) { - Vec4 texel = tp->coarse_heightmap->lookup_nearest(u, v); - sn *= texel.x; - } - return sn; + return data->terrain->get_height(u, v); } \ No newline at end of file