1 #include <gmath/gmath.h>
12 static float calc_height(float u, float v, void *ptr);
25 vis_scene = new Scene;
30 void Terrain::destroy()
36 const TerrainParams *tp;
41 bool Terrain::generate(const TerrainParams ¶ms)
45 float txsz = params.xsz / params.xtiles;
46 float tysz = params.ysz / params.ytiles;
48 for(int i=0; i<params.ytiles; i++) {
49 for(int j=0; j<params.xtiles; j++) {
51 tile.mesh = gfx_create_mesh();
55 data.xoffs = (float)j / (float)params.xtiles;
56 data.yoffs = (float)i / (float)params.ytiles;
58 gen_heightfield(tile.mesh, txsz, tysz, params.max_height,
59 params.tile_usub, params.tile_vsub, calc_height,
62 float xoffs = j * txsz - params.xsz / 2.0 + txsz / 2.0;
63 float yoffs = i * tysz - params.ysz / 2.0 + tysz / 2.0;
66 offmat.translation(xoffs, 0, yoffs);
67 tile.mesh->transform(offmat);
69 tiles.push_back(tile);
72 the terrain scene stores objects only
73 no need to fill the mat, mesh std::vectors
75 Object *o = new Object;
77 o->material = &material;
78 o->transform = Mat4::identity;
80 vis_scene->objects.push_back(o);
86 Scene *Terrain::get_visible(const Camera *camera) const
91 static float calc_height(float u, float v, void *ptr)
94 fprintf(stderr, "Terrain parameters not found.\n");
98 GenData *data = (GenData*)ptr;
99 const TerrainParams *tp = data->tp;
101 // float ufreq = tp->noise_freq / tp->xtiles;
102 // float vfreq = tp->noise_freq / tp->ytiles;
104 u = u / tp->xtiles + data->xoffs;
105 v = v / tp->ytiles + data->yoffs;
107 float sn = gph::fbm(u * tp->noise_freq, v * tp->noise_freq, tp->num_octaves);
108 /* todo use the image later */