From aa098ef135131a0caa9e4d73c078faeb26bde2b0 Mon Sep 17 00:00:00 2001 From: skyjake Date: Tue, 12 Nov 2013 21:12:33 +0200 Subject: [PATCH] Renderer: Added console effect for lens flares fx::LensFlares instances use a shared data instance for common GL resources: texture atlas for flare images and a shader program. This commit sets up the basic mechanism using de::Counted. --- doomsday/client/client.pro | 2 + .../client/include/render/consoleeffect.h | 3 + .../client/include/render/fx/lensflares.h | 45 +++++++ doomsday/client/src/dd_main.cpp | 1 + doomsday/client/src/render/cameralensfx.cpp | 13 +- doomsday/client/src/render/consoleeffect.cpp | 6 + doomsday/client/src/render/fx/lensflares.cpp | 117 ++++++++++++++++++ doomsday/client/src/render/r_main.cpp | 1 - 8 files changed, 186 insertions(+), 2 deletions(-) create mode 100644 doomsday/client/include/render/fx/lensflares.h create mode 100644 doomsday/client/src/render/fx/lensflares.cpp diff --git a/doomsday/client/client.pro b/doomsday/client/client.pro index 6d162a40ba..a371119d5d 100644 --- a/doomsday/client/client.pro +++ b/doomsday/client/client.pro @@ -297,6 +297,7 @@ DENG_HEADERS += \ include/render/drawlist.h \ include/render/drawlists.h \ include/render/fx/colorfilter.h \ + include/render/fx/lensflares.h \ include/render/fx/vignette.h \ include/render/huecirclevisual.h \ include/render/lightdecoration.h \ @@ -660,6 +661,7 @@ SOURCES += \ src/render/drawlist.cpp \ src/render/drawlists.cpp \ src/render/fx/colorfilter.cpp \ + src/render/fx/lensflares.cpp \ src/render/fx/vignette.cpp \ src/render/huecirclevisual.cpp \ src/render/lightdecoration.cpp \ diff --git a/doomsday/client/include/render/consoleeffect.h b/doomsday/client/include/render/consoleeffect.h index e9454fbcf8..d9fb565d12 100644 --- a/doomsday/client/include/render/consoleeffect.h +++ b/doomsday/client/include/render/consoleeffect.h @@ -38,6 +38,9 @@ class ConsoleEffect */ int console() const; + virtual void glInit(); + virtual void glDeinit(); + virtual void draw(de::Rectanglei const &viewRect) = 0; private: diff --git a/doomsday/client/include/render/fx/lensflares.h b/doomsday/client/include/render/fx/lensflares.h new file mode 100644 index 0000000000..ac39b73ec9 --- /dev/null +++ b/doomsday/client/include/render/fx/lensflares.h @@ -0,0 +1,45 @@ +/** @file lensflares.h + * + * @authors Copyright (c) 2013 Jaakko Keränen + * + * @par License + * GPL: http://www.gnu.org/licenses/gpl.html + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. This program is distributed in the hope that it + * will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General + * Public License for more details. You should have received a copy of the GNU + * General Public License along with this program; if not, see: + * http://www.gnu.org/licenses + */ + +#ifndef DENG_CLIENT_FX_LENSFLARES_H +#define DENG_CLIENT_FX_LENSFLARES_H + +#include "render/consoleeffect.h" + +namespace fx { + +/** + * Draws lens flares for all visible light sources in the current frame. + */ +class LensFlares : public ConsoleEffect +{ +public: + LensFlares(int console); + + void glInit(); + void glDeinit(); + + void draw(de::Rectanglei const &viewRect); + +private: + DENG2_PRIVATE(d) +}; + +} // namespace fx + +#endif // DENG_CLIENT_FX_LENSFLARES_H diff --git a/doomsday/client/src/dd_main.cpp b/doomsday/client/src/dd_main.cpp index 28f333d6b7..5e4bd39bf1 100644 --- a/doomsday/client/src/dd_main.cpp +++ b/doomsday/client/src/dd_main.cpp @@ -1897,6 +1897,7 @@ boolean DD_Init(void) #ifdef __CLIENT__ GL_Init(); GL_InitRefresh(); + LensFx_Init(); #endif #ifdef __CLIENT__ diff --git a/doomsday/client/src/render/cameralensfx.cpp b/doomsday/client/src/render/cameralensfx.cpp index 6600c7e2a1..ce2dc90fed 100644 --- a/doomsday/client/src/render/cameralensfx.cpp +++ b/doomsday/client/src/render/cameralensfx.cpp @@ -20,6 +20,7 @@ #include "render/rend_main.h" #include "render/viewports.h" #include "render/fx/colorfilter.h" +#include "render/fx/lensflares.h" #include "render/fx/vignette.h" #include @@ -54,7 +55,12 @@ void LensFx_Init() ConsoleEffectStack &stack = fxConsole[i]; stack.effects.append(new fx::ColorFilter(i)); stack.effects.append(new fx::Vignette(i)); - //stack.effects.append(new LensFlareConsoleEffect(i)); + stack.effects.append(new fx::LensFlares(i)); + + foreach(ConsoleEffect *effect, stack.effects) + { + effect->glInit(); + } } } @@ -62,6 +68,11 @@ void LensFx_Shutdown() { for(int i = 0; i < DDMAXPLAYERS; ++i) { + foreach(ConsoleEffect *effect, fxConsole[i].effects) + { + effect->glDeinit(); + } + fxConsole[i].clear(); } } diff --git a/doomsday/client/src/render/consoleeffect.cpp b/doomsday/client/src/render/consoleeffect.cpp index c8816c9098..b173d9313f 100644 --- a/doomsday/client/src/render/consoleeffect.cpp +++ b/doomsday/client/src/render/consoleeffect.cpp @@ -35,3 +35,9 @@ int ConsoleEffect::console() const { return d->console; } + +void ConsoleEffect::glInit() +{} + +void ConsoleEffect::glDeinit() +{} diff --git a/doomsday/client/src/render/fx/lensflares.cpp b/doomsday/client/src/render/fx/lensflares.cpp new file mode 100644 index 0000000000..e7ba86d337 --- /dev/null +++ b/doomsday/client/src/render/fx/lensflares.cpp @@ -0,0 +1,117 @@ +/** @file lensflares.cpp + * + * @authors Copyright (c) 2013 Jaakko Keränen + * + * @par License + * GPL: http://www.gnu.org/licenses/gpl.html + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. This program is distributed in the hope that it + * will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General + * Public License for more details. You should have received a copy of the GNU + * General Public License along with this program; if not, see: + * http://www.gnu.org/licenses + */ + +#include "render/fx/lensflares.h" +#include "gl/gl_main.h" + +#include +#include +#include + +using namespace de; + +namespace fx { + +/** + * Shared GL resources for rendering lens flares. + */ +struct SharedFlareData : public Counted +{ + static SharedFlareData *instance; + + SharedFlareData() + { + DENG_ASSERT_IN_MAIN_THREAD(); + DENG_ASSERT_GL_CONTEXT_ACTIVE(); + } + + ~SharedFlareData() + { + DENG_ASSERT_IN_MAIN_THREAD(); + DENG_ASSERT_GL_CONTEXT_ACTIVE(); + + LOG_DEBUG("Releasing shared data"); + instance = 0; + } + + static SharedFlareData *hold() + { + if(!instance) + { + instance = new SharedFlareData; + LOG_DEBUG("Allocated shared data"); + } + else + { + // Hold a reference to the shared data. + instance->ref(); + } + return instance; + } +}; + +SharedFlareData *SharedFlareData::instance = 0; + +DENG2_PIMPL(LensFlares) +{ + SharedFlareData *res; + + Instance(Public *i) : Base(i), res(0) + { + } + + ~Instance() + { + releaseRef(res); + } + + void glInit() + { + // Acquire a reference to the shared flare data. + res = SharedFlareData::hold(); + } + + void glDeinit() + { + releaseRef(res); + } +}; + +LensFlares::LensFlares(int console) : ConsoleEffect(console), d(new Instance(this)) +{} + +void LensFlares::glInit() +{ + LOG_AS("fx::LensFlares"); + + d->glInit(); +} + +void LensFlares::glDeinit() +{ + LOG_AS("fx::LensFlares"); + + d->glDeinit(); +} + +void LensFlares::draw(Rectanglei const &viewRect) +{ + +} + +} // namespace fx diff --git a/doomsday/client/src/render/r_main.cpp b/doomsday/client/src/render/r_main.cpp index de58e9c2c2..a7cf898748 100644 --- a/doomsday/client/src/render/r_main.cpp +++ b/doomsday/client/src/render/r_main.cpp @@ -87,7 +87,6 @@ void R_Init() R_InitRawTexs(); R_InitSvgs(); #ifdef __CLIENT__ - LensFx_Init(); Viewports_Init(); #endif }