Skip to content
Snippets Groups Projects
Commit 7b1255a2 authored by Adam Nyberg's avatar Adam Nyberg
Browse files

WIP: issue with innverted normals fixed, we treated normals as nonlinear instead of linear

parent 82186544
No related branches found
No related tags found
No related merge requests found
...@@ -3,104 +3,97 @@ ...@@ -3,104 +3,97 @@
in vec2 TexCoords; in vec2 TexCoords;
out vec4 FragColor; out vec4 FragColor;
// The linear depth texture (0..1 -> zNear..zFar)
uniform sampler2D uDepthTex; uniform sampler2D uDepthTex;
// The inverse of the projection matrix (pass from CPU) // We need these to convert linear [0..1] to actual distance
uniform float zNear;
uniform float zFar;
// The inverse of the same projection matrix used when rendering depth
uniform mat4 invProjectionMatrix; uniform mat4 invProjectionMatrix;
// Optional max depth clamp // We can discard if depth is bigger than some clamp
uniform float maxDepth; // e.g., 1.0 if 1 means zFar uniform float maxDepth;
// Offset in texture coordinates for neighbor sampling // Pixel-size step for neighbor sampling
// Usually (1.0 / screenWidth, 1.0 / screenHeight)
uniform vec2 uTexelSize; uniform vec2 uTexelSize;
//////////////////////////////////////////////////////////
// Helper: Reconstruct eye-space position from linear depth
//////////////////////////////////////////////////////////
vec3 reconstructEyePos(vec2 uv, float depthLin)
{
// 1) Convert [0..1] -> actual eye-space distance
float dEye = depthLin * (zFar - zNear) + zNear;
// 2) Compute direction from camera through this UV.
// We'll treat the clip-space z = -1 (the near plane),
// so that we get a direction vector after inverse-projection.
float xNDC = uv.x * 2.0 - 1.0;
float yNDC = uv.y * 2.0 - 1.0;
vec4 clipPos = vec4(xNDC, yNDC, -1.0, 1.0);
vec4 eyePos4 = invProjectionMatrix * clipPos;
// perspective divide
vec3 dir = eyePos4.xyz / eyePos4.w;
dir = normalize(dir);
// 3) Actual position = direction * distance
return dir * dEye;
}
void main() void main()
{ {
//////////////////////////////////
// 1) Sample center depth // 1) Sample center depth
//////////////////////////////////
float depthLin = texture(uDepthTex, TexCoords).r; float depthLin = texture(uDepthTex, TexCoords).r;
// Validate if (depthLin < 0.0 || depthLin > 1.0 || depthLin > maxDepth) {
if (depthLin < 0.0 || depthLin > 1.0 || depthLin > maxDepth)
{
discard; discard;
} }
// 2) Convert to clip space // Reconstruct center pixel in eye space
// OpenGL clip space: X, Y in [-1,1], Z in [-1,1]. vec3 centerEye = reconstructEyePos(TexCoords, depthLin);
// Our linear depth is [0..1], we map it to clip Z by (depthLin * 2 - 1).
vec4 clipPos = vec4(
TexCoords.x * 2.0 - 1.0,
TexCoords.y * 2.0 - 1.0,
depthLin * 2.0 - 1.0,
1.0
);
// 3) Inverse-project to eye space
vec4 eyePos4 = invProjectionMatrix * clipPos;
// perspective divide
eyePos4.xyz /= eyePos4.w;
vec3 centerEye = eyePos4.xyz; // The actual eye-space position
// 4) Sample neighbors and reconstruct their eye-space positions //////////////////////////////////
// We'll do left-right in x direction // 2) Sample neighbors (x direction)
//////////////////////////////////
vec2 uvLeft = TexCoords + vec2(-uTexelSize.x, 0.0); vec2 uvLeft = TexCoords + vec2(-uTexelSize.x, 0.0);
vec2 uvRight = TexCoords + vec2( uTexelSize.x, 0.0); vec2 uvRight = TexCoords + vec2( uTexelSize.x, 0.0);
float depthL = texture(uDepthTex, uvLeft).r; float depthL = texture(uDepthTex, uvLeft).r;
float depthR = texture(uDepthTex, uvRight).r; float depthR = texture(uDepthTex, uvRight).r;
vec3 leftEye = vec3(0.0); // If invalid neighbors, optionally discard
vec3 rightEye = vec3(0.0); if (depthL < 0.0 || depthL > 1.0 || depthL > maxDepth) discard;
if (depthR < 0.0 || depthR > 1.0 || depthR > maxDepth) discard;
if (depthL >= 0.0 && depthL <= maxDepth) {
vec4 clipL = vec4(uvLeft.x * 2.0 - 1.0, uvLeft.y * 2.0 - 1.0, depthL * 2.0 - 1.0, 1.0);
vec4 epL = invProjectionMatrix * clipL;
leftEye = epL.xyz / epL.w;
} else {
discard; // or skip partial if invalid
}
if (depthR >= 0.0 && depthR <= maxDepth) { vec3 leftEye = reconstructEyePos(uvLeft, depthL);
vec4 clipR = vec4(uvRight.x * 2.0 - 1.0, uvRight.y * 2.0 - 1.0, depthR * 2.0 - 1.0, 1.0); vec3 rightEye = reconstructEyePos(uvRight, depthR);
vec4 epR = invProjectionMatrix * clipR;
rightEye = epR.xyz / epR.w;
} else {
discard;
}
// partial derivative in x // partial derivative in x
vec3 ddx = rightEye - centerEye; vec3 ddx = rightEye - centerEye;
vec3 ddx2 = centerEye - leftEye; vec3 ddx2 = centerEye - leftEye;
// Optionally pick whichever has smaller Z difference
if (abs(ddx2.z) < abs(ddx.z)) { if (abs(ddx2.z) < abs(ddx.z)) {
ddx = ddx2; ddx = ddx2;
} }
// 5) same approach for up-down //////////////////////////////////
// 3) Sample neighbors (y direction)
//////////////////////////////////
vec2 uvUp = TexCoords + vec2(0.0, uTexelSize.y); vec2 uvUp = TexCoords + vec2(0.0, uTexelSize.y);
vec2 uvDn = TexCoords + vec2(0.0, -uTexelSize.y); vec2 uvDn = TexCoords + vec2(0.0, -uTexelSize.y);
float depthU = texture(uDepthTex, uvUp).r; float depthU = texture(uDepthTex, uvUp).r;
float depthD = texture(uDepthTex, uvDn).r; float depthD = texture(uDepthTex, uvDn).r;
vec3 upEye = vec3(0.0); if (depthU < 0.0 || depthU > 1.0 || depthU > maxDepth) discard;
vec3 dnEye = vec3(0.0); if (depthD < 0.0 || depthD > 1.0 || depthD > maxDepth) discard;
if (depthU >= 0.0 && depthU <= maxDepth) {
vec4 clipU = vec4(uvUp.x * 2.0 - 1.0, uvUp.y * 2.0 - 1.0, depthU * 2.0 - 1.0, 1.0);
vec4 epU = invProjectionMatrix * clipU;
upEye = epU.xyz / epU.w;
} else {
discard;
}
if (depthD >= 0.0 && depthD <= maxDepth) { vec3 upEye = reconstructEyePos(uvUp, depthU);
vec4 clipD = vec4(uvDn.x * 2.0 - 1.0, uvDn.y * 2.0 - 1.0, depthD * 2.0 - 1.0, 1.0); vec3 dnEye = reconstructEyePos(uvDn, depthD);
vec4 epD = invProjectionMatrix * clipD;
dnEye = epD.xyz / epD.w;
} else {
discard;
}
vec3 ddy = upEye - centerEye; vec3 ddy = upEye - centerEye;
vec3 ddy2 = centerEye - dnEye; vec3 ddy2 = centerEye - dnEye;
...@@ -108,15 +101,17 @@ void main() ...@@ -108,15 +101,17 @@ void main()
ddy = ddy2; ddy = ddy2;
} }
// 6) Cross partial derivatives to get normal //////////////////////////////////
// 4) Cross partials => normal
//////////////////////////////////
vec3 N = normalize(cross(ddx, ddy)); vec3 N = normalize(cross(ddx, ddy));
// Optionally flip if needed // Some folks invert if N.z > 0
if (N.z > 0.0) { //if (N.z > 0.0) {
N = -N; // N = -N;
} //}
// Shift from [-1..1] to [0..1] // Map [-1..1] -> [0..1] for display
vec3 normalColor = 0.5 * (N + vec3(1.0)); vec3 normalColor = 0.5 * (N + vec3(1.0));
FragColor = vec4(normalColor, 1.0); FragColor = vec4(normalColor, 1.0);
} }
...@@ -51,7 +51,7 @@ void main() ...@@ -51,7 +51,7 @@ void main()
} }
// Expand [0..1] -> [-1..1], then normalize // Expand [0..1] -> [-1..1], then normalize
normal = normalize(normal * 2.0 - 1.0); //normal = normalize(normal * 2.0 - 1.0);
//--------------------------------------------------------- //---------------------------------------------------------
// 3) Reconstruct eye-space position from depth // 3) Reconstruct eye-space position from depth
...@@ -94,8 +94,8 @@ void main() ...@@ -94,8 +94,8 @@ void main()
//--------------------------------------------------------- //---------------------------------------------------------
// 6) Multiply by attenuation // 6) Multiply by attenuation
//--------------------------------------------------------- //---------------------------------------------------------
lighting *= attenuation; //lighting *= attenuation;
float alpha = 1 - attenuation;
//--------------------------------------------------------- //---------------------------------------------------------
// 7) Fresnel effect // 7) Fresnel effect
//--------------------------------------------------------- //---------------------------------------------------------
...@@ -112,5 +112,6 @@ void main() ...@@ -112,5 +112,6 @@ void main()
finalColor += fluidColor * fresnel; // tinted Fresnel finalColor += fluidColor * fresnel; // tinted Fresnel
// Done // Done
FragColor = vec4(finalColor, 1.0); //FragColor = vec4(finalColor, 0.5);
FragColor = vec4(normal, 1.0); //vec4(finalColor, 0.5);
} }
...@@ -19,7 +19,7 @@ void main() ...@@ -19,7 +19,7 @@ void main()
discard; discard;
// For thickness: each particle in this pixel adds some amount. // For thickness: each particle in this pixel adds some amount.
float thicknessIncrement = 0.01; float thicknessIncrement = 0.04;
// Output that in the red channel. Using GL32F only reed chanele sotred // Output that in the red channel. Using GL32F only reed chanele sotred
FragColor = vec4(thicknessIncrement, 0.0, 0.0, 1.0); FragColor = vec4(thicknessIncrement, 0.0, 0.0, 1.0);
......
...@@ -816,7 +816,7 @@ void FluidRenderer::renderDepthNormalsFrame() ...@@ -816,7 +816,7 @@ void FluidRenderer::renderDepthNormalsFrame()
// 3) Bind the *filtered depth* texture as input // 3) Bind the *filtered depth* texture as input
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, m_depthFilterProgram.fboTexture); glBindTexture(GL_TEXTURE_2D, m_depthRenderProgram.fboTexture); // temporary to se normals for spheres
glUniform1i(glGetUniformLocation(m_depthNormalsProgram.programID, "uDepthTex"), 0); glUniform1i(glGetUniformLocation(m_depthNormalsProgram.programID, "uDepthTex"), 0);
// 4) Set uniforms: zNear, zFar, uTexelSize, maxDepth, etc. // 4) Set uniforms: zNear, zFar, uTexelSize, maxDepth, etc.
...@@ -898,7 +898,7 @@ void FluidRenderer::renderThicknessFrame(size_t particleCount, size_t boundaryCo ...@@ -898,7 +898,7 @@ void FluidRenderer::renderThicknessFrame(size_t particleCount, size_t boundaryCo
void FluidRenderer::renderThicknessFilterFrame() void FluidRenderer::renderThicknessFilterFrame()
{ {
int radius = 5; // or something int radius = 24; // or something
std::vector<float> weights; std::vector<float> weights;
computeGaussianWeights(radius, weights); computeGaussianWeights(radius, weights);
...@@ -989,7 +989,7 @@ void FluidRenderer::renderThicknessAttenFrame() ...@@ -989,7 +989,7 @@ void FluidRenderer::renderThicknessAttenFrame()
glUniform1i(glGetUniformLocation(m_thicknessAttenProgram.programID, "uThicknessTex"), 0); glUniform1i(glGetUniformLocation(m_thicknessAttenProgram.programID, "uThicknessTex"), 0);
// 3) Set uniform: absorption, fluidColor, etc. // 3) Set uniform: absorption, fluidColor, etc.
float absorption = 0.2f; // tweak to taste float absorption = 0.8f; // tweak to taste
GLuint absorpLoc = glGetUniformLocation(m_thicknessAttenProgram.programID, "absorption"); GLuint absorpLoc = glGetUniformLocation(m_thicknessAttenProgram.programID, "absorption");
glUniform1f(absorpLoc, absorption); glUniform1f(absorpLoc, absorption);
...@@ -1016,11 +1016,14 @@ void FluidRenderer::renderFinalCombineFrame() ...@@ -1016,11 +1016,14 @@ void FluidRenderer::renderFinalCombineFrame()
glBindFramebuffer(GL_FRAMEBUFFER, m_finalCombineProgram.fbo); // Or 0 for screen glBindFramebuffer(GL_FRAMEBUFFER, m_finalCombineProgram.fbo); // Or 0 for screen
glViewport(0, 0, m_fbWidth, m_fbHeight); glViewport(0, 0, m_fbWidth, m_fbHeight);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Clear to black glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // Clear to black
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDisable(GL_DEPTH_TEST); // Disable depth testing for fullscreen quad glDisable(GL_DEPTH_TEST); // Disable depth testing for fullscreen quad
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glUseProgram(m_finalCombineProgram.programID); glUseProgram(m_finalCombineProgram.programID);
// Bind the required textures // Bind the required textures
......
...@@ -23,7 +23,7 @@ namespace FluidSimulation ...@@ -23,7 +23,7 @@ namespace FluidSimulation
FluidSimulation* m_fluidSimulation; FluidSimulation* m_fluidSimulation;
bool m_updatePhysics; bool m_updatePhysics;
RenderStage selectedStage = RenderStage::ParticleView; RenderStage selectedStage = RenderStage::ScreenSpacedRender;
void RenderControlPannel(); void RenderControlPannel();
void RenderFluidView(); void RenderFluidView();
......
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