mpri-graphics-project/render/GlutRender.cpp

94 lines
2.2 KiB
C++
Raw Normal View History

#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);
2018-02-06 19:10:08 +01:00
glutInitDisplayMode(GLUT_DOUBLE);
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() {
}
void GlutRender::add_mesh(const Mesh* mesh) {
meshes.insert(mesh);
}
void GlutRender::remove_mesh(const Mesh* mesh) {
meshes.erase(mesh);
}
void GlutRender::run() {
2018-02-06 19:10:08 +01:00
glutMainLoop();
}
void GlutRender::reshape(int wid, int hei) {
2018-02-06 19:10:08 +01:00
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() {
2018-02-06 19:10:08 +01:00
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
for(const Mesh* mesh: meshes) {
2018-02-06 19:10:08 +01:00
glLoadIdentity();
glTranslatef(.0f, .0f, -6.0f);
const std::vector<Point>& points = mesh->get_vertices();
2018-02-06 19:10:08 +01:00
glBegin(GL_TRIANGLES);
for(const Face& face: mesh->get_faces()) {
const Point& p0 = face.pt(0, points),
2018-02-06 19:10:08 +01:00
p1 = face.pt(1, points),
p2 = face.pt(2, points);
2018-02-06 19:10:08 +01:00
glColor3f(1.0f, 1.0f, 0.0f);
glVertex3f(p0[0], p0[1], p0[2]);
glVertex3f(p1[0], p1[1], p1[2]);
glVertex3f(p2[0], p2[1], p2[2]);
}
2018-02-06 19:10:08 +01:00
glEnd();
}
2018-02-06 19:10:08 +01:00
glutSwapBuffers();
}
void GlutRender::reshape_handle(int wid, int hei) {
get_instance().reshape(wid, hei);
}
void GlutRender::display_handle() {
get_instance().display();
}