X-Git-Url: http://git.mutantstargoat.com?a=blobdiff_plain;f=src%2Fterrain.cc;h=0f100742ea7f9045eb6adb9bea02aa5e42a79338;hb=136a7fcbfabb2f0b0b3e340213e36a548715a370;hp=e69de29bb2d1d6434b8b29ae775ad8c2e48c5391;hpb=54cc76a89b4221fac1b7f4a58259ca0ce836177d;p=demo diff --git a/src/terrain.cc b/src/terrain.cc index e69de29..0f10074 100644 --- a/src/terrain.cc +++ b/src/terrain.cc @@ -0,0 +1,133 @@ +#include + +#include "camera.h" +#include "gfxapi.h" +#include "mesh.h" +#include "meshgen.h" +#include "object.h" +#include "scene.h" +#include "terrain.h" + +static float calc_height(float u, float v, void *ptr); + +Terrain::Terrain() +{ + vis_scene = 0; +} + +Terrain::~Terrain() +{ +} + +bool Terrain::init() +{ + vis_scene = new Scene; + + return true; +} + +void Terrain::destroy() +{ + delete vis_scene; +} + +struct GenData { + const Terrain *terrain; + const TerrainParams *tp; + float xoffs; + float yoffs; +}; + +bool Terrain::generate(const TerrainParams ¶ms) +{ + tiles.clear(); + this->params = params; + + float txsz = params.xsz / params.xtiles; + float tysz = params.ysz / params.ytiles; + + for(int i=0; itransform(offmat); + + tiles.push_back(tile); + +/* + the terrain scene stores objects only + no need to fill the mat, mesh std::vectors +*/ + Object *o = new Object; + o->mesh = tile.mesh; + o->material = &material; + o->transform = Mat4::identity; + + vis_scene->objects.push_back(o); + } + } + return true; +} + +Scene *Terrain::get_visible(const Camera *camera) const +{ + return vis_scene; +} + +float Terrain::get_height(const Vec3 &pos) const +{ + 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_linear(u, v, 1.0 / params.tile_usub, 1.0 / params.tile_vsub); + 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; + + return data->terrain->get_height(u, v); +} \ No newline at end of file