2024-02-21 13:28:50 +08:00
|
|
|
#include "first_app.hpp"
|
2024-04-03 22:22:29 +08:00
|
|
|
#include "simple_render_system.hpp"
|
2024-04-12 23:24:53 +08:00
|
|
|
#include "rainbow_system.hpp"
|
2025-12-18 22:40:57 +08:00
|
|
|
|
|
|
|
|
#include "keyboard_movement_controller.hpp"
|
2025-12-15 20:37:01 +08:00
|
|
|
#include "hk_camera.hpp"
|
2024-02-21 13:28:50 +08:00
|
|
|
|
2024-04-03 21:28:08 +08:00
|
|
|
// libs
|
|
|
|
|
#define GLM_FORCE_RADIANS
|
|
|
|
|
#define GLM_FORCE_DEPTH_ZERO_TO_ONE
|
|
|
|
|
#include <glm/glm.hpp>
|
|
|
|
|
#include <glm/gtc/constants.hpp>
|
|
|
|
|
|
2024-02-21 13:28:50 +08:00
|
|
|
// std
|
|
|
|
|
#include <stdexcept>
|
2025-12-18 22:40:57 +08:00
|
|
|
#include <chrono>
|
2024-02-21 13:28:50 +08:00
|
|
|
#include <array>
|
|
|
|
|
|
2024-04-12 23:24:53 +08:00
|
|
|
|
2024-02-21 13:28:50 +08:00
|
|
|
namespace hk
|
|
|
|
|
{
|
|
|
|
|
FirstApp::FirstApp()
|
|
|
|
|
{
|
2024-04-03 21:28:08 +08:00
|
|
|
loadGameObjects();
|
2024-02-21 13:28:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FirstApp::~FirstApp()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FirstApp::run()
|
|
|
|
|
{
|
2024-04-03 22:22:29 +08:00
|
|
|
SimpleRenderSystem simpleRenderSystem{m_device, m_renderer.getSwapChainRenderPass()};
|
2024-04-12 23:24:53 +08:00
|
|
|
RainbowSystem rainbowSystem(2000.0f);
|
2025-12-15 20:37:01 +08:00
|
|
|
Camera camera{};
|
2025-12-18 22:40:57 +08:00
|
|
|
|
|
|
|
|
auto viewerObject = GameObject::createGameObject();
|
|
|
|
|
KeyboardMovementController cameraController{};
|
|
|
|
|
|
|
|
|
|
auto currentTime = std::chrono::high_resolution_clock::now();
|
2024-04-12 23:24:53 +08:00
|
|
|
|
2024-04-03 21:28:08 +08:00
|
|
|
while (!m_window.shouldClose())
|
2024-02-21 13:28:50 +08:00
|
|
|
{
|
|
|
|
|
glfwPollEvents();
|
2025-12-18 22:40:57 +08:00
|
|
|
|
|
|
|
|
auto newTime = std::chrono::high_resolution_clock::now();
|
|
|
|
|
float frameTime = std::chrono::duration<float, std::chrono::seconds::period>(newTime - currentTime).count();
|
|
|
|
|
currentTime = newTime;
|
|
|
|
|
|
|
|
|
|
cameraController.moveInPlaneXZ(m_window.getWindow(), frameTime, viewerObject);
|
|
|
|
|
camera.setViewYXZ(viewerObject.m_transform.translation, viewerObject.m_transform.rotation);
|
2024-04-03 21:28:08 +08:00
|
|
|
|
2024-04-12 23:24:53 +08:00
|
|
|
// update objects color
|
|
|
|
|
rainbowSystem.update(5.0f, m_gameObjects);
|
|
|
|
|
|
2025-12-15 20:37:01 +08:00
|
|
|
float aspect = m_renderer.getAspectRatio();
|
|
|
|
|
camera.setPerspectiveProjection(glm::radians(50.f), aspect, 0.1f, 10.f);
|
2024-04-03 21:28:08 +08:00
|
|
|
if (auto commandBuffer = m_renderer.beginFrame())
|
|
|
|
|
{
|
|
|
|
|
m_renderer.beginSwapChainRenderPass(commandBuffer);
|
2025-12-15 20:37:01 +08:00
|
|
|
simpleRenderSystem.renderGameObjects(commandBuffer, m_gameObjects, camera);
|
2024-04-03 21:28:08 +08:00
|
|
|
m_renderer.endSwapChainRenderPass(commandBuffer);
|
|
|
|
|
m_renderer.endFrame();
|
|
|
|
|
}
|
2024-02-21 13:28:50 +08:00
|
|
|
}
|
|
|
|
|
|
2024-04-03 21:28:08 +08:00
|
|
|
vkDeviceWaitIdle(m_device.device());
|
2024-02-21 13:28:50 +08:00
|
|
|
}
|
|
|
|
|
|
2025-12-14 16:29:58 +08:00
|
|
|
// temporary helper function, creates a 1x1x1 cube centered at offset
|
|
|
|
|
std::unique_ptr<Model> createCubeModel(hk::Device &device, glm::vec3 offset)
|
2024-02-21 13:28:50 +08:00
|
|
|
{
|
|
|
|
|
std::vector<Model::Vertex> vertices{
|
2025-12-14 16:29:58 +08:00
|
|
|
|
|
|
|
|
// left face (white)
|
|
|
|
|
{{-.5f, -.5f, -.5f}, {.9f, .9f, .9f}},
|
|
|
|
|
{{-.5f, .5f, .5f}, {.9f, .9f, .9f}},
|
|
|
|
|
{{-.5f, -.5f, .5f}, {.9f, .9f, .9f}},
|
|
|
|
|
{{-.5f, -.5f, -.5f}, {.9f, .9f, .9f}},
|
|
|
|
|
{{-.5f, .5f, -.5f}, {.9f, .9f, .9f}},
|
|
|
|
|
{{-.5f, .5f, .5f}, {.9f, .9f, .9f}},
|
|
|
|
|
|
|
|
|
|
// right face (yellow)
|
|
|
|
|
{{.5f, -.5f, -.5f}, {.8f, .8f, .1f}},
|
|
|
|
|
{{.5f, .5f, .5f}, {.8f, .8f, .1f}},
|
|
|
|
|
{{.5f, -.5f, .5f}, {.8f, .8f, .1f}},
|
|
|
|
|
{{.5f, -.5f, -.5f}, {.8f, .8f, .1f}},
|
|
|
|
|
{{.5f, .5f, -.5f}, {.8f, .8f, .1f}},
|
|
|
|
|
{{.5f, .5f, .5f}, {.8f, .8f, .1f}},
|
|
|
|
|
|
|
|
|
|
// top face (orange, remember y axis points down)
|
|
|
|
|
{{-.5f, -.5f, -.5f}, {.9f, .6f, .1f}},
|
|
|
|
|
{{.5f, -.5f, .5f}, {.9f, .6f, .1f}},
|
|
|
|
|
{{-.5f, -.5f, .5f}, {.9f, .6f, .1f}},
|
|
|
|
|
{{-.5f, -.5f, -.5f}, {.9f, .6f, .1f}},
|
|
|
|
|
{{.5f, -.5f, -.5f}, {.9f, .6f, .1f}},
|
|
|
|
|
{{.5f, -.5f, .5f}, {.9f, .6f, .1f}},
|
|
|
|
|
|
|
|
|
|
// bottom face (red)
|
|
|
|
|
{{-.5f, .5f, -.5f}, {.8f, .1f, .1f}},
|
|
|
|
|
{{.5f, .5f, .5f}, {.8f, .1f, .1f}},
|
|
|
|
|
{{-.5f, .5f, .5f}, {.8f, .1f, .1f}},
|
|
|
|
|
{{-.5f, .5f, -.5f}, {.8f, .1f, .1f}},
|
|
|
|
|
{{.5f, .5f, -.5f}, {.8f, .1f, .1f}},
|
|
|
|
|
{{.5f, .5f, .5f}, {.8f, .1f, .1f}},
|
|
|
|
|
|
|
|
|
|
// nose face (blue)
|
|
|
|
|
{{-.5f, -.5f, 0.5f}, {.1f, .1f, .8f}},
|
|
|
|
|
{{.5f, .5f, 0.5f}, {.1f, .1f, .8f}},
|
|
|
|
|
{{-.5f, .5f, 0.5f}, {.1f, .1f, .8f}},
|
|
|
|
|
{{-.5f, -.5f, 0.5f}, {.1f, .1f, .8f}},
|
|
|
|
|
{{.5f, -.5f, 0.5f}, {.1f, .1f, .8f}},
|
|
|
|
|
{{.5f, .5f, 0.5f}, {.1f, .1f, .8f}},
|
|
|
|
|
|
|
|
|
|
// tail face (green)
|
|
|
|
|
{{-.5f, -.5f, -0.5f}, {.1f, .8f, .1f}},
|
|
|
|
|
{{.5f, .5f, -0.5f}, {.1f, .8f, .1f}},
|
|
|
|
|
{{-.5f, .5f, -0.5f}, {.1f, .8f, .1f}},
|
|
|
|
|
{{-.5f, -.5f, -0.5f}, {.1f, .8f, .1f}},
|
|
|
|
|
{{.5f, -.5f, -0.5f}, {.1f, .8f, .1f}},
|
|
|
|
|
{{.5f, .5f, -0.5f}, {.1f, .8f, .1f}},
|
|
|
|
|
|
2024-02-21 13:28:50 +08:00
|
|
|
};
|
2025-12-14 16:29:58 +08:00
|
|
|
for (auto &v : vertices)
|
|
|
|
|
{
|
|
|
|
|
v.position += offset;
|
|
|
|
|
}
|
|
|
|
|
return std::make_unique<Model>(device, vertices);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FirstApp::loadGameObjects()
|
|
|
|
|
{
|
|
|
|
|
std::shared_ptr<Model> cubeModel = createCubeModel(m_device, {0.f, 0.f, 0.f});
|
|
|
|
|
auto cube = GameObject::createGameObject();
|
|
|
|
|
cube.m_model = cubeModel;
|
2025-12-15 20:37:01 +08:00
|
|
|
cube.m_transform.translation = {0.f, 0.f, 2.5f};
|
2025-12-14 16:29:58 +08:00
|
|
|
cube.m_transform.scale = {.5f, .5f, .5f};
|
|
|
|
|
m_gameObjects.push_back(std::move(cube));
|
2024-02-21 13:28:50 +08:00
|
|
|
}
|
2024-04-03 22:22:29 +08:00
|
|
|
} // namespace hk
|