~sircmpwn/xrgears

ref: 0644636700efc4567fb90c8a5578c60b79fe7ee1 xrgears/vitamin-k/system/vikApplicationVks.hpp -rw-r--r-- 6.1 KiB
06446367 — Lubosz Sarnecki rename semaphores. 4 years ago
                                                                                
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*
* Vulkan Example base class
*
* Copyright (C) 2016 by Sascha Willems - www.saschawillems.de
*
* This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)
*/

#pragma once

#include <sys/stat.h>

#include <vulkan/vulkan.h>

#define GLM_FORCE_RADIANS
#define GLM_FORCE_DEPTH_ZERO_TO_ONE
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/glm.hpp>

#include <iostream>
#include <chrono>
#include <string>
#include <array>
#include <vector>

#include "render/vikTools.hpp"
#include "render/vikInitializers.hpp"

#include "scene/vikCameraBase.hpp"
#include "render/vikRendererTextOverlay.hpp"

#include "render/vikTimer.hpp"

#include "vikApplication.hpp"

namespace vik {
class Window;

class ApplicationVks : public Application {

public:
  RendererTextOverlay *renderer;
  CameraBase camera;

  bool viewUpdated = false;

  float zoom = 0;
  float rotationSpeed = 1.0f;
  float zoomSpeed = 1.0f;

  glm::vec3 rotation = glm::vec3();
  glm::vec3 cameraPos = glm::vec3();
  glm::vec2 mousePos;

  std::string title = "Vulkan Example";
  std::string name = "vulkanExample";

  struct {
    bool left = false;
    bool right = false;
    bool middle = false;
  } mouseButtons;

  ApplicationVks(int argc, char *argv[]) : Application(argc, argv) {
    init_window_from_settings();
    renderer = new RendererTextOverlay(&settings, window);

    std::function<void()> set_window_resize_cb = [this]() { resize(); };
    renderer->set_window_resize_cb(set_window_resize_cb);

    std::function<void()> enabled_features_cb = [this]() { get_enabled_features(); };
    renderer->set_enabled_features_cb(enabled_features_cb);

    auto pointer_motion_cb = [this](double x, double y) {
      double dx = mousePos.x - x;
      double dy = mousePos.y - y;

      if (mouseButtons.left) {
        rotation.x += dy * 1.25f * rotationSpeed;
        rotation.y -= dx * 1.25f * rotationSpeed;
        camera.rotate(glm::vec3(
                        dy * camera.rotationSpeed,
                        -dx * camera.rotationSpeed,
                        0.0f));
        viewUpdated = true;
      }

      if (mouseButtons.right) {
        zoom += dy * .005f * zoomSpeed;
        camera.translate(glm::vec3(-0.0f, 0.0f, dy * .005f * zoomSpeed));
        viewUpdated = true;
      }

      if (mouseButtons.middle) {
        cameraPos.x -= dx * 0.01f;
        cameraPos.y -= dy * 0.01f;
        camera.translate(glm::vec3(-dx * 0.01f, -dy * 0.01f, 0.0f));
        viewUpdated = true;
      }
      mousePos = glm::vec2(x, y);
    };

    auto pointer_button_cb = [this](Input::MouseButton button, bool state) {
      switch (button) {
        case Input::MouseButton::Left:
          mouseButtons.left = state;
          break;
        case Input::MouseButton::Middle:
          mouseButtons.middle = state;
          break;
        case Input::MouseButton::Right:
          mouseButtons.right = state;
          break;
      }
    };

    auto pointer_axis_cb = [this](Input::MouseScrollAxis axis, double value) {
      switch (axis) {
        case Input::MouseScrollAxis::X:
          zoom += value * 0.005f * zoomSpeed;
          camera.translate(glm::vec3(0.0f, 0.0f, value * 0.005f * zoomSpeed));
          viewUpdated = true;
          break;
        default:
          break;
      }
    };

    auto keyboard_key_cb = [this](Input::Key key, bool state) {
      switch (key) {
        case Input::Key::W:
          camera.keys.up = state;
          break;
        case Input::Key::S:
          camera.keys.down = state;
          break;
        case Input::Key::A:
          camera.keys.left = state;
          break;
        case Input::Key::D:
          camera.keys.right = state;
          break;
        case Input::Key::P:
          if (state)
            renderer->timer.toggle_animation_pause();
          break;
        case Input::Key::F1:
          if (state && settings.enable_text_overlay)
            renderer->textOverlay->visible = !renderer->textOverlay->visible;
          break;
        case Input::Key::ESCAPE:
          quit = true;
          break;
      }

      if (state)
        key_pressed(key);
    };

    window->set_pointer_motion_cb(pointer_motion_cb);
    window->set_pointer_button_cb(pointer_button_cb);
    window->set_pointer_axis_cb(pointer_axis_cb);
    window->set_keyboard_key_cb(keyboard_key_cb);

    auto quit_cb = [this]() { quit = true; };
    window->set_quit_cb(quit_cb);

    // TODO: move to renderer
    auto configure_cb = [this](uint32_t width, uint32_t height) {
      if (((width != renderer->width) || (height != renderer->height))) {
        renderer->destWidth = width;
        renderer->destHeight = height;
        if ((renderer->destWidth > 0) && (renderer->destHeight > 0))
          resize();
      }
    };
    window->set_configure_cb(configure_cb);

  }

  ~ApplicationVks() {
    delete renderer;
  }

  virtual void render() = 0;
  virtual void view_changed_cb() {}
  virtual void key_pressed(uint32_t) {}
  virtual void build_command_buffers() {}
  virtual void get_enabled_features() {}

  void check_view_update() {
    if (viewUpdated) {
      viewUpdated = false;
      view_changed_cb();
    }
  }

  virtual void init() {
    renderer->init(name, title);
  }

  void loop() {
    while (!quit) {
      renderer->timer.start();
      check_view_update();

      window->iterate(nullptr, nullptr);

      //if (prepared)
        render();
      renderer->timer.increment();
      float frame_time = renderer->timer.update_frame_time();
      update_camera(frame_time);
      renderer->timer.update_animation_timer();
      renderer->check_tick_finnished(title);
    }

    renderer->wait_idle();
  }

  void update_camera(float frame_time) {
    camera.update(frame_time);
    if (camera.moving())
      viewUpdated = true;
  }

  void resize() {
    renderer->resize();

    build_command_buffers();

    vkDeviceWaitIdle(renderer->device);

    if (settings.enable_text_overlay) {
      renderer->textOverlay->reallocateCommandBuffers();
      renderer->update_text_overlay(title);
    }

    camera.update_aspect_ratio(renderer->get_aspect_ratio());

    // Notify derived class
    //windowResized();
    view_changed_cb();
  }


};
}