#include "first_app.hpp" #include "simple_render_system.hpp" #include "rainbow_system.hpp" #include "hk_camera.hpp" // libs #define GLM_FORCE_RADIANS #define GLM_FORCE_DEPTH_ZERO_TO_ONE #include #include // std #include #include namespace hk { FirstApp::FirstApp() { loadGameObjects(); } FirstApp::~FirstApp() { } void FirstApp::run() { SimpleRenderSystem simpleRenderSystem{m_device, m_renderer.getSwapChainRenderPass()}; RainbowSystem rainbowSystem(2000.0f); Camera camera{}; while (!m_window.shouldClose()) { glfwPollEvents(); // update objects color rainbowSystem.update(5.0f, m_gameObjects); float aspect = m_renderer.getAspectRatio(); //camera.setOrthographicProjection(-aspect, aspect, -1, 1, -1, 1); camera.setPerspectiveProjection(glm::radians(50.f), aspect, 0.1f, 10.f); if (auto commandBuffer = m_renderer.beginFrame()) { m_renderer.beginSwapChainRenderPass(commandBuffer); simpleRenderSystem.renderGameObjects(commandBuffer, m_gameObjects, camera); m_renderer.endSwapChainRenderPass(commandBuffer); m_renderer.endFrame(); } } vkDeviceWaitIdle(m_device.device()); } // temporary helper function, creates a 1x1x1 cube centered at offset std::unique_ptr createCubeModel(hk::Device &device, glm::vec3 offset) { std::vector vertices{ // 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}}, }; for (auto &v : vertices) { v.position += offset; } return std::make_unique(device, vertices); } void FirstApp::loadGameObjects() { std::shared_ptr cubeModel = createCubeModel(m_device, {0.f, 0.f, 0.f}); auto cube = GameObject::createGameObject(); cube.m_model = cubeModel; cube.m_transform.translation = {0.f, 0.f, 2.5f}; cube.m_transform.scale = {.5f, .5f, .5f}; m_gameObjects.push_back(std::move(cube)); } } // namespace hk