Skip to content

Commit

Permalink
Magicavoxel style volumes (Incomplete)
Browse files Browse the repository at this point in the history
  • Loading branch information
knightcrawler25 committed Jan 14, 2022
1 parent 40e84e8 commit b6d7311
Show file tree
Hide file tree
Showing 11 changed files with 221 additions and 117 deletions.
4 changes: 2 additions & 2 deletions assets/cornell_box.scene
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,15 @@ mesh
{
file cornell_box/cbox_smallbox.obj
material small_box_white
position .1855 .0825 .169
position .1855 .0835 .169
scale 0.01 0.01 0.01
}

mesh
{
file cornell_box/cbox_largebox.obj
material large_box_white
position .3685 .165 .35125
position .3685 .166 .35125
scale 0.01 0.01 0.01
}

Expand Down
2 changes: 1 addition & 1 deletion assets/cornell_box2.scene
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ mesh
{
file cornell_box/cbox_largebox.obj
material large_box_white
position .3685 .165 .35125
position .3685 .166 .35125
scale 0.01 0.01 0.01
}

Expand Down
41 changes: 34 additions & 7 deletions src/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ void LoadScene(std::string sceneName)
success = LoadSceneFromFile(sceneName, scene, renderOptions);
else if (ext == "gltf")
success = LoadGLTF(sceneName, scene, renderOptions, xform, false);
else if(ext == "glb")
else if (ext == "glb")
success = LoadGLTF(sceneName, scene, renderOptions, xform, true);

if (!success)
Expand Down Expand Up @@ -277,7 +277,7 @@ void MainLoop(void* arg)
if (event.window.event == SDL_WINDOWEVENT_RESIZED)
{
renderOptions.windowResolution = iVec2(event.window.data1, event.window.data2);

if (!renderOptions.independentRenderSize)
renderOptions.renderResolution = renderOptions.windowResolution;

Expand Down Expand Up @@ -330,7 +330,7 @@ void MainLoop(void* arg)
std::vector<const char*> envMapsList;
for (int i = 0; i < envMaps.size(); ++i)
envMapsList.push_back(envMaps[i].c_str());

if (ImGui::Combo("EnvMaps", &envMapIdx, envMapsList.data(), envMapsList.size()))
{
scene->AddEnvMap(envMaps[envMapIdx]);
Expand All @@ -345,7 +345,7 @@ void MainLoop(void* arg)
{
optionsChanged |= ImGui::SliderInt("Max Spp", &renderOptions.maxSpp, -1, 256);
optionsChanged |= ImGui::SliderInt("Max Depth", &renderOptions.maxDepth, 1, 10);

reloadShaders |= ImGui::Checkbox("Enable Russian Roulette", &renderOptions.enableRR);
reloadShaders |= ImGui::SliderInt("Russian Roulette Depth", &renderOptions.RRDepth, 1, 10);
reloadShaders |= ImGui::Checkbox("Enable Roughness Mollification", &renderOptions.enableRoughnessMollification);
Expand All @@ -356,7 +356,6 @@ void MainLoop(void* arg)
{
reloadShaders |= ImGui::Checkbox("Enable Uniform Light", &renderOptions.useUniformLight);

// Gamma correction for color picker. Internally, the renderer uses linear RGB values for colors
Vec3 uniformLightCol = Vec3::Pow(renderOptions.uniformLightCol, 1.0 / 2.2);
optionsChanged |= ImGui::ColorEdit3("Uniform Light Color (Gamma Corrected)", (float*)(&uniformLightCol), 0);
renderOptions.uniformLightCol = Vec3::Pow(uniformLightCol, 2.2);
Expand Down Expand Up @@ -428,7 +427,7 @@ void MainLoop(void* arg)

// Material Properties
Material* mat = &scene->materials[scene->meshInstances[selectedInstance].materialID];

// Gamma correction for color picker. Internally, the renderer uses linear RGB values for colors
Vec3 albedo = Vec3::Pow(mat->baseColor, 1.0 / 2.2);
objectPropChanged |= ImGui::ColorEdit3("Albedo (Gamma Corrected)", (float*)(&albedo), 0);
Expand All @@ -446,6 +445,34 @@ void MainLoop(void* arg)
objectPropChanged |= ImGui::SliderFloat("Transmission", &mat->specTrans, 0.0f, 1.0f);
objectPropChanged |= ImGui::SliderFloat("Ior", &mat->ior, 1.001f, 2.0f);

int mediumType = (int)mat->mediumType;
if (ImGui::Combo("Medium Type", &mediumType, "None\0Absorb\0Scatter\0Emissive\0"))
{
reloadShaders = true;
objectPropChanged = true;
mat->mediumType = mediumType;
}

if (mat->mediumType != MediumType::None)
{
Vec3 mediumColor = Vec3::Pow(mat->mediumColor, 1.0 / 2.2);
objectPropChanged |= ImGui::ColorEdit3("Medium Color (Gamma Corrected)", (float*)(&mediumColor), 0);
mat->mediumColor = Vec3::Pow(mediumColor, 2.2);

objectPropChanged |= ImGui::SliderFloat("Medium Density", &mat->mediumDensity, 0.0f, 5.0f);
}

int alphaMode = (int)mat->alphaMode;
if (ImGui::Combo("Alpha Mode", &alphaMode, "Opaque\0Blend"))
{
reloadShaders = true;
objectPropChanged = true;
mat->alphaMode = alphaMode;
}

if (mat->alphaMode != AlphaMode::Opaque)
objectPropChanged |= ImGui::SliderFloat("Opacity", &mat->opacity, 0.0f, 1.0f);

// Transforms
ImGui::Separator();
ImGui::Text("Transforms");
Expand Down Expand Up @@ -619,5 +646,5 @@ int main(int argc, char** argv)
SDL_DestroyWindow(loopdata.mWindow);
SDL_Quit();
return 0;
}
}

32 changes: 20 additions & 12 deletions src/core/Material.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ namespace GLSLPT
Mask
};

enum MediumType
{
None,
Absorb,
Scatter,
Emissive
};

class Material
{
public:
Expand All @@ -57,13 +65,13 @@ namespace GLSLPT
clearcoat = 0.0f;
clearcoatGloss = 0.0f;

specTrans = 0.0f;
ior = 1.5f;
atDistance = 1.0f;
// padding2
specTrans = 0.0f;
ior = 1.5f;
mediumType = 0.0f;
mediumDensity = 0.0f;

extinction = Vec3(1.0f, 1.0f, 1.0f);
// padding3
mediumColor = Vec3(1.0f, 1.0f, 1.0f);
mediumPhase = 0.0f;

baseColorTexId = -1.0f;
metallicRoughnessTexID = -1.0f;
Expand All @@ -73,7 +81,7 @@ namespace GLSLPT
opacity = 1.0f;
alphaMode = 0.0f;
alphaCutoff = 0.0f;
// padding4
// padding2
};

Vec3 baseColor;
Expand All @@ -94,11 +102,11 @@ namespace GLSLPT

float specTrans;
float ior;
float atDistance;
float padding2;
float mediumType;
float mediumDensity;

Vec3 extinction;
float padding3;
Vec3 mediumColor;
float mediumPhase;

float baseColorTexId;
float metallicRoughnessTexID;
Expand All @@ -108,6 +116,6 @@ namespace GLSLPT
float opacity;
float alphaMode;
float alphaCutoff;
float padding4;
float padding2;
};
}
9 changes: 9 additions & 0 deletions src/core/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,15 @@ namespace GLSLPT
if (scene->renderOptions.enableRoughnessMollification)
pathtraceDefines += "#define OPT_ROUGHNESS_MOLLIFICATION\n";

for (int i = 0; i < scene->materials.size(); i++)
{
if (scene->materials[i].mediumType != -1)
{
pathtraceDefines += "#define OPT_MEDIUM\n";
break;
}
}

if (pathtraceDefines.size() > 0)
{
size_t idx = pathTraceShaderSrcObj.src.find("#version");
Expand Down
21 changes: 16 additions & 5 deletions src/loaders/Loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ namespace GLSLPT
char normalTexName[100] = "None";
char emissionTexName[100] = "None";
char alphaMode[20] = "None";
char mediumType[20] = "None";

while (fgets(line, kMaxLineLength, file))
{
Expand All @@ -108,12 +109,14 @@ namespace GLSLPT
sscanf(line, " clearcoatGloss %f", &material.clearcoatGloss);
sscanf(line, " transmission %f", &material.specTrans);
sscanf(line, " ior %f", &material.ior);
sscanf(line, " extinction %f %f %f", &material.extinction.x, &material.extinction.y, &material.extinction.z);
sscanf(line, " atDistance %f", &material.atDistance);
sscanf(line, " albedoTexture %s", albedoTexName);
sscanf(line, " metallicRoughnessTexture %s", metallicRoughnessTexName);
sscanf(line, " normalTexture %s", normalTexName);
sscanf(line, " emissionTexture %s", emissionTexName);
sscanf(line, " mediumType %s", mediumType);
sscanf(line, " mediumDensity %f", &material.mediumDensity);
sscanf(line, " mediumColor %f %f %f", &material.mediumColor.x, &material.mediumColor.y, &material.mediumColor.z);
sscanf(line, " mediumPhase %f", &material.mediumPhase);
}

// Albedo Texture
Expand All @@ -133,13 +136,21 @@ namespace GLSLPT
material.emissionmapTexID = scene->AddTexture(path + emissionTexName);

// AlphaMode
if (strcmp(alphaMode, "Opaque") == 0)
if (strcmp(alphaMode, "opaque") == 0)
material.alphaMode = AlphaMode::Opaque;
else if (strcmp(alphaMode, "Blend") == 0)
else if (strcmp(alphaMode, "blend") == 0)
material.alphaMode = AlphaMode::Blend;
else if (strcmp(alphaMode, "Mask") == 0)
else if (strcmp(alphaMode, "mask") == 0)
material.alphaMode = AlphaMode::Mask;

// MediumType
if (strcmp(mediumType, "absorb") == 0)
material.mediumType = MediumType::Absorb;
else if (strcmp(mediumType, "scatter") == 0)
material.mediumType = MediumType::Scatter;
else if (strcmp(mediumType, "emissive") == 0)
material.mediumType = MediumType::Emissive;

// add material to map
if (materialMap.find(name) == materialMap.end()) // New material
{
Expand Down
11 changes: 3 additions & 8 deletions src/shaders/common/anyhit.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,9 @@ bool AnyHit(Ray r, float maxDist)
float alphaCutoff = alphaParams.z;
opacity *= alpha;

bool ignoreHit = false;

if (alphaMode == ALPHA_MODE_MASK && opacity < alphaCutoff)
ignoreHit = true;
else if (alphaMode == ALPHA_MODE_BLEND && rand() > opacity)
ignoreHit = true;

if (!ignoreHit)
// End traversal if alpha test fails
if (!((alphaMode == ALPHA_MODE_MASK && opacity < alphaCutoff) ||
(alphaMode == ALPHA_MODE_BLEND && rand() > opacity)))
return true;
#else
return true;
Expand Down
12 changes: 9 additions & 3 deletions src/shaders/common/envmap.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,22 @@ vec2 BinarySearch(float value)
return vec2(x, y) / envMapRes;
}

float EnvMapPdf(vec3 color)
vec4 EvalEnvMap(Ray r)
{
return ((Luminance(color) / envMapTotalSum) * envMapRes.x * envMapRes.y) / (TWO_PI * PI);
float theta = acos(clamp(r.direction.y, -1.0, 1.0));
vec2 uv = vec2((PI + atan(r.direction.z, r.direction.x)) * INV_TWO_PI, theta * INV_PI) + vec2(envMapRot, 0.0);

vec3 color = texture(envMapTex, uv).rgb;
float pdf = Luminance(color) / envMapTotalSum;

return vec4(color, (pdf * envMapRes.x * envMapRes.y) / (TWO_PI * PI * sin(theta)));
}

vec4 SampleEnvMap(inout vec3 color)
{
vec2 uv = BinarySearch(rand() * envMapTotalSum);

color = texture(envMapTex, uv).xyz;
color = texture(envMapTex, uv).rgb;
float pdf = Luminance(color) / envMapTotalSum;

uv.x -= envMapRot;
Expand Down
19 changes: 14 additions & 5 deletions src/shaders/common/globals.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@
#define ALPHA_MODE_BLEND 1
#define ALPHA_MODE_MASK 2

#define MEDIUM_NONE 0
#define MEDIUM_ABSORB 1
#define MEDIUM_SCATTER 2
#define MEDIUM_EMISSIVE 3

struct Ray
{
vec3 origin;
Expand All @@ -62,11 +67,14 @@ struct Material
float clearcoatRoughness;
float specTrans;
float ior;
float atDistance;
vec3 extinction;
// Roughness calculated from anisotropic param
float ax;
float ay;
};

struct Medium
{
int type;
float density;
vec3 color;
float phase;
};

struct Camera
Expand Down Expand Up @@ -108,6 +116,7 @@ struct State
vec2 texCoord;
int matID;
Material mat;
Medium medium;
};

struct ScatterSampleRec
Expand Down
Loading

0 comments on commit b6d7311

Please sign in to comment.