Revision 432442f9
Added by Leszek Koltunski almost 8 years ago
src/main/java/org/distorted/library/Distorted.java | ||
---|---|---|
19 | 19 |
|
20 | 20 |
package org.distorted.library; |
21 | 21 |
|
22 |
import java.io.BufferedReader; |
|
23 |
import java.io.IOException; |
|
24 |
import java.io.InputStream; |
|
25 |
import java.io.InputStreamReader; |
|
26 |
|
|
27 | 22 |
import android.content.Context; |
28 | 23 |
import android.opengl.GLES20; |
29 |
import android.os.Build;
|
|
24 |
import org.distorted.library.program.*;
|
|
30 | 25 |
|
31 |
import org.distorted.library.exception.*;
|
|
26 |
import java.io.InputStream;
|
|
32 | 27 |
|
33 | 28 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
34 | 29 |
/** |
... | ... | |
77 | 72 |
|
78 | 73 |
private static boolean mInitialized = false; |
79 | 74 |
|
80 |
static int mPositionH; // model position information |
|
81 |
static int mNormalH; // model normal information. |
|
82 |
static int mTextureCoordH; // model texture coordinate information. |
|
75 |
static int[] mAttributes; |
|
83 | 76 |
|
84 | 77 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
85 | 78 |
|
... | ... | |
88 | 81 |
|
89 | 82 |
} |
90 | 83 |
|
91 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
92 |
|
|
93 |
private static void sanitizeMaxValues() throws VertexUniformsException,FragmentUniformsException |
|
94 |
{ |
|
95 |
int maxV,maxF; |
|
96 |
int[] param = new int[1]; |
|
97 |
|
|
98 |
GLES20.glGetIntegerv(GLES20.GL_MAX_VERTEX_UNIFORM_VECTORS, param, 0); |
|
99 |
maxV = param[0]; |
|
100 |
GLES20.glGetIntegerv(GLES20.GL_MAX_FRAGMENT_UNIFORM_VECTORS, param, 0); |
|
101 |
maxF = param[0]; |
|
102 |
|
|
103 |
//Log.d("Distorted", "Max vectors in vertex shader: "+maxV); |
|
104 |
//Log.d("Distorted", "Max vectors in fragment shader: "+maxF); |
|
105 |
|
|
106 |
if( !Build.FINGERPRINT.contains("generic") ) |
|
107 |
{ |
|
108 |
int realMaxV = (maxV-11)/4; // adjust this in case of changes to the shaders... |
|
109 |
int realMaxF = (maxF- 2)/4; // |
|
110 |
|
|
111 |
if( getMaxVertex() > realMaxV ) |
|
112 |
{ |
|
113 |
throw new VertexUniformsException("Too many effects in the vertex shader, max is "+realMaxV, realMaxV); |
|
114 |
} |
|
115 |
if( getMaxFragment() > realMaxF ) |
|
116 |
{ |
|
117 |
throw new FragmentUniformsException("Too many effects in the fragment shader, max is "+realMaxF, realMaxF); |
|
118 |
} |
|
119 |
} |
|
120 |
} |
|
121 |
|
|
122 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
123 |
|
|
124 |
private static int compileShader(final int shaderType, final String shaderSource) throws FragmentCompilationException,VertexCompilationException |
|
125 |
{ |
|
126 |
int shaderHandle = GLES20.glCreateShader(shaderType); |
|
127 |
|
|
128 |
if (shaderHandle != 0) |
|
129 |
{ |
|
130 |
GLES20.glShaderSource(shaderHandle, "#version 100 \n"+ generateShaderHeader(shaderType) + shaderSource); |
|
131 |
GLES20.glCompileShader(shaderHandle); |
|
132 |
final int[] compileStatus = new int[1]; |
|
133 |
GLES20.glGetShaderiv(shaderHandle, GLES20.GL_COMPILE_STATUS, compileStatus, 0); |
|
134 |
|
|
135 |
if (compileStatus[0] != GLES20.GL_TRUE ) |
|
136 |
{ |
|
137 |
GLES20.glDeleteShader(shaderHandle); |
|
138 |
shaderHandle = 0; |
|
139 |
} |
|
140 |
} |
|
141 |
|
|
142 |
if (shaderHandle == 0) |
|
143 |
{ |
|
144 |
String error = GLES20.glGetShaderInfoLog(shaderHandle); |
|
145 |
|
|
146 |
switch(shaderType) |
|
147 |
{ |
|
148 |
case GLES20.GL_VERTEX_SHADER : throw new VertexCompilationException(error); |
|
149 |
case GLES20.GL_FRAGMENT_SHADER: throw new FragmentCompilationException(error); |
|
150 |
default : throw new RuntimeException(error); |
|
151 |
} |
|
152 |
} |
|
153 |
|
|
154 |
return shaderHandle; |
|
155 |
} |
|
156 |
|
|
157 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
158 |
|
|
159 |
private static int createAndLinkProgram(final int vertexShaderHandle, final int fragmentShaderHandle, final String[] attributes) throws LinkingException |
|
160 |
{ |
|
161 |
int programHandle = GLES20.glCreateProgram(); |
|
162 |
|
|
163 |
if (programHandle != 0) |
|
164 |
{ |
|
165 |
GLES20.glAttachShader(programHandle, vertexShaderHandle); |
|
166 |
GLES20.glAttachShader(programHandle, fragmentShaderHandle); |
|
167 |
|
|
168 |
if (attributes != null) |
|
169 |
{ |
|
170 |
final int size = attributes.length; |
|
171 |
|
|
172 |
for (int i = 0; i < size; i++) |
|
173 |
{ |
|
174 |
GLES20.glBindAttribLocation(programHandle, i, attributes[i]); |
|
175 |
} |
|
176 |
} |
|
177 |
|
|
178 |
GLES20.glLinkProgram(programHandle); |
|
179 |
|
|
180 |
final int[] linkStatus = new int[1]; |
|
181 |
GLES20.glGetProgramiv(programHandle, GLES20.GL_LINK_STATUS, linkStatus, 0); |
|
182 |
|
|
183 |
if (linkStatus[0] != GLES20.GL_TRUE ) |
|
184 |
{ |
|
185 |
String error = GLES20.glGetProgramInfoLog(programHandle); |
|
186 |
GLES20.glDeleteProgram(programHandle); |
|
187 |
throw new LinkingException(error); |
|
188 |
} |
|
189 |
|
|
190 |
final int[] numberOfUniforms = new int[1]; |
|
191 |
GLES20.glGetProgramiv(programHandle, GLES20.GL_ACTIVE_UNIFORMS, numberOfUniforms, 0); |
|
192 |
|
|
193 |
//android.util.Log.d("Distorted", "number of active uniforms="+numberOfUniforms[0]); |
|
194 |
} |
|
195 |
|
|
196 |
return programHandle; |
|
197 |
} |
|
198 |
|
|
199 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
200 |
|
|
201 |
private static String generateShaderHeader(final int type) |
|
202 |
{ |
|
203 |
String header=""; |
|
204 |
|
|
205 |
switch(type) |
|
206 |
{ |
|
207 |
case GLES20.GL_VERTEX_SHADER : header += ("#define NUM_VERTEX " + getMaxVertex()+"\n"); |
|
208 |
|
|
209 |
for(EffectNames name: EffectNames.values() ) |
|
210 |
{ |
|
211 |
if( name.getType()==EffectTypes.VERTEX) |
|
212 |
header += ("#define "+name.name()+" "+name.ordinal()+"\n"); |
|
213 |
} |
|
214 |
break; |
|
215 |
case GLES20.GL_FRAGMENT_SHADER: header += ("#define NUM_FRAGMENT "+ getMaxFragment()+"\n"); |
|
216 |
|
|
217 |
for(EffectNames name: EffectNames.values() ) |
|
218 |
{ |
|
219 |
if( name.getType()==EffectTypes.FRAGMENT) |
|
220 |
header += ("#define "+name.name()+" "+name.ordinal()+"\n"); |
|
221 |
} |
|
222 |
break; |
|
223 |
} |
|
224 |
|
|
225 |
//Log.d(TAG,""+header); |
|
226 |
|
|
227 |
return header; |
|
228 |
} |
|
229 |
|
|
230 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
231 |
|
|
232 |
private static String readTextFileFromRawResource(final Context c, final int resourceId) |
|
233 |
{ |
|
234 |
final InputStream inputStream = c.getResources().openRawResource(resourceId); |
|
235 |
final InputStreamReader inputStreamReader = new InputStreamReader(inputStream); |
|
236 |
final BufferedReader bufferedReader = new BufferedReader(inputStreamReader); |
|
237 |
|
|
238 |
String nextLine; |
|
239 |
final StringBuilder body = new StringBuilder(); |
|
240 |
|
|
241 |
try |
|
242 |
{ |
|
243 |
while ((nextLine = bufferedReader.readLine()) != null) |
|
244 |
{ |
|
245 |
body.append(nextLine); |
|
246 |
body.append('\n'); |
|
247 |
} |
|
248 |
} |
|
249 |
catch (IOException e) |
|
250 |
{ |
|
251 |
return null; |
|
252 |
} |
|
253 |
|
|
254 |
return body.toString(); |
|
255 |
} |
|
256 |
|
|
257 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
258 |
|
|
259 |
private static String getVertexShader(final Context c) |
|
260 |
{ |
|
261 |
return readTextFileFromRawResource( c, R.raw.main_vertex_shader); |
|
262 |
} |
|
263 |
|
|
264 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
265 |
|
|
266 |
private static String getFragmentShader(final Context c) |
|
267 |
{ |
|
268 |
return readTextFileFromRawResource( c, R.raw.main_fragment_shader); |
|
269 |
} |
|
270 |
|
|
271 | 84 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
272 | 85 |
|
273 | 86 |
static boolean isInitialized() |
... | ... | |
278 | 91 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
279 | 92 |
/** |
280 | 93 |
* When OpenGL context gets created, you need to call this method so that the library can initialise its internal data structures. |
281 |
* I.e. best called from GLSurfaceView.onSurfaceCreated().
|
|
94 |
* I.e. best called from GLSurfaceView.onCreate().
|
|
282 | 95 |
* <p> |
283 |
* Compiles the vertex and fragment shaders, establishes the addresses of all uniforms.
|
|
96 |
* Needs to be called from a thread holding the OpenGL context.
|
|
284 | 97 |
* |
285 | 98 |
* @param context Context of the App using the library - used to open up Resources and read Shader code. |
286 | 99 |
* @throws FragmentCompilationException |
... | ... | |
289 | 102 |
* @throws FragmentUniformsException |
290 | 103 |
* @throws LinkingException |
291 | 104 |
*/ |
292 |
public static void onSurfaceCreated(final Context context) throws FragmentCompilationException,VertexCompilationException,VertexUniformsException,FragmentUniformsException,LinkingException |
|
105 |
public static void onCreate(final Context context) |
|
106 |
throws FragmentCompilationException,VertexCompilationException,VertexUniformsException,FragmentUniformsException,LinkingException |
|
293 | 107 |
{ |
294 | 108 |
mInitialized = true; |
295 | 109 |
|
296 |
final String vertexShader = Distorted.getVertexShader(context); |
|
297 |
final String fragmentShader = Distorted.getFragmentShader(context); |
|
298 |
|
|
299 |
sanitizeMaxValues(); |
|
300 |
|
|
301 |
final int vertexShaderHandle = compileShader(GLES20.GL_VERTEX_SHADER , vertexShader ); |
|
302 |
final int fragmentShaderHandle = compileShader(GLES20.GL_FRAGMENT_SHADER, fragmentShader); |
|
303 |
|
|
304 |
int programH = createAndLinkProgram(vertexShaderHandle, fragmentShaderHandle, new String[] {"a_Position", "a_Color", "a_Normal", "a_TexCoordinate"}); |
|
110 |
final InputStream vertexStream = context.getResources().openRawResource(R.raw.main_vertex_shader); |
|
111 |
final InputStream fragmentStream = context.getResources().openRawResource(R.raw.main_fragment_shader); |
|
305 | 112 |
|
113 |
DistortedProgram mainProgram = new DistortedProgram(vertexStream,fragmentStream); |
|
114 |
int programH = mainProgram.getProgramHandle(); |
|
306 | 115 |
GLES20.glUseProgram(programH); |
116 |
mainProgram.bindAndEnableAttributes(); |
|
117 |
mAttributes = mainProgram.getAttributes(); |
|
307 | 118 |
|
308 | 119 |
int textureUniformH = GLES20.glGetUniformLocation(programH, "u_Texture"); |
309 |
mPositionH = GLES20.glGetAttribLocation( programH, "a_Position"); |
|
310 |
mNormalH = GLES20.glGetAttribLocation( programH, "a_Normal"); |
|
311 |
mTextureCoordH = GLES20.glGetAttribLocation( programH, "a_TexCoordinate"); |
|
312 | 120 |
|
313 | 121 |
GLES20.glEnable (GLES20.GL_DEPTH_TEST); |
314 | 122 |
GLES20.glDepthFunc(GLES20.GL_LEQUAL); |
... | ... | |
323 | 131 |
EffectQueueFragment.getUniforms(programH); |
324 | 132 |
EffectQueueVertex.getUniforms(programH); |
325 | 133 |
EffectQueueMatrix.getUniforms(programH); |
326 |
|
|
327 |
GLES20.glEnableVertexAttribArray(mPositionH); |
|
328 |
GLES20.glEnableVertexAttribArray(mNormalH); |
|
329 |
GLES20.glEnableVertexAttribArray(mTextureCoordH); |
|
330 |
|
|
134 |
|
|
331 | 135 |
DistortedTree.reset(); |
332 | 136 |
EffectMessageSender.startSending(); |
333 | 137 |
} |
... | ... | |
348 | 152 |
|
349 | 153 |
mInitialized = false; |
350 | 154 |
} |
351 |
|
|
352 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
353 |
/** |
|
354 |
* Returns the maximum number of Matrix effects. |
|
355 |
* |
|
356 |
* @return The maximum number of Matrix effects |
|
357 |
*/ |
|
358 |
public static int getMaxMatrix() |
|
359 |
{ |
|
360 |
return EffectQueue.getMax(EffectTypes.MATRIX.ordinal()); |
|
361 |
} |
|
362 |
|
|
363 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
364 |
/** |
|
365 |
* Returns the maximum number of Vertex effects. |
|
366 |
* |
|
367 |
* @return The maximum number of Vertex effects |
|
368 |
*/ |
|
369 |
public static int getMaxVertex() |
|
370 |
{ |
|
371 |
return EffectQueue.getMax(EffectTypes.VERTEX.ordinal()); |
|
372 |
} |
|
373 |
|
|
374 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
375 |
/** |
|
376 |
* Returns the maximum number of Fragment effects. |
|
377 |
* |
|
378 |
* @return The maximum number of Fragment effects |
|
379 |
*/ |
|
380 |
public static int getMaxFragment() |
|
381 |
{ |
|
382 |
return EffectQueue.getMax(EffectTypes.FRAGMENT.ordinal()); |
|
383 |
} |
|
384 |
|
|
385 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
386 |
/** |
|
387 |
* Returns the maximum number of Postprocess effects. |
|
388 |
* |
|
389 |
* @return The maximum number of Postprocess effects |
|
390 |
*/ |
|
391 |
public static int getMaxPostprocess() |
|
392 |
{ |
|
393 |
return EffectQueue.getMax(EffectTypes.POSTPROCESS.ordinal()); |
|
394 |
} |
|
395 |
|
|
396 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
397 |
/** |
|
398 |
* Sets the maximum number of Matrix effects that can be stored in a single EffectQueue at one time. |
|
399 |
* This can fail if: |
|
400 |
* <ul> |
|
401 |
* <li>the value of 'max' is outside permitted range (0 ≤ max ≤ Byte.MAX_VALUE) |
|
402 |
* <li>We try to increase the value of 'max' when it is too late to do so already. It needs to be called |
|
403 |
* before the Vertex Shader gets compiled, i.e. before the call to {@link #onSurfaceCreated}. After this |
|
404 |
* time only decreasing the value of 'max' is permitted. |
|
405 |
* <li>Furthermore, this needs to be called before any instances of the DistortedEffects class get created. |
|
406 |
* </ul> |
|
407 |
* |
|
408 |
* @param max new maximum number of simultaneous Matrix Effects. Has to be a non-negative number not greater |
|
409 |
* than Byte.MAX_VALUE |
|
410 |
* @return <code>true</code> if operation was successful, <code>false</code> otherwise. |
|
411 |
*/ |
|
412 |
public static boolean setMaxMatrix(int max) |
|
413 |
{ |
|
414 |
return EffectQueue.setMax(EffectTypes.MATRIX.ordinal(),max); |
|
415 |
} |
|
416 |
|
|
417 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
418 |
/** |
|
419 |
* Sets the maximum number of Vertex effects that can be stored in a single EffectQueue at one time. |
|
420 |
* This can fail if: |
|
421 |
* <ul> |
|
422 |
* <li>the value of 'max' is outside permitted range (0 ≤ max ≤ Byte.MAX_VALUE) |
|
423 |
* <li>We try to increase the value of 'max' when it is too late to do so already. It needs to be called |
|
424 |
* before the Vertex Shader gets compiled, i.e. before the call to {@link #onSurfaceCreated}. After this |
|
425 |
* time only decreasing the value of 'max' is permitted. |
|
426 |
* <li>Furthermore, this needs to be called before any instances of the DistortedEffects class get created. |
|
427 |
* </ul> |
|
428 |
* |
|
429 |
* @param max new maximum number of simultaneous Vertex Effects. Has to be a non-negative number not greater |
|
430 |
* than Byte.MAX_VALUE |
|
431 |
* @return <code>true</code> if operation was successful, <code>false</code> otherwise. |
|
432 |
*/ |
|
433 |
public static boolean setMaxVertex(int max) |
|
434 |
{ |
|
435 |
return EffectQueue.setMax(EffectTypes.VERTEX.ordinal(),max); |
|
436 |
} |
|
437 |
|
|
438 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
439 |
/** |
|
440 |
* Sets the maximum number of Fragment effects that can be stored in a single EffectQueue at one time. |
|
441 |
* This can fail if: |
|
442 |
* <ul> |
|
443 |
* <li>the value of 'max' is outside permitted range (0 ≤ max ≤ Byte.MAX_VALUE) |
|
444 |
* <li>We try to increase the value of 'max' when it is too late to do so already. It needs to be called |
|
445 |
* before the Fragment Shader gets compiled, i.e. before the call to {@link #onSurfaceCreated}. After this |
|
446 |
* time only decreasing the value of 'max' is permitted. |
|
447 |
* <li>Furthermore, this needs to be called before any instances of the DistortedEffects class get created. |
|
448 |
* </ul> |
|
449 |
* |
|
450 |
* @param max new maximum number of simultaneous Fragment Effects. Has to be a non-negative number not greater |
|
451 |
* than Byte.MAX_VALUE |
|
452 |
* @return <code>true</code> if operation was successful, <code>false</code> otherwise. |
|
453 |
*/ |
|
454 |
public static boolean setMaxFragment(int max) |
|
455 |
{ |
|
456 |
return EffectQueue.setMax(EffectTypes.FRAGMENT.ordinal(),max); |
|
457 |
} |
|
458 |
|
|
459 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
460 |
/** |
|
461 |
* Sets the maximum number of Postprocess effects that can be stored in a single EffectQueue at one time. |
|
462 |
* This can fail if: |
|
463 |
* <ul> |
|
464 |
* <li>the value of 'max' is outside permitted range (0 ≤ max ≤ Byte.MAX_VALUE) |
|
465 |
* <li>We try to increase the value of 'max' when it is too late to do so already. It needs to be called |
|
466 |
* before the Fragment Shader gets compiled, i.e. before the call to {@link #onSurfaceCreated}. After this |
|
467 |
* time only decreasing the value of 'max' is permitted. |
|
468 |
* <li>Furthermore, this needs to be called before any instances of the DistortedEffects class get created. |
|
469 |
* </ul> |
|
470 |
* |
|
471 |
* @param max new maximum number of simultaneous Postprocess Effects. Has to be a non-negative number not greater |
|
472 |
* than Byte.MAX_VALUE |
|
473 |
* @return <code>true</code> if operation was successful, <code>false</code> otherwise. |
|
474 |
*/ |
|
475 |
public static boolean setMaxPostprocess(int max) |
|
476 |
{ |
|
477 |
return EffectQueue.setMax(EffectTypes.POSTPROCESS.ordinal(),max); |
|
478 |
} |
|
479 | 155 |
} |
src/main/java/org/distorted/library/DistortedEffects.java | ||
---|---|---|
294 | 294 |
|
295 | 295 |
return false; |
296 | 296 |
} |
297 |
|
|
297 |
|
|
298 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
299 |
/** |
|
300 |
* Returns the maximum number of Matrix effects. |
|
301 |
* |
|
302 |
* @return The maximum number of Matrix effects |
|
303 |
*/ |
|
304 |
public static int getMaxMatrix() |
|
305 |
{ |
|
306 |
return EffectQueue.getMax(EffectTypes.MATRIX.ordinal()); |
|
307 |
} |
|
308 |
|
|
309 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
310 |
/** |
|
311 |
* Returns the maximum number of Vertex effects. |
|
312 |
* |
|
313 |
* @return The maximum number of Vertex effects |
|
314 |
*/ |
|
315 |
public static int getMaxVertex() |
|
316 |
{ |
|
317 |
return EffectQueue.getMax(EffectTypes.VERTEX.ordinal()); |
|
318 |
} |
|
319 |
|
|
320 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
321 |
/** |
|
322 |
* Returns the maximum number of Fragment effects. |
|
323 |
* |
|
324 |
* @return The maximum number of Fragment effects |
|
325 |
*/ |
|
326 |
public static int getMaxFragment() |
|
327 |
{ |
|
328 |
return EffectQueue.getMax(EffectTypes.FRAGMENT.ordinal()); |
|
329 |
} |
|
330 |
|
|
331 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
332 |
/** |
|
333 |
* Returns the maximum number of Postprocess effects. |
|
334 |
* |
|
335 |
* @return The maximum number of Postprocess effects |
|
336 |
*/ |
|
337 |
public static int getMaxPostprocess() |
|
338 |
{ |
|
339 |
return EffectQueue.getMax(EffectTypes.POSTPROCESS.ordinal()); |
|
340 |
} |
|
341 |
|
|
342 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
343 |
/** |
|
344 |
* Sets the maximum number of Matrix effects that can be stored in a single EffectQueue at one time. |
|
345 |
* This can fail if: |
|
346 |
* <ul> |
|
347 |
* <li>the value of 'max' is outside permitted range (0 ≤ max ≤ Byte.MAX_VALUE) |
|
348 |
* <li>We try to increase the value of 'max' when it is too late to do so already. It needs to be called |
|
349 |
* before the Vertex Shader gets compiled, i.e. before the call to {@link Distorted#onCreate}. After this |
|
350 |
* time only decreasing the value of 'max' is permitted. |
|
351 |
* <li>Furthermore, this needs to be called before any instances of the DistortedEffects class get created. |
|
352 |
* </ul> |
|
353 |
* |
|
354 |
* @param max new maximum number of simultaneous Matrix Effects. Has to be a non-negative number not greater |
|
355 |
* than Byte.MAX_VALUE |
|
356 |
* @return <code>true</code> if operation was successful, <code>false</code> otherwise. |
|
357 |
*/ |
|
358 |
public static boolean setMaxMatrix(int max) |
|
359 |
{ |
|
360 |
return EffectQueue.setMax(EffectTypes.MATRIX.ordinal(),max); |
|
361 |
} |
|
362 |
|
|
363 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
364 |
/** |
|
365 |
* Sets the maximum number of Vertex effects that can be stored in a single EffectQueue at one time. |
|
366 |
* This can fail if: |
|
367 |
* <ul> |
|
368 |
* <li>the value of 'max' is outside permitted range (0 ≤ max ≤ Byte.MAX_VALUE) |
|
369 |
* <li>We try to increase the value of 'max' when it is too late to do so already. It needs to be called |
|
370 |
* before the Vertex Shader gets compiled, i.e. before the call to {@link Distorted#onCreate}. After this |
|
371 |
* time only decreasing the value of 'max' is permitted. |
|
372 |
* <li>Furthermore, this needs to be called before any instances of the DistortedEffects class get created. |
|
373 |
* </ul> |
|
374 |
* |
|
375 |
* @param max new maximum number of simultaneous Vertex Effects. Has to be a non-negative number not greater |
|
376 |
* than Byte.MAX_VALUE |
|
377 |
* @return <code>true</code> if operation was successful, <code>false</code> otherwise. |
|
378 |
*/ |
|
379 |
public static boolean setMaxVertex(int max) |
|
380 |
{ |
|
381 |
return EffectQueue.setMax(EffectTypes.VERTEX.ordinal(),max); |
|
382 |
} |
|
383 |
|
|
384 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
385 |
/** |
|
386 |
* Sets the maximum number of Fragment effects that can be stored in a single EffectQueue at one time. |
|
387 |
* This can fail if: |
|
388 |
* <ul> |
|
389 |
* <li>the value of 'max' is outside permitted range (0 ≤ max ≤ Byte.MAX_VALUE) |
|
390 |
* <li>We try to increase the value of 'max' when it is too late to do so already. It needs to be called |
|
391 |
* before the Fragment Shader gets compiled, i.e. before the call to {@link Distorted#onCreate}. After this |
|
392 |
* time only decreasing the value of 'max' is permitted. |
|
393 |
* <li>Furthermore, this needs to be called before any instances of the DistortedEffects class get created. |
|
394 |
* </ul> |
|
395 |
* |
|
396 |
* @param max new maximum number of simultaneous Fragment Effects. Has to be a non-negative number not greater |
|
397 |
* than Byte.MAX_VALUE |
|
398 |
* @return <code>true</code> if operation was successful, <code>false</code> otherwise. |
|
399 |
*/ |
|
400 |
public static boolean setMaxFragment(int max) |
|
401 |
{ |
|
402 |
return EffectQueue.setMax(EffectTypes.FRAGMENT.ordinal(),max); |
|
403 |
} |
|
404 |
|
|
405 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
406 |
/** |
|
407 |
* Sets the maximum number of Postprocess effects that can be stored in a single EffectQueue at one time. |
|
408 |
* This can fail if: |
|
409 |
* <ul> |
|
410 |
* <li>the value of 'max' is outside permitted range (0 ≤ max ≤ Byte.MAX_VALUE) |
|
411 |
* <li>We try to increase the value of 'max' when it is too late to do so already. It needs to be called |
|
412 |
* before the Fragment Shader gets compiled, i.e. before the call to {@link Distorted#onCreate}. After this |
|
413 |
* time only decreasing the value of 'max' is permitted. |
|
414 |
* <li>Furthermore, this needs to be called before any instances of the DistortedEffects class get created. |
|
415 |
* </ul> |
|
416 |
* |
|
417 |
* @param max new maximum number of simultaneous Postprocess Effects. Has to be a non-negative number not greater |
|
418 |
* than Byte.MAX_VALUE |
|
419 |
* @return <code>true</code> if operation was successful, <code>false</code> otherwise. |
|
420 |
*/ |
|
421 |
public static boolean setMaxPostprocess(int max) |
|
422 |
{ |
|
423 |
return EffectQueue.setMax(EffectTypes.POSTPROCESS.ordinal(),max); |
|
424 |
} |
|
425 |
|
|
298 | 426 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
299 | 427 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
300 | 428 |
// Individual effect functions. |
src/main/java/org/distorted/library/DistortedFramebuffer.java | ||
---|---|---|
273 | 273 |
* <p> |
274 | 274 |
* Must be called from a thread holding OpenGL Context. |
275 | 275 |
* |
276 |
* @param fbo The Framebuffer (previously created with the first constructor, drawing FROM the screen
|
|
276 |
* @param fbo The Framebuffer (previously created with the first constructor, drawing FROM the screen |
|
277 | 277 |
* is unsupported!) whose COLOR attachment 0 will be used as input texture. |
278 | 278 |
* Please note that rendering from an FBO to itself is unsupported by OpenGL! |
279 | 279 |
* @param grid Class descendant from GridObject |
src/main/java/org/distorted/library/DistortedTexture.java | ||
---|---|---|
231 | 231 |
* Sets the underlying android.graphics.Bitmap object. |
232 | 232 |
* <p> |
233 | 233 |
* You can only recycle() the passed Bitmap once the OpenGL context gets created (i.e. after call |
234 |
* to onSurfaceCreated) because only after this point can the Library upload it to the GPU! |
|
234 |
* to GLSurfaceView.onSurfaceCreated) because only after this point can the Library upload it to the GPU!
|
|
235 | 235 |
* |
236 | 236 |
* @param bmp The android.graphics.Bitmap object to apply effects to and display. |
237 | 237 |
*/ |
src/main/java/org/distorted/library/DistortedTree.java | ||
---|---|---|
49 | 49 |
{ |
50 | 50 |
long ID; |
51 | 51 |
int numPointingNodes; |
52 |
DistortedFramebuffer mDF; |
|
53 | 52 |
int numRendered; |
53 |
DistortedFramebuffer mDF; |
|
54 | 54 |
|
55 | 55 |
NodeData(long id) |
56 | 56 |
{ |
57 | 57 |
ID = id; |
58 | 58 |
numPointingNodes= 1; |
59 |
mDF = null; |
|
60 | 59 |
numRendered = 0; |
60 |
mDF = null; |
|
61 | 61 |
} |
62 | 62 |
} |
63 | 63 |
|
... | ... | |
296 | 296 |
{ |
297 | 297 |
mParent = null; |
298 | 298 |
mTexture= new DistortedTexture(node.mTexture,flags); |
299 |
mEffects = new DistortedEffects(node.mEffects, flags);
|
|
299 |
mEffects= new DistortedEffects(node.mEffects,flags);
|
|
300 | 300 |
mGrid = node.mGrid; |
301 | 301 |
|
302 | 302 |
if( (flags & Distorted.CLONE_CHILDREN) != 0 ) |
src/main/java/org/distorted/library/GridObject.java | ||
---|---|---|
55 | 55 |
|
56 | 56 |
void draw() |
57 | 57 |
{ |
58 |
GLES20.glVertexAttribPointer(Distorted.mPositionH , POSITION_DATA_SIZE, GLES20.GL_FLOAT, false, 0, mGridPositions);
|
|
59 |
GLES20.glVertexAttribPointer(Distorted.mNormalH , NORMAL_DATA_SIZE , GLES20.GL_FLOAT, false, 0, mGridNormals );
|
|
60 |
GLES20.glVertexAttribPointer(Distorted.mTextureCoordH, TEX_DATA_SIZE , GLES20.GL_FLOAT, false, 0, mGridTexture );
|
|
58 |
GLES20.glVertexAttribPointer(Distorted.mAttributes[0], POSITION_DATA_SIZE, GLES20.GL_FLOAT, false, 0, mGridPositions);
|
|
59 |
GLES20.glVertexAttribPointer(Distorted.mAttributes[1], NORMAL_DATA_SIZE , GLES20.GL_FLOAT, false, 0, mGridNormals );
|
|
60 |
GLES20.glVertexAttribPointer(Distorted.mAttributes[2], TEX_DATA_SIZE , GLES20.GL_FLOAT, false, 0, mGridTexture );
|
|
61 | 61 |
|
62 | 62 |
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, dataLength); |
63 | 63 |
} |
src/main/java/org/distorted/library/exception/FragmentCompilationException.java | ||
---|---|---|
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.exception; |
|
21 |
|
|
22 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
23 |
/** |
|
24 |
* Thrown by {@link org.distorted.library.Distorted#onSurfaceCreated(android.content.Context)} |
|
25 |
* if compilation of the fragment shader fails for some other reason than too many uniforms. |
|
26 |
* <p> |
|
27 |
* This can happen on older OpenGL ES 2.0 devices if they, say, do not support variable loops in the shaders. |
|
28 |
* Theoretically should never happen on devices supporting at least OpenGL ES 3.0. |
|
29 |
*/ |
|
30 |
|
|
31 |
@SuppressWarnings("serial") |
|
32 |
public class FragmentCompilationException extends Exception |
|
33 |
{ |
|
34 |
|
|
35 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
36 |
/** |
|
37 |
* Default empty constructor |
|
38 |
*/ |
|
39 |
public FragmentCompilationException() |
|
40 |
{ |
|
41 |
|
|
42 |
} |
|
43 |
|
|
44 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
45 |
/** |
|
46 |
* Constructor with a message describing why compilation failed. |
|
47 |
* |
|
48 |
* @param detailMessage Message describing why compilation failed |
|
49 |
*/ |
|
50 |
public FragmentCompilationException(String detailMessage) |
|
51 |
{ |
|
52 |
super(detailMessage); |
|
53 |
} |
|
54 |
|
|
55 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
56 |
/** |
|
57 |
* Constructor necessary to make Chained Exceptions working. |
|
58 |
* |
|
59 |
* @param throwable The parent Throwable. |
|
60 |
*/ |
|
61 |
public FragmentCompilationException(Throwable throwable) |
|
62 |
{ |
|
63 |
super(throwable); |
|
64 |
} |
|
65 |
|
|
66 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
67 |
/** |
|
68 |
* Constructor necessary to make Chained Exceptions working. |
|
69 |
* |
|
70 |
* @param detailMessage Message describing why compilation failed |
|
71 |
* @param throwable The parent Throwable. |
|
72 |
*/ |
|
73 |
public FragmentCompilationException(String detailMessage, Throwable throwable) |
|
74 |
{ |
|
75 |
super(detailMessage, throwable); |
|
76 |
} |
|
77 |
|
|
78 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
79 |
} |
src/main/java/org/distorted/library/exception/FragmentUniformsException.java | ||
---|---|---|
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.exception; |
|
21 |
|
|
22 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
23 |
/** |
|
24 |
* Thrown by {@link org.distorted.library.Distorted#onSurfaceCreated(android.content.Context)} |
|
25 |
* if compilation of the fragment shader fails because of too many uniforms there, i.e. because |
|
26 |
* we have set {@link org.distorted.library.Distorted#setMaxFragment(int)} to too high value. |
|
27 |
*/ |
|
28 |
|
|
29 |
@SuppressWarnings("serial") |
|
30 |
public class FragmentUniformsException extends Exception |
|
31 |
{ |
|
32 |
private int max=0; |
|
33 |
|
|
34 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
35 |
/** |
|
36 |
* Default empty constructor |
|
37 |
*/ |
|
38 |
public FragmentUniformsException() |
|
39 |
{ |
|
40 |
|
|
41 |
} |
|
42 |
|
|
43 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
44 |
/** |
|
45 |
* Constructor with a message describing why compilation failed. |
|
46 |
* |
|
47 |
* @param detailMessage Message describing why compilation failed |
|
48 |
*/ |
|
49 |
public FragmentUniformsException(String detailMessage) |
|
50 |
{ |
|
51 |
super(detailMessage); |
|
52 |
} |
|
53 |
|
|
54 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
55 |
/** |
|
56 |
* Constructor necessary to make Chained Exceptions working. |
|
57 |
* |
|
58 |
* @param throwable The parent Throwable. |
|
59 |
*/ |
|
60 |
public FragmentUniformsException(Throwable throwable) |
|
61 |
{ |
|
62 |
super(throwable); |
|
63 |
} |
|
64 |
|
|
65 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
66 |
/** |
|
67 |
* Constructor necessary to make Chained Exceptions working. |
|
68 |
* |
|
69 |
* @param detailMessage Message describing why compilation failed |
|
70 |
* @param throwable The parent Throwable. |
|
71 |
*/ |
|
72 |
public FragmentUniformsException(String detailMessage, Throwable throwable) |
|
73 |
{ |
|
74 |
super(detailMessage, throwable); |
|
75 |
} |
|
76 |
|
|
77 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
78 |
/** |
|
79 |
* Constructor with a message describing why compilation failed and integer holding the maximum |
|
80 |
* number of uniforms in Fragment Shader supported by current hardware. |
|
81 |
* |
|
82 |
* @param detailMessage Message describing why compilation failed |
|
83 |
* @param m maximum number of uniforms in Fragment Shader supported by current hardware. |
|
84 |
*/ |
|
85 |
public FragmentUniformsException(String detailMessage, int m) |
|
86 |
{ |
|
87 |
super(detailMessage); |
|
88 |
max = m; |
|
89 |
} |
|
90 |
|
|
91 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
92 |
/** |
|
93 |
* Gets the maximum number of uniforms in fragment shader supported by current hardware. |
|
94 |
* |
|
95 |
* @return Maximum number of uniforms in fragment shader supported by current hardware. |
|
96 |
*/ |
|
97 |
public int getMax() |
|
98 |
{ |
|
99 |
return max; |
|
100 |
} |
|
101 |
|
|
102 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
103 |
} |
src/main/java/org/distorted/library/exception/LinkingException.java | ||
---|---|---|
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.exception; |
|
21 |
|
|
22 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
23 |
/** |
|
24 |
* Thrown by {@link org.distorted.library.Distorted#onSurfaceCreated(android.content.Context)} |
|
25 |
* if linking of the Shaders fails. |
|
26 |
* <p> |
|
27 |
* Theoretically this should never happen. |
|
28 |
*/ |
|
29 |
@SuppressWarnings("serial") |
|
30 |
public class LinkingException extends Exception |
|
31 |
{ |
|
32 |
|
|
33 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
34 |
/** |
|
35 |
* Default empty constructor |
|
36 |
*/ |
|
37 |
public LinkingException() |
|
38 |
{ |
|
39 |
|
|
40 |
} |
|
41 |
|
|
42 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
43 |
/** |
|
44 |
* Constructor with a message describing why linking failed. |
|
45 |
* |
|
46 |
* @param detailMessage Message describing why linking failed |
|
47 |
*/ |
|
48 |
public LinkingException(String detailMessage) |
|
49 |
{ |
|
50 |
super(detailMessage); |
|
51 |
} |
|
52 |
|
|
53 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
54 |
/** |
|
55 |
* Constructor necessary to make Chained Exceptions working. |
|
56 |
* |
|
57 |
* @param throwable The parent Throwable. |
|
58 |
*/ |
|
59 |
public LinkingException(Throwable throwable) |
|
60 |
{ |
|
61 |
super(throwable); |
|
62 |
} |
|
63 |
|
|
64 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
65 |
/** |
|
66 |
* Constructor necessary to make Chained Exceptions working. |
|
67 |
* |
|
68 |
* @param detailMessage Message describing why linking failed |
|
69 |
* @param throwable The parent Throwable. |
|
70 |
*/ |
|
71 |
public LinkingException(String detailMessage, Throwable throwable) |
|
72 |
{ |
|
73 |
super(detailMessage, throwable); |
|
74 |
} |
|
75 |
|
|
76 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
77 |
} |
src/main/java/org/distorted/library/exception/VertexCompilationException.java | ||
---|---|---|
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.exception; |
|
21 |
|
|
22 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
23 |
/** |
|
24 |
* Thrown by {@link org.distorted.library.Distorted#onSurfaceCreated(android.content.Context)} |
|
25 |
* if compilation of the vertex shader fails for some other reason than too many uniforms. |
|
26 |
* <p> |
|
27 |
* This can happen on older OpenGL ES 2.0 devices if they, say, do not support variable loops in the shaders. |
|
28 |
* Theoretically should never happen on devices supporting at least OpenGL ES 3.0. |
|
29 |
*/ |
|
30 |
|
|
31 |
@SuppressWarnings("serial") |
|
32 |
public class VertexCompilationException extends Exception |
|
33 |
{ |
|
34 |
|
|
35 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
36 |
/** |
|
37 |
* Default empty constructor |
|
38 |
*/ |
|
39 |
public VertexCompilationException() |
|
40 |
{ |
|
41 |
|
|
42 |
} |
|
43 |
|
|
44 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
45 |
/** |
|
46 |
* Constructor with a message describing why compilation failed. |
|
47 |
* |
|
48 |
* @param detailMessage Message describing why compilation failed |
|
49 |
*/ |
|
50 |
public VertexCompilationException(String detailMessage) |
|
51 |
{ |
|
52 |
super(detailMessage); |
|
53 |
} |
|
54 |
|
|
55 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
56 |
/** |
|
57 |
* Constructor necessary to make Chained Exceptions working. |
|
58 |
* |
|
59 |
* @param throwable The parent Throwable. |
|
60 |
*/ |
|
61 |
public VertexCompilationException(Throwable throwable) |
|
62 |
{ |
|
63 |
super(throwable); |
|
64 |
} |
|
65 |
|
|
66 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
67 |
/** |
|
68 |
* Constructor necessary to make Chained Exceptions working. |
|
69 |
* |
|
70 |
* @param detailMessage Message describing why compilation failed |
|
71 |
* @param throwable The parent Throwable. |
|
72 |
*/ |
|
73 |
public VertexCompilationException(String detailMessage, Throwable throwable) |
|
74 |
{ |
|
75 |
super(detailMessage, throwable); |
|
76 |
} |
|
77 |
|
|
78 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
79 |
} |
src/main/java/org/distorted/library/exception/VertexUniformsException.java | ||
---|---|---|
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.exception; |
|
21 |
|
|
22 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
23 |
/** |
|
24 |
* Thrown by {@link org.distorted.library.Distorted#onSurfaceCreated(android.content.Context)} |
|
25 |
* if compilation of the Vertex Shader fails because of too many uniforms there, i.e. because |
|
26 |
* we have set {@link org.distorted.library.Distorted#setMaxVertex(int)} to too high value. |
|
27 |
*/ |
|
28 |
|
|
29 |
@SuppressWarnings("serial") |
|
30 |
public class VertexUniformsException extends Exception |
|
31 |
{ |
|
32 |
private int max=0; |
|
33 |
|
|
34 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
35 |
/** |
|
36 |
* Default empty constructor |
|
37 |
*/ |
|
38 |
public VertexUniformsException() |
|
39 |
{ |
|
40 |
|
|
41 |
} |
|
42 |
|
|
43 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
44 |
/** |
|
45 |
* Constructor with a message describing why compilation failed. |
|
46 |
* |
|
47 |
* @param detailMessage Message describing why compilation failed |
|
48 |
*/ |
|
49 |
public VertexUniformsException(String detailMessage) |
|
50 |
{ |
|
51 |
super(detailMessage); |
|
52 |
} |
|
53 |
|
|
54 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
55 |
/** |
|
56 |
* Constructor necessary to make Chained Exceptions working. |
|
57 |
* |
|
58 |
* @param throwable The parent Throwable. |
|
59 |
*/ |
|
60 |
public VertexUniformsException(Throwable throwable) |
|
61 |
{ |
|
62 |
super(throwable); |
|
63 |
} |
|
64 |
|
|
65 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
66 |
/** |
|
67 |
* Constructor necessary to make Chained Exceptions working. |
|
68 |
* |
|
69 |
* @param detailMessage Message describing why compilation failed |
|
70 |
* @param throwable The parent Throwable. |
|
71 |
*/ |
|
72 |
public VertexUniformsException(String detailMessage, Throwable throwable) |
|
73 |
{ |
|
74 |
super(detailMessage, throwable); |
|
75 |
} |
|
76 |
|
|
77 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
78 |
/** |
|
79 |
* Constructor with a message describing why compilation failed and integer holding the maximum |
|
80 |
* number of uniforms in Vertex Shader supported by current hardware. |
|
81 |
* |
|
82 |
* @param detailMessage Message describing why compilation failed |
|
83 |
* @param m maximum number of uniforms in Vertex Shader supported by current hardware. |
|
84 |
*/ |
|
85 |
public VertexUniformsException(String detailMessage, int m) |
|
86 |
{ |
|
87 |
super(detailMessage); |
|
88 |
max = m; |
|
89 |
} |
|
90 |
|
|
91 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
92 |
/** |
|
93 |
* Gets the maximum number of uniforms in Vertex Shader supported by current hardware. |
|
94 |
* |
|
95 |
* @return Maximum number of uniforms in Vertex Shader supported by current hardware. |
|
96 |
*/ |
|
97 |
public int getMax() |
|
98 |
{ |
|
99 |
return max; |
|
100 |
} |
|
101 |
|
|
102 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
103 |
} |
src/main/java/org/distorted/library/program/DistortedProgram.java | ||
---|---|---|
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.program; |
|
21 |
|
|
22 |
import android.opengl.GLES20; |
|
23 |
import android.os.Build; |
|
24 |
|
|
25 |
import org.distorted.library.DistortedEffects; |
|
26 |
import org.distorted.library.EffectNames; |
|
27 |
import org.distorted.library.EffectTypes; |
|
28 |
|
|
29 |
import java.io.BufferedReader; |
|
30 |
import java.io.IOException; |
|
31 |
import java.io.InputStream; |
|
32 |
import java.io.InputStreamReader; |
|
33 |
|
|
34 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
35 |
/** |
|
36 |
* An object which encapsulates a vertex/fragment shader combo, aka Shader Program. |
|
37 |
*/ |
|
38 |
public class DistortedProgram |
|
39 |
{ |
|
40 |
private int mProgramHandle; |
|
41 |
|
|
42 |
private int mNumAttributes; |
|
43 |
private int[] mAttribute; |
|
44 |
private String[] mAttributeName; |
|
45 |
private String mAttributeTmp; |
|
46 |
|
|
47 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
48 |
|
|
49 |
private int createAndLinkProgram(final int vertexShaderHandle, final int fragmentShaderHandle, final String[] attributes) |
|
50 |
throws LinkingException |
|
51 |
{ |
|
52 |
int programHandle = GLES20.glCreateProgram(); |
|
53 |
|
|
54 |
if (programHandle != 0) |
|
55 |
{ |
|
56 |
GLES20.glAttachShader(programHandle, vertexShaderHandle); |
|
57 |
GLES20.glAttachShader(programHandle, fragmentShaderHandle); |
|
58 |
|
|
59 |
if (attributes != null) |
|
60 |
{ |
|
61 |
final int size = attributes.length; |
|
62 |
|
|
63 |
for (int i = 0; i < size; i++) |
|
64 |
{ |
|
65 |
GLES20.glBindAttribLocation(programHandle, i, attributes[i]); |
|
66 |
} |
|
67 |
} |
|
68 |
|
|
69 |
GLES20.glLinkProgram(programHandle); |
|
70 |
|
|
71 |
final int[] linkStatus = new int[1]; |
|
72 |
GLES20.glGetProgramiv(programHandle, GLES20.GL_LINK_STATUS, linkStatus, 0); |
|
73 |
|
|
74 |
if (linkStatus[0] != GLES20.GL_TRUE ) |
|
75 |
{ |
|
76 |
String error = GLES20.glGetProgramInfoLog(programHandle); |
|
77 |
GLES20.glDeleteProgram(programHandle); |
|
78 |
throw new LinkingException(error); |
|
79 |
} |
|
80 |
|
|
81 |
final int[] numberOfUniforms = new int[1]; |
|
82 |
GLES20.glGetProgramiv(programHandle, GLES20.GL_ACTIVE_UNIFORMS, numberOfUniforms, 0); |
|
83 |
|
|
84 |
//android.util.Log.d("program", "number of active uniforms="+numberOfUniforms[0]); |
|
85 |
} |
|
86 |
|
|
87 |
return programHandle; |
|
88 |
} |
|
89 |
|
|
90 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
91 |
|
|
92 |
private void doAttributes(final String line) |
|
93 |
{ |
|
94 |
int len = line.length(); |
|
95 |
int whiteSpace, semicolon, nameBegin; |
|
96 |
char currChar; |
|
97 |
|
|
98 |
for(whiteSpace=0; whiteSpace<len; whiteSpace++) |
|
99 |
{ |
|
100 |
currChar = line.charAt(whiteSpace); |
|
101 |
if( currChar!=' ' && currChar!='\t') break; |
|
102 |
} |
|
103 |
|
|
104 |
for(semicolon=whiteSpace; semicolon<len; semicolon++) |
|
105 |
{ |
|
106 |
currChar = line.charAt(semicolon); |
|
107 |
if( currChar==';') break; |
|
108 |
} |
|
109 |
|
|
110 |
if( semicolon<len && semicolon-whiteSpace>=11 ) // "attribute a;" --> 11 |
|
111 |
{ |
|
112 |
String subline = line.substring(whiteSpace,semicolon); |
|
113 |
int subLen = semicolon-whiteSpace; |
|
114 |
|
|
115 |
if( subline.startsWith("attribute")) |
|
116 |
{ |
|
117 |
//android.util.Log.e("program", "GOOD LINE: " +subline+" subLen="+subLen); |
|
118 |
|
|
119 |
for(nameBegin=subLen-1; nameBegin>8; nameBegin--) |
|
120 |
{ |
|
121 |
currChar=subline.charAt(nameBegin); |
|
122 |
|
|
123 |
if( currChar==' ' || currChar=='\t' ) |
|
124 |
{ |
|
125 |
subline = subline.substring(nameBegin+1,subLen); |
|
126 |
//android.util.Log.d("program", "new attribute: "+subline); |
|
127 |
|
|
128 |
if( mAttributeTmp.length()>0 ) mAttributeTmp+=" "; |
|
129 |
mAttributeTmp += subline; |
|
130 |
mNumAttributes++; |
|
131 |
break; |
|
132 |
} |
|
133 |
} |
|
134 |
} |
|
135 |
} |
|
136 |
} |
|
137 |
|
|
138 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
139 |
|
|
140 |
private String readTextFileFromRawResource(final InputStream inputStream, boolean doAttributes) |
|
141 |
{ |
|
142 |
final InputStreamReader inputStreamReader = new InputStreamReader(inputStream); |
|
143 |
final BufferedReader bufferedReader = new BufferedReader(inputStreamReader); |
|
144 |
|
|
145 |
String nextLine; |
|
146 |
final StringBuilder body = new StringBuilder(); |
|
147 |
|
|
148 |
try |
|
149 |
{ |
|
150 |
while ((nextLine = bufferedReader.readLine()) != null) |
|
151 |
{ |
|
152 |
body.append(nextLine); |
|
153 |
body.append('\n'); |
|
154 |
|
|
155 |
if( doAttributes ) doAttributes(nextLine); |
|
156 |
} |
|
157 |
} |
|
158 |
catch (IOException e) |
|
159 |
{ |
|
160 |
return null; |
|
161 |
} |
|
162 |
|
|
163 |
if( doAttributes ) |
|
164 |
{ |
|
165 |
mAttribute = new int[mNumAttributes]; |
|
166 |
mAttributeName = mAttributeTmp.split(" "); |
|
167 |
mAttributeTmp = ""; |
|
168 |
/* |
|
169 |
int len = mAttributeName.length; |
|
170 |
|
|
171 |
for(int i=0; i<len; i++) |
|
172 |
{ |
|
173 |
android.util.Log.e("program","ATTRIBUTE "+i+" :" + mAttributeName[i]); |
|
174 |
} |
|
175 |
|
|
176 |
android.util.Log.e("program","mNumAttributes: "+mNumAttributes); |
|
177 |
*/ |
|
178 |
} |
|
179 |
|
|
180 |
return body.toString(); |
|
181 |
} |
|
182 |
|
|
183 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
184 |
|
|
185 |
private static void sanitizeMaxValues() |
|
186 |
throws VertexUniformsException,FragmentUniformsException |
|
187 |
{ |
|
188 |
int maxV,maxF; |
|
189 |
int[] param = new int[1]; |
|
190 |
|
|
191 |
GLES20.glGetIntegerv(GLES20.GL_MAX_VERTEX_UNIFORM_VECTORS, param, 0); |
|
192 |
maxV = param[0]; |
|
193 |
GLES20.glGetIntegerv(GLES20.GL_MAX_FRAGMENT_UNIFORM_VECTORS, param, 0); |
|
194 |
maxF = param[0]; |
|
195 |
|
|
196 |
//Log.d("program", "Max vectors in vertex shader: "+maxV); |
|
197 |
//Log.d("program", "Max vectors in fragment shader: "+maxF); |
|
198 |
|
|
199 |
if( !Build.FINGERPRINT.contains("generic") ) |
|
200 |
{ |
|
201 |
int realMaxV = (maxV-11)/4; // adjust this in case of changes to the shaders... |
|
202 |
int realMaxF = (maxF- 2)/4; // |
|
203 |
|
|
204 |
if( DistortedEffects.getMaxVertex() > realMaxV ) |
|
205 |
{ |
|
206 |
throw new VertexUniformsException("Too many effects in the vertex shader, max is "+realMaxV, realMaxV); |
|
207 |
} |
|
208 |
if( DistortedEffects.getMaxFragment() > realMaxF ) |
|
209 |
{ |
|
210 |
throw new FragmentUniformsException("Too many effects in the fragment shader, max is "+realMaxF, realMaxF); |
|
211 |
} |
|
212 |
} |
|
213 |
} |
|
214 |
|
|
215 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
216 |
|
|
217 |
private static int compileShader(final int shaderType, final String shaderSource) |
|
218 |
throws FragmentCompilationException,VertexCompilationException |
|
219 |
{ |
|
220 |
int shaderHandle = GLES20.glCreateShader(shaderType); |
|
221 |
|
|
222 |
if (shaderHandle != 0) |
|
223 |
{ |
|
224 |
GLES20.glShaderSource(shaderHandle, "#version 100 \n"+ generateShaderHeader(shaderType) + shaderSource); |
|
225 |
GLES20.glCompileShader(shaderHandle); |
|
226 |
final int[] compileStatus = new int[1]; |
|
227 |
GLES20.glGetShaderiv(shaderHandle, GLES20.GL_COMPILE_STATUS, compileStatus, 0); |
|
228 |
|
|
229 |
if (compileStatus[0] != GLES20.GL_TRUE ) |
|
230 |
{ |
|
231 |
GLES20.glDeleteShader(shaderHandle); |
|
232 |
shaderHandle = 0; |
|
233 |
} |
|
234 |
} |
|
235 |
|
|
236 |
if (shaderHandle == 0) |
|
237 |
{ |
|
238 |
String error = GLES20.glGetShaderInfoLog(shaderHandle); |
|
239 |
|
|
240 |
switch(shaderType) |
|
241 |
{ |
|
242 |
case GLES20.GL_VERTEX_SHADER : throw new VertexCompilationException(error); |
|
243 |
case GLES20.GL_FRAGMENT_SHADER: throw new FragmentCompilationException(error); |
|
244 |
default : throw new RuntimeException(error); |
|
245 |
} |
|
246 |
} |
|
247 |
|
|
248 |
return shaderHandle; |
|
249 |
} |
|
250 |
|
|
251 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
252 |
|
|
253 |
private static String generateShaderHeader(final int type) |
|
254 |
{ |
|
255 |
String header=""; |
|
256 |
|
|
257 |
switch(type) |
|
258 |
{ |
|
259 |
case GLES20.GL_VERTEX_SHADER : header += ("#define NUM_VERTEX " + DistortedEffects.getMaxVertex()+"\n"); |
|
260 |
|
|
261 |
for(EffectNames name: EffectNames.values() ) |
|
262 |
{ |
|
263 |
if( name.getType()== EffectTypes.VERTEX) |
|
264 |
header += ("#define "+name.name()+" "+name.ordinal()+"\n"); |
|
265 |
} |
|
266 |
break; |
|
267 |
case GLES20.GL_FRAGMENT_SHADER: header += ("#define NUM_FRAGMENT "+ DistortedEffects.getMaxFragment()+"\n"); |
|
268 |
|
|
269 |
for(EffectNames name: EffectNames.values() ) |
|
270 |
{ |
|
271 |
if( name.getType()==EffectTypes.FRAGMENT) |
|
272 |
header += ("#define "+name.name()+" "+name.ordinal()+"\n"); |
|
273 |
} |
|
274 |
break; |
|
275 |
} |
|
276 |
|
|
277 |
//Log.d(TAG,""+header); |
|
278 |
|
|
279 |
return header; |
|
280 |
} |
|
281 |
|
|
282 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
283 |
// PUBLIC API |
|
284 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
285 |
/** |
|
286 |
* Create a new Shader Program from source stored in resource files. |
|
287 |
* <p> |
|
288 |
* Needs to be called from a thread holding the OpenGL context. |
|
289 |
* |
|
290 |
* @param vertex InputStream containing the opened Resource file from where to read vertex shader code. |
|
291 |
* @param fragment InputStream containing the opened Resource file from where to read fragment shader code. |
|
292 |
* |
|
293 |
*/ |
|
294 |
|
|
295 |
public DistortedProgram(final InputStream vertex, final InputStream fragment) |
|
296 |
throws FragmentCompilationException,VertexCompilationException,VertexUniformsException,FragmentUniformsException,LinkingException |
|
297 |
{ |
|
298 |
mNumAttributes = 0; |
|
299 |
mAttributeTmp = ""; |
|
300 |
|
|
301 |
final String vertexShader = readTextFileFromRawResource(vertex , true ); |
|
302 |
final String fragmentShader = readTextFileFromRawResource(fragment, false); |
|
303 |
|
|
304 |
sanitizeMaxValues(); |
|
305 |
|
|
306 |
final int vertexShaderHandle = compileShader(GLES20.GL_VERTEX_SHADER , vertexShader ); |
|
307 |
final int fragmentShaderHandle = compileShader(GLES20.GL_FRAGMENT_SHADER, fragmentShader); |
|
308 |
|
|
309 |
mProgramHandle = createAndLinkProgram(vertexShaderHandle, fragmentShaderHandle, mAttributeName); |
|
310 |
} |
|
311 |
|
|
312 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
313 |
|
|
314 |
public int[] getAttributes() |
|
315 |
{ |
|
316 |
return mAttribute; |
|
317 |
} |
|
318 |
|
|
319 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
320 |
|
|
321 |
public String[] getAttributeNames() |
|
322 |
{ |
|
323 |
return mAttributeName; |
|
324 |
} |
|
325 |
|
|
326 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
327 |
|
|
328 |
public int getProgramHandle() |
|
329 |
{ |
|
330 |
return mProgramHandle; |
|
331 |
} |
|
332 |
|
|
333 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
334 |
// glUseProgram first; needs to be called from a thread holding the OpenGL context |
|
335 |
|
|
336 |
public void bindAndEnableAttributes() |
|
337 |
{ |
|
338 |
for(int i=0; i<mNumAttributes; i++) |
|
339 |
{ |
|
340 |
mAttribute[i] = GLES20.glGetAttribLocation( mProgramHandle, mAttributeName[i]); |
|
341 |
GLES20.glEnableVertexAttribArray(mAttribute[i]); |
|
342 |
} |
|
343 |
} |
|
344 |
} |
src/main/java/org/distorted/library/program/FragmentCompilationException.java | ||
---|---|---|
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.program; |
|
21 |
|
|
22 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
23 |
/** |
|
24 |
* Thrown by {@link org.distorted.library.Distorted#onCreate(android.content.Context)} |
|
25 |
* if compilation of the fragment shader fails for some other reason than too many uniforms. |
|
26 |
* <p> |
|
27 |
* This can happen on older OpenGL ES 2.0 devices if they, say, do not support variable loops in the shaders. |
|
28 |
* Theoretically should never happen on devices supporting at least OpenGL ES 3.0. |
|
29 |
*/ |
|
30 |
|
|
31 |
@SuppressWarnings("serial") |
|
32 |
public class FragmentCompilationException extends Exception |
|
33 |
{ |
|
34 |
|
|
35 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
36 |
/** |
|
37 |
* Default empty constructor |
|
38 |
*/ |
|
39 |
public FragmentCompilationException() |
Also available in: Unified diff
New DistortedProgram class.