Vec4 Image::get_pixel(int x, int y) const
{
if(is_float) {
- return ((Vec4*)pixels)[y * w + x];
+ return ((Vec4 *)pixels)[y * w + x];
}
Vec4 color;
- unsigned char *pptr = ((unsigned char*)pixels) + (y * w + x) * 4;
+ unsigned char *pptr = ((unsigned char *)pixels) + (y * w + x) * 4;
color.x = pptr[0] / 255.0;
color.y = pptr[1] / 255.0;
color.z = pptr[2] / 255.0;
Vec4 p00 = get_pixel(x0, y0);
Vec4 p01 = get_pixel(x0, y1);
Vec4 p11 = get_pixel(x1, y1);
- Vec4 p10 = get_pixel(x1, y0);
+ Vec4 p10 = get_pixel(x1, y0);
Vec4 ptop = lerp(p01, p11, tu);
Vec4 pbot = lerp(p00, p10, tu);
Object *cow = new Object;
*cow = *cow0;
- if (!gen_poisson(cow_pos, cow_gap, disk_radius))
+ if(!gen_poisson(cow_pos, cow_gap, disk_radius))
goto cowgen_end;
Vec2 pos = cow_pos.back();
float y = terrain.get_height(Vec3(pos.x, 1, pos.y));
cam_dist = 0.0;
}
}
- }
+}
static void clbk_mouse(GLFWwindow *win, int bn, int action, int mods)
{
static bool gen_poisson(std::vector<Vec2> &points, float min_dist, float radius)
{
/* poisson radius */
- for (int i = 0; i < 1000; i++)
- {
+ for(int i = 0; i < 1000; i++) {
float angle = (float)rand() / (float)RAND_MAX * 2 * M_PI;
float r = sqrt((float)rand() / (float)RAND_MAX) * radius;
p.x = cos(angle) * r;
p.y = sin(angle) * r;
- bool valid = true;
+ bool valid = true;
for(size_t j=0; j<points.size(); j++) {
if(length_sq(points[j] - p) < min_dist * min_dist) {
valid = false;
// ------ heightfield ------
static Vec3 hfield_vertex(float u, float v, float h, float xsz,
- float ysz, float height)
+ float ysz, float height)
{
float x = u * xsz - xsz / 2.0;
float y = h * height;
}
void gen_heightfield(Mesh *mesh, float xsz, float ysz, float height, int usub,
- int vsub, float (*calc_height)(float u, float v, void *ptr), void *ptr)
+ int vsub, float (*calc_height)(float u, float v, void *ptr), void *ptr)
{
- /*
+ /*
usub and vsub is the number of subdivision at each axis
- (heightfield = grid)
+ (heightfield = grid)
*/
if(usub < 1)
usub = 1;
slopes in x, z, axis */
Vec3 tangent = hfield_vertex(u + du, v, calc_height(u + du, v, ptr),
- xsz, ysz, height) - vtx;
+ xsz, ysz, height) - vtx;
Vec3 bitangent = hfield_vertex(u, v + dv, calc_height(u, v + dv, ptr),
- xsz, ysz, height) - vtx;
+ xsz, ysz, height) - vtx;
Vec3 normal = normalize(cross(bitangent, tangent));
}
/*
- indices:
+ indices:
*/
uint16_t *iptr = &mesh->indices[0];
/* generates geodesic sphere: if hemi is true we only gen the hemisphere */
void gen_geosphere(Mesh *mesh, float rad, int subdiv, bool hemi = false);
void gen_heightfield(Mesh *mesh, float xsz, float ysz, float height, int usub,
- int vsub, float (*calc_height)(float u, float v, void *ptr), void *ptr);
+ int vsub, float (*calc_height)(float u, float v, void *ptr), void *ptr);
#endif // MESHGEN_H_
for(size_t i=0; i<scene->objects.size(); i++) {
float t = (sin(time_sec + 7.3 * noise(i * M_PI)) + 1) * 0.5;
- if (t_loc != -1)
+ if(t_loc != -1)
sprog->set_uniformf(t_loc, t);
if(mmod_loc != -1)
void MeshGL::draw() const
{
if(!vdata_valid) {
- ((MeshGL*)this)->update_vertex_data();
+ ((MeshGL *)this)->update_vertex_data();
}
glBindVertexArray(vao);
glGenBuffers(1, &nvbo);
glBindBuffer(GL_ARRAY_BUFFER, nvbo);
-
+
glBufferData(GL_ARRAY_BUFFER, normals.size() * sizeof(Vec3) * 2, 0, GL_STATIC_DRAW);
Vec3 *data = (Vec3 *)glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
- for (size_t i = 0; i < normals.size(); i++)
- {
+ for(size_t i = 0; i < normals.size(); i++) {
*data++ = vertices[i];
*data++ = vertices[i] + normals[i] * scale;
}
glVertexAttribPointer(MESH_VERTEX, 3, GL_FLOAT, GL_FALSE, sizeof(Vec3), 0);
glEnableVertexAttribArray(MESH_VERTEX);
glBindBuffer(GL_ARRAY_BUFFER, 0);
- } else {
+ }
+ else {
glBindVertexArray(nvao);
}
glDeleteBuffers(1, &vbo_tex_coords);
if(ibo)
glDeleteBuffers(1, &ibo);
- if (vao)
+ if(vao)
glDeleteVertexArrays(1, &vao);
if(nvbo)
data.yoffs = (float)i / (float)params.ytiles;
gen_heightfield(tile.mesh, txsz, tysz, params.max_height,
- params.tile_usub, params.tile_vsub, calc_height,
- (void*)&data);
+ params.tile_usub, params.tile_vsub, calc_height,
+ (void *)&data);
float xoffs = j * txsz - params.xsz / 2.0 + txsz / 2.0;
float yoffs = i * tysz - params.ysz / 2.0 + tysz / 2.0;
tiles.push_back(tile);
-/*
- the terrain scene stores objects only
- no need to fill the mat, mesh std::vectors
-*/
+ /*
+ 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;
static float calc_height(float u, float v, void *ptr)
{
- GenData *data = (GenData*)ptr;
+ GenData *data = (GenData *)ptr;
const TerrainParams *tp = data->tp;
u = u / tp->xtiles + data->xoffs;
mutable Scene *vis_scene; /* set of visible tiles returned by get_visible */
std::vector<TerrainTile> tiles;
-
+
public:
Material material;
bool generate(const TerrainParams ¶ms);
Scene *get_visible(const Camera *camera) const;
- float get_height(float u, float v) const;
- float get_height(const Vec3 &pos) const; /* world coordinates */
+ float get_height(float u, float v) const;
+ float get_height(const Vec3 &pos) const; /* world coordinates */
};
#endif // TERRAIN_H_
\ No newline at end of file