Skip to content

Commit

Permalink
Improved CMS blending
Browse files Browse the repository at this point in the history
  • Loading branch information
patricklbell committed Oct 29, 2022
1 parent e949848 commit d34c870
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 40 deletions.
Binary file modified data/levels/lightmap_gazebo.level.0.tga
Binary file not shown.
Binary file modified data/levels/lightmap_gazebo.level.2.tga
Binary file not shown.
Binary file modified data/levels/lightmap_test.level
Binary file not shown.
Binary file modified data/levels/lightmap_test.level.1.tga
Binary file not shown.
Binary file modified data/levels/lightmap_test.level.2.tga
Binary file not shown.
2 changes: 1 addition & 1 deletion data/shaders/lib/pbr.gl
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ vec3 brdfSchlickGGX(vec3 position, vec3 normal, vec3 albedo, float metallic, flo

vec3 lo = (kD * albedo / PI + specular) * radiance * NdotL;
#if SHADOWS
lo *= shadowness(NdotL, position);
lo *= 1.0 - shadowness(NdotL, position);
#endif

#if IBL
Expand Down
16 changes: 9 additions & 7 deletions data/shaders/lib/shadows.gl
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ float random(vec3 seed, int i){
// gl_FragColor.xyz = (color * NdotL + pow(max(dot(R, L), 0), 64)) * shadow * texture2DProj(spot, shadowMapPos) + color * 0.1;
//}

#define CSM_BLEND_BAND 0.1
#define CSM_BLEND_BAND 0.5

float calculatePcfShadowness(float NdotL, vec3 position, int layer) {
vec4 shadow_pos = shadow_vps[layer] * vec4(position, 1.0);
Expand All @@ -102,7 +102,7 @@ float calculatePcfShadowness(float NdotL, vec3 position, int layer) {
// return unshadowed result when point is beyond closer than near_plane of the light's frustum.
if (shadow_depth > 1.0)
{
return 1.0;
return 0.0;
}

// calculate bias (based on depth map resolution and slope)
Expand All @@ -124,7 +124,7 @@ float calculatePcfShadowness(float NdotL, vec3 position, int layer) {
for(int y = -1; y <= 1; ++y)
{
float pcf_depth = texture(shadow_map, vec3(shadow_coord.xy + vec2(x, y) * texel_size, layer)).r;
shadow += (shadow_depth - bias) > pcf_depth ? 0.0 : 1.0;
shadow += (shadow_depth - bias) > pcf_depth ? 1.0 : 0.0;
}
}
shadow /= 9.0f;
Expand Down Expand Up @@ -152,10 +152,12 @@ float shadowness(float NdotL, vec3 position){

// Blend between shadow maps as suggested by, seems a bit expensive:
// https://learn.microsoft.com/en-us/windows/win32/dxtecharts/cascaded-shadow-maps
float delta = abs(shadow_cascade_distances[layer] - frag_dist);
if(abs(delta) < CSM_BLEND_BAND) { // Do blending
float blend = smoothstep(0.0, CSM_BLEND_BAND, abs(delta));
shadowness = mix(calculatePcfShadowness(NdotL, position, layer + 1), shadowness, blend);
if(layer > 0) {
float delta = shadow_cascade_distances[layer - 1] - frag_dist;
if(delta <= CSM_BLEND_BAND) { // Do blending
float blend = smoothstep(0.0, CSM_BLEND_BAND, delta);
shadowness = mix(shadowness, calculatePcfShadowness(NdotL, position, layer - 1), blend);
}
}

return shadowness;
Expand Down
35 changes: 19 additions & 16 deletions data/shaders/unified.gl
Original file line number Diff line number Diff line change
Expand Up @@ -113,22 +113,25 @@ void main() {
normal = TBN * normal;
vec3 albedo = albedo_mult*texture(albedo_map, texcoord).xyz;

// @debug, renders different layers of csm in colors
//float frag_dist = abs((view * vec4(position, 1.0)).z);
//int layer = CASCADE_NUM;
//for (int i = 0; i < CASCADE_NUM; ++i)
//{
// if (frag_dist < shadow_cascade_distances[i])
// {
// layer = i;
// break;
// }
//}
//
//const vec3 colors[] = {vec3(1.0, 0.0, 0.0), vec3(0.0, 1.0, 0.0), vec3(0.0, 0.0, 1.0), vec3(1.0, 1.0, 0.0), vec3(1.0,
// 0.0, 1.0)};
//float NdotL = dot(normal, -sun_direction);
//out_color = vec4(vec3(shadowness(NdotL)*colors[layer]), 1.0);
// @debug, renders different layers of csm in colors
//#if SHADOWS
// float frag_dist = abs((view * vec4(position, 1.0)).z);
// int layer = CASCADE_NUM;
// for (int i = 0; i < CASCADE_NUM; ++i)
// {
// if (frag_dist < shadow_cascade_distances[i])
// {
// layer = i;
// break;
// }
// }
//
// const vec3 colors[] = {vec3(1.0, 0.0, 0.0), vec3(0.0, 1.0, 0.0), vec3(0.0, 0.0, 1.0), vec3(1.0, 1.0, 0.0), vec3(1.0, 0.0, 1.0)};
// float NdotL = dot(normal, -sun_direction);
// out_color = vec4(vec3((1.0-0.8*shadowness(NdotL, position))*colors[layer]), 1.0);
//#else
// out_color = vec4(1.0);
//#endif

#ifndef PBR
// Treats albedo as diffuse texture
Expand Down
33 changes: 17 additions & 16 deletions src/lightmapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ bool runLightmapper(EntityManager& entity_manager, AssetManager &asset_manager,
auto m_e = reinterpret_cast<MeshEntity*>(entity_manager.entities[i]);
if (entity_manager.entities[i] == nullptr || !entityInherits(m_e->type, MESH_ENTITY) || m_e->mesh == nullptr || entityInherits(m_e->type, ANIMATED_MESH_ENTITY)) continue;

m_e->lightmap = asset_manager.getColorTexture(glm::vec3(0, 0, 0), GL_RGBA);
m_e->lightmap = asset_manager.getColorTexture(glm::vec3(0, 0, 0), GL_RGB);
m_e->lightmap->handle = level_path + "." + std::to_string(i) + ".tga";
}

Expand All @@ -98,13 +98,14 @@ bool runLightmapper(EntityManager& entity_manager, AssetManager &asset_manager,
!entityInherits(m_e->type, MESH_ENTITY) || m_e->mesh == nullptr ||
entityInherits(m_e->type, ANIMATED_MESH_ENTITY) || entityInherits(m_e->type, VEGETATION_ENTITY))
continue;
mesh_entities.push_back(m_e);
float* img = (float*)calloc((size_t)w * h * c, sizeof(float));

float* img = (float*)malloc((size_t)w * h * c * sizeof(float));
assert(img != NULL);
lightmaps.push_back(img);
mesh_entities.push_back(m_e);
}

constexpr int bounces = 3;
constexpr int bounces = 1;
for (int b = 0; b < bounces; b++) {
std::cout << "Bounce " << b << " / " << bounces - 1 << "\n";

Expand All @@ -114,7 +115,7 @@ bool runLightmapper(EntityManager& entity_manager, AssetManager &asset_manager,
const auto& img = lightmaps[i];
const auto& m_e = mesh_entities[i];

// @note relies on all 0 representing 0.0 float which is true for almost all standards
// @note relies on 0 bits representing 0.0 in floating format which is true for almost all standards
memset(img, 0, sizeof(float) * w * h * c); // clear lightmap to black
lmSetTargetLightmap(ctx, img, w, h, c);

Expand All @@ -129,7 +130,7 @@ bool runLightmapper(EntityManager& entity_manager, AssetManager &asset_manager,
auto model = g_model_pos * mesh->transforms[j] * g_model_rot_scl;

assert(mesh->uvs != nullptr); // Mesh should be parameterized and packed
lmSetGeometry(ctx, (float*)&model[0],
lmSetGeometry(ctx, (float*)&model[0][0],
LM_FLOAT, mesh->vertices, sizeof(*mesh->vertices),
LM_FLOAT, mesh->normals, sizeof(*mesh->normals),
LM_FLOAT, mesh->uvs, sizeof(*mesh->uvs),
Expand All @@ -149,15 +150,15 @@ bool runLightmapper(EntityManager& entity_manager, AssetManager &asset_manager,
);

// Used for debugging the hemicube's rendering
//using namespace std::this_thread; // sleep_for, sleep_until
//using namespace std::chrono; // nanoseconds, system_clock, seconds
//bindBackbuffer();
//if(vp[0] == 0 && vp[1] == 0)
// clearFramebuffer();
//glViewport(vp[0], vp[1], vp[2], vp[3]);
//drawEntitiesHdr(entity_manager, skybox, skybox_irradiance, skybox_specular, camera, true);
//glfwSwapBuffers(window);
//sleep_for(milliseconds(10));
using namespace std::this_thread; // sleep_for, sleep_until
using namespace std::chrono; // nanoseconds, system_clock, seconds
bindBackbuffer();
if(vp[0] == 0 && vp[1] == 0)
clearFramebuffer();
glViewport(vp[0], vp[1], vp[2], vp[3]);
drawEntitiesHdr(entity_manager, skybox, skybox_irradiance, skybox_specular, camera, true);
glfwSwapBuffers(window);
sleep_for(milliseconds(10));

glViewport(vp[0], vp[1], vp[2], vp[3]);
drawEntitiesHdr(entity_manager, skybox, skybox_irradiance, skybox_specular, camera, true);
Expand Down Expand Up @@ -203,7 +204,7 @@ bool runLightmapper(EntityManager& entity_manager, AssetManager &asset_manager,
m_e->lightmap->resolution = glm::ivec2(w, h);

glBindTexture(GL_TEXTURE_2D, m_e->lightmap->id);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_FLOAT, img);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB16F, w, h, 0, GL_RGBA, GL_FLOAT, img);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Expand Down

0 comments on commit d34c870

Please sign in to comment.