98 lines
2.8 KiB
C++
98 lines
2.8 KiB
C++
/** The most basic renderer — a stupid glut application */
|
|
|
|
#pragma once
|
|
|
|
#include "../Mesh.hpp"
|
|
#include "../Implicit.hpp"
|
|
#include <set>
|
|
#include <vector>
|
|
#include <functional>
|
|
|
|
class GlutRender {
|
|
public:
|
|
typedef std::function<void(unsigned char, int, int)> kb_handler_t;
|
|
|
|
static GlutRender& get_instance();
|
|
|
|
GlutRender(GlutRender const&) = delete;
|
|
void operator=(GlutRender const&) = delete;
|
|
|
|
void init(int* argc, char** argv,
|
|
int wid, int hei, const char* win_name);
|
|
void cleanup();
|
|
void add_mesh(Mesh* mesh);
|
|
void remove_mesh(Mesh* mesh);
|
|
void add_surface(ImplicitSurface* surf, const Cuboid& box,
|
|
bool always_render=true, double resolution=0.1);
|
|
void remove_surface(ImplicitSurface* surf);
|
|
void run();
|
|
|
|
void set_idle_func(void (*func)(void));
|
|
|
|
void add_kb_handler(kb_handler_t handler);
|
|
void add_kb_up_handler(kb_handler_t handler);
|
|
|
|
void follow_implicit_position(const ImplicitSurface* surf);
|
|
|
|
void set_camera(const Point& location, const Point& sight);
|
|
|
|
private:
|
|
struct SurfaceDetails {
|
|
SurfaceDetails(ImplicitSurface* surf, const Cuboid& box,
|
|
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 always_render;
|
|
Mesh* prerender;
|
|
|
|
bool operator==(const SurfaceDetails& oth) const {
|
|
return surface == oth.surface;
|
|
}
|
|
Mesh render() const;
|
|
void self_render() {
|
|
prerender = new Mesh(render());
|
|
}
|
|
};
|
|
|
|
|
|
private: //meth
|
|
GlutRender();
|
|
|
|
void display_mesh(Mesh& mesh) const;
|
|
|
|
protected:
|
|
void reshape(int wid, int hei);
|
|
void display();
|
|
void on_kb_evt(bool up, unsigned char key, int x, int y);
|
|
|
|
static void reshape_handle(int wid, int hei);
|
|
static void display_handle();
|
|
|
|
static void kb_evt_handle(unsigned char key, int x, int y);
|
|
static void kb_evt_up_handle(unsigned char key, int x, int y);
|
|
|
|
private: //attr
|
|
std::function<double()> rand_color;
|
|
|
|
std::set<Mesh*> meshes;
|
|
std::vector<SurfaceDetails> surfaces;
|
|
|
|
std::vector<kb_handler_t> kb_handlers;
|
|
std::vector<kb_handler_t> kb_up_handlers;
|
|
|
|
const ImplicitSurface* followed_implicit;
|
|
|
|
Point camera_position, camera_sight;
|
|
};
|