X-Git-Url: http://git.mutantstargoat.com?p=demo;a=blobdiff_plain;f=src%2Fterrain.cc;h=ff54f4603b6336af5b611f48a27f4443d7346858;hp=e69de29bb2d1d6434b8b29ae775ad8c2e48c5391;hb=fd601d4218b63fdf92c5e4dfa32eac8adbda82fa;hpb=54cc76a89b4221fac1b7f4a58259ca0ce836177d diff --git a/src/terrain.cc b/src/terrain.cc index e69de29..ff54f46 100644 --- a/src/terrain.cc +++ b/src/terrain.cc @@ -0,0 +1,111 @@ +#include + +#include "camera.h" +#include "gfxapi.h" +#include "image.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 TerrainParams *tp; + float xoffs; + float yoffs; +}; + +bool Terrain::generate(const TerrainParams ¶ms) +{ + tiles.clear(); + + 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; +} + +static float calc_height(float u, float v, void *ptr) +{ + if(!ptr) { + fprintf(stderr, "Terrain parameters not found.\n"); + return 0; + } + + 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; +} \ No newline at end of file