From 783c15b0724ad775d75a37f5b2ebea5eb01e462d Mon Sep 17 00:00:00 2001 From: kaetemi Date: Wed, 3 Jul 2013 03:53:32 +0200 Subject: [PATCH] Make bloom work together with render target used for the rift shader, ref #43 --- code/nel/include/nel/3d/bloom_effect.h | 2 + code/nel/include/nel/3d/stereo_display.h | 6 +-- code/nel/include/nel/3d/stereo_ovr.h | 4 +- code/nel/src/3d/bloom_effect.cpp | 38 +++++++++++++++---- code/nel/src/3d/stereo_ovr.cpp | 11 ++---- .../client/src/snowballs_client.cpp | 8 +++- 6 files changed, 47 insertions(+), 22 deletions(-) diff --git a/code/nel/include/nel/3d/bloom_effect.h b/code/nel/include/nel/3d/bloom_effect.h index 77673741c..c10967254 100644 --- a/code/nel/include/nel/3d/bloom_effect.h +++ b/code/nel/include/nel/3d/bloom_effect.h @@ -138,6 +138,8 @@ private: NLMISC::CSmartPtr _BlurFinalTex; // used as render target in first blur pass, and as displayed texture on second blur pass. NLMISC::CSmartPtr _BlurHorizontalTex; + // original render target + NLMISC::CSmartPtr _OriginalRenderTarget; // materials diff --git a/code/nel/include/nel/3d/stereo_display.h b/code/nel/include/nel/3d/stereo_display.h index aa98dae50..5819f21eb 100644 --- a/code/nel/include/nel/3d/stereo_display.h +++ b/code/nel/include/nel/3d/stereo_display.h @@ -121,9 +121,9 @@ public: /// 2D Interface virtual bool wantInterface2D() = 0; - /// Returns non-NULL if a new render target was set - virtual UTexture *beginRenderTarget(bool set) = 0; - /// Returns true if a render target was fully drawn + /// Returns true if a new render target was set, always fase if not using render targets + virtual bool beginRenderTarget() = 0; + /// Returns true if a render target was fully drawn, always false if not using render targets virtual bool endRenderTarget() = 0; static const char *getLibraryName(CStereoDeviceInfo::TStereoDeviceLibrary library); diff --git a/code/nel/include/nel/3d/stereo_ovr.h b/code/nel/include/nel/3d/stereo_ovr.h index 857c5368f..be3880bef 100644 --- a/code/nel/include/nel/3d/stereo_ovr.h +++ b/code/nel/include/nel/3d/stereo_ovr.h @@ -109,8 +109,8 @@ public: /// 2D Interface virtual bool wantInterface2D(); - /// Returns non-NULL if a new render target was set, always NULL if not using render targets - virtual UTexture *beginRenderTarget(bool set); + /// Returns true if a new render target was set, always fase if not using render targets + virtual bool beginRenderTarget(); /// Returns true if a render target was fully drawn, always false if not using render targets virtual bool endRenderTarget(); diff --git a/code/nel/src/3d/bloom_effect.cpp b/code/nel/src/3d/bloom_effect.cpp index 927f8688c..3eb24518e 100644 --- a/code/nel/src/3d/bloom_effect.cpp +++ b/code/nel/src/3d/bloom_effect.cpp @@ -285,6 +285,8 @@ void CBloomEffect::initBloom() // clientcfg if(!_Init) init(); + _OriginalRenderTarget = static_cast(_Driver)->getDriver()->getRenderTarget(); + // if window resize, reinitialize textures if(_WndWidth!=_Driver->getWindowWidth() || _WndHeight!=_Driver->getWindowHeight()) { @@ -349,11 +351,14 @@ void CBloomEffect::initBloom() // clientcfg } } - NL3D::CTextureUser txt = (_InitBloomEffect) ? (CTextureUser(_InitText)) : (CTextureUser()); - if(!((CDriverUser *) _Driver)->setRenderTarget(txt, 0, 0, _WndWidth, _WndHeight)) + if (!_OriginalRenderTarget) { - nlwarning("setRenderTarget return false with initial texture for bloom effect\n"); - return; + NL3D::CTextureUser txt = (_InitBloomEffect) ? (CTextureUser(_InitText)) : (CTextureUser()); + if(!(static_cast(_Driver)->setRenderTarget(txt, 0, 0, _WndWidth, _WndHeight))) + { + nlwarning("setRenderTarget return false with initial texture for bloom effect\n"); + return; + } } } @@ -370,7 +375,7 @@ void CBloomEffect::endBloom() // clientcfg if(_Driver->getWindowWidth()==0 || _Driver->getWindowHeight()==0) return; - CTextureUser txt1 = (_InitBloomEffect) ? (CTextureUser(_InitText)) : (CTextureUser()); + CTextureUser txt1 = _OriginalRenderTarget ? CTextureUser(_OriginalRenderTarget) : ((_InitBloomEffect) ? (CTextureUser(_InitText)) : (CTextureUser())); CTextureUser txt2(_BlurFinalTex); CRect rect1(0, 0, _WndWidth, _WndHeight); CRect rect2(0, 0, _BlurWidth, _BlurHeight); @@ -394,15 +399,30 @@ void CBloomEffect::applyBlur() { NL3D::IDriver *drvInternal = ((CDriverUser *) _Driver)->getDriver(); + /*if (_OriginalRenderTarget) + { + CTextureUser txt(_OriginalRenderTarget); + if(!(static_cast(_Driver)->setRenderTarget(txt, 0, 0, _WndWidth, _WndHeight))) + { + nlwarning("setRenderTarget return false with original render target for bloom effect\n"); + return; + } + } // in opengl, display in init texture - if(_InitBloomEffect) + else if(_InitBloomEffect) { CTextureUser txt(_InitText); - if(!((CDriverUser *) _Driver)->setRenderTarget(txt, 0, 0, _WndWidth, _WndHeight)) + if(!(static_cast(_Driver)->setRenderTarget(txt, 0, 0, _WndWidth, _WndHeight))) { nlwarning("setRenderTarget return false with initial texture for bloom effect\n"); return; } + }*/ + CTextureUser txtApply = _OriginalRenderTarget ? CTextureUser(_OriginalRenderTarget) : ((_InitBloomEffect) ? (CTextureUser(_InitText)) : (CTextureUser())); + if(!(static_cast(_Driver)->setRenderTarget(txtApply, 0, 0, _WndWidth, _WndHeight))) + { + nlwarning("setRenderTarget return false with initial texture for bloom effect\n"); + return; } // display blur texture @@ -459,7 +479,7 @@ void CBloomEffect::endInterfacesDisplayBloom() // clientcfg { // Render from render target to screen if necessary. // Don't do this when the blend was done to the screen or when rendering to a user provided rendertarget. - if (_InitBloomEffect) + if ((_OriginalRenderTarget.getPtr() == NULL) && _InitBloomEffect) { if(!_Driver->supportBloomEffect() || !_Init) return; @@ -492,6 +512,8 @@ void CBloomEffect::endInterfacesDisplayBloom() // clientcfg _Driver->drawQuad(_DisplayQuad, _DisplayInitMat); _Driver->setMatrixMode3D(pCam); } + + _OriginalRenderTarget = NULL; } diff --git a/code/nel/src/3d/stereo_ovr.cpp b/code/nel/src/3d/stereo_ovr.cpp index d6fc56cb9..a5397e77f 100644 --- a/code/nel/src/3d/stereo_ovr.cpp +++ b/code/nel/src/3d/stereo_ovr.cpp @@ -488,19 +488,16 @@ bool CStereoOVR::wantInterface2D() /// Returns non-NULL if a new render target was set -UTexture *CStereoOVR::beginRenderTarget(bool set) +bool CStereoOVR::beginRenderTarget() { // render target always set before driver clear // nlassert(m_SubStage <= 1); if (m_Driver && m_Stage == 1) { - if (set) - { - (static_cast(m_Driver))->setRenderTarget(*m_BarrelTexU, 0, 0, 0, 0); - } - return m_BarrelTexU; + static_cast(m_Driver)->setRenderTarget(*m_BarrelTexU, 0, 0, 0, 0); + return true; } - return NULL; + return false; } /// Returns true if a render target was fully drawn diff --git a/code/snowballs2/client/src/snowballs_client.cpp b/code/snowballs2/client/src/snowballs_client.cpp index 2522f48b6..c510af7ec 100644 --- a/code/snowballs2/client/src/snowballs_client.cpp +++ b/code/snowballs2/client/src/snowballs_client.cpp @@ -754,15 +754,19 @@ void loopIngame() StereoDisplay->getCurrentFrustum(0, &SkyCamera); StereoDisplay->getCurrentMatrix(0, &Camera); } + + if (StereoDisplay) + { + StereoDisplay->beginRenderTarget(); + } if (!StereoDisplay || StereoDisplay->wantClear()) { - NL3D::UTexture *rt = StereoDisplay ? StereoDisplay->beginRenderTarget(!s_EnableBloom) : NULL; if (s_EnableBloom) { nlassert(bloomStage == 0); - CBloomEffect::instance().initBloom(/*rt*/); // start bloom effect (just before the first scene element render) + CBloomEffect::instance().initBloom(); // start bloom effect (just before the first scene element render) bloomStage = 1; }