Skip to content

Commit

Permalink
[tutorial 25] multiple rotating lights done
Browse files Browse the repository at this point in the history
  • Loading branch information
DiantArts committed Aug 6, 2022
1 parent d44d001 commit feb60b9
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 66 deletions.
19 changes: 0 additions & 19 deletions .gitignore

This file was deleted.

Binary file added binaries/xrnEcs
Binary file not shown.
30 changes: 30 additions & 0 deletions cccontrolReminder
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<C-o> ::std::
<C-A-o> ::xrn::
<C-e> this->
<C-A-e> *this
<C-u> ::std::vector
<C-A-u> ::std::span
<C-u> ::std::make_unique<>()
<C-A-u> ::std::make_shared<>()
<C-l> const
<C-a-l> constexpr
<C-c> static
<C-g> [[ nodiscard ]]
<C-f> noexcept
<C-n> if () {<CR>}<Esc><Up>A
<C-A-n> switch () {<CR>// case : break;<CR>default: break;<CR>}
<C-t> for (const auto& elem : ) {<CR>}
<C-A-t> for (auto i{ 0uz }; i < ; ++i) {<CR>}
<C-d> while () {<CR>}<Esc><Up>A
<C-b> <Esc>:call AppendNamespace()<CR>
<C-x> ::std::cout << << ::std::endl;
<C-A-x> ::std::cout << "" << ::std::endl;
<C-k> auto&&... args
<C-A-k> ::std::forward<decltype(args)>(args)...
p :cnext
P :cprevious
y :cc
Y :copen
f :copen .build/errorFile
F :cclose
g :AsyncRun make hide_color=true
18 changes: 0 additions & 18 deletions shaders/compile.sh

This file was deleted.

24 changes: 14 additions & 10 deletions sources/Vksb/AScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,19 +179,23 @@ auto ::vksb::AScene::update()
}
}

{
auto lightIndex{ 0uz };
m_registry.view<::vksb::component::PointLight, ::vksb::component::Position>().each(
[this, &lightIndex](auto& pointLight, auto& position) {
m_pointLightSystem.update(m_frameInfo, pointLight, position, lightIndex);
++lightIndex;
}
);
m_frameInfo.ubo.numOfLights = lightIndex;
}

{
auto& position{ m_registry.get<::vksb::component::Position>(m_camera.getId()) };
auto& rotation{ m_registry.get<::vksb::component::Rotation>(m_camera.getId()) };
m_camera.setViewDirection(position, rotation.getDirection());
// m_camera.setViewDirection(::glm::vec3{ 0.0f, 0.0f, -2.5f }, ::glm::vec3{ 0.0f, 0.0f, 1.0f });
}

auto lightIndex{ 0uz };
m_registry.view<::vksb::component::PointLight>().each([this, &lightIndex](auto& pointLight) {
m_pointLightSystem.update(m_frameInfo, pointLight, lightIndex);
++lightIndex;
});
m_frameInfo.ubo.numOfLights = lightIndex;
return true;
}

Expand Down Expand Up @@ -232,9 +236,9 @@ void ::vksb::AScene::draw()
});

m_pointLightSystem.bind(m_frameInfo);
m_registry.view<::vksb::component::PointLight>().each([this](auto& pointLight) {
m_pointLightSystem(m_frameInfo, pointLight);
});
m_registry.view<::vksb::component::PointLight, ::vksb::component::Position>().each(
[this](auto& pointLight, auto position) { m_pointLightSystem(m_frameInfo, pointLight, position); }
);

m_renderer.endSwapChainRenderPass(m_frameInfo.commandBuffer);
m_renderer.endFrame();
Expand Down
24 changes: 21 additions & 3 deletions sources/Vksb/App.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,26 @@ void ::vksb::App::loadObjects()
}

{
auto entity{ m_registry.create() };
m_registry.emplace<::vksb::component::Position>(entity, 0.0f, 0.5f, 0.0f);
m_registry.emplace<::vksb::component::PointLight>(entity);
std::vector<glm::vec3> lightColors{
{ 1.f, .1f, .1f },
{ .1f, .1f, 1.f },
{ .1f, 1.f, .1f },
{ 1.f, 1.f, .1f },
{ .1f, 1.f, 1.f },
{ 1.f, 1.f, 1.f }
};
for (auto i{ 0uz }; const auto& color : lightColors) {
auto rotation{ ::glm::rotate(
::glm::mat4(1.0f),
i * ::glm::two_pi<float>() / lightColors.size(),
{ 0.0f, -1.0f, 0.0f }
) };
auto entity{ m_registry.create() };
m_registry.emplace<::vksb::component::Position>(
entity, ::glm::vec3{ rotation * ::glm::vec4{ -0.0f, -0.0f, -0.5f, 1.0f } }
);
m_registry.emplace<::vksb::component::PointLight>(entity, color);
++i;
}
}
}
30 changes: 22 additions & 8 deletions sources/Vksb/Component/PointLight.hpp
Original file line number Diff line number Diff line change
@@ -1,21 +1,35 @@
#pragma once

#include <Vksb/Component/Position.hpp>

namespace vksb::component {

struct PointLight {

struct PushConstant {
PushConstant() = default;
PushConstant(
const ::vksb::component::PointLight& pointLight,
const ::vksb::component::Position& position
)
: color{ pointLight.color }
, position{ position.get(), pointLight.radius }
{}
::glm::vec4 color; // w == intensity
::glm::vec4 position; // w == radius
};

PointLight(
::glm::vec3 thatColor = ::glm::vec3{ 1.0f },
float intensity = 0.2f,
::glm::vec3 color = ::glm::vec3{ 1.0f },
::glm::vec3 position = ::glm::vec3{ 0.0f },
float radius = 0.03f
float thatRadius = 0.03f
)
: m_color{ color, intensity }
, m_position{ position, radius }
: color{ thatColor, intensity }
, radius{ thatRadius }
{}

::glm::vec4 m_color; // w == intensity
::glm::vec4 m_position; // w == radius
// position is relative if a component::Position is contained
::glm::vec4 color; // w == intensity
float radius;
};

} // namespace vksb::component
1 change: 0 additions & 1 deletion sources/Vksb/Event/KeyPressed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ void ::vksb::event::KeyPressed::resolve(
} else if (m_keyCode == ::vksb::configuration.keyBinding.moveUp) {
playerController.startMovingUp();
} else if (m_keyCode == ::vksb::configuration.keyBinding.moveDown) {
::fmt::print("hello\n");
playerController.startMovingDown();

// move arrows
Expand Down
15 changes: 10 additions & 5 deletions sources/Vksb/System/PointLight.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ void ::vksb::system::PointLight::createPipelineLayout(
::VkPushConstantRange pushConstantRange{};
pushConstantRange.stageFlags = ::VK_SHADER_STAGE_VERTEX_BIT | ::VK_SHADER_STAGE_FRAGMENT_BIT;
pushConstantRange.offset = 0;
pushConstantRange.size = sizeof(::vksb::component::PointLight);
pushConstantRange.size = sizeof(::vksb::component::PointLight::PushConstant);

::std::vector<::VkDescriptorSetLayout> descriptorSetLayouts{ descriptorSetLayout };

Expand Down Expand Up @@ -98,16 +98,18 @@ void ::vksb::system::PointLight::createPipeline(
///////////////////////////////////////////////////////////////////////////
void ::vksb::system::PointLight::operator()(
::vksb::FrameInfo& frameInfo,
const ::vksb::component::PointLight& pointLight
const ::vksb::component::PointLight& pointLight,
const ::vksb::component::Position& position
) const
{
::vksb::component::PointLight::PushConstant pushConstant{ pointLight, position };
::vkCmdPushConstants(
frameInfo.commandBuffer,
m_pipelineLayout,
VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT,
0,
sizeof(pointLight),
&pointLight
sizeof(pushConstant),
&pushConstant
);
::vkCmdDraw(frameInfo.commandBuffer, 6, 1, 0, 0);
}
Expand All @@ -134,8 +136,11 @@ void ::vksb::system::PointLight::bind(
void ::vksb::system::PointLight::update(
::vksb::FrameInfo& frameInfo,
::vksb::component::PointLight& pointLight,
::vksb::component::Position& position,
::std::size_t lightIndex
)
{
frameInfo.ubo.pointLights[lightIndex] = auto{ pointLight };
auto rotation{ ::glm::rotate(::glm::mat4(1.0f), (float)frameInfo.deltaTime.get() / 1000, { 0.0f, -1.0f, 0.0f }) };
position = ::glm::vec3{ rotation * ::glm::vec4{ ::glm::vec3{ position }, 1.0f } };
frameInfo.ubo.pointLights[lightIndex] = ::vksb::component::PointLight::PushConstant{ pointLight, position };
}
4 changes: 3 additions & 1 deletion sources/Vksb/System/PointLight.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ class PointLight {
///////////////////////////////////////////////////////////////////////////
void operator()(
::vksb::FrameInfo& frameInfo,
const ::vksb::component::PointLight& pointLight
const ::vksb::component::PointLight& pointLight,
const ::vksb::component::Position& position
) const;

///////////////////////////////////////////////////////////////////////////
Expand All @@ -115,6 +116,7 @@ class PointLight {
void update(
::vksb::FrameInfo& frameInfo,
::vksb::component::PointLight& pointLight,
::vksb::component::Position& position,
::std::size_t lightIndex
);

Expand Down
2 changes: 1 addition & 1 deletion sources/Vksb/Ubo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ struct Ubo {
::glm::vec4 ambientLightColor{ 1.0f, 1.0f, 1.0f, 0.02f }; // w is intensity

static inline constexpr const ::std::size_t maxLights{ 10uz };
::vksb::component::PointLight pointLights[maxLights];
::vksb::component::PointLight::PushConstant pointLights[maxLights];
::std::size_t numOfLights;
};

Expand Down

0 comments on commit feb60b9

Please sign in to comment.