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
158
159
160
161
162
163
|
#include <iostream>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#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/World.hpp"
#define APP_NAME "Meowcraft"
#define WINDOW_WIDTH 800
#define WINDOW_HEIGHT 600
#define ASPECT (static_cast<float>(WINDOW_WIDTH) / WINDOW_HEIGHT)
#define FOV 90
void run();
void render(const MC::GFX::BindableMesh&, const MC::GFX::Texture&);
void process_input(MC::GFX::Window&, MC::GFX::Camera&);
void setup_gl();
void fix_macos_render(const 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* _, 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::World world;
MC::GFX::Camera camera{};
camera.set_position({0.0f, MC::World::Chunk::Height / 2.0f, 0.0f});
MC::GFX::Shading::Program program(
MC::GFX::Shading::Shader::create_vertex(),
MC::GFX::Shading::Shader::create_fragment()
);
auto model_uniform = program.uniform("model_matrix");
auto view_uniform = program.uniform("view_matrix");
auto projection_uniform = program.uniform("projection_matrix");
auto sun_direction_uniform = program.uniform("sun_direction");
auto sky_color_uniform = program.uniform("sky_color");
program.bind();
auto projection = Math::MVP::perspective_projection(ASPECT, FOV, 0.1f, 1000.0f);
projection_uniform.set(projection);
Vector<3> sun_direction{1.0f, -1.0f, 0.0f};
sun_direction_uniform.set(sun_direction);
Vector<3> sky_color{0.85f, 0.85f, 0.85f}; // #DBDBDB
sky_color_uniform.set(sky_color);
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 view = Math::MVP::view(camera.position(), camera.angles());
view_uniform.set(view);
glClearColor(sky_color.x(), sky_color.y(), sky_color.z(), 1.0f); // #DBDBDB
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
for (auto chunk : world.get_visible_chunks(camera.position())) {
auto model = Math::MVP::model(chunk->chunk.value().position(), {});
model_uniform.set(model);
render(chunk->mesh.value(), texture);
}
time++;
}
}
void render(const MC::GFX::BindableMesh& mesh, const MC::GFX::Texture& texture) {
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 k) -> float { return window.key(k, 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);
float boost = key(GLFW_KEY_LEFT_CONTROL) * 2.0f;
auto move_speed = 0.2f + boost;
auto rotation_speed = 0.1f;
camera.move_relative({x * move_speed, 0.0f, z * move_speed});
camera.move({0.0f, y * move_speed, 0.0f});
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(const 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;
}
}
|