1 #include <gmath/gmath.h>
11 static float calc_height(float u, float v, void *ptr);
24 vis_scene = new Scene;
29 void Terrain::destroy()
35 const Terrain *terrain;
36 const TerrainParams *tp;
41 bool Terrain::generate(const TerrainParams ¶ms)
44 this->params = params;
46 float txsz = params.xsz / params.xtiles;
47 float tysz = params.ysz / params.ytiles;
49 for(int i=0; i<params.ytiles; i++) {
50 for(int j=0; j<params.xtiles; j++) {
52 tile.mesh = gfx_create_mesh();
57 data.xoffs = (float)j / (float)params.xtiles;
58 data.yoffs = (float)i / (float)params.ytiles;
60 gen_heightfield(tile.mesh, txsz, tysz, params.max_height,
61 params.tile_usub, params.tile_vsub, calc_height,
64 float xoffs = j * txsz - params.xsz / 2.0 + txsz / 2.0;
65 float yoffs = i * tysz - params.ysz / 2.0 + tysz / 2.0;
68 offmat.translation(xoffs, 0, yoffs);
69 tile.mesh->transform(offmat);
71 tiles.push_back(tile);
74 the terrain scene stores objects only
75 no need to fill the mat, mesh std::vectors
77 Object *o = new Object;
79 o->material = &material;
80 o->transform = Mat4::identity;
82 vis_scene->objects.push_back(o);
88 Scene *Terrain::get_visible(const Camera *camera) const
93 float Terrain::get_height(const Vec3 &pos) const
98 float half_width = params.xsz / 2.0;
99 float half_height = params.ysz / 2.0;
101 if(x < -half_width || x > half_width)
103 if(y < -half_height || y > half_height)
106 float u = (x + half_width) / params.xsz;
107 float v = (y + half_height) / params.ysz;
109 return get_height(u, v) * params.max_height;
112 float Terrain::get_height(float u, float v) const
114 float sn = gph::fbm(u * params.noise_freq, v * params.noise_freq, params.num_octaves);
117 if(params.coarse_heightmap.pixels) {
118 Vec4 texel = params.coarse_heightmap.lookup_linear(u, v, 1.0 / params.tile_usub, 1.0 / params.tile_vsub);
124 static float calc_height(float u, float v, void *ptr)
126 GenData *data = (GenData *)ptr;
127 const TerrainParams *tp = data->tp;
129 u = u / tp->xtiles + data->xoffs;
130 v = v / tp->ytiles + data->yoffs;
132 return data->terrain->get_height(u, v);