diff --git a/samples/03_model_render/light_manager.cpp b/samples/03_model_render/light_manager.cpp index 2c96b2e..3dbd7c6 100644 --- a/samples/03_model_render/light_manager.cpp +++ b/samples/03_model_render/light_manager.cpp @@ -115,7 +115,7 @@ LightManager::operator=(LightManager &&other) noexcept } LightHandle -LightManager::AddDirectional(const vec3 &direction, const vec3 &color) +LightManager::AddDirectional(const vec3 &direction, const vec3 &color, f32 intensity) { const vec3 normDirection = normalize(direction); 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_Range = 1.0f; light.um_Direction = normDirection; + light.m_Intensity = intensity; 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_Range = 1.0f; m_Lights[m_DirectionalLightCount].um_Direction = normDirection; + m_Lights[m_DirectionalLightCount].m_Intensity = intensity; const u16 index = m_DirectionalLightCount; ++m_DirectionalLightCount; @@ -175,7 +177,7 @@ LightManager::AddDirectional(const vec3 &direction, const vec3 &color) } 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(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_Range = radius; light->um_Position = position; + light->m_Intensity = intensity; 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_Range = radius; light->um_Position = position; + light->m_Intensity = intensity; ++m_PointLightCount; ++m_MetaInfo.m_PointLightMaxCount; diff --git a/samples/03_model_render/light_manager.h b/samples/03_model_render/light_manager.h index 6d4fc23..e2f0298 100644 --- a/samples/03_model_render/light_manager.h +++ b/samples/03_model_render/light_manager.h @@ -68,8 +68,8 @@ struct LightManager constexpr static u16 UPDATE_REQUIRED_BIT = 1; constexpr static u16 CAPACITY_MASK = ~(UPDATE_REQUIRED_BIT); - LightHandle AddDirectional(const vec3 &direction, const vec3 &color); - LightHandle AddPoint(const vec3 &position, const vec3 &color, f32 radius); + LightHandle AddDirectional(const vec3 &direction, const vec3 &color, f32 intensity); + LightHandle AddPoint(const vec3 &position, const vec3 &color, f32 radius, f32 intensity); void Update(); void RemoveLight(LightHandle handle); diff --git a/samples/03_model_render/model_render.cpp b/samples/03_model_render/model_render.cpp index ce54756..48eedf8 100644 --- a/samples/03_model_render/model_render.cpp +++ b/samples/03_model_render/model_render.cpp @@ -35,8 +35,61 @@ struct Camera { mat4 m_View; 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; + + 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 @@ -88,9 +141,14 @@ main(int, char **) vk::Format attachmentFormat = vk::Format::eR8G8B8A8Srgb; Pipeline pipeline = CreatePipeline(&device, attachmentFormat, &resourceManager); - 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}, {0.0f, 0.0f, 1.0f}, 15.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}, {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(); @@ -119,17 +177,12 @@ main(int, char **) vk::Extent2D internalResolution = {1920, 1080}; - Camera camera = { - .m_View = lookAt(vec3(0.0f, 2.0f, 2.0f), vec3(0.0f), vec3(0.0f, 1.0f, 0.0f)), - .m_Perspective = glm::perspective( - 70_deg, Cast(swapchain.m_Extent.width) / Cast(swapchain.m_Extent.height), 0.1f, 100.0f), - .m_Position = vec4{0.0f, 2.0f, 2.0f, 1.0f}, - .m_AspectRatio = Cast(swapchain.m_Extent.width) / Cast(swapchain.m_Extent.height), - }; + CameraController cameraController = {vec3{0.0f, 2.0f, 2.0f}, vec3{0.0f}, 70_deg, + Cast(swapchain.m_Extent.width) / Cast(swapchain.m_Extent.height)}; UniformBuffer ubo; - ubo.Init(&device, sizeof camera, "Camera UBO"); - ubo.Write(&device, 0, sizeof camera, &camera); + ubo.Init(&device, sizeof cameraController.m_Camera, "Camera UBO"); + ubo.Write(&device, 0, sizeof cameraController.m_Camera, &cameraController.m_Camera); vk::DescriptorBufferInfo descriptorBufferInfo = { .buffer = ubo.m_Buffer, .offset = 0, @@ -275,11 +328,12 @@ main(int, char **) bool rotating = false; bool lockToScreen = true; i32 height = Cast(internalResolution.height); + vec2 camPitchYaw = vec2(0.0f); + vec3 camPosition = cameraController.m_Camera.m_Position; vk::Extent2D inputResolution = internalResolution; - swapchain.RegisterResizeCallback([&camera](vk::Extent2D extent) { - camera.m_AspectRatio = Cast(extent.width) / Cast(extent.height); - camera.m_Perspective = glm::perspective(70_deg, camera.m_AspectRatio, 0.1f, 100.0f); + swapchain.RegisterResizeCallback([&cameraController](vk::Extent2D extent) { + cameraController.SetAspectRatio(Cast(extent.width) / Cast(extent.height)); }); Time::Init(); @@ -303,7 +357,7 @@ main(int, char **) } inputResolution.height = height; - inputResolution.width = Cast(camera.m_AspectRatio * Cast(inputResolution.height)); + inputResolution.width = Cast(cameraController.m_AspectRatio * Cast(inputResolution.height)); if (gui::Button("Change Resolution")) { @@ -333,6 +387,15 @@ main(int, char **) gui::Separator(); gui::Text("Delta: %0.6f ms", 1000.0f * Time::m_Delta); gui::Text("FPS: %0.6f", 1.0f / Time::m_Delta); + gui::Separator(); + if (gui::DragFloat2("Camera Orientation", Recast(&camPitchYaw))) + { + cameraController.SetPitchYaw(glm::radians(camPitchYaw.x), glm::radians(camPitchYaw.y)); + } + if (gui::InputFloat3("Camera Position", Recast(&camPosition))) + { + cameraController.SetPosition(camPosition); + } gui::Checkbox("Rotate", &rotating); if (gui::Button("Exit")) { @@ -348,7 +411,7 @@ main(int, char **) rotate(model.GetModelTransform(), Cast(45.0_deg * Time::m_Delta), vec3(0.0f, 1.0f, 0.0f))); } 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); diff --git a/samples/03_model_render/shader/model.ps.hlsl b/samples/03_model_render/shader/model.ps.hlsl index 8354f46..c716893 100644 --- a/samples/03_model_render/shader/model.ps.hlsl +++ b/samples/03_model_render/shader/model.ps.hlsl @@ -110,7 +110,7 @@ float3 GetPBRContrib(float3 Albedo, float3 LightColor, float3 ViewDir, float3 No float NdotL = max(dot(Normal, LightDir), 0.0f); float3 Radiance = LightColor * Attenuation; - + float NormalDistribution = TrowbridgeReitzGGX(Normal, Halfway, Roughness); float Geometry = GeometrySmith(NdotV, NdotL, Roughness); float3 Fresnel = FresnelSchlick(CosineFactor, F_0); @@ -167,7 +167,7 @@ float3 GetPointLightInfluence(float3 Albedo, float2 MetalRough, float3 Position, float G = (Light.Color & 0x00FF0000) >> 16; 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); } @@ -209,7 +209,7 @@ float3 GetDirectionalLightInfluence(float3 Albedo, float2 MetalRough, float3 Pos float G = (Light.Color & 0x00FF0000) >> 16; 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); }