Skip to content

Commit

Permalink
tutorial 14
Browse files Browse the repository at this point in the history
https://www.youtube.com/watch?v=rvJHkYnAR3w
Signed-off-by: Chakib_Chemso <chemsoblakblak@gmail.com>
  • Loading branch information
chakibchemso committed May 9, 2023
1 parent b4173f0 commit e3efb79
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 11 deletions.
9 changes: 5 additions & 4 deletions Vulkan Engine/apps/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ namespace vk_engine
{
const vk_simple_render_system simple_render_system{device, renderer.get_swap_chain_render_pass()};
vk_camera camera{};

//camera.set_view_direction(glm::vec3{0.f}, glm::vec3{0.5f, 0.f, 1.f});
//camera.set_view_target(glm::vec3(-1, -2, -2), glm::vec3(0, 0, 2.5));

while (!window.should_close())
{
Expand All @@ -33,7 +34,6 @@ namespace vk_engine

if (const auto command_buffer = renderer.begin_frame())
{

renderer.begin_swap_chain_render_pass(command_buffer);
// update rotations
int i = 0;
Expand Down Expand Up @@ -105,18 +105,19 @@ namespace vk_engine
{{-.5f, -.5f, -0.5f}, {.1f, .8f, .1f}},
{{.5f, -.5f, -0.5f}, {.1f, .8f, .1f}},
{{.5f, .5f, -0.5f}, {.1f, .8f, .1f}},

};

for (auto& [position, color] : vertices)
{
position += offset;
}

return std::make_unique<vk_model>(device, vertices);
}

void application::load_game_objects()
{
std::shared_ptr<vk_model> model = create_cube_model(device, {0.f, 0.f, 0.f});
const std::shared_ptr<vk_model> model = create_cube_model(device, {0.f, 0.f, 0.f});

auto cube = vk_game_object::create_game_object();
cube.model = model;
Expand Down
71 changes: 66 additions & 5 deletions Vulkan Engine/engine/vk_camera.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
#include "vk_camera.hpp"

void vk_engine::vk_camera::set_orthographic_projection(
using namespace glm;

using vk_engine::vk_camera;

void vk_camera::set_orthographic_projection(
const float left, const float right,
const float bottom, const float top,
const float near, const float far)
{
projection_matrix = glm::mat4{1.0f};
projection_matrix = mat4{1.0f};
projection_matrix[0][0] = 2.f / (right - left);
projection_matrix[1][1] = 2.f / (bottom - top);
projection_matrix[2][2] = 1.f / (far - near);
Expand All @@ -14,21 +18,78 @@ void vk_engine::vk_camera::set_orthographic_projection(
projection_matrix[3][2] = -near / (far - near);
}

void vk_engine::vk_camera::set_perspective_projection(
void vk_camera::set_perspective_projection(
const float fov, const float aspect,
const float near, const float far)
{
assert(glm::abs(aspect - std::numeric_limits<float>::epsilon()) > 0.0f);
const float tan_half_fov = tan(fov / 2.f);
projection_matrix = glm::mat4{0.0f};
projection_matrix = mat4{0.0f};
projection_matrix[0][0] = 1.f / (aspect * tan_half_fov);
projection_matrix[1][1] = 1.f / (tan_half_fov);
projection_matrix[2][2] = far / (far - near);
projection_matrix[2][3] = 1.f;
projection_matrix[3][2] = -(far * near) / (far - near);
}

const glm::mat4& vk_engine::vk_camera::get_projection_matrix() const
void vk_camera::set_view_direction(const vec3 position, const vec3 direction, const vec3 up)
{
const vec3 w{normalize(direction)};
const vec3 u{normalize(cross(w, up))};
const vec3 v{cross(w, u)};

view_matrix = mat4{1.f};
view_matrix[0][0] = u.x;
view_matrix[1][0] = u.y;
view_matrix[2][0] = u.z;
view_matrix[0][1] = v.x;
view_matrix[1][1] = v.y;
view_matrix[2][1] = v.z;
view_matrix[0][2] = w.x;
view_matrix[1][2] = w.y;
view_matrix[2][2] = w.z;
view_matrix[3][0] = -dot(u, position);
view_matrix[3][1] = -dot(v, position);
view_matrix[3][2] = -dot(w, position);
}

void vk_camera::set_view_target(const vec3 position, const vec3 target, const vec3 up)
{
set_view_direction(position, target - position, up);
}

void vk_camera::set_view_yxz(const vec3 position, const vec3 rotation)
{
const float c3 = cos(rotation.z);
const float s3 = sin(rotation.z);
const float c2 = cos(rotation.x);
const float s2 = sin(rotation.x);
const float c1 = cos(rotation.y);
const float s1 = sin(rotation.y);
const vec3 u{(c1 * c3 + s1 * s2 * s3), (c2 * s3), (c1 * s2 * s3 - c3 * s1)};
const vec3 v{(c3 * s1 * s2 - c1 * s3), (c2 * c3), (c1 * c3 * s2 + s1 * s3)};
const vec3 w{(c2 * s1), (-s2), (c1 * c2)};
view_matrix = mat4{1.f};
view_matrix[0][0] = u.x;
view_matrix[1][0] = u.y;
view_matrix[2][0] = u.z;
view_matrix[0][1] = v.x;
view_matrix[1][1] = v.y;
view_matrix[2][1] = v.z;
view_matrix[0][2] = w.x;
view_matrix[1][2] = w.y;
view_matrix[2][2] = w.z;
view_matrix[3][0] = -dot(u, position);
view_matrix[3][1] = -dot(v, position);
view_matrix[3][2] = -dot(w, position);
}

const mat4& vk_camera::get_projection() const
{
return projection_matrix;
}

const mat4& vk_camera::get_view() const
{
return view_matrix;
}
7 changes: 6 additions & 1 deletion Vulkan Engine/engine/vk_camera.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@ namespace vk_engine
public:
void set_orthographic_projection(float left, float right, float bottom, float top, float near, float far);
void set_perspective_projection(float fov, float aspect, float near, float far);
const glm::mat4& get_projection_matrix() const;
void set_view_direction(glm::vec3 position, glm::vec3 direction, glm::vec3 up = glm::vec3{0.f, -1.f, 0.f});
void set_view_target(glm::vec3 position, glm::vec3 target, glm::vec3 up = glm::vec3{0.f, -1.f, 0.f});
void set_view_yxz(glm::vec3 position, glm::vec3 rotation);
const glm::mat4& get_projection() const;
const glm::mat4& get_view() const;

private:
glm::mat4 projection_matrix{1.f};
glm::mat4 view_matrix{1.f};
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,12 @@ namespace vk_engine
{
pipeline->bind(command_buffer);

const auto projection_view = camera.get_projection() * camera.get_view();

for (auto& game_object : game_objects)
{
simple_push_const_data push{};
push.transform = camera.get_projection_matrix() * game_object.transform.mat4();
push.transform = projection_view * game_object.transform.mat4();
push.color = game_object.color;

vkCmdPushConstants(
Expand Down

0 comments on commit e3efb79

Please sign in to comment.