diff --git a/VulkanTest b/VulkanTest index a0bf688..a6d84c4 100755 Binary files a/VulkanTest and b/VulkanTest differ diff --git a/first_app.cpp b/first_app.cpp index e0a6014..15bc658 100644 --- a/first_app.cpp +++ b/first_app.cpp @@ -30,7 +30,8 @@ namespace hk SimpleRenderSystem simpleRenderSystem{m_device, m_renderer.getSwapChainRenderPass()}; RainbowSystem rainbowSystem(2000.0f); Camera camera{}; - + //camera.setViewDirection(glm::vec3(0.f), glm::vec3(0.5f, 0.f, 1.f)); + camera.setViewTarget(glm::vec3(-1.f, -2.f, -2.f), glm::vec3(0.f, 0.f, 2.5f)); while (!m_window.shouldClose()) { diff --git a/first_app.hpp b/first_app.hpp index 31dc085..fd85798 100644 --- a/first_app.hpp +++ b/first_app.hpp @@ -24,7 +24,6 @@ namespace hk FirstApp &operator=(const FirstApp &) = delete; void run(); - void runGravitySystem(); private: void loadGameObjects(); diff --git a/hk_camera.cpp b/hk_camera.cpp index f6fce96..7d6191e 100644 --- a/hk_camera.cpp +++ b/hk_camera.cpp @@ -24,4 +24,54 @@ namespace hk m_projectionMatrix[2][3] = 1.f; m_projectionMatrix[3][2] = -(far * near) / (far - near); } + void Camera::setViewDirection(glm::vec3 position, glm::vec3 direction, glm::vec3 up) + { + const glm::vec3 w = glm::normalize(direction); + const glm::vec3 u = glm::normalize(glm::cross(w, up)); + const glm::vec3 v = glm::cross(w, u); + + m_viewMatrix = glm::mat4{1.0f}; + m_viewMatrix[0][0] = u.x; + m_viewMatrix[1][0] = u.y; + m_viewMatrix[2][0] = u.z; + m_viewMatrix[0][1] = v.x; + m_viewMatrix[1][1] = v.y; + m_viewMatrix[2][1] = v.z; + m_viewMatrix[0][2] = w.x; + m_viewMatrix[1][2] = w.y; + m_viewMatrix[2][2] = w.z; + m_viewMatrix[3][0] = -glm::dot(u, position); + m_viewMatrix[3][1] = -glm::dot(v, position); + m_viewMatrix[3][2] = -glm::dot(w, position); + } + void Camera::setViewTarget(glm::vec3 position, glm::vec3 target, glm::vec3 up) + { + setViewDirection(position, target - position, up); + } + void Camera::setViewYXZ(glm::vec3 position, glm::vec3 rotation) + { + const float c3 = glm::cos(rotation.z); + const float s3 = glm::sin(rotation.z); + const float c2 = glm::cos(rotation.x); + const float s2 = glm::sin(rotation.x); + const float c1 = glm::cos(rotation.y); + const float s1 = glm::sin(rotation.y); + const glm::vec3 u{(c1 * c3 + s1 * s2 * s3), (c2 * s3), (c1 * s2 * s3 - c3 * s1)}; + const glm::vec3 v{(c3 * s1 * s2 - c1 * s3), (c2 * c3), (c1 * c3 * s2 + s1 * s3)}; + const glm::vec3 w{(c2 * s1), (-s2), (c1 * c2)}; + + m_viewMatrix = glm::mat4{1.0f}; + m_viewMatrix[0][0] = u.x; + m_viewMatrix[1][0] = u.y; + m_viewMatrix[2][0] = u.z; + m_viewMatrix[0][1] = v.x; + m_viewMatrix[1][1] = v.y; + m_viewMatrix[2][1] = v.z; + m_viewMatrix[0][2] = w.x; + m_viewMatrix[1][2] = w.y; + m_viewMatrix[2][2] = w.z; + m_viewMatrix[3][0] = -glm::dot(u, position); + m_viewMatrix[3][1] = -glm::dot(v, position); + m_viewMatrix[3][2] = -glm::dot(w, position); + } } // namespace hk \ No newline at end of file diff --git a/hk_camera.hpp b/hk_camera.hpp index 6750192..c9395e5 100644 --- a/hk_camera.hpp +++ b/hk_camera.hpp @@ -14,11 +14,16 @@ namespace hk ~Camera() = default; void setOrthographicProjection(float left, float right, float top, float bottom, float near, float far); - void setPerspectiveProjection(float fovY, float aspect, float near, float far); + void setViewDirection(glm::vec3 position, glm::vec3 direction, glm::vec3 up = glm::vec3{0.f, -1.f, 0.f}); + void setViewTarget(glm::vec3 position, glm::vec3 target, glm::vec3 up = glm::vec3{0.f, -1.f, 0.f}); + void setViewYXZ(glm::vec3 position, glm::vec3 rotation); + const glm::mat4& getProjection() const { return m_projectionMatrix; } + const glm::mat4& getView() const { return m_viewMatrix; } private: glm::mat4 m_projectionMatrix{1.0f}; + glm::mat4 m_viewMatrix{1.0f}; }; } // namespace hk \ No newline at end of file diff --git a/simple_render_system.cpp b/simple_render_system.cpp index 46a153c..166339f 100644 --- a/simple_render_system.cpp +++ b/simple_render_system.cpp @@ -69,6 +69,9 @@ namespace hk void SimpleRenderSystem::renderGameObjects(VkCommandBuffer commandBuffer, std::vector &gameObjects, const Camera &camera) { m_pipeline->bind(commandBuffer); + + auto projectionView = camera.getProjection() * camera.getView(); + for (auto &obj : gameObjects) { obj.m_transform.rotation.y = glm::mod(obj.m_transform.rotation.y + 0.01f, glm::two_pi()); @@ -76,7 +79,7 @@ namespace hk SimplePushConstantData push{}; push.color = obj.m_color; - push.transform = camera.getProjection() * obj.m_transform.mat4(); + push.transform = projectionView * obj.m_transform.mat4(); vkCmdPushConstants( commandBuffer,