#include "GlutRender.hpp" #include "../MarchingCubes.hpp" #include #include #include #include #include GlutRender& GlutRender::get_instance() { static GlutRender instance; return instance; } GlutRender::GlutRender() { std::default_random_engine rand_engine(time(NULL)); std::uniform_real_distribution 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); glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH); glutInitWindowSize(wid, hei); glutCreateWindow(win_name); glutDisplayFunc(display_handle); glutReshapeFunc(reshape_handle); 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() { } void GlutRender::add_mesh(Mesh* mesh) { meshes.insert(mesh); } void GlutRender::remove_mesh(Mesh* mesh) { meshes.erase(mesh); } 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() { glutMainLoop(); } void GlutRender::set_idle_func(void (*func)(void)) { glutIdleFunc(func); } void GlutRender::reshape(int wid, int hei) { if (hei == 0) hei = 1; GLfloat aspect = (GLfloat)wid / (GLfloat)hei; glViewport(0, 0, wid, hei); glMatrixMode(GL_PROJECTION); glLoadIdentity(); // Enable perspective projection with fovy, aspect, zNear and zFar gluPerspective(45.0f, aspect, 0.1f, 100.0f); } void GlutRender::display_mesh(Mesh& mesh) const { const Point& mesh_center = mesh.get_center(); const std::vector& points = mesh.get_vertices(); #ifdef DEBUG_DISPLAY_NORMAL glBegin(GL_LINES); for(size_t vert_id=0; vert_id < points.size(); ++vert_id) { const Point& pt = points[vert_id]; Point normal = mesh.get_normal(vert_id); Point locVert = pt + mesh.get_center(); Point outwards = locVert + normal; Point inwards = locVert + ((-0.2) * normal); glColor3f(1., 0., 0.); glVertex3f(locVert[0], locVert[1], locVert[2]); glVertex3f(outwards[0], outwards[1], outwards[2]); glColor3f(0., 0., 1.); glVertex3f(locVert[0], locVert[1], locVert[2]); glVertex3f(inwards[0], inwards[1], inwards[2]); } glEnd(); #endif // DEBUG_DISPLAY_NORMAL 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; Point n0 = mesh.get_normal(face[0]), n1 = mesh.get_normal(face[1]), n2 = mesh.get_normal(face[2]); glColor3f(rand_color(), rand_color(), rand_color()); glNormal3f(n0[0], n0[1], n0[2]); glVertex3f(p0[0], p0[1], p0[2]); glNormal3f(n1[0], n1[1], n1[2]); glVertex3f(p1[0], p1[1], p1[2]); glNormal3f(n2[0], n2[1], n2[2]); glVertex3f(p2[0], p2[1], p2[2]); } glEnd(); } void GlutRender::display() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); // Camera position and orientation glLoadIdentity(); //glTranslatef(1., 2., -10.); gluLookAt(0, 5, -20, 0, 5, 0, 0, 1, 0); for(Mesh* mesh: meshes) { display_mesh(*mesh); } for(const SurfaceDetails& surface: surfaces) { Mesh mesh = MarchingCubes(*surface.surface, surface.box)(); display_mesh(mesh); } glutSwapBuffers(); } void GlutRender::reshape_handle(int wid, int hei) { get_instance().reshape(wid, hei); } void GlutRender::display_handle() { get_instance().display(); }