1 #include "app/assets.hpp"
2 #include "app/config.hpp"
3 #include "app/init.hpp"
4 #include "app/keymap.hpp"
5 #include "graphics/camera.hpp"
6 #include "graphics/shader.hpp"
7 #include "graphics/viewport.hpp"
8 #include "graphics/window.hpp"
9 #include "rand/SimplexNoise.hpp"
10 #include "world/Floor.hpp"
14 #include <glm/glm.hpp>
15 #include <glm/gtc/matrix_transform.hpp>
17 using namespace tacos;
19 int main(int argc, char *argv[]) {
21 config.ReadArgs(argc, argv);
24 AssetLoader asset_loader(config);
25 Window window(1440, 900);
26 Viewport viewport(1440, 900);
31 constexpr float noise_scale = 1.0f/64.0f;
32 constexpr float height_scale = 5.0f;
35 Shader floor_vert(asset_loader.LoadVertexShader("floor"));
36 Shader floor_frag(asset_loader.LoadFragmentShader("floor"));
37 Program floor_program;
38 floor_program.Attach(floor_vert);
39 floor_program.Attach(floor_frag);
41 GLint mv_location = floor_program.UniformLocation("MV");
42 GLint mvp_location = floor_program.UniformLocation("MVP");
45 SimplexNoise noise(0);
46 for (int z = 0; z < floor.Depth(); ++z) {
47 for (int x = 0; x < floor.Width(); ++x) {
48 floor.SetElevation(x, z, noise(glm::vec3(x * noise_scale, 0.0f, z * noise_scale)) * height_scale);
51 floor.GenerateVertices();
54 camera.Warp(glm::vec3(20.0f, 0.0f, 20.0f));
61 constexpr float cam_speed = 15.0f; // units per second
62 constexpr float cam_rot_speed = 2.0f; // radians per second
64 glm::vec3 cam_fwd(0.0f);
65 glm::vec3 cam_rev(0.0f);
66 float cam_near = 0.0f;
68 glm::vec2 cam_rot_ccw(0.0f);
69 glm::vec2 cam_rot_cw(0.0f);
74 Uint32 last = SDL_GetTicks();
77 Uint32 now = SDL_GetTicks();
79 while (SDL_PollEvent(&event)) {
82 switch (keymap.Lookup(event.key)) {
83 case Keymap::CAMERA_RIGHT:
86 case Keymap::CAMERA_LEFT:
89 case Keymap::CAMERA_UP:
92 case Keymap::CAMERA_DOWN:
95 case Keymap::CAMERA_BACK:
98 case Keymap::CAMERA_FORWARD:
101 case Keymap::CAMERA_NEARER:
104 case Keymap::CAMERA_FARTHER:
107 case Keymap::CAMERA_PITCH_CCW:
108 cam_rot_ccw.x = 1.0f;
110 case Keymap::CAMERA_PITCH_CW:
113 case Keymap::CAMERA_YAW_CCW:
114 cam_rot_ccw.y = 1.0f;
116 case Keymap::CAMERA_YAW_CW:
128 switch (keymap.Lookup(event.key)) {
129 case Keymap::CAMERA_RIGHT:
132 case Keymap::CAMERA_LEFT:
135 case Keymap::CAMERA_UP:
138 case Keymap::CAMERA_DOWN:
141 case Keymap::CAMERA_BACK:
144 case Keymap::CAMERA_FORWARD:
147 case Keymap::CAMERA_NEARER:
150 case Keymap::CAMERA_FARTHER:
153 case Keymap::CAMERA_PITCH_CCW:
154 cam_rot_ccw.x = 0.0f;
156 case Keymap::CAMERA_PITCH_CW:
159 case Keymap::CAMERA_YAW_CCW:
160 cam_rot_ccw.y = 0.0f;
162 case Keymap::CAMERA_YAW_CW:
173 case SDL_WINDOWEVENT:
174 if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
175 viewport.Resize(event.window.data1, event.window.data2);
184 float dt = (now - last) * 0.001f;
185 camera.Move((cam_fwd - cam_rev) * cam_speed * dt);
186 camera.BackOff((cam_far - cam_near) * cam_speed * dt);
187 camera.Rotate((cam_rot_ccw - cam_rot_cw) * cam_rot_speed * dt);
191 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
192 for (int z = 0; z < floor.VAODepth(); ++z) {
193 for (int x = 0; x < floor.VAOWidth(); ++x) {
194 M = glm::translate(glm::mat4(1.0f), floor.VAOPosition(x, z));
195 MV = camera.View() * M;
196 MVP = viewport.Perspective() * MV;
197 floor_program.Uniform(mv_location, MV);
198 floor_program.Uniform(mvp_location, MVP);