Skip to content

Commit

Permalink
Support for transparent background
Browse files Browse the repository at this point in the history
  • Loading branch information
knightcrawler25 committed Dec 29, 2021
1 parent 4a41dcb commit 39a66c3
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 34 deletions.
4 changes: 4 additions & 0 deletions src/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ void MainLoop(void* arg)
{
bool requiresReload = false;
Vec3* uniformLightCol = &renderOptions.uniformLightCol;
Vec3* backgroundCol = &renderOptions.backgroundCol;

optionsChanged |= ImGui::SliderInt("Max Depth", &renderOptions.maxDepth, 1, 10);
requiresReload |= ImGui::Checkbox("Enable Environment Map", &renderOptions.useEnvMap);
Expand All @@ -330,6 +331,9 @@ void MainLoop(void* arg)
requiresReload |= ImGui::Checkbox("Enable Uniform Light", &renderOptions.useUniformLight);
optionsChanged |= ImGui::ColorEdit3("Uniform Light Color", (float*)uniformLightCol, 0);
requiresReload |= ImGui::Checkbox("Hide Emitters", &renderOptions.hideEmitters);
requiresReload |= ImGui::Checkbox("Enable Background", &renderOptions.enableBackground);
optionsChanged |= ImGui::ColorEdit3("Background Color", (float*)backgroundCol, 0);
requiresReload |= ImGui::Checkbox("Transparent Background", &renderOptions.transparentBackground);
ImGui::Checkbox("Enable Denoiser", &renderOptions.enableDenoiser);
ImGui::SliderInt("Number of Frames to skip", &renderOptions.denoiserFrameCnt, 5, 50);
ImGui::Checkbox("Enable Tonemap", &renderOptions.enableTonemap);
Expand Down
6 changes: 6 additions & 0 deletions src/core/Renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ namespace GLSLPT
texArrayHeight = 4096;
openglNormalMap = true;
hideEmitters = false;
enableBackground = false;
backgroundCol = Vec3(1.0f, 1.0f, 1.0f);
transparentBackground = false;
}

iVec2 resolution;
Expand All @@ -77,6 +80,9 @@ namespace GLSLPT
int texArrayHeight;
bool openglNormalMap;
bool hideEmitters;
Vec3 backgroundCol;
bool enableBackground;
bool transparentBackground;
};

class Scene;
Expand Down
43 changes: 33 additions & 10 deletions src/core/TiledRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,22 +94,33 @@ namespace GLSLPT

// Add preprocessor defines for conditional compilation
std::string pathtraceDefines = "";
std::string tonemapDefines = "";

if (scene->renderOptions.useEnvMap && scene->hdrData != nullptr)
pathtraceDefines += "#define ENVMAP\n";
pathtraceDefines += "#define OPT_ENVMAP\n";
if (!scene->lights.empty())
pathtraceDefines += "#define LIGHTS\n";
pathtraceDefines += "#define OPT_LIGHTS\n";
if (scene->renderOptions.enableRR)
{
pathtraceDefines += "#define RR\n";
pathtraceDefines += "#define RR_DEPTH " + std::to_string(scene->renderOptions.RRDepth) + "\n";
pathtraceDefines += "#define OPT_RR\n";
pathtraceDefines += "#define OPT_RR_DEPTH " + std::to_string(scene->renderOptions.RRDepth) + "\n";
}
if (scene->renderOptions.useUniformLight)
pathtraceDefines += "#define UNIFORM_LIGHT\n";
pathtraceDefines += "#define OPT_UNIFORM_LIGHT\n";
if (scene->renderOptions.openglNormalMap)
pathtraceDefines += "#define OPENGL_NORMALMAP\n";
pathtraceDefines += "#define OPT_OPENGL_NORMALMAP\n";
if (scene->renderOptions.hideEmitters)
pathtraceDefines += "#define HIDE_EMITTERS\n";
pathtraceDefines += "#define OPT_HIDE_EMITTERS\n";
if (scene->renderOptions.enableBackground)
{
pathtraceDefines += "#define OPT_BACKGROUND\n";
tonemapDefines += "#define OPT_BACKGROUND\n";
}
if (scene->renderOptions.transparentBackground)
{
pathtraceDefines += "#define OPT_TRANSPARENT_BACKGROUND\n";
tonemapDefines += "#define OPT_TRANSPARENT_BACKGROUND\n";
}

if (pathtraceDefines.size() > 0)
{
Expand All @@ -128,6 +139,16 @@ namespace GLSLPT
pathTraceShaderLowResSrcObj.src.insert(idx + 1, pathtraceDefines);
}

if (tonemapDefines.size() > 0)
{
size_t idx = tonemapShaderSrcObj.src.find("#version");
if (idx != -1)
idx = tonemapShaderSrcObj.src.find("\n", idx);
else
idx = 0;
tonemapShaderSrcObj.src.insert(idx + 1, tonemapDefines);
}

pathTraceShader = LoadShaders(vertexShaderSrcObj, pathTraceShaderSrcObj);
pathTraceShaderLowRes = LoadShaders(vertexShaderSrcObj, pathTraceShaderLowResSrcObj);
outputShader = LoadShaders(vertexShaderSrcObj, outputShaderSrcObj);
Expand Down Expand Up @@ -176,7 +197,7 @@ namespace GLSLPT
//Create Texture for FBO
glGenTextures(1, &accumTexture);
glBindTexture(GL_TEXTURE_2D, accumTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F, screenSize.x, screenSize.y, 0, GL_RGB, GL_FLOAT, 0);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, screenSize.x, screenSize.y, 0, GL_RGBA, GL_FLOAT, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glBindTexture(GL_TEXTURE_2D, 0);
Expand Down Expand Up @@ -417,6 +438,7 @@ namespace GLSLPT
// Denoise Image
if (scene->renderOptions.enableDenoiser && frameCounter % (scene->renderOptions.denoiserFrameCnt * (numTiles.x * numTiles.y)) == 0)
{
// FIXME: Figure out a way to have transparency with denoiser
glBindTexture(GL_TEXTURE_2D, tileOutputTexture[1 - currentBuffer]);
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGB, GL_FLOAT, denoiserInputFramePtr);

Expand All @@ -426,8 +448,8 @@ namespace GLSLPT

// Create a denoising filter
oidn::FilterRef filter = device.newFilter("RT"); // generic ray tracing filter
filter.setImage("color", denoiserInputFramePtr, oidn::Format::Float3, screenSize.x, screenSize.y);
filter.setImage("output", frameOutputPtr, oidn::Format::Float3, screenSize.x, screenSize.y);
filter.setImage("color", denoiserInputFramePtr, oidn::Format::Float3, screenSize.x, screenSize.y, 0, 0, 0);
filter.setImage("output", frameOutputPtr, oidn::Format::Float3, screenSize.x, screenSize.y, 0, 0, 0);
filter.set("hdr", false);
filter.commit();

Expand Down Expand Up @@ -518,6 +540,7 @@ namespace GLSLPT
glUniform1i(glGetUniformLocation(shaderObject, "enableTonemap"), scene->renderOptions.enableTonemap);
glUniform1i(glGetUniformLocation(shaderObject, "useAces"), scene->renderOptions.useAces);
glUniform1i(glGetUniformLocation(shaderObject, "simpleAcesFit"), scene->renderOptions.simpleAcesFit);
glUniform3f(glGetUniformLocation(shaderObject, "backgroundCol"), scene->renderOptions.backgroundCol.x, scene->renderOptions.backgroundCol.y, scene->renderOptions.backgroundCol.z);
tonemapShader->StopUsing();
}
}
15 changes: 15 additions & 0 deletions src/loaders/Loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,8 @@ namespace GLSLPT
char enableRR[10] = "None";
char openglNormalMap[10] = "None";
char hideEmitters[10] = "None";
char transparentBackground[10] = "None";
char enableBackground[10] = "None";

while (fgets(line, kMaxLineLength, file))
{
Expand All @@ -237,6 +239,9 @@ namespace GLSLPT
sscanf(line, " texArrayHeight %i", &renderOptions.texArrayHeight);
sscanf(line, " openglNormalMap %s", openglNormalMap);
sscanf(line, " hideEmitters %s", hideEmitters);
sscanf(line, " enableBackground %s", enableBackground);
sscanf(line, " transparentBackground %s", transparentBackground);
sscanf(line, " backgroundColor %f %f %f", &renderOptions.backgroundCol.x, &renderOptions.backgroundCol.y, &renderOptions.backgroundCol.z);
}

if (strcmp(envMap, "None") != 0)
Expand All @@ -261,6 +266,16 @@ namespace GLSLPT
renderOptions.hideEmitters = false;
else if (strcmp(hideEmitters, "True") == 0)
renderOptions.hideEmitters = true;

if (strcmp(enableBackground, "False") == 0)
renderOptions.enableBackground = false;
else if (strcmp(enableBackground, "True") == 0)
renderOptions.enableBackground = true;

if (strcmp(transparentBackground, "False") == 0)
renderOptions.transparentBackground = false;
else if (strcmp(transparentBackground, "True") == 0)
renderOptions.transparentBackground = true;
}


Expand Down
4 changes: 2 additions & 2 deletions src/shaders/common/closest_hit.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ bool ClosestHit(Ray r, inout State state, inout LightSampleRec lightSampleRec)
float t = INF;
float d;

#ifdef LIGHTS
#ifdef OPT_LIGHTS
// Intersect Emitters
#ifdef HIDE_EMITTERS
#ifdef OPT_HIDE_EMITTERS
if(state.depth > 0)
#endif
for (int i = 0; i < numOfLights; i++)
Expand Down
32 changes: 19 additions & 13 deletions src/shaders/common/pathtrace.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ vec3 DirectLight(in Ray r, in State state)
BsdfSampleRec bsdfSampleRec;

// Environment Light
#ifdef ENVMAP
#ifndef UNIFORM_LIGHT
#ifdef OPT_ENVMAP
#ifndef OPT_UNIFORM_LIGHT
{
vec3 color;
vec4 dirPdf = SampleEnvMap(color);
Expand All @@ -135,7 +135,7 @@ vec3 DirectLight(in Ray r, in State state)
#endif

// Analytic Lights
#ifdef LIGHTS
#ifdef OPT_LIGHTS
{
LightSampleRec lightSampleRec;
Light light;
Expand Down Expand Up @@ -179,14 +179,15 @@ vec3 DirectLight(in Ray r, in State state)
return Li;
}

vec3 PathTrace(Ray r)
vec4 PathTrace(Ray r)
{
vec3 radiance = vec3(0.0);
vec3 throughput = vec3(1.0);
State state;
LightSampleRec lightSampleRec;
BsdfSampleRec bsdfSampleRec;
vec3 absorption = vec3(0.0);
float alpha = 1.0;

for (int depth = 0; depth < maxDepth; depth++)
{
Expand All @@ -195,13 +196,18 @@ vec3 PathTrace(Ray r)

if (!hit)
{
#ifdef UNIFORM_LIGHT
#ifdef HIDE_EMITTERS
#if defined(OPT_BACKGROUND) || defined(OPT_TRANSPARENT_BACKGROUND)
if (state.depth == 0)
alpha = 0.0;
#endif

#ifdef OPT_UNIFORM_LIGHT
#ifdef OPT_HIDE_EMITTERS
if(state.depth > 0)
#endif
radiance += uniformLightCol * throughput;
#else
#ifdef ENVMAP
#ifdef OPT_ENVMAP
{
float misWeight = 1.0f;
vec2 uv = vec2((PI + atan(r.direction.z, r.direction.x)) * INV_TWO_PI, acos(r.direction.y) * INV_PI);
Expand All @@ -212,14 +218,14 @@ vec3 PathTrace(Ray r)
float lightPdf = EnvMapPdf(r);
misWeight = PowerHeuristic(bsdfSampleRec.pdf, lightPdf);
}
#ifdef HIDE_EMITTERS
#ifdef OPT_HIDE_EMITTERS
if (state.depth > 0)
#endif
radiance += misWeight * texture(hdrTex, uv).xyz * throughput * hdrMultiplier;
}
#endif
#endif
return radiance;
return vec4(radiance, alpha);
}

GetMaterials(state, r);
Expand All @@ -230,7 +236,7 @@ vec3 PathTrace(Ray r)

radiance += state.mat.emission * throughput;

#ifdef LIGHTS
#ifdef OPT_LIGHTS
if (state.isEmitter)
{
radiance += EmitterSample(r, state, lightSampleRec, bsdfSampleRec) * throughput;
Expand All @@ -254,9 +260,9 @@ vec3 PathTrace(Ray r)
else
break;

#ifdef RR
#ifdef OPT_RR
// Russian roulette
if (depth >= RR_DEPTH)
if (depth >= OPT_RR_DEPTH)
{
float q = min(max(throughput.x, max(throughput.y, throughput.z)) + 0.001, 0.95);
if (rand() > q)
Expand All @@ -269,5 +275,5 @@ vec3 PathTrace(Ray r)
r.origin = state.fhp + r.direction * EPS;
}

return radiance;
return vec4(radiance, alpha);
}
4 changes: 2 additions & 2 deletions src/shaders/common/sampling.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,8 @@ void SampleOneLight(in Light light, in vec3 surfacePos, inout LightSampleRec lig
SampleDistantLight(light, surfacePos, lightSampleRec);
}

#ifdef ENVMAP
#ifndef UNIFORM_LIGHT
#ifdef OPT_ENVMAP
#ifndef OPT_UNIFORM_LIGHT

float EnvMapPdf(in Ray r)
{
Expand Down
4 changes: 2 additions & 2 deletions src/shaders/preview.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

#version 330

out vec3 color;
out vec4 color;
in vec2 TexCoords;

#include common/uniforms.glsl
Expand Down Expand Up @@ -64,7 +64,7 @@ void main(void)

Ray ray = Ray(camera.position + randomAperturePos, finalRayDir);

vec3 pixelColor = PathTrace(ray);
vec4 pixelColor = PathTrace(ray);

color = pixelColor;
}
6 changes: 3 additions & 3 deletions src/shaders/tile.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

#version 330

out vec3 color;
out vec4 color;
in vec2 TexCoords;

#include common/uniforms.glsl
Expand Down Expand Up @@ -66,9 +66,9 @@ void main(void)

Ray ray = Ray(camera.position + randomAperturePos, finalRayDir);

vec3 accumColor = texture(accumTexture, coordsTile).xyz;
vec4 accumColor = texture(accumTexture, coordsTile);

vec3 pixelColor = PathTrace(ray);
vec4 pixelColor = PathTrace(ray);

color = pixelColor + accumColor;
}
23 changes: 21 additions & 2 deletions src/shaders/tonemap.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@

#version 330

out vec3 color;
out vec4 outCol;
in vec2 TexCoords;

uniform sampler2D pathTraceTexture;
uniform float invSampleCounter;
uniform bool enableTonemap;
uniform bool useAces;
uniform bool simpleAcesFit;
uniform vec3 backgroundCol;

#include common/globals.glsl

Expand Down Expand Up @@ -95,7 +96,9 @@ vec3 Tonemap(in vec3 c, float limit)

void main()
{
color = texture(pathTraceTexture, TexCoords).xyz * invSampleCounter;
vec4 col = texture(pathTraceTexture, TexCoords) * invSampleCounter;
vec3 color = col.rgb;
float alpha = col.a;

if (enableTonemap)
{
Expand All @@ -111,4 +114,20 @@ void main()
}

color = pow(color, vec3(1.0 / 2.2));

float outAlpha = 1.0;
vec3 bgCol = backgroundCol;

#ifdef OPT_TRANSPARENT_BACKGROUND
outAlpha = alpha;
float checkerSize = 10.0;
float res = max(sign(mod(floor(gl_FragCoord.x / checkerSize) + floor(gl_FragCoord.y / checkerSize), 2.0)), 0.0);
bgCol = mix(vec3(0.1), vec3(0.2), res);
#endif

#if defined(OPT_BACKGROUND) || defined(OPT_TRANSPARENT_BACKGROUND)
outCol = vec4(mix(bgCol, color, alpha), outAlpha);
#else
outCol = vec4(color, 1.0);
#endif
}

0 comments on commit 39a66c3

Please sign in to comment.