mpri-graphics-project/render/GlutRender.cpp

128 lines
3.2 KiB
C++
Raw Normal View History

#include "GlutRender.hpp"
2018-02-12 18:40:05 +01:00
#include "../MarchingCubes.hpp"
#include <GL/gl.h>
#include <GL/glut.h>
#include <cstring>
#include <random>
#include <ctime>
GlutRender& GlutRender::get_instance() {
static GlutRender instance;
return instance;
}
2018-02-12 18:40:05 +01:00
GlutRender::GlutRender() {
std::default_random_engine rand_engine(time(NULL));
std::uniform_real_distribution<double> distribution;
rand_color = std::bind(distribution, rand_engine);
}
void GlutRender::init(int* argc, char** argv,
int wid, int hei, const char* win_name)
{
glutInit(argc, argv);
2018-02-06 22:15:19 +01:00
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(wid, hei);
glutCreateWindow(win_name);
glutDisplayFunc(display_handle);
glutReshapeFunc(reshape_handle);
2018-02-06 19:10:08 +01:00
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Background color
glClearDepth(1.0f); // Set background depth to farthest
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glShadeModel(GL_SMOOTH); // Enable smooth shading
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}
void GlutRender::cleanup() {
}
2018-02-06 21:05:45 +01:00
void GlutRender::add_mesh(Mesh* mesh) {
meshes.insert(mesh);
}
2018-02-06 21:05:45 +01:00
void GlutRender::remove_mesh(Mesh* mesh) {
meshes.erase(mesh);
}
2018-02-12 18:40:05 +01:00
void GlutRender::add_surface(ImplicitSurface* surf, const Cuboid& box) {
surfaces.insert(SurfaceDetails(surf, box));
}
void GlutRender::remove_surface(ImplicitSurface* surf) {
surfaces.erase(SurfaceDetails(surf, Cuboid::empty()));
}
void GlutRender::run() {
2018-02-06 19:10:08 +01:00
glutMainLoop();
}
2018-02-12 18:40:05 +01:00
void GlutRender::set_idle_func(void (*func)(void)) {
glutIdleFunc(func);
}
void GlutRender::reshape(int wid, int hei) {
2018-02-06 22:15:35 +01:00
if (hei == 0)
2018-02-06 19:10:08 +01:00
hei = 1;
2018-02-06 22:15:35 +01:00
GLfloat aspect = (GLfloat)wid / (GLfloat)hei;
2018-02-06 19:10:08 +01:00
2018-02-06 22:15:35 +01:00
glViewport(0, 0, wid, hei);
2018-02-06 19:10:08 +01:00
2018-02-06 22:15:35 +01:00
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Enable perspective projection with fovy, aspect, zNear and zFar
gluPerspective(45.0f, aspect, 0.1f, 100.0f);
}
2018-02-12 18:40:05 +01:00
void GlutRender::display_mesh(const Mesh& mesh) const {
const Point& mesh_center = mesh.get_center();
const std::vector<Point>& points = mesh.get_vertices();
glBegin(GL_TRIANGLES);
for(const Face& face: mesh.get_faces()) {
Point p0 = face.pt(0, points) + mesh_center,
p1 = face.pt(1, points) + mesh_center,
p2 = face.pt(2, points) + mesh_center;
glColor3f(rand_color(), rand_color(), rand_color());
glVertex3f(p0[0], p0[1], p0[2]);
glVertex3f(p1[0], p1[1], p1[2]);
glVertex3f(p2[0], p2[1], p2[2]);
}
glEnd();
}
void GlutRender::display() {
2018-02-06 22:15:35 +01:00
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
2018-02-06 19:10:08 +01:00
2018-02-06 21:05:45 +01:00
// Camera position and orientation
glLoadIdentity();
2018-02-12 18:40:05 +01:00
//glTranslatef(1., 2., -10.);
gluLookAt(0, 5, -20,
0, 5, 0,
2018-02-12 18:40:05 +01:00
0, 1, 0);
2018-02-06 21:05:45 +01:00
for(Mesh* mesh: meshes) {
2018-02-12 18:40:05 +01:00
display_mesh(*mesh);
}
for(const SurfaceDetails& surface: surfaces) {
Mesh mesh = MarchingCubes(*surface.surface, surface.box)();
display_mesh(mesh);
}
2018-02-06 19:10:08 +01:00
2018-02-06 22:15:35 +01:00
glutSwapBuffers();
}
void GlutRender::reshape_handle(int wid, int hei) {
get_instance().reshape(wid, hei);
}
void GlutRender::display_handle() {
get_instance().display();
}