1 |
8eccf334
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2 |
cc62b34a
|
Leszek Koltunski
|
// Copyright 2017 Leszek Koltunski leszek@koltunski.pl //
|
3 |
8eccf334
|
Leszek Koltunski
|
// //
|
4 |
46b572b5
|
Leszek Koltunski
|
// This file is part of Distorted. //
|
5 |
8eccf334
|
Leszek Koltunski
|
// //
|
6 |
cc62b34a
|
Leszek Koltunski
|
// This library is free software; you can redistribute it and/or //
|
7 |
|
|
// modify it under the terms of the GNU Lesser General Public //
|
8 |
|
|
// License as published by the Free Software Foundation; either //
|
9 |
|
|
// version 2.1 of the License, or (at your option) any later version. //
|
10 |
8eccf334
|
Leszek Koltunski
|
// //
|
11 |
cc62b34a
|
Leszek Koltunski
|
// This library is distributed in the hope that it will be useful, //
|
12 |
8eccf334
|
Leszek Koltunski
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
|
13 |
cc62b34a
|
Leszek Koltunski
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU //
|
14 |
|
|
// Lesser General Public License for more details. //
|
15 |
8eccf334
|
Leszek Koltunski
|
// //
|
16 |
cc62b34a
|
Leszek Koltunski
|
// You should have received a copy of the GNU Lesser General Public //
|
17 |
|
|
// License along with this library; if not, write to the Free Software //
|
18 |
|
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA //
|
19 |
8eccf334
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
20 |
|
|
|
21 |
|
|
package org.distorted.library.effect;
|
22 |
|
|
|
23 |
43814a57
|
Leszek Koltunski
|
import org.distorted.library.effectqueue.EffectQueue;
|
24 |
e5f796bc
|
Leszek Koltunski
|
import org.distorted.library.type.Static1D;
|
25 |
|
|
import org.distorted.library.type.Static3D;
|
26 |
dd89c7f4
|
Leszek Koltunski
|
import org.distorted.library.type.Static4D;
|
27 |
e5f796bc
|
Leszek Koltunski
|
import org.distorted.library.type.Static5D;
|
28 |
dd89c7f4
|
Leszek Koltunski
|
|
29 |
f046b159
|
Leszek Koltunski
|
import java.lang.reflect.Method;
|
30 |
43814a57
|
Leszek Koltunski
|
import java.util.ArrayList;
|
31 |
f046b159
|
Leszek Koltunski
|
|
32 |
8eccf334
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
33 |
faa3ff56
|
Leszek Koltunski
|
/**
|
34 |
a20f274f
|
Leszek Koltunski
|
* Abstract class that represents an Effect that works by injecting certain code into the main Vertex shader.
|
35 |
faa3ff56
|
Leszek Koltunski
|
*/
|
36 |
8eccf334
|
Leszek Koltunski
|
public abstract class VertexEffect extends Effect
|
37 |
|
|
{
|
38 |
96e3b88a
|
Leszek Koltunski
|
/**
|
39 |
|
|
* 12: 5 per-effect interpolated values, 3-dimensional center, 4-dimensional Region
|
40 |
|
|
*/
|
41 |
|
|
public static final int NUM_FLOAT_UNIFORMS = 12;
|
42 |
|
|
/**
|
43 |
24804c15
|
Leszek Koltunski
|
* 4: name, AND association, reserved, EQU Association
|
44 |
96e3b88a
|
Leszek Koltunski
|
*/
|
45 |
24804c15
|
Leszek Koltunski
|
public static final int NUM_INT_UNIFORMS = 4;
|
46 |
96e3b88a
|
Leszek Koltunski
|
|
47 |
b24e4719
|
Leszek Koltunski
|
static final int VALUES_OFFSET = 0;
|
48 |
|
|
static final int CENTER_OFFSET = 5;
|
49 |
|
|
static final int REGION_OFFSET = 8;
|
50 |
7cd24173
|
leszek
|
private static String mGLSL = "";
|
51 |
|
|
private static int mNumEnabled = 0;
|
52 |
15aa7d94
|
Leszek Koltunski
|
|
53 |
f046b159
|
Leszek Koltunski
|
private static String mFullGLSL = "";
|
54 |
|
|
private static int mFullEnabled = 0;
|
55 |
|
|
private static boolean mFullPrepared = false;
|
56 |
|
|
|
57 |
dd89c7f4
|
Leszek Koltunski
|
final static Static4D MAX_REGION = new Static4D(0,0,0,1000000);
|
58 |
|
|
|
59 |
43814a57
|
Leszek Koltunski
|
ArrayList<EffectQueue> mQueues;
|
60 |
|
|
|
61 |
b547aaba
|
leszek
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
62 |
|
|
|
63 |
a0d5e302
|
Leszek Koltunski
|
VertexEffect(EffectName name)
|
64 |
b547aaba
|
leszek
|
{
|
65 |
9d0d8530
|
leszek
|
super(name);
|
66 |
b547aaba
|
leszek
|
}
|
67 |
7cd24173
|
leszek
|
|
68 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
69 |
|
|
|
70 |
f046b159
|
Leszek Koltunski
|
private static String retSection(int effect, String code)
|
71 |
7cd24173
|
leszek
|
{
|
72 |
f046b159
|
Leszek Koltunski
|
return
|
73 |
7cd24173
|
leszek
|
|
74 |
24804c15
|
Leszek Koltunski
|
"if( vProperties[i].x =="+effect+" )\n" +
|
75 |
7cd24173
|
leszek
|
"{\n" +
|
76 |
|
|
code +"\n" +
|
77 |
|
|
"}\n" +
|
78 |
|
|
"else\n";
|
79 |
|
|
}
|
80 |
|
|
|
81 |
f046b159
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
82 |
|
|
// prepare the code to be injected into the Full program, i.e. code of ALL vertex effects.
|
83 |
|
|
|
84 |
|
|
private static void prepareFull()
|
85 |
|
|
{
|
86 |
|
|
Method method;
|
87 |
|
|
|
88 |
|
|
for(EffectName name: EffectName.values())
|
89 |
|
|
{
|
90 |
|
|
if( name.getType() == EffectType.VERTEX )
|
91 |
|
|
{
|
92 |
|
|
Class<? extends Effect> cls = name.getEffectClass();
|
93 |
|
|
|
94 |
|
|
try
|
95 |
|
|
{
|
96 |
|
|
method = cls.getDeclaredMethod("code");
|
97 |
|
|
}
|
98 |
|
|
catch(NoSuchMethodException ex)
|
99 |
|
|
{
|
100 |
|
|
android.util.Log.e("Effect", "exception getting method: "+ex.getMessage());
|
101 |
|
|
method = null;
|
102 |
|
|
}
|
103 |
|
|
|
104 |
|
|
try
|
105 |
|
|
{
|
106 |
|
|
if( method!=null )
|
107 |
|
|
{
|
108 |
|
|
Object value = method.invoke(null);
|
109 |
|
|
String code = (String)value;
|
110 |
|
|
mFullGLSL += retSection(name.ordinal(),code);
|
111 |
|
|
mFullEnabled++;
|
112 |
|
|
}
|
113 |
|
|
}
|
114 |
|
|
catch(Exception ex)
|
115 |
|
|
{
|
116 |
|
|
android.util.Log.e("Effect", "exception invoking method: "+ex.getMessage());
|
117 |
|
|
}
|
118 |
|
|
}
|
119 |
|
|
}
|
120 |
|
|
}
|
121 |
|
|
|
122 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
123 |
|
|
// prepare code to be injected into the 'main_vertex_shader' main() function.
|
124 |
|
|
|
125 |
|
|
static void addEffect(EffectName name, String code)
|
126 |
|
|
{
|
127 |
|
|
int effect = name.ordinal();
|
128 |
|
|
|
129 |
|
|
if( !mEnabled[effect] )
|
130 |
|
|
{
|
131 |
|
|
mEnabled[effect] = true;
|
132 |
|
|
mNumEnabled ++;
|
133 |
|
|
mGLSL += retSection(effect,code);
|
134 |
|
|
}
|
135 |
|
|
}
|
136 |
|
|
|
137 |
7cd24173
|
leszek
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
138 |
faa3ff56
|
Leszek Koltunski
|
/**
|
139 |
|
|
* Only for use by the library itself.
|
140 |
|
|
*
|
141 |
|
|
* @y.exclude
|
142 |
|
|
*/
|
143 |
7cd24173
|
leszek
|
public static String getGLSL()
|
144 |
|
|
{
|
145 |
|
|
return mGLSL + "{}";
|
146 |
|
|
}
|
147 |
|
|
|
148 |
f046b159
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
149 |
|
|
/**
|
150 |
|
|
* Only for use by the library itself.
|
151 |
|
|
*
|
152 |
|
|
* @y.exclude
|
153 |
|
|
*/
|
154 |
|
|
public static String getAllGLSL()
|
155 |
|
|
{
|
156 |
|
|
if( !mFullPrepared )
|
157 |
|
|
{
|
158 |
|
|
prepareFull();
|
159 |
|
|
mFullPrepared = true;
|
160 |
|
|
}
|
161 |
|
|
|
162 |
|
|
return mFullGLSL + "{}";
|
163 |
|
|
}
|
164 |
|
|
|
165 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
166 |
|
|
/**
|
167 |
|
|
* Only for use by the library itself.
|
168 |
|
|
*
|
169 |
|
|
* @y.exclude
|
170 |
|
|
*/
|
171 |
|
|
public static int getAllEnabled()
|
172 |
|
|
{
|
173 |
|
|
if( !mFullPrepared )
|
174 |
|
|
{
|
175 |
|
|
prepareFull();
|
176 |
|
|
mFullPrepared = true;
|
177 |
|
|
}
|
178 |
|
|
|
179 |
|
|
return mFullEnabled;
|
180 |
|
|
}
|
181 |
|
|
|
182 |
7cd24173
|
leszek
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
183 |
2f1f7570
|
Leszek Koltunski
|
|
184 |
|
|
static void destroyStatics()
|
185 |
7cd24173
|
leszek
|
{
|
186 |
faa3ff56
|
Leszek Koltunski
|
mNumEnabled = 0;
|
187 |
|
|
mGLSL = "";
|
188 |
f046b159
|
Leszek Koltunski
|
mFullEnabled= 0;
|
189 |
|
|
mFullGLSL = "";
|
190 |
|
|
mFullPrepared = false;
|
191 |
7cd24173
|
leszek
|
}
|
192 |
|
|
|
193 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
194 |
faa3ff56
|
Leszek Koltunski
|
// PUBLIC API
|
195 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
196 |
|
|
/**
|
197 |
6b816678
|
Leszek Koltunski
|
* Return the number of Vertex effects enabled.
|
198 |
faa3ff56
|
Leszek Koltunski
|
*/
|
199 |
|
|
public static int getNumEnabled()
|
200 |
7cd24173
|
leszek
|
{
|
201 |
faa3ff56
|
Leszek Koltunski
|
return mNumEnabled;
|
202 |
7cd24173
|
leszek
|
}
|
203 |
bc208a9c
|
Leszek Koltunski
|
|
204 |
43814a57
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
205 |
|
|
/**
|
206 |
|
|
* Only for use by the library itself.
|
207 |
|
|
*
|
208 |
|
|
* @y.exclude
|
209 |
|
|
*/
|
210 |
|
|
public void addQueue(EffectQueue queue)
|
211 |
|
|
{
|
212 |
|
|
if( mQueues==null ) mQueues = new ArrayList<>();
|
213 |
|
|
|
214 |
|
|
mQueues.add(queue);
|
215 |
|
|
}
|
216 |
|
|
|
217 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
218 |
|
|
/**
|
219 |
|
|
* Only for use by the library itself.
|
220 |
|
|
*
|
221 |
|
|
* @y.exclude
|
222 |
|
|
*/
|
223 |
|
|
public void remQueue(EffectQueue queue)
|
224 |
|
|
{
|
225 |
|
|
if( mQueues==null ) mQueues = new ArrayList<>();
|
226 |
|
|
|
227 |
|
|
mQueues.remove(queue);
|
228 |
|
|
}
|
229 |
|
|
|
230 |
bc208a9c
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
231 |
|
|
/**
|
232 |
1e672c1d
|
Leszek Koltunski
|
* Set Mesh association.
|
233 |
bc208a9c
|
Leszek Koltunski
|
*
|
234 |
2aeb75aa
|
Leszek Koltunski
|
* This creates an association between a Component of a Mesh and this Vertex Effect.
|
235 |
|
|
* One can set two types of associations - an 'logical and' and a 'equal' associations and the Effect
|
236 |
|
|
* will only be active on vertices of Components such that
|
237 |
36d65d88
|
Leszek Koltunski
|
*
|
238 |
2aeb75aa
|
Leszek Koltunski
|
* (effect andAssoc) & (component andAssoc) != 0 || (effect equAssoc) == (mesh equAssoc)
|
239 |
36d65d88
|
Leszek Koltunski
|
*
|
240 |
|
|
* (see main_vertex_shader)
|
241 |
bc208a9c
|
Leszek Koltunski
|
*
|
242 |
|
|
* The point: this way we can configure the system so that each Vertex Effect acts only on a certain
|
243 |
1e672c1d
|
Leszek Koltunski
|
* subset of a Mesh, thus potentially significantly reducing the number of render calls.
|
244 |
bc208a9c
|
Leszek Koltunski
|
*/
|
245 |
2aeb75aa
|
Leszek Koltunski
|
public void setMeshAssociation(int andAssociation, int equAssociation)
|
246 |
bc208a9c
|
Leszek Koltunski
|
{
|
247 |
2aeb75aa
|
Leszek Koltunski
|
mAndAssociation = andAssociation;
|
248 |
|
|
mEquAssociation = equAssociation;
|
249 |
9becf30e
|
Leszek Koltunski
|
|
250 |
43814a57
|
Leszek Koltunski
|
long id = getID();
|
251 |
|
|
int numQueues = mQueues==null ? 0: mQueues.size();
|
252 |
|
|
|
253 |
|
|
for(int i=0; i<numQueues; i++)
|
254 |
|
|
{
|
255 |
|
|
EffectQueue queue = mQueues.get(i);
|
256 |
|
|
queue.setAssociation(id);
|
257 |
|
|
}
|
258 |
bc208a9c
|
Leszek Koltunski
|
}
|
259 |
e5f796bc
|
Leszek Koltunski
|
|
260 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
261 |
|
|
/**
|
262 |
|
|
* Return a constructed VertexEffect based on its name, the 5 variables, center & region.
|
263 |
|
|
*/
|
264 |
|
|
public static VertexEffect constructEffect(String name,float[] vars, float[] center, float[] region)
|
265 |
|
|
{
|
266 |
|
|
Static3D staCenter = new Static3D(center[0],center[1],center[2]);
|
267 |
|
|
Static4D staRegion = new Static4D(region[0],region[1],region[2],region[3]);
|
268 |
|
|
|
269 |
|
|
if( name.equals(EffectName.DISTORT.name()) )
|
270 |
|
|
{
|
271 |
|
|
Static3D staVars = new Static3D(vars[2],vars[3],vars[4]);
|
272 |
|
|
return new VertexEffectDistort(staVars,staCenter,staRegion);
|
273 |
|
|
}
|
274 |
|
|
if( name.equals(EffectName.DEFORM.name()) )
|
275 |
|
|
{
|
276 |
|
|
Static3D staVars = new Static3D(vars[1],vars[2],vars[3]);
|
277 |
|
|
Static1D staRadius = new Static1D(vars[4]);
|
278 |
|
|
return new VertexEffectDeform(staVars,staRadius,staCenter,staRegion);
|
279 |
|
|
}
|
280 |
|
|
if( name.equals(EffectName.SINK.name()) )
|
281 |
|
|
{
|
282 |
|
|
Static1D staSink = new Static1D(vars[4]);
|
283 |
|
|
return new VertexEffectSink(staSink,staCenter,staRegion);
|
284 |
|
|
}
|
285 |
|
|
if( name.equals(EffectName.PINCH.name()) )
|
286 |
|
|
{
|
287 |
|
|
Static3D staVars = new Static3D(vars[2],vars[3],vars[4]);
|
288 |
|
|
return new VertexEffectPinch(staVars,staCenter,staRegion);
|
289 |
|
|
}
|
290 |
|
|
if( name.equals(EffectName.SWIRL.name()) )
|
291 |
|
|
{
|
292 |
|
|
Static1D staSwirl = new Static1D(vars[4]);
|
293 |
|
|
return new VertexEffectSwirl(staSwirl,staCenter,staRegion);
|
294 |
|
|
}
|
295 |
|
|
if( name.equals(EffectName.WAVE.name()) )
|
296 |
|
|
{
|
297 |
|
|
Static5D staWave = new Static5D(vars[0],vars[1],vars[2],vars[3],vars[4]);
|
298 |
|
|
return new VertexEffectWave(staWave,staCenter,staRegion);
|
299 |
|
|
}
|
300 |
|
|
if( name.equals(EffectName.DISAPPEAR.name()) )
|
301 |
|
|
{
|
302 |
|
|
return new VertexEffectDisappear();
|
303 |
|
|
}
|
304 |
|
|
if( name.equals(EffectName.VERTEX_MOVE.name()) )
|
305 |
|
|
{
|
306 |
|
|
Static3D staVector = new Static3D(vars[2],vars[3],vars[4]);
|
307 |
|
|
return new VertexEffectMove(staVector);
|
308 |
|
|
}
|
309 |
|
|
if( name.equals(EffectName.VERTEX_QUATERNION.name()) )
|
310 |
|
|
{
|
311 |
|
|
Static4D staQuat = new Static4D(vars[1],vars[2],vars[3],vars[4]);
|
312 |
|
|
return new VertexEffectQuaternion(staQuat,staCenter);
|
313 |
|
|
}
|
314 |
|
|
if( name.equals(EffectName.VERTEX_ROTATE.name()) )
|
315 |
|
|
{
|
316 |
|
|
Static1D staAngle = new Static1D(vars[1]);
|
317 |
|
|
Static3D staAxis = new Static3D(vars[2],vars[3],vars[4]);
|
318 |
|
|
return new VertexEffectRotate(staAngle,staAxis,staCenter);
|
319 |
|
|
}
|
320 |
|
|
if( name.equals(EffectName.VERTEX_SCALE.name()) )
|
321 |
|
|
{
|
322 |
|
|
Static3D staScale = new Static3D(vars[2],vars[3],vars[4]);
|
323 |
|
|
return new VertexEffectScale(staScale);
|
324 |
|
|
}
|
325 |
|
|
if( name.equals(EffectName.VERTEX_SHEAR.name()) )
|
326 |
|
|
{
|
327 |
|
|
Static3D staShear = new Static3D(vars[2],vars[3],vars[4]);
|
328 |
|
|
return new VertexEffectShear(staShear,staCenter);
|
329 |
|
|
}
|
330 |
|
|
|
331 |
|
|
return null;
|
332 |
|
|
}
|
333 |
8eccf334
|
Leszek Koltunski
|
}
|