Create a basic glut renderer — yet untested
This commit is contained in:
parent
1375560cb4
commit
5c19700e42
3 changed files with 118 additions and 0 deletions
|
@ -3,10 +3,22 @@
|
|||
**/
|
||||
|
||||
#include <functional> // Hash
|
||||
#include <vector>
|
||||
#include <cassert>
|
||||
|
||||
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<Point>& pts) const {
|
||||
assert(0 <= id && id < 3);
|
||||
return pts[vert[id]];
|
||||
}
|
||||
};
|
||||
|
||||
namespace std {
|
||||
|
|
71
render/GlutRender.cpp
Normal file
71
render/GlutRender.cpp
Normal file
|
@ -0,0 +1,71 @@
|
|||
#include "GlutRender.hpp"
|
||||
|
||||
#include <GL/gl.h>
|
||||
#include <GL/glut.h>
|
||||
#include <cstring>
|
||||
|
||||
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<Point>& 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();
|
||||
}
|
31
render/GlutRender.hpp
Normal file
31
render/GlutRender.hpp
Normal file
|
@ -0,0 +1,31 @@
|
|||
/** The most basic renderer — a stupid glut application */
|
||||
|
||||
#include "../Mesh.hpp"
|
||||
#include <set>
|
||||
|
||||
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<const Mesh*> meshes;
|
||||
};
|
Loading…
Reference in a new issue