Added light intensity and camera controls.

This commit is contained in:
Anish Bhobe 2024-07-25 21:48:02 +02:00
parent abdd7137ab
commit 88d74de291
4 changed files with 92 additions and 25 deletions

View File

@ -115,7 +115,7 @@ LightManager::operator=(LightManager &&other) noexcept
} }
LightHandle LightHandle
LightManager::AddDirectional(const vec3 &direction, const vec3 &color) LightManager::AddDirectional(const vec3 &direction, const vec3 &color, f32 intensity)
{ {
const vec3 normDirection = normalize(direction); const vec3 normDirection = normalize(direction);
if (m_DirectionalLightCount < m_MetaInfo.m_DirectionalLightMaxCount) if (m_DirectionalLightCount < m_MetaInfo.m_DirectionalLightMaxCount)
@ -130,6 +130,7 @@ LightManager::AddDirectional(const vec3 &direction, const vec3 &color)
light.m_Color_ = (ToColor32(color) & Light::COLOR_MASK) | Light::TYPE_DIRECTIONAL | gen; light.m_Color_ = (ToColor32(color) & Light::COLOR_MASK) | Light::TYPE_DIRECTIONAL | gen;
light.m_Range = 1.0f; light.m_Range = 1.0f;
light.um_Direction = normDirection; light.um_Direction = normDirection;
light.m_Intensity = intensity;
m_GpuBufferCapacity_ |= UPDATE_REQUIRED_BIT; m_GpuBufferCapacity_ |= UPDATE_REQUIRED_BIT;
@ -166,6 +167,7 @@ LightManager::AddDirectional(const vec3 &direction, const vec3 &color)
m_Lights[m_DirectionalLightCount].m_Color_ = (ToColor32(color) & Light::COLOR_MASK) | Light::TYPE_DIRECTIONAL | gen; m_Lights[m_DirectionalLightCount].m_Color_ = (ToColor32(color) & Light::COLOR_MASK) | Light::TYPE_DIRECTIONAL | gen;
m_Lights[m_DirectionalLightCount].m_Range = 1.0f; m_Lights[m_DirectionalLightCount].m_Range = 1.0f;
m_Lights[m_DirectionalLightCount].um_Direction = normDirection; m_Lights[m_DirectionalLightCount].um_Direction = normDirection;
m_Lights[m_DirectionalLightCount].m_Intensity = intensity;
const u16 index = m_DirectionalLightCount; const u16 index = m_DirectionalLightCount;
++m_DirectionalLightCount; ++m_DirectionalLightCount;
@ -175,7 +177,7 @@ LightManager::AddDirectional(const vec3 &direction, const vec3 &color)
} }
LightHandle LightHandle
LightManager::AddPoint(const vec3 &position, const vec3 &color, const f32 radius) LightManager::AddPoint(const vec3 &position, const vec3 &color, const f32 radius, f32 intensity)
{ {
assert(m_PointLightCount <= m_MetaInfo.m_PointLightMaxCount); assert(m_PointLightCount <= m_MetaInfo.m_PointLightMaxCount);
assert(radius >= 0.0f); assert(radius >= 0.0f);
@ -191,6 +193,7 @@ LightManager::AddPoint(const vec3 &position, const vec3 &color, const f32 radius
light->m_Color_ = (ToColor32(color) & Light::COLOR_MASK) | Light::TYPE_POINT | gen; light->m_Color_ = (ToColor32(color) & Light::COLOR_MASK) | Light::TYPE_POINT | gen;
light->m_Range = radius; light->m_Range = radius;
light->um_Position = position; light->um_Position = position;
light->m_Intensity = intensity;
m_GpuBufferCapacity_ |= UPDATE_REQUIRED_BIT; m_GpuBufferCapacity_ |= UPDATE_REQUIRED_BIT;
@ -211,6 +214,7 @@ LightManager::AddPoint(const vec3 &position, const vec3 &color, const f32 radius
light->m_Color_ = (ToColor32(color) & Light::COLOR_MASK) | Light::TYPE_POINT | gen; light->m_Color_ = (ToColor32(color) & Light::COLOR_MASK) | Light::TYPE_POINT | gen;
light->m_Range = radius; light->m_Range = radius;
light->um_Position = position; light->um_Position = position;
light->m_Intensity = intensity;
++m_PointLightCount; ++m_PointLightCount;
++m_MetaInfo.m_PointLightMaxCount; ++m_MetaInfo.m_PointLightMaxCount;

View File

@ -68,8 +68,8 @@ struct LightManager
constexpr static u16 UPDATE_REQUIRED_BIT = 1; constexpr static u16 UPDATE_REQUIRED_BIT = 1;
constexpr static u16 CAPACITY_MASK = ~(UPDATE_REQUIRED_BIT); constexpr static u16 CAPACITY_MASK = ~(UPDATE_REQUIRED_BIT);
LightHandle AddDirectional(const vec3 &direction, const vec3 &color); LightHandle AddDirectional(const vec3 &direction, const vec3 &color, f32 intensity);
LightHandle AddPoint(const vec3 &position, const vec3 &color, f32 radius); LightHandle AddPoint(const vec3 &position, const vec3 &color, f32 radius, f32 intensity);
void Update(); void Update();
void RemoveLight(LightHandle handle); void RemoveLight(LightHandle handle);

View File

@ -35,8 +35,61 @@ struct Camera
{ {
mat4 m_View; mat4 m_View;
mat4 m_Perspective; mat4 m_Perspective;
vec3 m_Position; vec4 m_Position;
};
struct CameraController
{
constexpr static vec3 UP = vec3(0.0f, 1.0f, 0.0f);
f32 m_Fov;
f32 m_Pitch;
f32 m_Yaw;
f32 m_AspectRatio; f32 m_AspectRatio;
Camera m_Camera;
CameraController(const vec3 &position, const vec3 &target, const f32 vFov, const f32 aspectRatio)
: m_Fov(vFov)
, m_Pitch{0.0f}
, m_Yaw{0.0f}
, m_AspectRatio{aspectRatio}
, m_Camera{
.m_View = lookAt(position, target, UP),
.m_Perspective = glm::perspective(vFov, aspectRatio, 0.1f, 100.0f),
.m_Position = vec4{0.0f, 2.0f, 2.0f, 1.0f},
}
{
}
void
SetAspectRatio(const f32 aspectRatio)
{
m_AspectRatio = aspectRatio;
m_Camera.m_Perspective = glm::perspective(m_Fov, aspectRatio, 0.1f, 100.0f);
}
void
SetPosition(const vec3 &position)
{
m_Camera.m_Position = vec4(position, 1.0f);
f32 cosPitch = cos(m_Pitch);
const vec3 target = vec3(sin(m_Yaw) * cosPitch, sin(m_Pitch), -cos(m_Yaw) * cosPitch);
m_Camera.m_View = lookAt(position, position + target, UP);
}
void
SetPitchYaw(f32 pitch, f32 yaw)
{
m_Pitch = pitch;
m_Yaw = yaw;
f32 cosPitch = cos(m_Pitch);
const vec3 target = vec3(sin(m_Yaw) * cosPitch, sin(m_Pitch), -cos(m_Yaw) * cosPitch);
const vec3 position = vec3{m_Camera.m_Position};
m_Camera.m_View = lookAt(position, position + target, UP);
}
}; };
int int
@ -88,9 +141,14 @@ main(int, char **)
vk::Format attachmentFormat = vk::Format::eR8G8B8A8Srgb; vk::Format attachmentFormat = vk::Format::eR8G8B8A8Srgb;
Pipeline pipeline = CreatePipeline(&device, attachmentFormat, &resourceManager); Pipeline pipeline = CreatePipeline(&device, attachmentFormat, &resourceManager);
lightManager.AddDirectional(vec3(0.0f, -1.0f, 0.0f), {0.0f, 0.7f, 0.0f}); //lightManager.AddDirectional(vec3(0.0f, -1.0f, 0.0f), {0.0f, 0.7f, 0.0f});
lightManager.AddPoint(vec3{2.0f, 1.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, 15.0f); //lightManager.AddPoint(vec3{2.0f, 1.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, 15.0f);
lightManager.AddPoint(vec3{-2.0f, 1.0f, 0.0f}, {0.0f, 0.0f, 1.0f}, 15.0f); //lightManager.AddPoint(vec3{-2.0f, 1.0f, 0.0f}, {0.0f, 0.0f, 1.0f}, 15.0f);
lightManager.AddPoint(vec3{-5.0f, -5.0f, 5.0f}, vec3{1.0f}, 30.0f, 16.0f);
lightManager.AddPoint(vec3{5.0f, -5.0f, 5.0f}, vec3{1.0f}, 30.0f, 16.0f);
lightManager.AddPoint(vec3{-5.0f, 5.0f, 5.0f}, vec3{1.0f}, 30.0f, 16.0f);
lightManager.AddPoint(vec3{5.0f, 5.0f, 5.0f}, vec3{1.0f}, 30.0f, 16.0f);
lightManager.Update(); lightManager.Update();
@ -119,17 +177,12 @@ main(int, char **)
vk::Extent2D internalResolution = {1920, 1080}; vk::Extent2D internalResolution = {1920, 1080};
Camera camera = { CameraController cameraController = {vec3{0.0f, 2.0f, 2.0f}, vec3{0.0f}, 70_deg,
.m_View = lookAt(vec3(0.0f, 2.0f, 2.0f), vec3(0.0f), vec3(0.0f, 1.0f, 0.0f)), Cast<f32>(swapchain.m_Extent.width) / Cast<f32>(swapchain.m_Extent.height)};
.m_Perspective = glm::perspective(
70_deg, Cast<f32>(swapchain.m_Extent.width) / Cast<f32>(swapchain.m_Extent.height), 0.1f, 100.0f),
.m_Position = vec4{0.0f, 2.0f, 2.0f, 1.0f},
.m_AspectRatio = Cast<f32>(swapchain.m_Extent.width) / Cast<f32>(swapchain.m_Extent.height),
};
UniformBuffer ubo; UniformBuffer ubo;
ubo.Init(&device, sizeof camera, "Camera UBO"); ubo.Init(&device, sizeof cameraController.m_Camera, "Camera UBO");
ubo.Write(&device, 0, sizeof camera, &camera); ubo.Write(&device, 0, sizeof cameraController.m_Camera, &cameraController.m_Camera);
vk::DescriptorBufferInfo descriptorBufferInfo = { vk::DescriptorBufferInfo descriptorBufferInfo = {
.buffer = ubo.m_Buffer, .buffer = ubo.m_Buffer,
.offset = 0, .offset = 0,
@ -275,11 +328,12 @@ main(int, char **)
bool rotating = false; bool rotating = false;
bool lockToScreen = true; bool lockToScreen = true;
i32 height = Cast<i32>(internalResolution.height); i32 height = Cast<i32>(internalResolution.height);
vec2 camPitchYaw = vec2(0.0f);
vec3 camPosition = cameraController.m_Camera.m_Position;
vk::Extent2D inputResolution = internalResolution; vk::Extent2D inputResolution = internalResolution;
swapchain.RegisterResizeCallback([&camera](vk::Extent2D extent) { swapchain.RegisterResizeCallback([&cameraController](vk::Extent2D extent) {
camera.m_AspectRatio = Cast<f32>(extent.width) / Cast<f32>(extent.height); cameraController.SetAspectRatio(Cast<f32>(extent.width) / Cast<f32>(extent.height));
camera.m_Perspective = glm::perspective(70_deg, camera.m_AspectRatio, 0.1f, 100.0f);
}); });
Time::Init(); Time::Init();
@ -303,7 +357,7 @@ main(int, char **)
} }
inputResolution.height = height; inputResolution.height = height;
inputResolution.width = Cast<i32>(camera.m_AspectRatio * Cast<f32>(inputResolution.height)); inputResolution.width = Cast<i32>(cameraController.m_AspectRatio * Cast<f32>(inputResolution.height));
if (gui::Button("Change Resolution")) if (gui::Button("Change Resolution"))
{ {
@ -333,6 +387,15 @@ main(int, char **)
gui::Separator(); gui::Separator();
gui::Text("Delta: %0.6f ms", 1000.0f * Time::m_Delta); gui::Text("Delta: %0.6f ms", 1000.0f * Time::m_Delta);
gui::Text("FPS: %0.6f", 1.0f / Time::m_Delta); gui::Text("FPS: %0.6f", 1.0f / Time::m_Delta);
gui::Separator();
if (gui::DragFloat2("Camera Orientation", Recast<f32 *>(&camPitchYaw)))
{
cameraController.SetPitchYaw(glm::radians(camPitchYaw.x), glm::radians(camPitchYaw.y));
}
if (gui::InputFloat3("Camera Position", Recast<f32 *>(&camPosition)))
{
cameraController.SetPosition(camPosition);
}
gui::Checkbox("Rotate", &rotating); gui::Checkbox("Rotate", &rotating);
if (gui::Button("Exit")) if (gui::Button("Exit"))
{ {
@ -348,7 +411,7 @@ main(int, char **)
rotate(model.GetModelTransform(), Cast<f32>(45.0_deg * Time::m_Delta), vec3(0.0f, 1.0f, 0.0f))); rotate(model.GetModelTransform(), Cast<f32>(45.0_deg * Time::m_Delta), vec3(0.0f, 1.0f, 0.0f)));
} }
model.Update(); model.Update();
ubo.Write(&device, 0, sizeof camera, &camera); ubo.Write(&device, 0, sizeof cameraController.m_Camera, &cameraController.m_Camera);
Frame *currentFrame = frameManager.GetNextFrame(&swapchain, &window); Frame *currentFrame = frameManager.GetNextFrame(&swapchain, &window);

View File

@ -167,7 +167,7 @@ float3 GetPointLightInfluence(float3 Albedo, float2 MetalRough, float3 Position,
float G = (Light.Color & 0x00FF0000) >> 16; float G = (Light.Color & 0x00FF0000) >> 16;
float B = (Light.Color & 0x0000FF00) >> 8; float B = (Light.Color & 0x0000FF00) >> 8;
float3 LightColor = LightAmp * float3(R, G, B) * 0.00392156862f; // 0.00392156862 = 1/255 float3 LightColor = LightAmp * Light.Intensity * float3(R, G, B) * 0.00392156862f; // 0.00392156862 = 1/255
Contrib += GetPBRContrib(Albedo, LightColor, ViewDir, Normal, Metallic, Roughness, F_0, LightDir, LightDistance); Contrib += GetPBRContrib(Albedo, LightColor, ViewDir, Normal, Metallic, Roughness, F_0, LightDir, LightDistance);
} }
@ -209,7 +209,7 @@ float3 GetDirectionalLightInfluence(float3 Albedo, float2 MetalRough, float3 Pos
float G = (Light.Color & 0x00FF0000) >> 16; float G = (Light.Color & 0x00FF0000) >> 16;
float B = (Light.Color & 0x0000FF00) >> 8; float B = (Light.Color & 0x0000FF00) >> 8;
float3 LightColor = LightAmp * float3(R, G, B) * 0.00392156862f; // 0.00392156862 = 1/255 float3 LightColor = LightAmp * Light.Intensity * float3(R, G, B) * 0.00392156862f; // 0.00392156862 = 1/255
Contrib += GetPBRContrib(Albedo, LightColor, ViewDir, Normal, Metallic, Roughness, F_0, LightDir, 1.0f); Contrib += GetPBRContrib(Albedo, LightColor, ViewDir, Normal, Metallic, Roughness, F_0, LightDir, 1.0f);
} }