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.type.Static4D;
|
23
|
|
24
|
import java.lang.reflect.Method;
|
25
|
|
26
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
27
|
/**
|
28
|
* Abstract class that represents an Effect that works by injecting certain code into the main Vertex shader.
|
29
|
*/
|
30
|
public abstract class VertexEffect extends Effect
|
31
|
{
|
32
|
/**
|
33
|
* 12: 5 per-effect interpolated values, 3-dimensional center, 4-dimensional Region
|
34
|
*/
|
35
|
public static final int NUM_UNIFORMS = 12;
|
36
|
static final int VALUES_OFFSET = 0;
|
37
|
static final int CENTER_OFFSET = 5;
|
38
|
static final int REGION_OFFSET = 8;
|
39
|
private static String mGLSL = "";
|
40
|
private static int mNumEnabled = 0;
|
41
|
|
42
|
private static String mFullGLSL = "";
|
43
|
private static int mFullEnabled = 0;
|
44
|
private static boolean mFullPrepared = false;
|
45
|
|
46
|
final static Static4D MAX_REGION = new Static4D(0,0,0,1000000);
|
47
|
|
48
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
49
|
|
50
|
VertexEffect(EffectName name)
|
51
|
{
|
52
|
super(name);
|
53
|
}
|
54
|
|
55
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
56
|
|
57
|
private static String retSection(int effect, String code)
|
58
|
{
|
59
|
return
|
60
|
|
61
|
"if( vName[i]=="+effect+" )\n" +
|
62
|
"{\n" +
|
63
|
code +"\n" +
|
64
|
"}\n" +
|
65
|
"else\n";
|
66
|
}
|
67
|
|
68
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
69
|
// prepare the code to be injected into the Full program, i.e. code of ALL vertex effects.
|
70
|
|
71
|
private static void prepareFull()
|
72
|
{
|
73
|
Method method;
|
74
|
|
75
|
for(EffectName name: EffectName.values())
|
76
|
{
|
77
|
if( name.getType() == EffectType.VERTEX )
|
78
|
{
|
79
|
Class<? extends Effect> cls = name.getEffectClass();
|
80
|
|
81
|
try
|
82
|
{
|
83
|
method = cls.getDeclaredMethod("code");
|
84
|
}
|
85
|
catch(NoSuchMethodException ex)
|
86
|
{
|
87
|
android.util.Log.e("Effect", "exception getting method: "+ex.getMessage());
|
88
|
method = null;
|
89
|
}
|
90
|
|
91
|
try
|
92
|
{
|
93
|
if( method!=null )
|
94
|
{
|
95
|
Object value = method.invoke(null);
|
96
|
String code = (String)value;
|
97
|
mFullGLSL += retSection(name.ordinal(),code);
|
98
|
mFullEnabled++;
|
99
|
}
|
100
|
}
|
101
|
catch(Exception ex)
|
102
|
{
|
103
|
android.util.Log.e("Effect", "exception invoking method: "+ex.getMessage());
|
104
|
}
|
105
|
}
|
106
|
}
|
107
|
}
|
108
|
|
109
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
110
|
// prepare code to be injected into the 'main_vertex_shader' main() function.
|
111
|
|
112
|
static void addEffect(EffectName name, String code)
|
113
|
{
|
114
|
int effect = name.ordinal();
|
115
|
|
116
|
if( !mEnabled[effect] )
|
117
|
{
|
118
|
mEnabled[effect] = true;
|
119
|
mNumEnabled ++;
|
120
|
mGLSL += retSection(effect,code);
|
121
|
}
|
122
|
}
|
123
|
|
124
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
125
|
/**
|
126
|
* Only for use by the library itself.
|
127
|
*
|
128
|
* @y.exclude
|
129
|
*/
|
130
|
public static String getGLSL()
|
131
|
{
|
132
|
return mGLSL + "{}";
|
133
|
}
|
134
|
|
135
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
136
|
/**
|
137
|
* Only for use by the library itself.
|
138
|
*
|
139
|
* @y.exclude
|
140
|
*/
|
141
|
public static String getAllGLSL()
|
142
|
{
|
143
|
if( !mFullPrepared )
|
144
|
{
|
145
|
prepareFull();
|
146
|
mFullPrepared = true;
|
147
|
}
|
148
|
|
149
|
return mFullGLSL + "{}";
|
150
|
}
|
151
|
|
152
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
153
|
/**
|
154
|
* Only for use by the library itself.
|
155
|
*
|
156
|
* @y.exclude
|
157
|
*/
|
158
|
public static int getAllEnabled()
|
159
|
{
|
160
|
if( !mFullPrepared )
|
161
|
{
|
162
|
prepareFull();
|
163
|
mFullPrepared = true;
|
164
|
}
|
165
|
|
166
|
return mFullEnabled;
|
167
|
}
|
168
|
|
169
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
170
|
|
171
|
static void destroyStatics()
|
172
|
{
|
173
|
mNumEnabled = 0;
|
174
|
mGLSL = "";
|
175
|
mFullEnabled= 0;
|
176
|
mFullGLSL = "";
|
177
|
mFullPrepared = false;
|
178
|
}
|
179
|
|
180
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
181
|
// PUBLIC API
|
182
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
183
|
/**
|
184
|
* Return the number of Vertex effects enabled.
|
185
|
*/
|
186
|
public static int getNumEnabled()
|
187
|
{
|
188
|
return mNumEnabled;
|
189
|
}
|
190
|
|
191
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
192
|
/**
|
193
|
* Set Mesh association.
|
194
|
*
|
195
|
* This creates an association between this Vertex Effect and a Component of a Mesh.
|
196
|
* One can set the association of an Effect and of a Component, and the Effect will only be active on
|
197
|
* vertices of Components such that
|
198
|
*
|
199
|
* (effect assoc) & (component assoc) != 0 || (effect component) == (mesh component)
|
200
|
*
|
201
|
* (see main_vertex_shader)
|
202
|
*
|
203
|
* The point: this way we can configure the system so that each Vertex Effect acts only on a certain
|
204
|
* subset of a Mesh, thus potentially significantly reducing the number of render calls.
|
205
|
*/
|
206
|
public void setMeshAssociation(int component, int association)
|
207
|
{
|
208
|
mComponent = component;
|
209
|
mAssociation = association;
|
210
|
}
|
211
|
}
|