14.Camera (View) Transform
This commit is contained in:
parent
ee3aa35567
commit
2e7056c246
BIN
VulkanTest
BIN
VulkanTest
Binary file not shown.
|
|
@ -30,7 +30,8 @@ namespace hk
|
||||||
SimpleRenderSystem simpleRenderSystem{m_device, m_renderer.getSwapChainRenderPass()};
|
SimpleRenderSystem simpleRenderSystem{m_device, m_renderer.getSwapChainRenderPass()};
|
||||||
RainbowSystem rainbowSystem(2000.0f);
|
RainbowSystem rainbowSystem(2000.0f);
|
||||||
Camera camera{};
|
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())
|
while (!m_window.shouldClose())
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -24,7 +24,6 @@ namespace hk
|
||||||
FirstApp &operator=(const FirstApp &) = delete;
|
FirstApp &operator=(const FirstApp &) = delete;
|
||||||
|
|
||||||
void run();
|
void run();
|
||||||
void runGravitySystem();
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void loadGameObjects();
|
void loadGameObjects();
|
||||||
|
|
|
||||||
|
|
@ -24,4 +24,54 @@ namespace hk
|
||||||
m_projectionMatrix[2][3] = 1.f;
|
m_projectionMatrix[2][3] = 1.f;
|
||||||
m_projectionMatrix[3][2] = -(far * near) / (far - near);
|
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
|
} // namespace hk
|
||||||
|
|
@ -14,11 +14,16 @@ namespace hk
|
||||||
~Camera() = default;
|
~Camera() = default;
|
||||||
|
|
||||||
void setOrthographicProjection(float left, float right, float top, float bottom, float near, float far);
|
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 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& getProjection() const { return m_projectionMatrix; }
|
||||||
|
const glm::mat4& getView() const { return m_viewMatrix; }
|
||||||
private:
|
private:
|
||||||
glm::mat4 m_projectionMatrix{1.0f};
|
glm::mat4 m_projectionMatrix{1.0f};
|
||||||
|
glm::mat4 m_viewMatrix{1.0f};
|
||||||
};
|
};
|
||||||
} // namespace hk
|
} // namespace hk
|
||||||
|
|
@ -69,6 +69,9 @@ namespace hk
|
||||||
void SimpleRenderSystem::renderGameObjects(VkCommandBuffer commandBuffer, std::vector<GameObject> &gameObjects, const Camera &camera)
|
void SimpleRenderSystem::renderGameObjects(VkCommandBuffer commandBuffer, std::vector<GameObject> &gameObjects, const Camera &camera)
|
||||||
{
|
{
|
||||||
m_pipeline->bind(commandBuffer);
|
m_pipeline->bind(commandBuffer);
|
||||||
|
|
||||||
|
auto projectionView = camera.getProjection() * camera.getView();
|
||||||
|
|
||||||
for (auto &obj : gameObjects)
|
for (auto &obj : gameObjects)
|
||||||
{
|
{
|
||||||
obj.m_transform.rotation.y = glm::mod(obj.m_transform.rotation.y + 0.01f, glm::two_pi<float>());
|
obj.m_transform.rotation.y = glm::mod(obj.m_transform.rotation.y + 0.01f, glm::two_pi<float>());
|
||||||
|
|
@ -76,7 +79,7 @@ namespace hk
|
||||||
|
|
||||||
SimplePushConstantData push{};
|
SimplePushConstantData push{};
|
||||||
push.color = obj.m_color;
|
push.color = obj.m_color;
|
||||||
push.transform = camera.getProjection() * obj.m_transform.mat4();
|
push.transform = projectionView * obj.m_transform.mat4();
|
||||||
|
|
||||||
vkCmdPushConstants(
|
vkCmdPushConstants(
|
||||||
commandBuffer,
|
commandBuffer,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue