Skip to content
Snippets Groups Projects
Commit fa597eb8 authored by Alexander Gauggel's avatar Alexander Gauggel
Browse files

[#81] Add normal and sun direction to voxelization

parent 419f5437
No related branches found
No related tags found
1 merge request!68Resolve "Complete Voxelization"
struct LightInfo{
vec3 L; float padding;
vec3 sunColor;
float sunStrength;
mat4 lightMatrix;
};
\ No newline at end of file
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
#extension GL_GOOGLE_include_directive : enable #extension GL_GOOGLE_include_directive : enable
#include "perMeshResources.inc" #include "perMeshResources.inc"
#include "lightInfo.inc"
layout(location = 0) in vec3 passNormal; layout(location = 0) in vec3 passNormal;
layout(location = 1) in vec2 passUV; layout(location = 1) in vec2 passUV;
...@@ -11,16 +12,13 @@ layout(location = 2) in vec3 passPos; ...@@ -11,16 +12,13 @@ layout(location = 2) in vec3 passPos;
layout(location = 0) out vec3 outColor; layout(location = 0) out vec3 outColor;
layout(set=0, binding=0) uniform sunBuffer { layout(set=0, binding=0) uniform sunBuffer {
vec3 L; float padding; LightInfo lightInfo;
vec3 sunColor;
float sunStrength;
mat4 lightMatrix;
}; };
layout(set=0, binding=1) uniform texture2D shadowMap; layout(set=0, binding=1) uniform texture2D shadowMap;
layout(set=0, binding=2) uniform sampler shadowMapSampler; layout(set=0, binding=2) uniform sampler shadowMapSampler;
float shadowTest(vec3 worldPos){ float shadowTest(vec3 worldPos){
vec4 lightPos = lightMatrix * vec4(worldPos, 1); vec4 lightPos = lightInfo.lightMatrix * vec4(worldPos, 1);
lightPos /= lightPos.w; lightPos /= lightPos.w;
lightPos.xy = lightPos.xy * 0.5 + 0.5; lightPos.xy = lightPos.xy * 0.5 + 0.5;
...@@ -38,7 +36,7 @@ float shadowTest(vec3 worldPos){ ...@@ -38,7 +36,7 @@ float shadowTest(vec3 worldPos){
void main() { void main() {
vec3 N = normalize(passNormal); vec3 N = normalize(passNormal);
vec3 sun = sunStrength * sunColor * clamp(dot(N, L), 0, 1); vec3 sun = lightInfo.sunStrength * lightInfo.sunColor * clamp(dot(N, lightInfo.L), 0, 1);
sun *= shadowTest(passPos); sun *= shadowTest(passPos);
vec3 ambient = vec3(0.05); vec3 ambient = vec3(0.05);
vec3 albedo = texture(sampler2D(albedoTexture, textureSampler), passUV).rgb; vec3 albedo = texture(sampler2D(albedoTexture, textureSampler), passUV).rgb;
......
...@@ -4,9 +4,11 @@ ...@@ -4,9 +4,11 @@
#include "voxel.inc" #include "voxel.inc"
#include "perMeshResources.inc" #include "perMeshResources.inc"
#include "lightInfo.inc"
layout(location = 0) in vec3 passPos; layout(location = 0) in vec3 passPos;
layout(location = 1) out vec2 passUV; layout(location = 1) out vec2 passUV;
layout(location = 2) in vec3 passN;
layout(set=0, binding=0, std430) buffer voxelizationBuffer{ layout(set=0, binding=0, std430) buffer voxelizationBuffer{
uint packedVoxelData[]; uint packedVoxelData[];
...@@ -18,6 +20,10 @@ layout(set=0, binding=1) uniform voxelizationInfo{ ...@@ -18,6 +20,10 @@ layout(set=0, binding=1) uniform voxelizationInfo{
layout(set=0, binding=2, r8) uniform image3D voxelImage; layout(set=0, binding=2, r8) uniform image3D voxelImage;
layout(set=0, binding=3) uniform sunBuffer {
LightInfo lightInfo;
};
vec3 worldToVoxelCoordinates(vec3 world, VoxelInfo info){ vec3 worldToVoxelCoordinates(vec3 world, VoxelInfo info){
return (world - info.offset) / info.extent + 0.5f; return (world - info.offset) / info.extent + 0.5f;
} }
...@@ -38,6 +44,11 @@ void main() { ...@@ -38,6 +44,11 @@ void main() {
// for some reason the automatic mip level here does not work // for some reason the automatic mip level here does not work
// biasing does not work either // biasing does not work either
// as a workaround a fixed, high mip level is chosen // as a workaround a fixed, high mip level is chosen
vec3 color = textureLod(sampler2D(albedoTexture, textureSampler), passUV, 10.f).rgb; vec3 albedo = textureLod(sampler2D(albedoTexture, textureSampler), passUV, 10.f).rgb;
vec3 N = normalize(passN);
float NoL = clamp(dot(N, lightInfo.L), 0, 1);
vec3 color = albedo * NoL;
atomicMax(packedVoxelData[flatIndex], packVoxelInfo(color)); atomicMax(packedVoxelData[flatIndex], packVoxelInfo(color));
} }
\ No newline at end of file
...@@ -6,9 +6,11 @@ layout (triangle_strip, max_vertices = 3) out; ...@@ -6,9 +6,11 @@ layout (triangle_strip, max_vertices = 3) out;
layout(location = 0) in vec3 passPosIn[3]; layout(location = 0) in vec3 passPosIn[3];
layout(location = 1) in vec2 passUVIn[3]; layout(location = 1) in vec2 passUVIn[3];
layout(location = 2) in vec3 passNIn[3];
layout(location = 0) out vec3 passPos; layout(location = 0) out vec3 passPos;
layout(location = 1) out vec2 passUV; layout(location = 1) out vec2 passUV;
layout(location = 2) out vec3 passN;
void main() { void main() {
// compute geometric normal, no normalization necessary // compute geometric normal, no normalization necessary
...@@ -29,6 +31,7 @@ void main() { ...@@ -29,6 +31,7 @@ void main() {
gl_Position.z = gl_Position.z * 0.5 + 0.5; // xyz are kept in NDC range [-1, 1] so swizzling works, but vulkan needs final z in range [0, 1] gl_Position.z = gl_Position.z * 0.5 + 0.5; // xyz are kept in NDC range [-1, 1] so swizzling works, but vulkan needs final z in range [0, 1]
passPos = passPosIn[i]; passPos = passPosIn[i];
passUV = passUVIn[i]; passUV = passUVIn[i];
passN = passNIn[i];
EmitVertex(); EmitVertex();
} }
EndPrimitive(); EndPrimitive();
......
...@@ -7,6 +7,7 @@ layout(location = 2) in vec2 inUV; ...@@ -7,6 +7,7 @@ layout(location = 2) in vec2 inUV;
layout(location = 0) out vec3 passPos; layout(location = 0) out vec3 passPos;
layout(location = 1) out vec2 passUV; layout(location = 1) out vec2 passUV;
layout(location = 2) out vec3 passN;
layout( push_constant ) uniform constants{ layout( push_constant ) uniform constants{
mat4 mvp; mat4 mvp;
...@@ -17,4 +18,5 @@ void main() { ...@@ -17,4 +18,5 @@ void main() {
gl_Position = mvp * vec4(inPosition, 1.0); gl_Position = mvp * vec4(inPosition, 1.0);
passPos = (model * vec4(inPosition, 1)).xyz; passPos = (model * vec4(inPosition, 1)).xyz;
passUV = inUV; passUV = inUV;
passN = inNormal;
} }
\ No newline at end of file
...@@ -62,7 +62,7 @@ const uint32_t voxelResolution = 128; ...@@ -62,7 +62,7 @@ const uint32_t voxelResolution = 128;
uint32_t voxelCount = voxelResolution * voxelResolution * voxelResolution; uint32_t voxelCount = voxelResolution * voxelResolution * voxelResolution;
const vk::Format voxelizationDummyFormat = vk::Format::eR8Unorm; const vk::Format voxelizationDummyFormat = vk::Format::eR8Unorm;
Voxelization::Voxelization(vkcv::Core* corePtr, const Dependencies& dependencies) Voxelization::Voxelization(vkcv::Core* corePtr, const Dependencies& dependencies, vkcv::BufferHandle lightInfoBuffer)
: :
m_corePtr(corePtr), m_corePtr(corePtr),
m_voxelImage(m_corePtr->createImage(vk::Format::eR16G16B16A16Sfloat, voxelResolution, voxelResolution, voxelResolution, false, true)), m_voxelImage(m_corePtr->createImage(vk::Format::eR16G16B16A16Sfloat, voxelResolution, voxelResolution, voxelResolution, false, true)),
...@@ -100,7 +100,10 @@ Voxelization::Voxelization(vkcv::Core* corePtr, const Dependencies& dependencies ...@@ -100,7 +100,10 @@ Voxelization::Voxelization(vkcv::Core* corePtr, const Dependencies& dependencies
vkcv::DescriptorWrites voxelizationDescriptorWrites; vkcv::DescriptorWrites voxelizationDescriptorWrites;
voxelizationDescriptorWrites.storageBufferWrites = { vkcv::StorageBufferDescriptorWrite(0, m_voxelBuffer.getHandle()) }; voxelizationDescriptorWrites.storageBufferWrites = { vkcv::StorageBufferDescriptorWrite(0, m_voxelBuffer.getHandle()) };
voxelizationDescriptorWrites.uniformBufferWrites = { vkcv::UniformBufferDescriptorWrite(1, m_voxelInfoBuffer.getHandle()) }; voxelizationDescriptorWrites.uniformBufferWrites = {
vkcv::UniformBufferDescriptorWrite(1, m_voxelInfoBuffer.getHandle()),
vkcv::UniformBufferDescriptorWrite(3, lightInfoBuffer)
};
voxelizationDescriptorWrites.storageImageWrites = { vkcv::StorageImageDescriptorWrite(2, m_voxelImage.getHandle()) }; voxelizationDescriptorWrites.storageImageWrites = { vkcv::StorageImageDescriptorWrite(2, m_voxelImage.getHandle()) };
m_corePtr->writeDescriptorSet(m_voxelizationDescriptorSet, voxelizationDescriptorWrites); m_corePtr->writeDescriptorSet(m_voxelizationDescriptorSet, voxelizationDescriptorWrites);
......
...@@ -9,7 +9,7 @@ public: ...@@ -9,7 +9,7 @@ public:
vk::Format colorBufferFormat; vk::Format colorBufferFormat;
vk::Format depthBufferFormat; vk::Format depthBufferFormat;
}; };
Voxelization(vkcv::Core* corePtr, const Dependencies& dependencies); Voxelization(vkcv::Core* corePtr, const Dependencies& dependencies, vkcv::BufferHandle lightInfoBuffer);
void voxelizeMeshes( void voxelizeMeshes(
vkcv::CommandStreamHandle cmdStream, vkcv::CommandStreamHandle cmdStream,
......
...@@ -319,7 +319,7 @@ int main(int argc, const char** argv) { ...@@ -319,7 +319,7 @@ int main(int argc, const char** argv) {
voxelDependencies.colorBufferFormat = colorBufferFormat; voxelDependencies.colorBufferFormat = colorBufferFormat;
voxelDependencies.depthBufferFormat = depthBufferFormat; voxelDependencies.depthBufferFormat = depthBufferFormat;
voxelDependencies.vertexLayout = vertexLayout; voxelDependencies.vertexLayout = vertexLayout;
Voxelization voxelization(&core, voxelDependencies); Voxelization voxelization(&core, voxelDependencies, lightBuffer.getHandle());
vkcv::gui::GUI gui(core, window); vkcv::gui::GUI gui(core, window);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment