1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2017 Leszek Koltunski //
|
3
|
// //
|
4
|
// This file is part of Distorted. //
|
5
|
// //
|
6
|
// Distorted is free software: you can redistribute it and/or modify //
|
7
|
// it under the terms of the GNU General Public License as published by //
|
8
|
// the Free Software Foundation, either version 2 of the License, or //
|
9
|
// (at your option) any later version. //
|
10
|
// //
|
11
|
// Distorted is distributed in the hope that it will be useful, //
|
12
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
|
13
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
|
14
|
// GNU General Public License for more details. //
|
15
|
// //
|
16
|
// You should have received a copy of the GNU General Public License //
|
17
|
// along with Distorted. If not, see <http://www.gnu.org/licenses/>. //
|
18
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
19
|
|
20
|
package org.distorted.library.effect;
|
21
|
|
22
|
import org.distorted.library.effectqueue.EffectQueue;
|
23
|
import org.distorted.library.type.Static4D;
|
24
|
|
25
|
import java.lang.reflect.Method;
|
26
|
import java.util.ArrayList;
|
27
|
|
28
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
29
|
/**
|
30
|
* Abstract class that represents an Effect that works by injecting certain code into the main Vertex shader.
|
31
|
*/
|
32
|
public abstract class VertexEffect extends Effect
|
33
|
{
|
34
|
/**
|
35
|
* 12: 5 per-effect interpolated values, 3-dimensional center, 4-dimensional Region
|
36
|
*/
|
37
|
public static final int NUM_FLOAT_UNIFORMS = 12;
|
38
|
/**
|
39
|
* 4: name, AND association, reserved, EQU Association
|
40
|
*/
|
41
|
public static final int NUM_INT_UNIFORMS = 4;
|
42
|
|
43
|
static final int VALUES_OFFSET = 0;
|
44
|
static final int CENTER_OFFSET = 5;
|
45
|
static final int REGION_OFFSET = 8;
|
46
|
private static String mGLSL = "";
|
47
|
private static int mNumEnabled = 0;
|
48
|
|
49
|
private static String mFullGLSL = "";
|
50
|
private static int mFullEnabled = 0;
|
51
|
private static boolean mFullPrepared = false;
|
52
|
|
53
|
final static Static4D MAX_REGION = new Static4D(0,0,0,1000000);
|
54
|
|
55
|
ArrayList<EffectQueue> mQueues;
|
56
|
|
57
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
58
|
|
59
|
VertexEffect(EffectName name)
|
60
|
{
|
61
|
super(name);
|
62
|
}
|
63
|
|
64
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
65
|
|
66
|
private static String retSection(int effect, String code)
|
67
|
{
|
68
|
return
|
69
|
|
70
|
"if( vProperties[i].x =="+effect+" )\n" +
|
71
|
"{\n" +
|
72
|
code +"\n" +
|
73
|
"}\n" +
|
74
|
"else\n";
|
75
|
}
|
76
|
|
77
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
78
|
// prepare the code to be injected into the Full program, i.e. code of ALL vertex effects.
|
79
|
|
80
|
private static void prepareFull()
|
81
|
{
|
82
|
Method method;
|
83
|
|
84
|
for(EffectName name: EffectName.values())
|
85
|
{
|
86
|
if( name.getType() == EffectType.VERTEX )
|
87
|
{
|
88
|
Class<? extends Effect> cls = name.getEffectClass();
|
89
|
|
90
|
try
|
91
|
{
|
92
|
method = cls.getDeclaredMethod("code");
|
93
|
}
|
94
|
catch(NoSuchMethodException ex)
|
95
|
{
|
96
|
android.util.Log.e("Effect", "exception getting method: "+ex.getMessage());
|
97
|
method = null;
|
98
|
}
|
99
|
|
100
|
try
|
101
|
{
|
102
|
if( method!=null )
|
103
|
{
|
104
|
Object value = method.invoke(null);
|
105
|
String code = (String)value;
|
106
|
mFullGLSL += retSection(name.ordinal(),code);
|
107
|
mFullEnabled++;
|
108
|
}
|
109
|
}
|
110
|
catch(Exception ex)
|
111
|
{
|
112
|
android.util.Log.e("Effect", "exception invoking method: "+ex.getMessage());
|
113
|
}
|
114
|
}
|
115
|
}
|
116
|
}
|
117
|
|
118
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
119
|
// prepare code to be injected into the 'main_vertex_shader' main() function.
|
120
|
|
121
|
static void addEffect(EffectName name, String code)
|
122
|
{
|
123
|
int effect = name.ordinal();
|
124
|
|
125
|
if( !mEnabled[effect] )
|
126
|
{
|
127
|
mEnabled[effect] = true;
|
128
|
mNumEnabled ++;
|
129
|
mGLSL += retSection(effect,code);
|
130
|
}
|
131
|
}
|
132
|
|
133
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
134
|
/**
|
135
|
* Only for use by the library itself.
|
136
|
*
|
137
|
* @y.exclude
|
138
|
*/
|
139
|
public static String getGLSL()
|
140
|
{
|
141
|
return mGLSL + "{}";
|
142
|
}
|
143
|
|
144
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
145
|
/**
|
146
|
* Only for use by the library itself.
|
147
|
*
|
148
|
* @y.exclude
|
149
|
*/
|
150
|
public static String getAllGLSL()
|
151
|
{
|
152
|
if( !mFullPrepared )
|
153
|
{
|
154
|
prepareFull();
|
155
|
mFullPrepared = true;
|
156
|
}
|
157
|
|
158
|
return mFullGLSL + "{}";
|
159
|
}
|
160
|
|
161
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
162
|
/**
|
163
|
* Only for use by the library itself.
|
164
|
*
|
165
|
* @y.exclude
|
166
|
*/
|
167
|
public static int getAllEnabled()
|
168
|
{
|
169
|
if( !mFullPrepared )
|
170
|
{
|
171
|
prepareFull();
|
172
|
mFullPrepared = true;
|
173
|
}
|
174
|
|
175
|
return mFullEnabled;
|
176
|
}
|
177
|
|
178
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
179
|
|
180
|
static void destroyStatics()
|
181
|
{
|
182
|
mNumEnabled = 0;
|
183
|
mGLSL = "";
|
184
|
mFullEnabled= 0;
|
185
|
mFullGLSL = "";
|
186
|
mFullPrepared = false;
|
187
|
}
|
188
|
|
189
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
190
|
// PUBLIC API
|
191
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
192
|
/**
|
193
|
* Return the number of Vertex effects enabled.
|
194
|
*/
|
195
|
public static int getNumEnabled()
|
196
|
{
|
197
|
return mNumEnabled;
|
198
|
}
|
199
|
|
200
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
201
|
/**
|
202
|
* Only for use by the library itself.
|
203
|
*
|
204
|
* @y.exclude
|
205
|
*/
|
206
|
public void addQueue(EffectQueue queue)
|
207
|
{
|
208
|
if( mQueues==null ) mQueues = new ArrayList<>();
|
209
|
|
210
|
mQueues.add(queue);
|
211
|
}
|
212
|
|
213
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
214
|
/**
|
215
|
* Only for use by the library itself.
|
216
|
*
|
217
|
* @y.exclude
|
218
|
*/
|
219
|
public void remQueue(EffectQueue queue)
|
220
|
{
|
221
|
if( mQueues==null ) mQueues = new ArrayList<>();
|
222
|
|
223
|
mQueues.remove(queue);
|
224
|
}
|
225
|
|
226
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
227
|
/**
|
228
|
* Set Mesh association.
|
229
|
*
|
230
|
* This creates an association between a Component of a Mesh and this Vertex Effect.
|
231
|
* One can set two types of associations - an 'logical and' and a 'equal' associations and the Effect
|
232
|
* will only be active on vertices of Components such that
|
233
|
*
|
234
|
* (effect andAssoc) & (component andAssoc) != 0 || (effect equAssoc) == (mesh equAssoc)
|
235
|
*
|
236
|
* (see main_vertex_shader)
|
237
|
*
|
238
|
* The point: this way we can configure the system so that each Vertex Effect acts only on a certain
|
239
|
* subset of a Mesh, thus potentially significantly reducing the number of render calls.
|
240
|
*/
|
241
|
public void setMeshAssociation(int andAssociation, int equAssociation)
|
242
|
{
|
243
|
mAndAssociation = andAssociation;
|
244
|
mEquAssociation = equAssociation;
|
245
|
|
246
|
long id = getID();
|
247
|
int numQueues = mQueues==null ? 0: mQueues.size();
|
248
|
|
249
|
for(int i=0; i<numQueues; i++)
|
250
|
{
|
251
|
EffectQueue queue = mQueues.get(i);
|
252
|
queue.setAssociation(id);
|
253
|
}
|
254
|
}
|
255
|
}
|