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
164
165
166
167
168
169
|
#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"
#include "Util/ImageViewer.hpp"
#define APP_NAME "Meowcraft"
#define WINDOW_WIDTH 1000
#define WINDOW_HEIGHT 800
#define ASPECT static_cast<float>(WINDOW_WIDTH) / WINDOW_HEIGHT
#define FOV 90
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 example_image = MC::GFX::Image::RawImage(10, 10, 3);
for (uint8_t x = 0; x < 10; x++) {
for (uint8_t y = 0; y < 10; y++) {
example_image.add({static_cast<uint8_t>(x * 20), 0, static_cast<uint8_t>(y * 20)});
}
}
auto viewer = MC::Util::ImageViewer(example_image);
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, 50.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");
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);
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(0.85f, 0.85f, 0.85f, 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->position(), {});
model_uniform.set(model);
render(chunk.mesh.value(), texture);
}
viewer.render();
time++;
}
}
void render(MC::GFX::BindableMesh& mesh, 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 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.2f;
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;
}
}
|