-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathASlider.cpp
executable file
·288 lines (208 loc) · 7.05 KB
/
ASlider.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
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
#include "ASlider.h"
using namespace ffglex;
static CFFGLPluginInfo PluginInfo(
PluginFactory< ASlider >,// Create method
"RE01", // Plugin unique ID of maximum length 4.
"ASlider", // Plugin name
2, // API major version number
1, // API minor version number
1, // Plugin major version number
0, // Plugin minor version number
FF_EFFECT, // Plugin type
"Slider", // Plugin description
"Alex" // About
);
static const char _dummyvertexShaderCode[] = R"(#version 410 core
uniform vec2 MaxUV;
layout( location = 0 ) in vec4 vPosition;
layout( location = 1 ) in vec2 vUV;
out vec2 uv;
out vec2 norm_uv;
void main()
{
gl_Position = vPosition;
uv = vUV * MaxUV;
norm_uv = vUV;
}
)";
static const char _dummyfragmentShaderCode[] = R"(#version 410 core
uniform sampler2D inputTexture;
in vec2 uv;
in vec2 norm_uv; // si on prend pas le normuv, ça dépeint d'un pixel
out vec4 fragColor;
void main()
{
vec4 smp0 = texture(inputTexture, norm_uv);
fragColor = smp0;
}
)";
static const char _vertexShaderCode[] = R"(#version 410 core
uniform vec2 MaxUV;
layout( location = 0 ) in vec4 vPosition;
layout( location = 1 ) in vec2 vUV;
out vec2 uv;
out vec2 norm_uv;
void main()
{
gl_Position = vPosition;
uv = vUV * MaxUV;
norm_uv = vUV;
}
)";
static const char _fragmentShaderCode[] = R"(#version 410 core
uniform sampler2D inputTexture;
uniform sampler2D inputTexture2;
uniform float Up;
uniform float Down;
in vec2 uv;
in vec2 norm_uv;
out vec4 fragColor;
void main()
{
vec4 su;
vec4 sd;
vec4 up;
vec4 down;
vec4 amount;
vec4 input0 = vec4(texture(inputTexture, uv));
// vec2 uv2new = norm_uv - vec2(1./488., 1./488.);
vec4 input1 = vec4(texture(inputTexture2, norm_uv));
// get contribution
amount.x = (input0.x > input1.x) ? 1. : 0.0;
amount.y = (input0.y > input1.y) ? 1. : 0.0;
amount.z = (input0.z > input1.z) ? 1. : 0.0;
amount.w = (input0.w > input1.w) ? 1. : 0.0;
// calculate slide down
float d = max(1.0, abs(Down));
sd = vec4(1.0 / d);
down = input1 + ((input0 - input1) * sd);
// calculate slide up
float u = max(1.0, abs(Up));
su = vec4(1.0 / u);
up = input1 + ((input0 - input1) * su);
// mix between down and up
vec4 out_color = mix(down, up, amount);
// if(out_color.r < 0.0080) out_color.r = 0; //003921568627
// if(out_color.g < 0.0080) out_color.g = 0; //003921568627
// if(out_color.b < 0.0080) out_color.b = 0; //003921568627
out_color -= vec4(0.004, 0.004, 0.004, 0.004);
fragColor = out_color;
}
)";
using namespace std;
ASlider::ASlider()
{
// Input properties
SetMinInputs( 1 );
SetMaxInputs( 1 );
AddParam( ffglqs::ParamRange::Create( "Up", 1., {1.0, 40.0} ) );
AddParam( ffglqs::ParamRange::Create( "Down", 1., {1.0, 40.0} ) );
last_width = -1;
last_height = -1;
}
ASlider::~ASlider()
{
}
FFResult ASlider::InitGL( const FFGLViewportStruct* vp )
{
if( !shader.Compile( _vertexShaderCode, _fragmentShaderCode ) )
{
cout << "!dummy_shader.Compile(_dummyvertexShaderCode, _dummyfragmentShaderCode)"<< "\n";
DeInitGL();
return FF_FAIL;
}
if( !dummy_shader.Compile(_dummyvertexShaderCode, _dummyfragmentShaderCode) )
{
DeInitGL();
cout << "!dummy_shader.Compile(_dummyvertexShaderCode, _dummyfragmentShaderCode)"<< "\n";
return FF_FAIL;
}
if( !quad.Initialise() )
{
cout << "!quad.Initialise()"<< "\n";
DeInitGL();
return FF_FAIL;
}
cout << "INITALIZED ASLIDER\n";
//Use base-class init as success result so that it retains the viewport.
return CFFGLPlugin::InitGL( vp );
}
bool ASlider::resolutionChanged(int width, int height)
{
bool bResolutionChange;
if((width != last_width) || (height != last_height))
{
last_width = width;
last_height = height;
bResolutionChange = true;
// printf("init change size %u %u %u %u\n", width, height, last_width);
}
else
bResolutionChange = false;
return bResolutionChange;
}
FFResult ASlider::ProcessOpenGL( ProcessOpenGLStruct* pGL )
{
if( pGL->numInputTextures < 1 )
return FF_FAIL;
if( pGL->inputTextures[ 0 ] == NULL )
return FF_FAIL;
FFGLTexCoords maxCoords = GetMaxGLTexCoords( *pGL->inputTextures[ 0 ] );
if(resolutionChanged(pGL->inputTextures[ 0 ]->Width, pGL->inputTextures[ 0 ]->Height))
{
Resize(¤tViewport);
// fbos[0].Initialise(pGL->inputTextures[ 0 ]->HardwareWidth, pGL->inputTextures[ 0 ]->HardwareHeight);
// fbos[1].Initialise(pGL->inputTextures[ 0 ]->HardwareWidth, pGL->inputTextures[ 0 ]->HardwareHeight);
}
{
ffglex::ScopedFBOBinding fbobind(fbos[index].GetGLID(), ScopedFBOBinding::RestoreBehaviour::RB_REVERT);
ScopedShaderBinding shaderBinding( shader.GetGLID() );
ScopedSamplerActivation activateSampler( 0 );
Scoped2DTextureBinding textureBinding( pGL->inputTextures[ 0 ]->Handle );
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
ScopedSamplerActivation activateSampler2( 1 );
Scoped2DTextureBinding textureBinding2( fbos[1 - index].GetTextureInfo().Handle );
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
shader.Set( "inputTexture", 0 );
shader.Set( "inputTexture2", 1 );
shader.Set( "MaxUV", maxCoords.s, maxCoords.t );
SendParams( shader );
quad.Draw();
}
ScopedShaderBinding s_shader_binding( dummy_shader.GetGLID() );
ScopedSamplerActivation s_activateSampler( 0 );
Scoped2DTextureBinding s_textureBinding( fbos[1-index].GetTextureInfo().Handle );
dummy_shader.Set( "inputTexture", 0 );
dummy_shader.Set( "MaxUV", maxCoords.s, maxCoords.t );
SendParams( dummy_shader );
quad.Draw();
index = 1 - index;
return FF_SUCCESS;
}
unsigned int ASlider::Resize( const FFGLViewportStruct* vp )
{
if(resolutionChanged(vp->width, vp->height))
{
fbos[0].Release();
fbos[1].Release();
fbos[0].Initialise(vp->width, vp->height);
fbos[1].Initialise(vp->width, vp->height);
}
currentViewport = *vp;
return FF_SUCCESS;
}
FFResult ASlider::DeInitGL()
{
shader.FreeGLResources();
dummy_shader.FreeGLResources();
fbos[0].Release();
fbos[1].Release();
quad.Release();
return FF_SUCCESS;
}