From 5c19700e42bd7b639c83f92f6c31aef38ed84373 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9ophile=20Bastian?= Date: Sat, 27 Jan 2018 18:33:50 +0100 Subject: [PATCH] =?UTF-8?q?Create=20a=20basic=20glut=20renderer=20?= =?UTF-8?q?=E2=80=94=20yet=20untested?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common_structures.hpp | 16 ++++++++++ render/GlutRender.cpp | 71 +++++++++++++++++++++++++++++++++++++++++++ render/GlutRender.hpp | 31 +++++++++++++++++++ 3 files changed, 118 insertions(+) create mode 100644 render/GlutRender.cpp create mode 100644 render/GlutRender.hpp diff --git a/common_structures.hpp b/common_structures.hpp index 3993966..b38e2d6 100644 --- a/common_structures.hpp +++ b/common_structures.hpp @@ -3,10 +3,22 @@ **/ #include // Hash +#include +#include struct Point { Point(double x, double y, double z) : x(x), y(y), z(z) {} double x, y, z; + + double operator[](unsigned i) const { + assert(0 <= i && i < 3); + switch(i) { + case 0: return x; + case 1: return y; + case 2: return z; + default: return 0; + } + } }; struct Face { @@ -19,6 +31,10 @@ struct Face { int operator[](unsigned i) const { return vert[i % 3]; // dodge errors } + const Point& pt(int id, const std::vector& pts) const { + assert(0 <= id && id < 3); + return pts[vert[id]]; + } }; namespace std { diff --git a/render/GlutRender.cpp b/render/GlutRender.cpp new file mode 100644 index 0000000..b9632ab --- /dev/null +++ b/render/GlutRender.cpp @@ -0,0 +1,71 @@ +#include "GlutRender.hpp" + +#include +#include +#include + +GlutRender& GlutRender::get_instance() { + static GlutRender instance; + return instance; +} + +GlutRender::GlutRender() { } + +void GlutRender::init(int* argc, char** argv, + int wid, int hei, const char* win_name) +{ + glutInit(argc, argv); + glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); + glutInitWindowSize(wid, hei); + glutCreateWindow(win_name); + + glClearColor(0.0, 0.0, 0.0, 0.0); + glShadeModel(GL_FLAT); + + glutDisplayFunc(display_handle); + glutReshapeFunc(reshape_handle); +} + +void GlutRender::cleanup() { +} + +void GlutRender::add_mesh(const Mesh* mesh) { + meshes.insert(mesh); +} + +void GlutRender::remove_mesh(const Mesh* mesh) { + meshes.erase(mesh); +} + +void GlutRender::run() { +} + +void GlutRender::reshape(int wid, int hei) { + glViewport(0, 0, (GLsizei) wid, (GLsizei) hei); + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + gluOrtho2D(0.0, (GLdouble) wid, 0.0, (GLdouble) hei); +} + +void GlutRender::display() { + for(const Mesh* mesh: meshes) { + const std::vector& points = mesh->get_vertices(); + for(const Face& face: mesh->get_faces()) { + const Point& p0 = face.pt(0, points), + p1 = face.pt(2, points), + p2 = face.pt(2, points); + glBegin(GL_TRIANGLES); + glVertex3d(p0[0], p0[1], p0[2]); + glVertex3d(p1[0], p1[1], p1[2]); + glVertex3d(p2[0], p2[1], p2[2]); + glEnd(); + } + } +} + +void GlutRender::reshape_handle(int wid, int hei) { + get_instance().reshape(wid, hei); +} +void GlutRender::display_handle() { + get_instance().display(); +} diff --git a/render/GlutRender.hpp b/render/GlutRender.hpp new file mode 100644 index 0000000..699a26a --- /dev/null +++ b/render/GlutRender.hpp @@ -0,0 +1,31 @@ +/** The most basic renderer — a stupid glut application */ + +#include "../Mesh.hpp" +#include + +class GlutRender { + public: + 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(const Mesh* mesh); + void remove_mesh(const Mesh* mesh); + void run(); + + private: //meth + GlutRender(); + + protected: + void reshape(int wid, int hei); + void display(); + + static void reshape_handle(int wid, int hei); + static void display_handle(); + private: + std::set meshes; +};