fast-nuclear-sim/src/graphics/window.cpp

135 lines
2.9 KiB
C++
Raw Normal View History

2024-01-21 01:36:21 +11:00
#include <GL/glew.h>
#include <GLFW/glfw3.h>
2024-01-21 12:58:37 +11:00
#include <glm/matrix.hpp>
2024-01-21 22:33:55 +11:00
#include <glm/ext/matrix_transform.hpp> // glm::translate, glm::rotate, glm::scale
#include <glm/ext/matrix_clip_space.hpp> // glm::perspective
2024-01-21 12:58:37 +11:00
2024-01-21 01:36:21 +11:00
#include <iostream>
2024-01-24 18:34:04 +11:00
#include "mesh/mesh.hpp"
#include "mesh/arrays.hpp"
#include "input/keyboard.hpp"
#include "input/mouse.hpp"
2024-01-22 00:20:54 +11:00
#include "camera.hpp"
2024-01-21 01:36:21 +11:00
#include "resize.hpp"
#include "window.hpp"
#include "shader.hpp"
2024-01-24 18:34:04 +11:00
#include "mesh/font.hpp"
2024-01-21 01:36:21 +11:00
using namespace sim::graphics;
static GLFWwindow* win;
static bool win_should_close = false;
2024-01-24 18:34:04 +11:00
static mesh MeshScene, MeshText;
2024-01-21 01:36:21 +11:00
void GLAPIENTRY cb_debug_message(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar* message, const void* userParam)
{
std::cout << "GL CALLBACK: " << message << "\n";
}
void window::create()
{
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, true);
2024-01-23 17:46:33 +11:00
#ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, true);
#endif
2024-01-21 01:36:21 +11:00
win = glfwCreateWindow(800, 600, "FastNuclearSim", nullptr, nullptr);
glfwMakeContextCurrent(win);
2024-01-24 18:34:04 +11:00
glfwSwapInterval(1);
2024-01-21 01:36:21 +11:00
GLenum err = glewInit();
if(err != GLEW_OK)
{
std::cout << "GLEW Init Failed: " << glewGetErrorString(err) << "\n";
close();
return;
}
glEnable(GL_DEBUG_OUTPUT);
glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
2024-01-23 17:46:33 +11:00
glEnable(GL_CULL_FACE);
2024-01-21 12:58:37 +11:00
glEnable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
2024-01-21 01:36:21 +11:00
glDebugMessageCallback(cb_debug_message, nullptr);
keyboard::init();
2024-01-22 00:20:54 +11:00
mouse::init();
2024-01-21 01:36:21 +11:00
resize::init();
font::init();
2024-01-22 00:20:54 +11:00
2024-01-21 01:36:21 +11:00
shader::init_program();
2024-01-21 22:33:55 +11:00
2024-01-24 18:34:04 +11:00
MeshScene.bind();
MeshScene.load_model("../assets", "scene.obj");
glm::mat4 mat = glm::mat4(1);
2024-01-24 18:49:00 +11:00
mat = glm::translate(mat, glm::vec3(-2.949, -1.7778 + 0.05, 3 - 0.05));
2024-01-24 18:34:04 +11:00
mat = glm::rotate(mat, glm::radians<float>(-90), glm::vec3(1, 0, 0));
mat = glm::rotate(mat, glm::radians<float>(-90), glm::vec3(0, 1, 0));
MeshText.model_matrix = mat;
MeshText.colour_matrix = {
2024-01-24 18:49:00 +11:00
1, 1, 1, 1,
2024-01-24 18:34:04 +11:00
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0
};
2024-01-21 01:36:21 +11:00
glViewport(0, 0, 800, 600);
}
void window::loop()
{
2024-01-24 18:34:04 +11:00
MeshText.bind();
2024-01-24 18:49:00 +11:00
font::generate(MeshText, "Hello, World!\nThis is cool!\n=)", 0.1);
2024-01-24 18:34:04 +11:00
2024-01-21 22:33:55 +11:00
glClearColor(0, 0, 0, 1.0f);
2024-01-21 01:36:21 +11:00
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
2024-01-21 12:58:37 +11:00
2024-01-22 00:20:54 +11:00
camera::update();
2024-01-24 18:34:04 +11:00
glm::mat4 mat_projection = glm::perspective(glm::radians(90.0f), 1.0f, 0.01f, 20.f);
2024-01-21 22:33:55 +11:00
glUniformMatrix4fv(shader::gl_projection, 1, false, &mat_projection[0][0]);
2024-01-21 12:58:37 +11:00
2024-01-24 18:34:04 +11:00
MeshScene.bind();
MeshScene.render();
MeshText.bind();
MeshText.render();
2024-01-21 01:36:21 +11:00
glfwSwapBuffers(win);
glfwPollEvents();
}
bool window::should_close()
{
return win_should_close || glfwWindowShouldClose(win);
}
void window::close()
{
win_should_close = true;
}
void window::destroy()
{
glfwTerminate();
}
GLFWwindow* window::get_window()
{
return win;
}