Use openGL lightings — still kind of bad for now

This commit is contained in:
Théophile Bastian 2018-02-13 14:47:51 +01:00
parent 36bc8e1b5c
commit 56d8e22f21
2 changed files with 50 additions and 23 deletions

View file

@ -21,21 +21,45 @@ GlutRender::GlutRender() {
void GlutRender::init(int* argc, char** argv, void GlutRender::init(int* argc, char** argv,
int wid, int hei, const char* win_name) int wid, int hei, const char* win_name)
{ {
glutInit(argc, argv); glutInit(argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH); glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(wid, hei); glutInitWindowSize(wid, hei);
glutCreateWindow(win_name); glutCreateWindow(win_name);
glutDisplayFunc(display_handle); // ==== Callbacks ====
glutReshapeFunc(reshape_handle); glutDisplayFunc(display_handle);
glutReshapeFunc(reshape_handle);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Background color // ==== Lighting ====
glClearDepth(1.0f); // Set background depth to farthest GLfloat light0_pos[] = {2., 10., 2.};
glEnable(GL_DEPTH_TEST); GLfloat light0_ambient[] = {0., 0., 0., 1.};
glDepthFunc(GL_LEQUAL); GLfloat light0_diffuse[] = {1., 1., 1., 1.};
glShadeModel(GL_SMOOTH); // Enable smooth shading GLfloat light0_specular[] = {1., 1., 1., 1.};
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); glLightfv(GL_LIGHT0, GL_POSITION, light0_pos);
glLightfv(GL_LIGHT0, GL_AMBIENT, light0_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light0_specular);
GLfloat light_ambient[] = {.2, .2, .2, 1.};
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, light_ambient);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glShadeModel(GL_SMOOTH); // Enable smooth shading
GLfloat material_specular[] = {1., 1., 1., 1.};
GLfloat material_emission[] = {0., 0., 0., 1.};
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, material_specular);
glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, material_emission);
glEnable(GL_COLOR_MATERIAL);
// ==== Misc ====
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);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
} }
void GlutRender::cleanup() { void GlutRender::cleanup() {

View file

@ -24,17 +24,6 @@ class GlutRender {
void set_idle_func(void (*func)(void)); void set_idle_func(void (*func)(void));
private: //meth
GlutRender();
void display_mesh(const Mesh& mesh) const;
protected:
void reshape(int wid, int hei);
void display();
static void reshape_handle(int wid, int hei);
static void display_handle();
private: private:
struct SurfaceDetails { struct SurfaceDetails {
SurfaceDetails(ImplicitSurface* surf, const Cuboid& box): SurfaceDetails(ImplicitSurface* surf, const Cuboid& box):
@ -48,6 +37,20 @@ class GlutRender {
} }
}; };
private: //meth
GlutRender();
void display_mesh(Mesh& mesh) const;
protected:
void reshape(int wid, int hei);
void display();
static void reshape_handle(int wid, int hei);
static void display_handle();
private: //attr
std::function<double()> rand_color; std::function<double()> rand_color;
std::set<Mesh*> meshes; std::set<Mesh*> meshes;