199 lines
4.8 KiB
C++
199 lines
4.8 KiB
C++
#include <iostream>
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
|
|
#include <glad/glad.h>
|
|
#ifndef __APPLE__
|
|
#include <GLUT/freeglut.h>
|
|
#else
|
|
#include <GLUT/glut.h>
|
|
#endif
|
|
#include <glm/glm.hpp>
|
|
#include <glm/gtc/matrix_transform.hpp>
|
|
#include <glm/gtc/type_ptr.hpp>
|
|
#include <glm/gtx/string_cast.hpp>
|
|
|
|
using namespace glm;
|
|
|
|
static const char * APP_TITLE = "GLUT Template";
|
|
|
|
const float NEAR_CLIP_PLANE = 0.01f;
|
|
const float FAR_CLIP_PLANE = 100.0f;
|
|
const float FIELD_OF_VIEW = 45.0f;
|
|
const glm::vec4 CLEAR_COLOR = glm::vec4(0.25f, 0.25f, 0.4f, 1.0f);
|
|
|
|
#define OUTPUT_REDIR
|
|
#ifdef OUTPUT_REDIR
|
|
static FILE * fout;
|
|
static FILE * ferr;
|
|
#endif
|
|
|
|
static int win_width = 800;
|
|
static int win_height = 600;
|
|
|
|
static glm::vec3 g_eye_position = glm::vec3(0.0f, 0.0f, -2.3f);
|
|
static int g_mouse_button = -1;
|
|
static int g_mouse_x = 0;
|
|
static int g_mouse_y = 0;
|
|
static float g_zoom_factor = 1.0f;
|
|
static float g_rotation_angle = 0.0f;
|
|
static float g_fov_mod = 1.0f;
|
|
|
|
static glm::mat4 g_projection_matrix = glm::mat4();
|
|
static glm::mat4 g_model_view_matrix = glm::mat4();
|
|
static glm::mat4 g_rotation_matrix = glm::mat4();
|
|
|
|
static void init_gl(void);
|
|
static void reshape(int _w, int _h);
|
|
static void keyboard(unsigned char key, int x, int y);
|
|
static void release_key(unsigned char key, int x, int y);
|
|
static void mouse(int button, int state, int x, int y);
|
|
static void motion(int x, int y);
|
|
static void idle(void);
|
|
static void render_scene(void);
|
|
static void clean_up(void);
|
|
|
|
int main(int argc, char ** argv) {
|
|
#ifdef OUTPUT_REDIR
|
|
fout = freopen("stdout.txt", "w", stdout);
|
|
ferr = freopen("stderr.txt", "w", stderr);
|
|
#endif
|
|
|
|
glutInit(&argc, argv);
|
|
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
|
|
glutInitWindowPosition(10, 10);
|
|
glutInitWindowSize(win_width, win_height);
|
|
glutCreateWindow(APP_TITLE);
|
|
glutKeyboardFunc(keyboard);
|
|
glutKeyboardUpFunc(release_key);
|
|
glutMouseFunc(mouse);
|
|
glutMotionFunc(motion);
|
|
glutIdleFunc(idle);
|
|
glutDisplayFunc(render_scene);
|
|
glutReshapeFunc(reshape);
|
|
|
|
if (!gladLoadGL()) {
|
|
printf("Something went wrong!\n");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
atexit(clean_up);
|
|
init_gl();
|
|
|
|
glutMainLoop();
|
|
|
|
return EXIT_SUCCESS;
|
|
}
|
|
|
|
void init_gl(void) {
|
|
/* Enable back-face culling. */
|
|
glEnable(GL_CULL_FACE);
|
|
glCullFace(GL_BACK);
|
|
|
|
/* Enable Z-buffer. */
|
|
glEnable(GL_DEPTH_TEST);
|
|
glDepthMask(GL_TRUE);
|
|
|
|
/* Set OpenGL state. */
|
|
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
|
|
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
|
}
|
|
|
|
void reshape(int _w, int _h) {
|
|
/* Reset viewport */
|
|
win_width = _w;
|
|
win_height = _h == 0 ? 1 : _h;
|
|
glViewport(0, 0, win_width, win_height);
|
|
|
|
/* Recalculate projection matrix. */
|
|
g_projection_matrix = glm::perspective(
|
|
FIELD_OF_VIEW * (g_fov_mod <= 0.5f ? 0.5f : g_fov_mod),
|
|
static_cast<float>(win_width) / win_height,
|
|
NEAR_CLIP_PLANE,
|
|
FAR_CLIP_PLANE
|
|
);
|
|
|
|
glutPostRedisplay();
|
|
}
|
|
|
|
void keyboard(unsigned char key, int x, int y) {
|
|
switch (key) {
|
|
case 27:
|
|
exit(0);
|
|
break;
|
|
}
|
|
|
|
glutPostRedisplay();
|
|
}
|
|
|
|
void release_key(unsigned char key, int x, int y) {
|
|
glutPostRedisplay();
|
|
}
|
|
|
|
void mouse(int button, int state, int x, int y) {
|
|
if (button == 3) {
|
|
// Zoom in when the user moves the mouse wheel up
|
|
if (state == GLUT_UP) return;
|
|
else g_zoom_factor *= 1.05f;
|
|
}
|
|
else if (button == 4) {
|
|
// Zoom out when the user moves the mouse wheel down
|
|
if (state == GLUT_UP) return;
|
|
else g_zoom_factor *= 0.95f;
|
|
}
|
|
else {
|
|
// Else, save the state of the mouse
|
|
g_mouse_x = x;
|
|
g_mouse_y = y;
|
|
g_mouse_button = button;
|
|
}
|
|
|
|
glutPostRedisplay();
|
|
}
|
|
|
|
void motion(int x, int y) {
|
|
// Compute the movement vector of the mouse
|
|
glm::vec2 move_vector = glm::vec2(x - g_mouse_x, y - g_mouse_y);
|
|
|
|
// Rotate when the user moves the mouse with the left button pressed
|
|
if (g_mouse_button == GLUT_LEFT_BUTTON) {
|
|
if (abs(x - g_mouse_x) > 0.01f || abs(y - g_mouse_y) > 0.01f) {
|
|
// Compute a rotation matrix using the movement vector as axis of rotation, and the length of the vector as the rotation angle
|
|
glm::mat4 rotation = glm::rotate(glm::mat4x4(), glm::length(move_vector) * 3.14f / 180.0f, glm::vec3(move_vector.y, move_vector.x, 0.0f));
|
|
g_rotation_matrix = rotation * g_rotation_matrix;
|
|
}
|
|
}
|
|
else if (g_mouse_button == GLUT_RIGHT_BUTTON) {
|
|
// Compute a translation matrix with 1 percent of the movement vector and translate the eye
|
|
glm::mat4 translation_matrix = glm::translate(glm::mat4x4(), glm::vec3(move_vector.x * 0.01f, -move_vector.y * 0.01f, 0.0f));
|
|
g_eye_position = translation_matrix * glm::vec4(g_eye_position.x, g_eye_position.y, g_eye_position.z, 1.0f);
|
|
}
|
|
|
|
// Save the current mouse position
|
|
g_mouse_x = x;
|
|
g_mouse_y = y;
|
|
|
|
glutPostRedisplay();
|
|
}
|
|
|
|
void idle(void) {
|
|
glutPostRedisplay();
|
|
}
|
|
|
|
void render_scene(void) {
|
|
glClearColor(0.0, 0.0, 0.0, 1.0);
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
glLoadIdentity();
|
|
|
|
glFlush();
|
|
glFinish();
|
|
glutSwapBuffers();
|
|
}
|
|
|
|
void clean_up(void) {
|
|
#ifdef OUTPUT_REDIR
|
|
fclose(fout);
|
|
fclose(ferr);
|
|
#endif
|
|
}
|