From 24d21d77cba25e31e7324c2e493acd3af9c74fb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9ophile=20Bastian?= Date: Wed, 14 Feb 2018 17:22:27 +0100 Subject: [PATCH] Use rendering at startup only for perlin ground --- main_bounce.cpp | 2 +- render/GlutRender.cpp | 34 +++++++++++++++++++++++++--------- render/GlutRender.hpp | 27 +++++++++++++++++++++------ 3 files changed, 47 insertions(+), 16 deletions(-) diff --git a/main_bounce.cpp b/main_bounce.cpp index 58d022b..bbef7f3 100644 --- a/main_bounce.cpp +++ b/main_bounce.cpp @@ -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)); diff --git a/render/GlutRender.cpp b/render/GlutRender.cpp index c00cc57..3a0c50d 100644 --- a/render/GlutRender.cpp +++ b/render/GlutRender.cpp @@ -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(); diff --git a/render/GlutRender.hpp b/render/GlutRender.hpp index cc76fa9..3705014 100644 --- a/render/GlutRender.hpp +++ b/render/GlutRender.hpp @@ -5,6 +5,7 @@ #include "../Mesh.hpp" #include "../Implicit.hpp" #include +#include #include 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 rand_color; std::set meshes; - std::set surfaces; + std::vector surfaces; std::vector kb_handlers; std::vector kb_up_handlers;