Revision 86e99907
Added by Leszek Koltunski over 7 years ago
src/main/java/org/distorted/library/Distorted.java | ||
---|---|---|
33 | 33 |
{ |
34 | 34 |
static int GLSL; |
35 | 35 |
static String GLSL_VERSION; |
36 |
|
|
37 |
//////////// DEBUG FLAGS ///////////////////////////////////////////// |
|
38 |
/** |
|
39 |
* When rendering a Screen, show FPS in the upper-left corner? |
|
40 |
*/ |
|
41 |
public static final int DEBUG_FPS = 1; |
|
42 |
|
|
43 |
//////////// END DEBUG FLAGS ///////////////////////////////////////// |
|
44 |
|
|
45 | 36 |
/** |
46 | 37 |
* When creating an instance of a DistortedTexture from another instance, clone the Bitmap that's |
47 | 38 |
* backing up our DistortedTexture. |
... | ... | |
84 | 75 |
|
85 | 76 |
private static boolean mInitialized=false; |
86 | 77 |
|
87 |
static int mDebugLevel = 0; |
|
88 |
static DistortedDebug mDebug; |
|
89 |
|
|
90 | 78 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
91 | 79 |
// private: hide this from Javadoc |
92 | 80 |
|
... | ... | |
102 | 90 |
return mInitialized; |
103 | 91 |
} |
104 | 92 |
|
105 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
106 |
/** |
|
107 |
* Make the library show various debugging information. |
|
108 |
* <p> |
|
109 |
* Currently only DEBUG_FPS - show FPS in the upper-left corner of every Screen - is defined. |
|
110 |
* |
|
111 |
* @param bitmask 0, or a bitmask of DEBUG_** flags to enable (currently only DEBUG_FPS defined) |
|
112 |
*/ |
|
113 |
public static void setDebug(int bitmask) |
|
114 |
{ |
|
115 |
mDebugLevel = bitmask; |
|
116 |
|
|
117 |
if( mDebugLevel!=0 && mDebug==null ) mDebug = new DistortedDebug(); |
|
118 |
} |
|
119 |
|
|
120 | 93 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
121 | 94 |
/** |
122 | 95 |
* When OpenGL context gets created, you need to call this method so that the library can initialise its internal data structures. |
... | ... | |
125 | 98 |
* Needs to be called from a thread holding the OpenGL context. |
126 | 99 |
* |
127 | 100 |
* @param context Context of the App using the library - used to open up Resources and read Shader code. |
128 |
* @throws FragmentCompilationException |
|
129 |
* @throws VertexCompilationException |
|
130 |
* @throws VertexUniformsException |
|
131 |
* @throws FragmentUniformsException |
|
132 |
* @throws LinkingException |
|
101 |
* @throws FragmentCompilationException Fragment Shader failed to compile
|
|
102 |
* @throws VertexCompilationException Vertex Shader failed to compile
|
|
103 |
* @throws VertexUniformsException Too many uniforms in the Vertex Shader
|
|
104 |
* @throws FragmentUniformsException Too many uniforms in the Fragment Shader
|
|
105 |
* @throws LinkingException Shader failed to link
|
|
133 | 106 |
*/ |
134 | 107 |
public static void onCreate(final Context context) |
135 | 108 |
throws FragmentCompilationException,VertexCompilationException,VertexUniformsException,FragmentUniformsException,LinkingException |
src/main/java/org/distorted/library/DistortedDebug.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; |
|
21 |
|
|
22 |
import android.graphics.Bitmap; |
|
23 |
import android.graphics.Canvas; |
|
24 |
import android.graphics.Paint; |
|
25 |
|
|
26 |
import org.distorted.library.type.Static3D; |
|
27 |
|
|
28 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
29 |
/** |
|
30 |
* Show various debugs. |
|
31 |
* <p> |
|
32 |
* Currently only showing the FPS in the upper-left corner of every DistortedScreen is supported. |
|
33 |
*/ |
|
34 |
class DistortedDebug |
|
35 |
{ |
|
36 |
private static final int NUM_FRAMES = 100; |
|
37 |
|
|
38 |
private MeshObject fpsMesh; |
|
39 |
private DistortedTexture fpsTexture; |
|
40 |
private DistortedEffects fpsEffects; |
|
41 |
private Canvas fpsCanvas; |
|
42 |
private Bitmap fpsBitmap; |
|
43 |
private Paint mPaint; |
|
44 |
private int fpsH, fpsW; |
|
45 |
private String fpsString = ""; |
|
46 |
private long lastTime=0; |
|
47 |
private long[] durations; |
|
48 |
private int currDuration; |
|
49 |
|
|
50 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
51 |
|
|
52 |
DistortedDebug() |
|
53 |
{ |
|
54 |
fpsW = 120; |
|
55 |
fpsH = 70; |
|
56 |
|
|
57 |
fpsBitmap = Bitmap.createBitmap(fpsW,fpsH, Bitmap.Config.ARGB_8888); |
|
58 |
fpsMesh = new MeshFlat(1,1); |
|
59 |
fpsTexture = new DistortedTexture(fpsW,fpsH); |
|
60 |
fpsTexture.setTexture(fpsBitmap); |
|
61 |
fpsCanvas = new Canvas(fpsBitmap); |
|
62 |
fpsEffects = new DistortedEffects(); |
|
63 |
fpsEffects.move( new Static3D(5,5,0) ); |
|
64 |
|
|
65 |
mPaint = new Paint(); |
|
66 |
mPaint.setAntiAlias(true); |
|
67 |
mPaint.setTextAlign(Paint.Align.CENTER); |
|
68 |
mPaint.setTextSize(0.7f*fpsH); |
|
69 |
|
|
70 |
durations = new long[NUM_FRAMES+1]; |
|
71 |
currDuration = 0; |
|
72 |
for(int i=0; i<NUM_FRAMES+1; i++) durations[i]=0; |
|
73 |
} |
|
74 |
|
|
75 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
76 |
|
|
77 |
private void computeFPS(long currTime) |
|
78 |
{ |
|
79 |
if( lastTime!=0 ) |
|
80 |
{ |
|
81 |
currDuration++; |
|
82 |
if( currDuration>=NUM_FRAMES ) currDuration = 0; |
|
83 |
durations[NUM_FRAMES] += ((currTime-lastTime)-durations[currDuration]); |
|
84 |
durations[currDuration] = currTime-lastTime; |
|
85 |
|
|
86 |
fpsString = "" + ((int)(10000.0f*NUM_FRAMES/durations[NUM_FRAMES]))/10.0f; |
|
87 |
|
|
88 |
mPaint.setColor(0xffffffff); |
|
89 |
fpsCanvas.drawRect(0, 0, fpsW, fpsH, mPaint); |
|
90 |
mPaint.setColor(0xff000000); |
|
91 |
fpsCanvas.drawText(fpsString, fpsW/2, 0.75f*fpsH, mPaint); |
|
92 |
fpsTexture.setTexture(fpsBitmap); |
|
93 |
} |
|
94 |
|
|
95 |
lastTime = currTime; |
|
96 |
} |
|
97 |
|
|
98 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
99 |
|
|
100 |
void renderFPS(long currTime, DistortedOutputSurface surface) |
|
101 |
{ |
|
102 |
computeFPS(currTime); |
|
103 |
|
|
104 |
fpsTexture.setAsInput(); |
|
105 |
surface.setAsOutput(); |
|
106 |
fpsEffects.drawPriv( fpsW/2, fpsH/2, fpsMesh, surface, currTime, 0); |
|
107 |
} |
|
108 |
} |
src/main/java/org/distorted/library/DistortedFramebuffer.java | ||
---|---|---|
31 | 31 |
public class DistortedFramebuffer extends DistortedOutputSurface implements DistortedInputSurface |
32 | 32 |
{ |
33 | 33 |
|
34 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
35 |
|
|
36 |
void prepareDebug(long time) {} |
|
37 |
void renderDebug(long time) {} |
|
38 |
|
|
34 | 39 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
35 | 40 |
// Must be called from a thread holding OpenGL Context |
36 | 41 |
|
... | ... | |
170 | 175 |
super(width,height,NOT_CREATED_YET,numcolors,depthStencil,NOT_CREATED_YET, type); |
171 | 176 |
} |
172 | 177 |
|
173 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
174 |
// For setting the Depth texture as input to fragment shaders that merge many planes. (currently: blur2) |
|
175 |
|
|
176 |
boolean setAsDepth() |
|
177 |
{ |
|
178 |
if( mDepthStencilH[0]>0 ) |
|
179 |
{ |
|
180 |
GLES30.glActiveTexture(GLES30.GL_TEXTURE1); |
|
181 |
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, mDepthStencilH[0]); |
|
182 |
return true; |
|
183 |
} |
|
184 |
|
|
185 |
return false; |
|
186 |
} |
|
187 |
|
|
188 | 178 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
189 | 179 |
// PUBLIC API |
190 | 180 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
src/main/java/org/distorted/library/DistortedOutputSurface.java | ||
---|---|---|
21 | 21 |
|
22 | 22 |
import android.opengl.GLES30; |
23 | 23 |
import android.opengl.Matrix; |
24 |
|
|
24 | 25 |
import java.util.ArrayList; |
25 | 26 |
|
26 | 27 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
27 | 28 |
|
28 | 29 |
abstract class DistortedOutputSurface extends DistortedSurface implements DistortedSlave |
29 | 30 |
{ |
31 |
//////////// DEBUG FLAGS ///////////////////////////////////////////// |
|
32 |
/** |
|
33 |
* When rendering a Screen, show FPS in the upper-left corner? |
|
34 |
*/ |
|
35 |
public static final int DEBUG_FPS = 1; |
|
36 |
//////////// END DEBUG FLAGS ///////////////////////////////////////// |
|
37 |
|
|
30 | 38 |
/** |
31 | 39 |
* Do not create DEPTH or STENCIL attachment |
32 | 40 |
*/ |
... | ... | |
82 | 90 |
private int mClear; |
83 | 91 |
float mMipmap; |
84 | 92 |
|
93 |
private int mDebugLevel; |
|
94 |
|
|
95 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
96 |
|
|
97 |
abstract void prepareDebug(long time); |
|
98 |
abstract void renderDebug(long time); |
|
99 |
|
|
85 | 100 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
86 | 101 |
|
87 | 102 |
DistortedOutputSurface(int width, int height, int createColor, int numcolors, int depthStencil, int fbo, int type) |
... | ... | |
100 | 115 |
mDepthStencilH[0]= 0; |
101 | 116 |
|
102 | 117 |
mTime = 0; |
118 |
mDebugLevel = 0; |
|
103 | 119 |
|
104 | 120 |
mClearR = 0.0f; |
105 | 121 |
mClearG = 0.0f; |
... | ... | |
309 | 325 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
310 | 326 |
// PUBLIC API |
311 | 327 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
328 |
/** |
|
329 |
* Make the library show various debugging information. |
|
330 |
* <p> |
|
331 |
* Currently only DEBUG_FPS - show FPS in the upper-left corner of every Screen - is defined. |
|
332 |
* |
|
333 |
* @param bitmask 0, or a bitmask of DEBUG_** flags to enable (currently only DEBUG_FPS defined) |
|
334 |
*/ |
|
335 |
public void setDebug(int bitmask) |
|
336 |
{ |
|
337 |
mDebugLevel = bitmask; |
|
338 |
} |
|
339 |
|
|
340 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
341 |
|
|
312 | 342 |
/** |
313 | 343 |
* Draws all the attached children to this OutputSurface. |
314 | 344 |
* <p> |
... | ... | |
319 | 349 |
*/ |
320 | 350 |
public int render(long time) |
321 | 351 |
{ |
352 |
boolean showDebug = ( mDebugLevel!=0 && this instanceof DistortedScreen ); |
|
353 |
|
|
354 |
if( showDebug ) prepareDebug(time); |
|
355 |
|
|
322 | 356 |
// change tree topology (attach and detach children) |
323 | 357 |
/* |
324 | 358 |
boolean changed1 = |
... | ... | |
363 | 397 |
setAsOutput(time); |
364 | 398 |
numRenders += renderChildren(time,mNumChildren,mChildren); |
365 | 399 |
|
366 |
if( Distorted.mDebugLevel!=0 && this instanceof DistortedScreen ) |
|
367 |
{ |
|
368 |
Distorted.mDebug.renderFPS(time,this); |
|
369 |
} |
|
400 |
if( showDebug ) renderDebug(time); |
|
370 | 401 |
|
371 | 402 |
return numRenders; |
372 | 403 |
} |
src/main/java/org/distorted/library/DistortedScreen.java | ||
---|---|---|
24 | 24 |
import android.app.ActivityManager; |
25 | 25 |
import android.content.Context; |
26 | 26 |
import android.content.pm.ConfigurationInfo; |
27 |
import android.graphics.Bitmap; |
|
28 |
import android.graphics.Canvas; |
|
29 |
import android.graphics.Paint; |
|
27 | 30 |
import android.opengl.GLSurfaceView; |
28 | 31 |
|
32 |
import org.distorted.library.type.Static3D; |
|
33 |
|
|
29 | 34 |
/** |
30 | 35 |
* Class which represents the Screen. |
31 | 36 |
* <p> |
... | ... | |
33 | 38 |
*/ |
34 | 39 |
public class DistortedScreen extends DistortedOutputSurface |
35 | 40 |
{ |
41 |
///// DEBUGGING ONLY ///////////////////////// |
|
42 |
private static final int NUM_FRAMES = 100; |
|
43 |
|
|
44 |
private MeshObject fpsMesh; |
|
45 |
private DistortedTexture fpsTexture; |
|
46 |
private DistortedEffects fpsEffects; |
|
47 |
private Canvas fpsCanvas; |
|
48 |
private Bitmap fpsBitmap; |
|
49 |
private Paint mPaint; |
|
50 |
private int fpsH, fpsW; |
|
51 |
private String fpsString = ""; |
|
52 |
private long lastTime=0; |
|
53 |
private long[] durations; |
|
54 |
private int currDuration; |
|
55 |
|
|
36 | 56 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
37 | 57 |
// here we don't manage underlying OpenGL assets ourselves |
38 | 58 |
|
... | ... | |
40 | 60 |
void delete() {} |
41 | 61 |
void recreate() {} |
42 | 62 |
|
63 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
64 |
|
|
65 |
void prepareDebug(long time) |
|
66 |
{ |
|
67 |
if( lastTime==0 ) lastTime = time; |
|
68 |
|
|
69 |
currDuration++; |
|
70 |
if (currDuration >= NUM_FRAMES) currDuration = 0; |
|
71 |
durations[NUM_FRAMES] += ((time - lastTime) - durations[currDuration]); |
|
72 |
durations[currDuration] = time - lastTime; |
|
73 |
|
|
74 |
fpsString = "" + ((int)(10000.0f*NUM_FRAMES/durations[NUM_FRAMES]))/10.0f; |
|
75 |
|
|
76 |
mPaint.setColor(0xffffffff); |
|
77 |
fpsCanvas.drawRect(0, 0, fpsW, fpsH, mPaint); |
|
78 |
mPaint.setColor(0xff000000); |
|
79 |
fpsCanvas.drawText(fpsString, fpsW/2, 0.75f*fpsH, mPaint); |
|
80 |
fpsTexture.setTexture(fpsBitmap); |
|
81 |
|
|
82 |
lastTime = time; |
|
83 |
} |
|
84 |
|
|
85 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
86 |
|
|
87 |
void renderDebug(long time) |
|
88 |
{ |
|
89 |
if (fpsTexture.setAsInput()) |
|
90 |
{ |
|
91 |
setAsOutput(time); |
|
92 |
fpsEffects.drawPriv(fpsW/2.0f, fpsH/2.0f, fpsMesh, this, time, 0); |
|
93 |
} |
|
94 |
} |
|
95 |
|
|
43 | 96 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
44 | 97 |
// PUBLIC API |
45 | 98 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
... | ... | |
61 | 114 |
final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo(); |
62 | 115 |
view.setEGLContextClientVersion((configurationInfo.reqGlEsVersion >> 16) >= 3 ? 3 : 2); |
63 | 116 |
} |
117 |
|
|
118 |
/////// DEBUGGING ONLY ////////////// |
|
119 |
fpsW = 120; |
|
120 |
fpsH = 70; |
|
121 |
|
|
122 |
fpsBitmap = Bitmap.createBitmap(fpsW,fpsH, Bitmap.Config.ARGB_8888); |
|
123 |
fpsMesh = new MeshFlat(1,1); |
|
124 |
fpsTexture = new DistortedTexture(fpsW,fpsH); |
|
125 |
fpsTexture.setTexture(fpsBitmap); |
|
126 |
fpsCanvas = new Canvas(fpsBitmap); |
|
127 |
fpsEffects = new DistortedEffects(); |
|
128 |
fpsEffects.move( new Static3D(5,5,0) ); |
|
129 |
|
|
130 |
mPaint = new Paint(); |
|
131 |
mPaint.setAntiAlias(true); |
|
132 |
mPaint.setTextAlign(Paint.Align.CENTER); |
|
133 |
mPaint.setTextSize(0.7f*fpsH); |
|
134 |
|
|
135 |
durations = new long[NUM_FRAMES+1]; |
|
136 |
currDuration = 0; |
|
137 |
|
|
138 |
for(int i=0; i<NUM_FRAMES+1; i++) durations[i]=0; |
|
64 | 139 |
} |
65 | 140 |
} |
Also available in: Unified diff
Correct the FPS work (now works even after a Pause() ! )