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) { if(perlin) {
perlin_ground.get_surface()->set_color(Color(0.13, 0.82, 0.21)); 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 { else {
ground_mesh.get_mesh()->set_color(Color(0.13, 0.82, 0.21)); 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; return instance;
} }
Mesh GlutRender::SurfaceDetails::render() const {
return MarchingCubes(*surface, box,
resolution)
.add_hint(surface->location_hint())
();
}
GlutRender::GlutRender() GlutRender::GlutRender()
: followed_implicit(nullptr), : followed_implicit(nullptr),
camera_position(0., 2.5, -10.), 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, 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) { 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() { void GlutRender::run() {
@ -222,12 +234,16 @@ void GlutRender::display() {
display_mesh(*mesh); display_mesh(*mesh);
} }
for(const SurfaceDetails& surface: surfaces) { for(SurfaceDetails& surface: surfaces) {
Mesh mesh = MarchingCubes(*surface.surface, surface.box, if(!surface.always_render) {
surface.resolution) if(surface.prerender == nullptr)
.add_hint(surface.surface->location_hint()) surface.self_render();
(); display_mesh(*surface.prerender);
display_mesh(mesh); }
else {
Mesh mesh = surface.render();
display_mesh(mesh);
}
} }
glutSwapBuffers(); glutSwapBuffers();

View file

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