summary refs log tree commit diff
path: root/src/main.cpp
blob: 3a8b55d10d3451f692e55b1a8fe023dd0987743c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <iostream>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <cstdint>

#include "GFX/Window.hpp"
#include "GFX/Camera.hpp"
#include "GFX/Binder.hpp"
#include "Math/MVP.hpp"
#include "GFX/Shading/Program.hpp"
#include "GFX/Texture.hpp"
#include "GFX/Image/PPMParser.hpp"
#include "World/Generator.hpp"

#define APP_NAME "Meowcraft"

#define WINDOW_WIDTH 1000
#define WINDOW_HEIGHT 800
#define ASPECT static_cast<float>(WINDOW_WIDTH) / WINDOW_HEIGHT

#define FOV 45

void run();
void render(MC::GFX::BindableMesh&, MC::GFX::Texture&);
void process_input(MC::GFX::Window&, MC::GFX::Camera&);
void setup_gl();
void fix_macos_render(MC::GFX::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::GFX::Window window(APP_NAME, WINDOW_WIDTH, WINDOW_HEIGHT);
    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::GFX::Image::PPMParser(MC::Assets::Images::atlas).parse();
    auto texture = MC::GFX::Texture(image);

    MC::World::Generator generator;
    auto chunk = generator.generate(0, 0);
    auto chunk_mesh = chunk.mesh();

    auto mesh = MC::GFX::Binder::load(chunk_mesh);

    MC::GFX::Camera camera{};
    camera.set_position({0.0f, 0.0f, 3.0f});

    MC::GFX::Shading::Program program(
        MC::GFX::Shading::Shader::create_fragment(),
        MC::GFX::Shading::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, 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::GFX::BindableMesh& mesh, MC::GFX::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, nullptr);
    mesh.unbind();
    texture.unbind();
}

void process_input(MC::GFX::Window& window, MC::GFX::Camera& camera) {
    if (window.key(GLFW_KEY_ESCAPE, GLFW_PRESS)) {
        window.close();
    }

    auto r = window.mouse_delta();

    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<const char*>(glewGetErrorString(error)));
        throw std::runtime_error("Failed to load GL functions: " + error_string);
    }
}

void fix_macos_render(MC::GFX::Window& window) {
    static bool moved = false;

    if(!moved) {
        int x, y;
        glfwGetWindowPos(window.get(), &x, &y);
        glfwSetWindowPos(window.get(), ++x, y);

        moved = true;
    }
}