/
two_pass_gaussian.cpp
194 lines (169 loc) · 6.55 KB
/
two_pass_gaussian.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
184
185
186
187
188
189
190
191
192
// *****************************************************************************
// two_pass_gaussian.cpp Tao3D project
// *****************************************************************************
//
// File description:
//
// Two-pass gaussian filter
//
//
//
//
//
//
//
//
// *****************************************************************************
// This software is licensed under the GNU General Public License v3
// (C) 2014, Baptiste Soulisse <baptiste.soulisse@taodyne.com>
// (C) 2013, Catherine Burvelle <catherine@taodyne.com>
// (C) 2013-2014,2019, Christophe de Dinechin <christophe@dinechin.org>
// (C) 2014, Jérôme Forissier <jerome@taodyne.com>
// *****************************************************************************
// This file is part of Tao3D
//
// Tao3D is free software: you can r redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Tao3D 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 Tao3D, in a file named COPYING.
// If not, see <https://www.gnu.org/licenses/>.
// *****************************************************************************
#include "two_pass_gaussian.h"
#include "tao/tao_gl.h"
#include <cmath>
#include <sstream>
#include <iomanip>
const QGLContext* TwoPassGaussian::context = NULL;
QGLShaderProgram* TwoPassGaussian::pgm[MAX_SAMPLES] = { NULL };
uint TwoPassGaussian::textureID[MAX_SAMPLES] = { 0 };
uint TwoPassGaussian::deltaID[MAX_SAMPLES] = { 0 };
TwoPassGaussian::TwoPassGaussian(uint w, uint h, uint n, bool vert)
// ----------------------------------------------------------------------------
// Two pass gaussian filter
// ----------------------------------------------------------------------------
: Filter(&context), width(w), height(h), samples(n), vertical(vert)
{}
TwoPassGaussian::~TwoPassGaussian()
// ----------------------------------------------------------------------------
// Destructor does nothing
// ----------------------------------------------------------------------------
{}
void TwoPassGaussian::createShaders()
// ----------------------------------------------------------------------------
// When changing GL context, erase older GLSL programs
// ----------------------------------------------------------------------------
{
IFTRACE(filters)
debug() << "Create shader for two-pass gaussian filter\n";
for (uint i = 0; i < MAX_SAMPLES; i++)
{
delete pgm[i];
pgm[i] = NULL;
}
}
QGLShaderProgram *TwoPassGaussian::shader()
// ----------------------------------------------------------------------------
// Create the shader for the current number of samples
// ----------------------------------------------------------------------------
{
uint s = samples-1;
if (!pgm[s])
{
std::ostringstream vertex, fragment;
// Compute the gauss multiplicative factors
float gaussFactor[64] = { 0 };
float scale = -1.0 / (0.2*samples * samples);
float mid = 0.5 * (samples-1);
float sum = 0.0;
for (uint i = 0; i < samples; i++)
{
gaussFactor[i] = exp(scale * (i-mid)*(i-mid));
sum += gaussFactor[i];
}
for (uint i = 0; i < samples; i++)
gaussFactor[i] /= sum;
// Compute the vertex shader
vertex <<
"/* Copyright (C) 2013 Taodyne SAS - All rights reserved */\n"
"uniform vec2 delta;\n"
"varying vec2 texCoord[" << samples << "];\n"
"void main()\n"
"{\n"
" gl_Position = ftransform();\n"
" vec4 tc4 = gl_TextureMatrix[0] * gl_MultiTexCoord0;\n"
" vec2 tc = tc4.st;\n";
for (uint i = 0; i < samples; i++)
vertex << " texCoord[" << i << "] = tc + "
<< std::fixed << std::setprecision(11)
<< (i - mid) << " * delta;\n";
vertex <<
"}\n";
// Compute the fragment shader
fragment <<
"/* Copyright (C) 2013 Taodyne - All rights reserved */\n"
"uniform sampler2D texture;\n"
"varying vec2 texCoord[" << samples << "];\n"
"void main()\n"
"{\n"
" gl_FragColor =\n";
for (uint i = 0; i < samples; i++)
fragment << (i > 0 ? "\n + " : " ") <<
"texture2D(texture, texCoord[" << i << "]) * " <<
std::fixed << std::setprecision(11) <<
gaussFactor[i];
fragment <<
";\n"
"}";
text vSrc = vertex.str();
text fSrc = fragment.str();
IFTRACE(filters)
debug() << "TWO-PASS GAUSSIAN - VERTEX:\n" << vSrc
<< "\n\nTWO_PASS_GAUSSIAN - FRAGMENT:\n" << fSrc << "\n";
QGLShaderProgram *p = new QGLShaderProgram(context);
if (!p->addShaderFromSourceCode(QGLShader::Vertex, vSrc.c_str()) ||
!p->addShaderFromSourceCode(QGLShader::Fragment, fSrc.c_str()) ||
!p->link())
{
text error = p->log().toUtf8().constData();
debug() << "Error building shader for two-pass gaussian filter:\n";
debug() << error << "\n";
delete p;
return NULL;
}
uint pid = p->programId();
textureID[s] = GL.GetUniformLocation(pid, "texture");
deltaID[s] = GL.GetUniformLocation(pid, "delta");
// Store for next time
pgm[s] = p;
}
return pgm[s];
}
void TwoPassGaussian::Draw()
// ----------------------------------------------------------------------------
// Draw the given pass for the gaussian pass
// ----------------------------------------------------------------------------
{
checkGLContext();
if (QGLShaderProgram *p = shader())
{
uint pid = p->programId();
if (pid)
{
tao->SetShader(pid);
uint s = samples-1;
GL.Uniform(textureID[s], 0);
if (vertical)
GL.Uniform(deltaID[s], 0.0f, 1.0f / height);
else
GL.Uniform(deltaID[s], 1.0f / width, 0.0f);
}
}
}