1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2016 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;
|
21
|
|
22
|
import java.io.BufferedReader;
|
23
|
import java.io.IOException;
|
24
|
import java.io.InputStream;
|
25
|
import java.io.InputStreamReader;
|
26
|
|
27
|
import android.content.Context;
|
28
|
import android.opengl.GLES20;
|
29
|
import android.os.Build;
|
30
|
|
31
|
import org.distorted.library.exception.*;
|
32
|
|
33
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
34
|
/**
|
35
|
* A singleton class used to control various global settings.
|
36
|
*/
|
37
|
public class Distorted
|
38
|
{
|
39
|
/**
|
40
|
* When creating an instance of a DistortedObject from another instance, do not clone anything.
|
41
|
* Used in the copy constructor.
|
42
|
*/
|
43
|
public static final int CLONE_NOTHING = 0x0;
|
44
|
/**
|
45
|
* When creating an instance of a DistortedObject from another instance, clone the Bitmap that's
|
46
|
* backing up our DistortedObject.
|
47
|
* <p>
|
48
|
* This way we can have two DistortedObjects, both backed up by the same Bitmap, to which we can
|
49
|
* apply different effects. Used in the copy constructor.
|
50
|
*/
|
51
|
public static final int CLONE_BITMAP = 0x1;
|
52
|
/**
|
53
|
* When creating an instance of a DistortedObject from another instance, clone the Matrix Effects.
|
54
|
* <p>
|
55
|
* This way we can have two different DistortedObjects with different Bitmaps behind them, both
|
56
|
* always displayed in exactly the same place on the screen. Applying any matrix-based effect to
|
57
|
* one of them automatically applies the effect to the other. Used in the copy constructor.
|
58
|
*/
|
59
|
public static final int CLONE_MATRIX = 0x2;
|
60
|
/**
|
61
|
* When creating an instance of a DistortedObject from another instance, clone the Vertex Effects.
|
62
|
* <p>
|
63
|
* This way we can have two different DistortedObjects with different Bitmaps behind them, both
|
64
|
* always with the same Vertex effects. Applying any vertex-based effect to one of them automatically
|
65
|
* applies the effect to the other. Used in the copy constructor.
|
66
|
*/
|
67
|
public static final int CLONE_VERTEX = 0x4;
|
68
|
/**
|
69
|
* When creating an instance of a DistortedObject from another instance, clone the Fragment Effects.
|
70
|
* <p>
|
71
|
* This way we can have two different DistortedObjects with different Bitmaps behind them, both
|
72
|
* always with the same Fragment effects. Applying any fragment-based effect to one of them automatically
|
73
|
* applies the effect to the other. Used in the copy constructor.
|
74
|
*/
|
75
|
public static final int CLONE_FRAGMENT= 0x8;
|
76
|
/**
|
77
|
* When creating an instance of a DistortedTree from another instance, clone the children Nodes.
|
78
|
* <p>
|
79
|
* This is mainly useful for creating many similar sub-trees of Bitmaps and rendering then at different places
|
80
|
* on the screen, with (optionally) different effects of the top-level root Bitmap.
|
81
|
*/
|
82
|
public static final int CLONE_CHILDREN= 0x10;
|
83
|
|
84
|
private static boolean mInitialized = false;
|
85
|
|
86
|
static int mPositionH; // model position information
|
87
|
static int mNormalH; // model normal information.
|
88
|
static int mTextureCoordH; // model texture coordinate information.
|
89
|
|
90
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
91
|
|
92
|
private Distorted()
|
93
|
{
|
94
|
|
95
|
}
|
96
|
|
97
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
98
|
|
99
|
private static void sanitizeMaxValues() throws VertexUniformsException,FragmentUniformsException
|
100
|
{
|
101
|
int maxV,maxF;
|
102
|
int[] param = new int[1];
|
103
|
|
104
|
GLES20.glGetIntegerv(GLES20.GL_MAX_VERTEX_UNIFORM_VECTORS, param, 0);
|
105
|
maxV = param[0];
|
106
|
GLES20.glGetIntegerv(GLES20.GL_MAX_FRAGMENT_UNIFORM_VECTORS, param, 0);
|
107
|
maxF = param[0];
|
108
|
|
109
|
//Log.d("Distorted", "Max vectors in vertex shader: "+maxV);
|
110
|
//Log.d("Distorted", "Max vectors in fragment shader: "+maxF);
|
111
|
|
112
|
if( !Build.FINGERPRINT.contains("generic") )
|
113
|
{
|
114
|
int realMaxV = (maxV-11)/4; // adjust this in case of changes to the shaders...
|
115
|
int realMaxF = (maxF- 2)/4; //
|
116
|
|
117
|
if( getMaxVertex() > realMaxV )
|
118
|
{
|
119
|
throw new VertexUniformsException("Too many effects in the vertex shader, max is "+realMaxV, realMaxV);
|
120
|
}
|
121
|
if( getMaxFragment() > realMaxF )
|
122
|
{
|
123
|
throw new FragmentUniformsException("Too many effects in the fragment shader, max is "+realMaxF, realMaxF);
|
124
|
}
|
125
|
}
|
126
|
}
|
127
|
|
128
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
129
|
|
130
|
private static int compileShader(final int shaderType, final String shaderSource) throws FragmentCompilationException,VertexCompilationException
|
131
|
{
|
132
|
int shaderHandle = GLES20.glCreateShader(shaderType);
|
133
|
|
134
|
if (shaderHandle != 0)
|
135
|
{
|
136
|
GLES20.glShaderSource(shaderHandle, "#version 100 \n"+ generateShaderHeader(shaderType) + shaderSource);
|
137
|
GLES20.glCompileShader(shaderHandle);
|
138
|
final int[] compileStatus = new int[1];
|
139
|
GLES20.glGetShaderiv(shaderHandle, GLES20.GL_COMPILE_STATUS, compileStatus, 0);
|
140
|
|
141
|
if (compileStatus[0] != GLES20.GL_TRUE )
|
142
|
{
|
143
|
GLES20.glDeleteShader(shaderHandle);
|
144
|
shaderHandle = 0;
|
145
|
}
|
146
|
}
|
147
|
|
148
|
if (shaderHandle == 0)
|
149
|
{
|
150
|
String error = GLES20.glGetShaderInfoLog(shaderHandle);
|
151
|
|
152
|
switch(shaderType)
|
153
|
{
|
154
|
case GLES20.GL_VERTEX_SHADER : throw new VertexCompilationException(error);
|
155
|
case GLES20.GL_FRAGMENT_SHADER: throw new FragmentCompilationException(error);
|
156
|
default : throw new RuntimeException(error);
|
157
|
}
|
158
|
}
|
159
|
|
160
|
return shaderHandle;
|
161
|
}
|
162
|
|
163
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
164
|
|
165
|
private static int createAndLinkProgram(final int vertexShaderHandle, final int fragmentShaderHandle, final String[] attributes) throws LinkingException
|
166
|
{
|
167
|
int programHandle = GLES20.glCreateProgram();
|
168
|
|
169
|
if (programHandle != 0)
|
170
|
{
|
171
|
GLES20.glAttachShader(programHandle, vertexShaderHandle);
|
172
|
GLES20.glAttachShader(programHandle, fragmentShaderHandle);
|
173
|
|
174
|
if (attributes != null)
|
175
|
{
|
176
|
final int size = attributes.length;
|
177
|
|
178
|
for (int i = 0; i < size; i++)
|
179
|
{
|
180
|
GLES20.glBindAttribLocation(programHandle, i, attributes[i]);
|
181
|
}
|
182
|
}
|
183
|
|
184
|
GLES20.glLinkProgram(programHandle);
|
185
|
|
186
|
final int[] linkStatus = new int[1];
|
187
|
GLES20.glGetProgramiv(programHandle, GLES20.GL_LINK_STATUS, linkStatus, 0);
|
188
|
|
189
|
if (linkStatus[0] != GLES20.GL_TRUE )
|
190
|
{
|
191
|
String error = GLES20.glGetProgramInfoLog(programHandle);
|
192
|
GLES20.glDeleteProgram(programHandle);
|
193
|
throw new LinkingException(error);
|
194
|
}
|
195
|
|
196
|
final int[] numberOfUniforms = new int[1];
|
197
|
GLES20.glGetProgramiv(programHandle, GLES20.GL_ACTIVE_UNIFORMS, numberOfUniforms, 0);
|
198
|
|
199
|
//android.util.Log.d("Distorted", "number of active uniforms="+numberOfUniforms[0]);
|
200
|
}
|
201
|
|
202
|
return programHandle;
|
203
|
}
|
204
|
|
205
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
206
|
|
207
|
private static String generateShaderHeader(final int type)
|
208
|
{
|
209
|
String header="";
|
210
|
|
211
|
switch(type)
|
212
|
{
|
213
|
case GLES20.GL_VERTEX_SHADER : header += ("#define NUM_VERTEX " + getMaxVertex()+"\n");
|
214
|
|
215
|
for(EffectNames name: EffectNames.values() )
|
216
|
{
|
217
|
if( name.getType()==EffectTypes.VERTEX)
|
218
|
header += ("#define "+name.name()+" "+name.ordinal()+"\n");
|
219
|
}
|
220
|
break;
|
221
|
case GLES20.GL_FRAGMENT_SHADER: header += ("#define NUM_FRAGMENT "+ getMaxFragment()+"\n");
|
222
|
|
223
|
for(EffectNames name: EffectNames.values() )
|
224
|
{
|
225
|
if( name.getType()==EffectTypes.FRAGMENT)
|
226
|
header += ("#define "+name.name()+" "+name.ordinal()+"\n");
|
227
|
}
|
228
|
break;
|
229
|
}
|
230
|
|
231
|
//Log.d(TAG,""+header);
|
232
|
|
233
|
return header;
|
234
|
}
|
235
|
|
236
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
237
|
|
238
|
private static String readTextFileFromRawResource(final Context c, final int resourceId)
|
239
|
{
|
240
|
final InputStream inputStream = c.getResources().openRawResource(resourceId);
|
241
|
final InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
|
242
|
final BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
|
243
|
|
244
|
String nextLine;
|
245
|
final StringBuilder body = new StringBuilder();
|
246
|
|
247
|
try
|
248
|
{
|
249
|
while ((nextLine = bufferedReader.readLine()) != null)
|
250
|
{
|
251
|
body.append(nextLine);
|
252
|
body.append('\n');
|
253
|
}
|
254
|
}
|
255
|
catch (IOException e)
|
256
|
{
|
257
|
return null;
|
258
|
}
|
259
|
|
260
|
return body.toString();
|
261
|
}
|
262
|
|
263
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
264
|
|
265
|
private static String getVertexShader(final Context c)
|
266
|
{
|
267
|
return readTextFileFromRawResource( c, R.raw.main_vertex_shader);
|
268
|
}
|
269
|
|
270
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
271
|
|
272
|
private static String getFragmentShader(final Context c)
|
273
|
{
|
274
|
return readTextFileFromRawResource( c, R.raw.main_fragment_shader);
|
275
|
}
|
276
|
|
277
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
278
|
|
279
|
static boolean isInitialized()
|
280
|
{
|
281
|
return mInitialized;
|
282
|
}
|
283
|
|
284
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
285
|
/**
|
286
|
* When OpenGL context gets created, you need to call this method so that the library can initialise its internal data structures.
|
287
|
* I.e. best called from GLSurfaceView.onSurfaceCreated().
|
288
|
* <p>
|
289
|
* Compiles the vertex and fragment shaders, establishes the addresses of all uniforms, and initialises all Bitmaps that have already
|
290
|
* been created.
|
291
|
*
|
292
|
* @param context Context of the App using the library - used to open up Resources and read Shader code.
|
293
|
* @throws FragmentCompilationException
|
294
|
* @throws VertexCompilationException
|
295
|
* @throws VertexUniformsException
|
296
|
* @throws FragmentUniformsException
|
297
|
* @throws LinkingException
|
298
|
*/
|
299
|
public static void onSurfaceCreated(final Context context) throws FragmentCompilationException,VertexCompilationException,VertexUniformsException,FragmentUniformsException,LinkingException
|
300
|
{
|
301
|
mInitialized = true;
|
302
|
|
303
|
final String vertexShader = Distorted.getVertexShader(context);
|
304
|
final String fragmentShader = Distorted.getFragmentShader(context);
|
305
|
|
306
|
sanitizeMaxValues();
|
307
|
|
308
|
final int vertexShaderHandle = compileShader(GLES20.GL_VERTEX_SHADER , vertexShader );
|
309
|
final int fragmentShaderHandle = compileShader(GLES20.GL_FRAGMENT_SHADER, fragmentShader);
|
310
|
|
311
|
int programH = createAndLinkProgram(vertexShaderHandle, fragmentShaderHandle, new String[] {"a_Position", "a_Color", "a_Normal", "a_TexCoordinate"});
|
312
|
|
313
|
GLES20.glUseProgram(programH);
|
314
|
|
315
|
int textureUniformH = GLES20.glGetUniformLocation(programH, "u_Texture");
|
316
|
mPositionH = GLES20.glGetAttribLocation( programH, "a_Position");
|
317
|
mNormalH = GLES20.glGetAttribLocation( programH, "a_Normal");
|
318
|
mTextureCoordH = GLES20.glGetAttribLocation( programH, "a_TexCoordinate");
|
319
|
|
320
|
GLES20.glEnable (GLES20.GL_DEPTH_TEST);
|
321
|
GLES20.glDepthFunc(GLES20.GL_LEQUAL);
|
322
|
GLES20.glEnable(GLES20.GL_BLEND);
|
323
|
GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);
|
324
|
GLES20.glEnable(GLES20.GL_CULL_FACE);
|
325
|
GLES20.glCullFace(GLES20.GL_BACK);
|
326
|
GLES20.glFrontFace(GLES20.GL_CW);
|
327
|
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
|
328
|
GLES20.glUniform1i(textureUniformH, 0);
|
329
|
|
330
|
EffectQueueFragment.getUniforms(programH);
|
331
|
EffectQueueVertex.getUniforms(programH);
|
332
|
EffectQueueMatrix.getUniforms(programH);
|
333
|
|
334
|
GLES20.glEnableVertexAttribArray(mPositionH);
|
335
|
GLES20.glEnableVertexAttribArray(mNormalH);
|
336
|
GLES20.glEnableVertexAttribArray(mTextureCoordH);
|
337
|
|
338
|
DistortedTree.reset();
|
339
|
EffectMessageSender.startSending();
|
340
|
}
|
341
|
|
342
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
343
|
/**
|
344
|
* Call this so that the Library can release its internal data structures.
|
345
|
* Must be called from Activity.onDestroy().
|
346
|
*/
|
347
|
public static void onDestroy()
|
348
|
{
|
349
|
DistortedTexture.onDestroy();
|
350
|
DistortedFramebuffer.onDestroy();
|
351
|
DistortedTree.onDestroy();
|
352
|
EffectQueue.onDestroy();
|
353
|
DistortedEffects.onDestroy();
|
354
|
EffectMessageSender.stopSending();
|
355
|
|
356
|
mInitialized = false;
|
357
|
}
|
358
|
|
359
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
360
|
/**
|
361
|
* Returns the maximum number of Matrix effects.
|
362
|
*
|
363
|
* @return The maximum number of Matrix effects
|
364
|
*/
|
365
|
public static int getMaxMatrix()
|
366
|
{
|
367
|
return EffectQueue.getMax(EffectTypes.MATRIX.ordinal());
|
368
|
}
|
369
|
|
370
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
371
|
/**
|
372
|
* Returns the maximum number of Vertex effects.
|
373
|
*
|
374
|
* @return The maximum number of Vertex effects
|
375
|
*/
|
376
|
public static int getMaxVertex()
|
377
|
{
|
378
|
return EffectQueue.getMax(EffectTypes.VERTEX.ordinal());
|
379
|
}
|
380
|
|
381
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
382
|
/**
|
383
|
* Returns the maximum number of Fragment effects.
|
384
|
*
|
385
|
* @return The maximum number of Fragment effects
|
386
|
*/
|
387
|
public static int getMaxFragment()
|
388
|
{
|
389
|
return EffectQueue.getMax(EffectTypes.FRAGMENT.ordinal());
|
390
|
}
|
391
|
|
392
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
393
|
/**
|
394
|
* Sets the maximum number of Matrix effects that can be applied to a single DistortedObject at one time.
|
395
|
* This can fail if:
|
396
|
* <ul>
|
397
|
* <li>the value of 'max' is outside permitted range (0 ≤ max ≤ Byte.MAX_VALUE)
|
398
|
* <li>We try to increase the value of 'max' when it is too late to do so already. It needs to be called
|
399
|
* before the Vertex Shader gets compiled, i.e. before the call to {@link #onSurfaceCreated}. After this
|
400
|
* time only decreasing the value of 'max' is permitted.
|
401
|
* <li>Furthermore, this needs to be called before any DistortedObject gets created.
|
402
|
* </ul>
|
403
|
*
|
404
|
* @param max new maximum number of simultaneous Matrix Effects. Has to be a non-negative number not greater
|
405
|
* than Byte.MAX_VALUE
|
406
|
* @return <code>true</code> if operation was successful, <code>false</code> otherwise.
|
407
|
*/
|
408
|
public static boolean setMaxMatrix(int max)
|
409
|
{
|
410
|
return EffectQueue.setMax(EffectTypes.MATRIX.ordinal(),max);
|
411
|
}
|
412
|
|
413
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
414
|
/**
|
415
|
* Sets the maximum number of Vertex effects that can be applied to a single DistortedObject at one time.
|
416
|
* This can fail if:
|
417
|
* <ul>
|
418
|
* <li>the value of 'max' is outside permitted range (0 ≤ max ≤ Byte.MAX_VALUE)
|
419
|
* <li>We try to increase the value of 'max' when it is too late to do so already. It needs to be called
|
420
|
* before the Vertex Shader gets compiled, i.e. before the call to {@link #onSurfaceCreated}. After this
|
421
|
* time only decreasing the value of 'max' is permitted.
|
422
|
* <li>Furthermore, this needs to be called before any DistortedObject gets created.
|
423
|
* </ul>
|
424
|
*
|
425
|
* @param max new maximum number of simultaneous Vertex Effects. Has to be a non-negative number not greater
|
426
|
* than Byte.MAX_VALUE
|
427
|
* @return <code>true</code> if operation was successful, <code>false</code> otherwise.
|
428
|
*/
|
429
|
public static boolean setMaxVertex(int max)
|
430
|
{
|
431
|
return EffectQueue.setMax(EffectTypes.VERTEX.ordinal(),max);
|
432
|
}
|
433
|
|
434
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
435
|
/**
|
436
|
* Sets the maximum number of Fragment effects that can be applied to a single DistortedObject at one time.
|
437
|
* This can fail if:
|
438
|
* <ul>
|
439
|
* <li>the value of 'max' is outside permitted range (0 ≤ max ≤ Byte.MAX_VALUE)
|
440
|
* <li>We try to increase the value of 'max' when it is too late to do so already. It needs to be called
|
441
|
* before the Fragment Shader gets compiled, i.e. before the call to {@link #onSurfaceCreated}. After this
|
442
|
* time only decreasing the value of 'max' is permitted.
|
443
|
* <li>Furthermore, this needs to be called before any DistortedObject gets created.
|
444
|
* </ul>
|
445
|
*
|
446
|
* @param max new maximum number of simultaneous Fragment Effects. Has to be a non-negative number not greater
|
447
|
* than Byte.MAX_VALUE
|
448
|
* @return <code>true</code> if operation was successful, <code>false</code> otherwise.
|
449
|
*/
|
450
|
public static boolean setMaxFragment(int max)
|
451
|
{
|
452
|
return EffectQueue.setMax(EffectTypes.FRAGMENT.ordinal(),max);
|
453
|
}
|
454
|
}
|