-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHelperLogic.cpp
More file actions
385 lines (358 loc) · 13.2 KB
/
HelperLogic.cpp
File metadata and controls
385 lines (358 loc) · 13.2 KB
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/*
-----------------------------------------------------------------------------
This source file is part of OGRE
(Object-oriented Graphics Rendering Engine)
For the latest info, see http://www.ogre3d.org/
Copyright (c) 2000-2012 Torus Knot Software Ltd
Also see acknowledgements in Readme.html
You may use this sample code for anything you like, it is not covered by the
same license as the rest of the engine.
-----------------------------------------------------------------------------
*/
#include "HelperLogic.hpp"
#include "stdafx.h"
//---------------------------------------------------------------------------
class HeatVisionListener: public Ogre::CompositorInstance::Listener
{
public:
HeatVisionListener();
virtual ~HeatVisionListener();
virtual void notifyMaterialSetup(Ogre::uint32 pass_id, Ogre::MaterialPtr &mat);
virtual void notifyMaterialRender(Ogre::uint32 pass_id, Ogre::MaterialPtr &mat);
protected:
Ogre::GpuProgramParametersSharedPtr fpParams;
float start, end, curr;
Ogre::Timer *timer;
};
//---------------------------------------------------------------------------
class HDRListener: public Ogre::CompositorInstance::Listener
{
protected:
int mVpWidth, mVpHeight;
int mBloomSize;
// Array params - have to pack in groups of 4 since this is how Cg generates them
// also prevents dependent texture read problems if ops don't require swizzle
float mBloomTexWeights[15][4];
float mBloomTexOffsetsHorz[15][4];
float mBloomTexOffsetsVert[15][4];
public:
HDRListener();
virtual ~HDRListener();
void notifyViewportSize(int width, int height);
void notifyCompositor(Ogre::CompositorInstance* instance);
virtual void notifyMaterialSetup(Ogre::uint32 pass_id, Ogre::MaterialPtr &mat);
virtual void notifyMaterialRender(Ogre::uint32 pass_id, Ogre::MaterialPtr &mat);
};
//---------------------------------------------------------------------------
class GaussianListener : public Ogre::CompositorInstance::Listener
{
protected:
int mVpWidth, mVpHeight;
// Array params - have to pack in groups of 4 since this is how Cg generates them
// also prevents dependent texture read problems if ops don't require swizzle
float mBloomTexWeights[15][4];
float mBloomTexOffsetsHorz[15][4];
float mBloomTexOffsetsVert[15][4];
public:
GaussianListener();
virtual ~GaussianListener();
void notifyViewportSize(int width, int height);
virtual void notifyMaterialSetup(Ogre::uint32 pass_id, Ogre::MaterialPtr &mat);
virtual void notifyMaterialRender(Ogre::uint32 pass_id, Ogre::MaterialPtr &mat);
};
//---------------------------------------------------------------------------
Ogre::CompositorInstance::Listener* HDRLogic::createListener(Ogre::CompositorInstance* instance)
{
HDRListener* listener = new HDRListener;
Ogre::Viewport* vp = instance->getChain()->getViewport();
listener->notifyViewportSize(vp->getActualWidth(), vp->getActualHeight());
listener->notifyCompositor(instance);
return listener;
}
//---------------------------------------------------------------------------
Ogre::CompositorInstance::Listener* HeatVisionLogic::createListener(Ogre::CompositorInstance* instance)
{
return new HeatVisionListener;
}
//---------------------------------------------------------------------------
Ogre::CompositorInstance::Listener* GaussianBlurLogic::createListener(Ogre::CompositorInstance* instance)
{
GaussianListener* listener = new GaussianListener;
Ogre::Viewport* vp = instance->getChain()->getViewport();
listener->notifyViewportSize(vp->getActualWidth(), vp->getActualHeight());
return listener;
}
/*************************************************************************
HeatVisionListener Methods
*************************************************************************/
//---------------------------------------------------------------------------
HeatVisionListener::HeatVisionListener()
{
timer = new Ogre::Timer();
start = end = curr = 0.0f;
}
//---------------------------------------------------------------------------
HeatVisionListener::~HeatVisionListener()
{
delete timer;
}
//---------------------------------------------------------------------------
void HeatVisionListener::notifyMaterialSetup(Ogre::uint32 pass_id, Ogre::MaterialPtr &mat)
{
if(pass_id == 0xDEADBABE)
{
timer->reset();
fpParams =
mat->getTechnique(0)->getPass(0)->getFragmentProgramParameters();
}
}
//---------------------------------------------------------------------------
void HeatVisionListener::notifyMaterialRender(Ogre::uint32 pass_id, Ogre::MaterialPtr &mat)
{
if(pass_id == 0xDEADBABE)
{
// "random_fractions" parameter
fpParams->setNamedConstant("random_fractions", Ogre::Vector4(Ogre::Math::RangeRandom(0.0, 1.0), Ogre::Math::RangeRandom(0, 1.0), 0, 0));
// "depth_modulator" parameter
float inc = ((float)timer->getMilliseconds())/1000.0f;
if ( (fabs(curr-end) <= 0.001) ) {
// take a new value to reach
end = Ogre::Math::RangeRandom(0.95, 1.0);
start = curr;
} else {
if (curr > end) curr -= inc;
else curr += inc;
}
timer->reset();
fpParams->setNamedConstant("depth_modulator", Ogre::Vector4(curr, 0, 0, 0));
}
}
//---------------------------------------------------------------------------
/*************************************************************************
HDRListener Methods
*************************************************************************/
//---------------------------------------------------------------------------
HDRListener::HDRListener()
{
}
//---------------------------------------------------------------------------
HDRListener::~HDRListener()
{
}
//---------------------------------------------------------------------------
void HDRListener::notifyViewportSize(int width, int height)
{
mVpWidth = width;
mVpHeight = height;
}
//---------------------------------------------------------------------------
void HDRListener::notifyCompositor(Ogre::CompositorInstance* instance)
{
// Get some RTT dimensions for later calculations
Ogre::CompositionTechnique::TextureDefinitionIterator defIter =
instance->getTechnique()->getTextureDefinitionIterator();
while (defIter.hasMoreElements())
{
Ogre::CompositionTechnique::TextureDefinition* def =
defIter.getNext();
if(def->name == "rt_bloom0")
{
mBloomSize = (int)def->width; // should be square
// Calculate gaussian texture offsets & weights
float deviation = 3.0f;
float texelSize = 1.0f / (float)mBloomSize;
// central sample, no offset
mBloomTexOffsetsHorz[0][0] = 0.0f;
mBloomTexOffsetsHorz[0][1] = 0.0f;
mBloomTexOffsetsVert[0][0] = 0.0f;
mBloomTexOffsetsVert[0][1] = 0.0f;
mBloomTexWeights[0][0] = mBloomTexWeights[0][1] =
mBloomTexWeights[0][2] = Ogre::Math::gaussianDistribution(0, 0, deviation);
mBloomTexWeights[0][3] = 1.0f;
// 'pre' samples
for(int i = 1; i < 8; ++i)
{
mBloomTexWeights[i][0] = mBloomTexWeights[i][1] =
mBloomTexWeights[i][2] = 1.25f * Ogre::Math::gaussianDistribution(i, 0, deviation);
mBloomTexWeights[i][3] = 1.0f;
mBloomTexOffsetsHorz[i][0] = i * texelSize;
mBloomTexOffsetsHorz[i][1] = 0.0f;
mBloomTexOffsetsVert[i][0] = 0.0f;
mBloomTexOffsetsVert[i][1] = i * texelSize;
}
// 'post' samples
for(int i = 8; i < 15; ++i)
{
mBloomTexWeights[i][0] = mBloomTexWeights[i][1] =
mBloomTexWeights[i][2] = mBloomTexWeights[i - 7][0];
mBloomTexWeights[i][3] = 1.0f;
mBloomTexOffsetsHorz[i][0] = -mBloomTexOffsetsHorz[i - 7][0];
mBloomTexOffsetsHorz[i][1] = 0.0f;
mBloomTexOffsetsVert[i][0] = 0.0f;
mBloomTexOffsetsVert[i][1] = -mBloomTexOffsetsVert[i - 7][1];
}
}
}
}
//---------------------------------------------------------------------------
void HDRListener::notifyMaterialSetup(Ogre::uint32 pass_id, Ogre::MaterialPtr &mat)
{
// Prepare the fragment params offsets
switch(pass_id)
{
//case 994: // rt_lum4
case 993: // rt_lum3
case 992: // rt_lum2
case 991: // rt_lum1
case 990: // rt_lum0
break;
case 800: // rt_brightpass
break;
case 701: // rt_bloom1
{
// horizontal bloom
mat->load();
Ogre::GpuProgramParametersSharedPtr fparams =
mat->getBestTechnique()->getPass(0)->getFragmentProgramParameters();
fparams->setNamedConstant("sampleOffsets", mBloomTexOffsetsHorz[0], 15);
fparams->setNamedConstant("sampleWeights", mBloomTexWeights[0], 15);
break;
}
case 700: // rt_bloom0
{
// vertical bloom
mat->load();
Ogre::GpuProgramParametersSharedPtr fparams =
mat->getTechnique(0)->getPass(0)->getFragmentProgramParameters();
fparams->setNamedConstant("sampleOffsets", mBloomTexOffsetsVert[0], 15);
fparams->setNamedConstant("sampleWeights", mBloomTexWeights[0], 15);
break;
}
}
}
//---------------------------------------------------------------------------
void HDRListener::notifyMaterialRender(Ogre::uint32 pass_id, Ogre::MaterialPtr &mat)
{
}
//---------------------------------------------------------------------------
/*************************************************************************
GaussianListener Methods
*************************************************************************/
//---------------------------------------------------------------------------
GaussianListener::GaussianListener()
{
}
//---------------------------------------------------------------------------
GaussianListener::~GaussianListener()
{
}
//---------------------------------------------------------------------------
void GaussianListener::notifyViewportSize(int width, int height)
{
mVpWidth = width;
mVpHeight = height;
// Calculate gaussian texture offsets & weights
float deviation = 3.0f;
float texelSize = 1.0f / (float)std::min(mVpWidth, mVpHeight) * 1.5f; // this modifier changes the intensity of the blur
// central sample, no offset
mBloomTexOffsetsHorz[0][0] = 0.0f;
mBloomTexOffsetsHorz[0][1] = 0.0f;
mBloomTexOffsetsVert[0][0] = 0.0f;
mBloomTexOffsetsVert[0][1] = 0.0f;
mBloomTexWeights[0][0] = mBloomTexWeights[0][1] =
mBloomTexWeights[0][2] = Ogre::Math::gaussianDistribution(0, 0, deviation);
mBloomTexWeights[0][3] = 1.0f;
// 'pre' samples
for(int i = 1; i < 8; ++i)
{
mBloomTexWeights[i][0] = mBloomTexWeights[i][1] =
mBloomTexWeights[i][2] = Ogre::Math::gaussianDistribution(i, 0, deviation);
mBloomTexWeights[i][3] = 1.0f;
mBloomTexOffsetsHorz[i][0] = i * texelSize;
mBloomTexOffsetsHorz[i][1] = 0.0f;
mBloomTexOffsetsVert[i][0] = 0.0f;
mBloomTexOffsetsVert[i][1] = i * texelSize;
}
// 'post' samples
for(int i = 8; i < 15; ++i)
{
mBloomTexWeights[i][0] = mBloomTexWeights[i][1] =
mBloomTexWeights[i][2] = mBloomTexWeights[i - 7][0];
mBloomTexWeights[i][3] = 1.0f;
mBloomTexOffsetsHorz[i][0] = -mBloomTexOffsetsHorz[i - 7][0];
mBloomTexOffsetsHorz[i][1] = 0.0f;
mBloomTexOffsetsVert[i][0] = 0.0f;
mBloomTexOffsetsVert[i][1] = -mBloomTexOffsetsVert[i - 7][1];
}
}
//---------------------------------------------------------------------------
void GaussianListener::notifyMaterialSetup(Ogre::uint32 pass_id, Ogre::MaterialPtr &mat)
{
// Prepare the fragment params offsets
switch(pass_id)
{
case 701: // blur horz
{
// horizontal bloom
mat->load();
Ogre::GpuProgramParametersSharedPtr fparams =
mat->getBestTechnique()->getPass(0)->getFragmentProgramParameters();
fparams->setNamedConstant("sampleOffsets", mBloomTexOffsetsHorz[0], 15);
fparams->setNamedConstant("sampleWeights", mBloomTexWeights[0], 15);
break;
}
case 700: // blur vert
{
// vertical bloom
mat->load();
Ogre::GpuProgramParametersSharedPtr fparams =
mat->getTechnique(0)->getPass(0)->getFragmentProgramParameters();
fparams->setNamedConstant("sampleOffsets", mBloomTexOffsetsVert[0], 15);
fparams->setNamedConstant("sampleWeights", mBloomTexWeights[0], 15);
break;
}
}
}
//---------------------------------------------------------------------------
void GaussianListener::notifyMaterialRender(Ogre::uint32 pass_id, Ogre::MaterialPtr &mat)
{
// This shit don't work like I want it.
return;
const double mod = 1.0;
// Prepare the fragment params offsets
switch(pass_id)
{
case 701: // blur horz
{
static double x = 0.0f;
x += 0.1f;
for(int i=0; i<15; ++i)
mBloomTexOffsetsHorz[i][0] *= Ogre::Math::Sin(x) * mod;
for(int i=0; i<15; ++i)
mBloomTexWeights[i][0] *= Ogre::Math::Sin(x) * mod;
// horizontal bloom
mat->load();
Ogre::GpuProgramParametersSharedPtr fparams =
mat->getBestTechnique()->getPass(0)->getFragmentProgramParameters();
fparams->setNamedConstant("sampleOffsets", mBloomTexOffsetsHorz[0], 15);
fparams->setNamedConstant("sampleWeights", mBloomTexWeights[0], 15);
break;
}
case 700: // blur vert
{
static double x = 0.0f;
x += 0.1f;
printf("x: %.2f\n", Ogre::Math::Sin(x));
for(int i=0; i<15; ++i)
mBloomTexOffsetsVert[i][0] *= Ogre::Math::Sin(x) * mod;
// vertical bloom
mat->load();
Ogre::GpuProgramParametersSharedPtr fparams =
mat->getTechnique(0)->getPass(0)->getFragmentProgramParameters();
fparams->setNamedConstant("sampleOffsets", mBloomTexOffsetsVert[0], 15);
fparams->setNamedConstant("sampleWeights", mBloomTexWeights[0], 15);
break;
}
}
}
//---------------------------------------------------------------------------