mpri-graphics-project/render/GlutRender.hpp

82 lines
2.3 KiB
C++

/** The most basic renderer — a stupid glut application */
#pragma once
#include "../Mesh.hpp"
#include "../Implicit.hpp"
#include <set>
#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,
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):
surface(surf), box(box), resolution(resolution) {}
ImplicitSurface* surface;
Cuboid box;
double resolution;
bool operator<(const SurfaceDetails& oth) const {
return surface < oth.surface;
}
};
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::set<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;
};