diff --git a/src/gl/renderer/gl_postprocess.cpp b/src/gl/renderer/gl_postprocess.cpp index 56032704e..512209454 100644 --- a/src/gl/renderer/gl_postprocess.cpp +++ b/src/gl/renderer/gl_postprocess.cpp @@ -179,8 +179,13 @@ void FGLRenderer::AmbientOccludeScene() mBuffers->BindSceneDepthTexture(0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + mBuffers->BindSceneColorTexture(1); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glActiveTexture(GL_TEXTURE0); mLinearDepthShader->Bind(multisample); mLinearDepthShader->DepthTexture[multisample].Set(0); + mLinearDepthShader->ColorTexture[multisample].Set(1); if (multisample) mLinearDepthShader->SampleCount[multisample].Set(gl_multisample); mLinearDepthShader->LinearizeDepthA[multisample].Set(1.0f / GetZFar() - 1.0f / GetZNear()); mLinearDepthShader->LinearizeDepthB[multisample].Set(MAX(1.0f / GetZNear(), 1.e-8f)); diff --git a/src/gl/renderer/gl_renderbuffers.cpp b/src/gl/renderer/gl_renderbuffers.cpp index b2cee5704..b0ea123a0 100644 --- a/src/gl/renderer/gl_renderbuffers.cpp +++ b/src/gl/renderer/gl_renderbuffers.cpp @@ -555,6 +555,21 @@ void FGLRenderBuffers::BindSceneFB() glBindFramebuffer(GL_FRAMEBUFFER, mSceneFB); } +//========================================================================== +// +// Binds the scene color texture to the specified texture unit +// +//========================================================================== + +void FGLRenderBuffers::BindSceneColorTexture(int index) +{ + glActiveTexture(GL_TEXTURE0 + index); + if (mSamples > 1) + glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, mSceneMultisample); + else + glBindTexture(GL_TEXTURE_2D, mPipelineTexture[0]); +} + //========================================================================== // // Binds the depth texture to the specified texture unit diff --git a/src/gl/renderer/gl_renderbuffers.h b/src/gl/renderer/gl_renderbuffers.h index 32430f2ed..2661908e3 100644 --- a/src/gl/renderer/gl_renderbuffers.h +++ b/src/gl/renderer/gl_renderbuffers.h @@ -23,6 +23,7 @@ public: bool Setup(int width, int height, int sceneWidth, int sceneHeight); void BindSceneFB(); + void BindSceneColorTexture(int index); void BindSceneDepthTexture(int index); void BlitSceneToTexture(); diff --git a/src/gl/scene/gl_portal.cpp b/src/gl/scene/gl_portal.cpp index cd3efb9e3..20ded84aa 100644 --- a/src/gl/scene/gl_portal.cpp +++ b/src/gl/scene/gl_portal.cpp @@ -429,14 +429,16 @@ void GLPortal::End(bool usestencil) glDepthFunc(GL_LEQUAL); glDepthRange(0, 1); { - ScopedColorMask colorMask(0, 0, 0, 0); - // glColorMask(0,0,0,0); // no graphics + ScopedColorMask colorMask(0, 0, 0, 1); // mark portal in alpha channel but don't touch color gl_RenderState.SetEffect(EFF_STENCIL); gl_RenderState.EnableTexture(false); + gl_RenderState.BlendFunc(GL_ONE, GL_ZERO); + gl_RenderState.BlendEquation(GL_ADD); + gl_RenderState.Apply(); DrawPortalStencil(); gl_RenderState.SetEffect(EFF_NONE); gl_RenderState.EnableTexture(true); - } // glColorMask(1, 1, 1, 1); + } glDepthFunc(GL_LESS); } PortalAll.Unclock(); diff --git a/src/gl/shaders/gl_ambientshader.cpp b/src/gl/shaders/gl_ambientshader.cpp index 54a651281..3fbd03434 100644 --- a/src/gl/shaders/gl_ambientshader.cpp +++ b/src/gl/shaders/gl_ambientshader.cpp @@ -60,6 +60,7 @@ void FLinearDepthShader::Bind(bool multisample) shader.Link("shaders/glsl/lineardepth"); shader.SetAttribLocation(0, "PositionInProjection"); DepthTexture[multisample].Init(shader, "DepthTexture"); + ColorTexture[multisample].Init(shader, "ColorTexture"); SampleCount[multisample].Init(shader, "SampleCount"); LinearizeDepthA[multisample].Init(shader, "LinearizeDepthA"); LinearizeDepthB[multisample].Init(shader, "LinearizeDepthB"); diff --git a/src/gl/shaders/gl_ambientshader.h b/src/gl/shaders/gl_ambientshader.h index 5b18ea85f..9c97791ba 100644 --- a/src/gl/shaders/gl_ambientshader.h +++ b/src/gl/shaders/gl_ambientshader.h @@ -9,6 +9,7 @@ public: void Bind(bool multisample); FBufferedUniformSampler DepthTexture[2]; + FBufferedUniformSampler ColorTexture[2]; FBufferedUniform1i SampleCount[2]; FBufferedUniform1f LinearizeDepthA[2]; FBufferedUniform1f LinearizeDepthB[2]; diff --git a/wadsrc/static/shaders/glsl/lineardepth.fp b/wadsrc/static/shaders/glsl/lineardepth.fp index 7e4eee745..61e5e8126 100644 --- a/wadsrc/static/shaders/glsl/lineardepth.fp +++ b/wadsrc/static/shaders/glsl/lineardepth.fp @@ -4,9 +4,11 @@ out vec4 FragColor; #if defined(MULTISAMPLE) uniform sampler2DMS DepthTexture; +uniform sampler2DMS ColorTexture; uniform int SampleCount; #else uniform sampler2D DepthTexture; +uniform sampler2D ColorTexture; #endif uniform float LinearizeDepthA; @@ -25,15 +27,15 @@ void main() ivec2 ipos = ivec2(uv * vec2(texSize)); float depth = 0.0; for (int i = 0; i < SampleCount; i++) - depth += texelFetch(DepthTexture, ipos, i).x; + depth += texelFetch(ColorTexture, ipos, i).a != 0.0 ? texelFetch(DepthTexture, ipos, i).x : 1.0; depth /= float(SampleCount); #else /*ivec2 texSize = textureSize(DepthTexture, 0); ivec2 ipos = ivec2(uv * vec2(texSize)); if (ipos.x < 0) ipos.x += texSize.x; if (ipos.y < 0) ipos.y += texSize.y; - float depth = texelFetch(DepthTexture, ipos, 0).x;*/ - float depth = texture(DepthTexture, uv).x; + float depth = texelFetch(ColorTexture, ipos, 0).a != 0.0 ? texelFetch(DepthTexture, ipos, 0).x : 1.0;*/ + float depth = texture(ColorTexture, uv).a != 0.0 ? texture(DepthTexture, uv).x : 1.0; #endif float normalizedDepth = clamp(InverseDepthRangeA * depth + InverseDepthRangeB, 0.0, 1.0); diff --git a/wadsrc/static/shaders/glsl/stencil.fp b/wadsrc/static/shaders/glsl/stencil.fp index d1b8745f6..65f12b405 100644 --- a/wadsrc/static/shaders/glsl/stencil.fp +++ b/wadsrc/static/shaders/glsl/stencil.fp @@ -3,6 +3,6 @@ out vec4 FragColor; void main() { - FragColor = vec4(1.0); + FragColor = vec4(1.0, 1.0, 1.0, 0.0); }