Use rendering at startup only for perlin ground

This commit is contained in:
Théophile Bastian 2018-02-14 17:22:27 +01:00
parent ffaa4ed7c8
commit 24d21d77cb
3 changed files with 47 additions and 16 deletions

View file

@ -50,7 +50,7 @@ int main(int argc, char** argv) {
if(perlin) {
perlin_ground.get_surface()->set_color(Color(0.13, 0.82, 0.21));
render.add_surface(perlin_ground.get_surface(), bbox_2, 1.2);
render.add_surface(perlin_ground.get_surface(), bbox_2, false, 0.50);
}
else {
ground_mesh.get_mesh()->set_color(Color(0.13, 0.82, 0.21));

View file

@ -12,6 +12,13 @@ GlutRender& GlutRender::get_instance() {
return instance;
}
Mesh GlutRender::SurfaceDetails::render() const {
return MarchingCubes(*surface, box,
resolution)
.add_hint(surface->location_hint())
();
}
GlutRender::GlutRender()
: followed_implicit(nullptr),
camera_position(0., 2.5, -10.),
@ -81,13 +88,18 @@ void GlutRender::remove_mesh(Mesh* mesh) {
}
void GlutRender::add_surface(ImplicitSurface* surf, const Cuboid& box,
double resolution)
bool always_render, double resolution)
{
surfaces.insert(SurfaceDetails(surf, box, resolution));
surfaces.push_back(SurfaceDetails(surf, box, resolution, always_render));
}
void GlutRender::remove_surface(ImplicitSurface* surf) {
surfaces.erase(SurfaceDetails(surf, Cuboid::empty(), 0.1));
for(auto it=surfaces.begin(); it != surfaces.end(); ++it) {
if(*it == SurfaceDetails(surf, Cuboid::empty(), 0.1, true)) {
surfaces.erase(it);
break;
}
}
}
void GlutRender::run() {
@ -222,12 +234,16 @@ void GlutRender::display() {
display_mesh(*mesh);
}
for(const SurfaceDetails& surface: surfaces) {
Mesh mesh = MarchingCubes(*surface.surface, surface.box,
surface.resolution)
.add_hint(surface.surface->location_hint())
();
display_mesh(mesh);
for(SurfaceDetails& surface: surfaces) {
if(!surface.always_render) {
if(surface.prerender == nullptr)
surface.self_render();
display_mesh(*surface.prerender);
}
else {
Mesh mesh = surface.render();
display_mesh(mesh);
}
}
glutSwapBuffers();

View file

@ -5,6 +5,7 @@
#include "../Mesh.hpp"
#include "../Implicit.hpp"
#include <set>
#include <vector>
#include <functional>
class GlutRender {
@ -22,7 +23,7 @@ class GlutRender {
void add_mesh(Mesh* mesh);
void remove_mesh(Mesh* mesh);
void add_surface(ImplicitSurface* surf, const Cuboid& box,
double resolution=0.1);
bool always_render=true, double resolution=0.1);
void remove_surface(ImplicitSurface* surf);
void run();
@ -38,15 +39,29 @@ class GlutRender {
private:
struct SurfaceDetails {
SurfaceDetails(ImplicitSurface* surf, const Cuboid& box,
double resolution):
surface(surf), box(box), resolution(resolution) {}
double resolution,
bool always_render):
surface(surf), box(box), resolution(resolution),
always_render(always_render), prerender(nullptr)
{}
~SurfaceDetails() {
if(prerender != nullptr)
delete prerender;
}
ImplicitSurface* surface;
Cuboid box;
double resolution;
bool operator<(const SurfaceDetails& oth) const {
return surface < oth.surface;
bool always_render;
Mesh* prerender;
bool operator==(const SurfaceDetails& oth) const {
return surface == oth.surface;
}
Mesh render() const;
void self_render() {
prerender = new Mesh(render());
}
};
@ -71,7 +86,7 @@ class GlutRender {
std::function<double()> rand_color;
std::set<Mesh*> meshes;
std::set<SurfaceDetails> surfaces;
std::vector<SurfaceDetails> surfaces;
std::vector<kb_handler_t> kb_handlers;
std::vector<kb_handler_t> kb_up_handlers;