/
cameralensfx.cpp
183 lines (155 loc) · 4.35 KB
/
cameralensfx.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/** @file cameralensfx.cpp Camera lens effects.
*
* Renders camera lens effects, i.e., special effects applied to a "raw" world
* frame. ConsoleEffect-derived isntances are put onto a stack; each console
* has its own effect stack.
*
* Given the following stack of effects:
* - A
* - B
* - C
*
* The following sequence of methods is called during the rendering of a frame:
* 1. A.beginFrame
* 2. B.beginFrame
* 3. C.beginFrame
* 4. A.draw
* 5. B.draw
* 6. C.draw
* 7. C.endFrame <-- reverse order
* 8. B.endFrame
* 9. A.endFrame
*
* @authors Copyright (c) 2013 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>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</small>
*/
#include "render/cameralensfx.h"
#include "render/rend_main.h"
#include "render/viewports.h"
#include "render/fx/colorfilter.h"
#include "render/fx/lensflares.h"
#include "render/fx/postprocessing.h"
#include "render/fx/vignette.h"
#include "con_main.h"
#include "ui/clientwindow.h"
#include <de/libdeng2.h>
#include <de/Rectangle>
#include <de/Drawable>
#include <de/GLTarget>
#include <QList>
using namespace de;
static int fxFramePlayerNum; ///< Player view currently being drawn.
struct ConsoleEffectStack
{
/// Dynamic stack of effects. Used currently as a fixed array, though.
typedef QList<ConsoleEffect *> EffectList;
EffectList effects;
~ConsoleEffectStack() {
clear();
}
void clear() {
qDeleteAll(effects);
effects.clear();
}
};
static ConsoleEffectStack fxConsole[DDMAXPLAYERS];
#define IDX_POST_PROCESSING 3
D_CMD(PostFx)
{
DENG_UNUSED(argc);
int console = String(argv[1]).toInt();
String const shader = argv[2];
TimeDelta const span = String(argv[3]).toFloat();
if(console < 0 || console >= DDMAXPLAYERS)
{
LOG_WARNING("Invalid console %i") << console;
return false;
}
fx::PostProcessing *post =
static_cast<fx::PostProcessing *>(fxConsole[console].effects[IDX_POST_PROCESSING]);
// Special case to clear out the current shader.
if(shader == "none")
{
post->fadeOut(span);
return true;
}
post->fadeInShader(shader, span);
return true;
}
void LensFx_Register()
{
C_CMD("postfx", "isf", PostFx);
}
void LensFx_Init()
{
for(int i = 0; i < DDMAXPLAYERS; ++i)
{
ConsoleEffectStack &stack = fxConsole[i];
stack.effects.append(new fx::ColorFilter(i));
stack.effects.append(new fx::Vignette(i));
stack.effects.append(new fx::LensFlares(i));
stack.effects.append(new fx::PostProcessing(i)); // IDX_POST_PROCESSING
}
}
void LensFx_Shutdown()
{
LensFx_GLRelease();
for(int i = 0; i < DDMAXPLAYERS; ++i)
{
fxConsole[i].clear();
}
}
void LensFx_GLRelease()
{
for(int i = 0; i < DDMAXPLAYERS; ++i)
{
foreach(ConsoleEffect *effect, fxConsole[i].effects)
{
if(effect->isInited())
{
effect->glDeinit();
}
}
}
}
void LensFx_BeginFrame(int playerNum)
{
fxFramePlayerNum = playerNum;
ConsoleEffectStack::EffectList const &effects = fxConsole[fxFramePlayerNum].effects;
// Initialize these effects if they currently are not.
foreach(ConsoleEffect *effect, effects)
{
if(!effect->isInited())
{
effect->glInit();
}
}
foreach(ConsoleEffect *effect, effects)
{
effect->beginFrame();
}
}
void LensFx_EndFrame()
{
ConsoleEffectStack::EffectList const &effects = fxConsole[fxFramePlayerNum].effects;
foreach(ConsoleEffect *effect, effects)
{
effect->draw();
}
for(int i = effects.size() - 1; i >= 0; --i)
{
effects.at(i)->endFrame();
}
}