3 #include "geometry.hpp"
11 Application::Application()
16 , ctx(window.CreateContext())
19 , move_velocity(0.003f)
20 , pitch_sensitivity(-0.0025f)
21 , yaw_sensitivity(-0.001f)
26 , outline_visible(false)
27 , outline_transform(1.0f)
40 GLContext::EnableVSync();
43 glGenVertexArrays(1, &VertexArrayID);
44 glBindVertexArray(VertexArrayID);
48 cam.Position(glm::vec3(0, 4, 4));
49 hud.Viewport(960, 600);
50 hud.Display(*world.BlockTypes()[place_id]);
52 glClearColor(0.0, 0.0, 0.0, 1.0);
56 void Application::Run() {
58 Uint32 last = SDL_GetTicks();
61 Uint32 now = SDL_GetTicks();
62 int delta = now - last;
68 void Application::Loop(int dt) {
75 void Application::HandleEvents() {
77 while (SDL_PollEvent(&event)) {
81 switch (event.key.keysym.sym) {
83 front = event.key.state == SDL_PRESSED;
86 back = event.key.state == SDL_PRESSED;
89 left = event.key.state == SDL_PRESSED;
92 right = event.key.state == SDL_PRESSED;
96 up = event.key.state == SDL_PRESSED;
100 down = event.key.state == SDL_PRESSED;
104 case SDL_MOUSEBUTTONDOWN:
105 if (event.button.button == 1) {
108 } else if (event.button.button == 2) {
111 } else if (event.button.button == 3) {
116 case SDL_MOUSEMOTION:
117 cam.RotateYaw(event.motion.xrel * yaw_sensitivity);
118 cam.RotatePitch(event.motion.yrel * pitch_sensitivity);
123 case SDL_WINDOWEVENT:
124 switch (event.window.event) {
125 case SDL_WINDOWEVENT_RESIZED:
126 cam.Viewport(event.window.data1, event.window.data2);
127 hud.Viewport(event.window.data1, event.window.data2);
139 void Application::Update(int dt) {
141 if (right && !left) {
142 vel.x = move_velocity;
143 } else if (left && !right) {
144 vel.x = -move_velocity;
147 vel.y = move_velocity;
148 } else if (down && !up) {
149 vel.y = -move_velocity;
151 if (back && !front) {
152 vel.z = move_velocity;
153 } else if (front && !back) {
154 vel.z = -move_velocity;
156 cam.OrientationVelocity(vel);
165 if (world.Intersection(aim, glm::mat4(1.0f), &chunk, &blkid, &dist, &normal)) {
166 glm::vec3 pos = Chunk::ToCoords(blkid);
167 outline_visible = true;
169 chunk->BlockAt(blkid).type->FillOutlineModel(outline);
170 outline_transform = glm::translate(chunk->Transform(), pos);
171 outline_transform = glm::scale(outline_transform, glm::vec3(1.0001f));
173 outline_visible = false;
178 place_id = chunk->BlockAt(blkid).type->id;
179 hud.Display(*world.BlockTypes()[place_id]);
185 chunk->BlockAt(blkid).type = world.BlockTypes()[remove_id];
192 Chunk *mod_chunk = chunk;
193 glm::vec3 next_pos = Chunk::ToCoords(blkid) + normal;
194 if (!Chunk::InBounds(next_pos)) {
195 mod_chunk = &world.Next(*chunk, normal);
196 next_pos -= normal * Chunk::Extent();
198 mod_chunk->BlockAt(next_pos).type = world.BlockTypes()[place_id];
199 mod_chunk->Invalidate();
205 void Application::Render() {
210 program.SetLightDirection({ -1.0f, -3.0f, -2.0f });
211 program.SetVP(cam.View(), cam.Projection());
213 for (Chunk &chunk : world.LoadedChunks()) {
214 program.SetM(chunk.Transform());
218 if (outline_visible) {
219 program.SetM(outline_transform);