#include #include #include #include #include #include "Window.hpp" #include "Mesh.hpp" #include "Camera.hpp" #include "Binder.hpp" #include "Math/MVP.hpp" #include "Shader/ShaderProgram.hpp" #include "Mouse.hpp" #include "Texture.hpp" #include "Image/PPMParser.hpp" #include "World/Generator.hpp" #define APP_NAME "Meowcraft" #define WINDOW_WIDTH 1000 #define WINDOW_HEIGHT 800 #define ASPECT static_cast(WINDOW_WIDTH) / WINDOW_HEIGHT #define FOV 45 void run(); void render(MC::BindableMesh&, MC::Texture&); void process_input(MC::Window&, MC::Mouse&, MC::Camera&); void setup_gl(); void fix_macos_render(MC::Window&); int main() { glfwInit(); try { run(); } catch (std::runtime_error& error) { std::cout << "An error occurred: " << error.what() << std::endl; glfwTerminate(); return 1; } glfwTerminate(); return 0; } void run() { MC::Window window(APP_NAME, WINDOW_WIDTH, WINDOW_HEIGHT); MC::Mouse mouse{}; setup_gl(); glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT); window.on_size_change([](GLFWwindow* window, int w, int h) { glViewport(0, 0, w, h); }); auto image = MC::Image::PPMParser(MC::Assets::Images::atlas).parse(); auto texture = MC::Texture(image); MC::Generator generator; auto chunk = generator.generate(0, 0); auto chunk_mesh = chunk.mesh(); auto mesh = MC::Binder::load(chunk_mesh); MC::Camera camera{}; camera.set_position({0.0f, 0.0f, 3.0f}); MC::ShaderProgram program(MC::Shader::create_fragment(), MC::Shader::create_vertex()); auto model_uniform = program.uniform("model_matrix"); auto view_uniform = program.uniform("view_matrix"); auto projection_uniform = program.uniform("projection_matrix"); program.bind(); auto projection = Math::MVP::projection(ASPECT, FOV, 0.1f, 100.0f); projection_uniform.set(projection); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); glEnable(GL_CULL_FACE); glCullFace(GL_BACK); uint64_t time = 0; while (!window.should_close()) { window.start_frame(); #ifdef __APPLE__ fix_macos_render(window); #endif process_input(window, mouse, camera); program.bind(); auto model = Math::MVP::model({}, {}); model_uniform.set(model); auto view = Math::MVP::view(camera.position(), camera.angles()); view_uniform.set(view); render(mesh, texture); time++; } } void render(MC::BindableMesh& mesh, MC::Texture& texture) { glClearColor(0.85f, 0.85f, 0.85f, 1.0f); // #DBDBDB glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); texture.bind(); mesh.bind(); glDrawElements(GL_TRIANGLES, mesh.size(), GL_UNSIGNED_INT, 0); mesh.unbind(); texture.unbind(); } void process_input(MC::Window& window, MC::Mouse& mouse, MC::Camera& camera) { if (window.key(GLFW_KEY_ESCAPE, GLFW_PRESS)) { window.close(); } auto r = mouse.update(window); auto key = [&](int key) -> float { return window.key(key, GLFW_PRESS); }; float x = key(GLFW_KEY_D) - key(GLFW_KEY_A); float y = key(GLFW_KEY_SPACE) - key(GLFW_KEY_LEFT_SHIFT); float z = key(GLFW_KEY_S) - key(GLFW_KEY_W); auto move_speed = 0.05f; auto rotation_speed = 0.1f; camera.move_relative({x * move_speed, y * move_speed, z * move_speed}); camera.rotate({r.y() * rotation_speed, r.x() * rotation_speed, 0.0f}); } void setup_gl() { GLenum error; if ((error = glewInit()) != GLEW_OK) { std::string error_string(reinterpret_cast(glewGetErrorString(error))); throw std::runtime_error("Failed to load GL functions: " + error_string); } } void fix_macos_render(MC::Window& window) { static bool moved = false; if(!moved) { int x, y; glfwGetWindowPos(window.get(), &x, &y); glfwSetWindowPos(window.get(), ++x, y); moved = true; } }