Revision 88c7b603
Added by Leszek Koltunski over 6 years ago
src/main/java/org/distorted/library/main/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 |
|
|
39 | 34 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
40 | 35 |
// Must be called from a thread holding OpenGL Context |
41 | 36 |
|
src/main/java/org/distorted/library/main/DistortedOutputSurface.java | ||
---|---|---|
34 | 34 |
*/ |
35 | 35 |
public abstract class DistortedOutputSurface extends DistortedSurface implements DistortedMaster.Slave |
36 | 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 |
//////////// END DEBUG FLAGS ///////////////////////////////////////// |
|
43 |
|
|
44 | 37 |
/** |
45 | 38 |
* Do not create DEPTH or STENCIL attachment |
46 | 39 |
*/ |
... | ... | |
96 | 89 |
private int mClear; |
97 | 90 |
float mMipmap; |
98 | 91 |
|
99 |
private int mDebugLevel; |
|
100 |
|
|
101 | 92 |
int mRealWidth; // the Surface can be backed up with a texture that is |
102 | 93 |
int mRealHeight; // larger than the viewport we have to it. |
103 | 94 |
// mWidth,mHeight are the sizes of the Viewport, those - |
104 | 95 |
// sizes of the backing up texture. |
105 | 96 |
|
106 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
107 |
|
|
108 |
abstract void prepareDebug(long time); |
|
109 |
abstract void renderDebug(long time); |
|
110 |
|
|
111 | 97 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
112 | 98 |
|
113 | 99 |
DistortedOutputSurface(int width, int height, int createColor, int numcolors, int depthStencil, int fbo, int type) |
... | ... | |
129 | 115 |
mDepthStencilH[0]= 0; |
130 | 116 |
|
131 | 117 |
mTime = 0; |
132 |
mDebugLevel = 0; |
|
133 | 118 |
|
134 | 119 |
mClearR = 0.0f; |
135 | 120 |
mClearG = 0.0f; |
... | ... | |
371 | 356 |
|
372 | 357 |
DistortedRenderState.enableStencil(); |
373 | 358 |
|
359 |
// we cannot compare DEPTH24_STENCIL8 format to DEPTH32 which oitRender's fragment shader expects. Remount! |
|
360 |
if( mDepthStencil==BOTH_DEPTH_STENCIL ) |
|
361 |
{ |
|
362 |
GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, mFBOH[0]); |
|
363 |
GLES31.glFramebufferTexture2D(GLES31.GL_FRAMEBUFFER, GLES31.GL_DEPTH_STENCIL_ATTACHMENT, GLES31.GL_TEXTURE_2D, 0, 0); |
|
364 |
GLES31.glFramebufferTexture2D(GLES31.GL_FRAMEBUFFER, GLES31.GL_DEPTH_ATTACHMENT, GLES31.GL_TEXTURE_2D, mDepthStencilH[0], 0); |
|
365 |
} |
|
366 |
|
|
374 | 367 |
DistortedEffects.oitRender(this, buffer.getWidthCorrection(), buffer.getHeightCorrection() ); |
368 |
|
|
369 |
if( mDepthStencil==BOTH_DEPTH_STENCIL ) |
|
370 |
{ |
|
371 |
GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, mFBOH[0]); |
|
372 |
GLES31.glFramebufferTexture2D(GLES31.GL_FRAMEBUFFER, GLES31.GL_DEPTH_ATTACHMENT, GLES31.GL_TEXTURE_2D, 0, 0); |
|
373 |
GLES31.glFramebufferTexture2D(GLES31.GL_FRAMEBUFFER, GLES31.GL_DEPTH_STENCIL_ATTACHMENT, GLES31.GL_TEXTURE_2D, mDepthStencilH[0], 0); |
|
374 |
} |
|
375 |
|
|
375 | 376 |
GLES31.glActiveTexture(GLES31.GL_TEXTURE1); |
376 | 377 |
GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, 0); |
377 | 378 |
|
... | ... | |
402 | 403 |
} |
403 | 404 |
} |
404 | 405 |
|
406 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
407 |
|
|
408 |
void clear() |
|
409 |
{ |
|
410 |
DistortedRenderState.colorDepthStencilOn(); |
|
411 |
GLES31.glClearColor(mClearR, mClearG, mClearB, mClearA); |
|
412 |
GLES31.glClearDepthf(mClearDepth); |
|
413 |
GLES31.glClearStencil(mClearStencil); |
|
414 |
GLES31.glClear(mClear); |
|
415 |
DistortedRenderState.colorDepthStencilRestore(); |
|
416 |
} |
|
417 |
|
|
405 | 418 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
406 | 419 |
// Render all children, one by one. If there are no postprocessing effects, just render to THIS. |
407 | 420 |
// Otherwise, render to a buffer and on each change of Postprocessing Bucket, apply the postprocessing |
... | ... | |
521 | 534 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
522 | 535 |
// PUBLIC API |
523 | 536 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
524 |
/** |
|
525 |
* Make the library show various debugging information. |
|
526 |
* <p> |
|
527 |
* Currently only DEBUG_FPS - show FPS in the upper-left corner of every Screen - is defined. |
|
528 |
* |
|
529 |
* @param bitmask 0, or a bitmask of DEBUG_** flags to enable (currently only DEBUG_FPS defined) |
|
530 |
*/ |
|
531 |
public void setDebug(int bitmask) |
|
532 |
{ |
|
533 |
if( this instanceof DistortedScreen ) |
|
534 |
mDebugLevel = bitmask; |
|
535 |
} |
|
536 |
|
|
537 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
538 |
|
|
539 | 537 |
/** |
540 | 538 |
* Draws all the attached children to this OutputSurface. |
541 | 539 |
* <p> |
... | ... | |
546 | 544 |
*/ |
547 | 545 |
public int render(long time) |
548 | 546 |
{ |
549 |
if( mDebugLevel!=0 ) prepareDebug(time); |
|
550 |
|
|
551 | 547 |
// change tree topology (attach and detach children) |
552 | 548 |
/* |
553 | 549 |
boolean changed1 = |
... | ... | |
592 | 588 |
setAsOutput(time); |
593 | 589 |
numRenders += renderChildren(time,mNumChildren,mChildren); |
594 | 590 |
|
595 |
if( mDebugLevel != 0 ) renderDebug(time); |
|
596 |
|
|
597 | 591 |
return numRenders; |
598 | 592 |
} |
599 | 593 |
|
... | ... | |
613 | 607 |
if( mTime!=time ) |
614 | 608 |
{ |
615 | 609 |
mTime = time; |
616 |
DistortedRenderState.colorDepthStencilOn(); |
|
617 |
GLES31.glClearColor(mClearR, mClearG, mClearB, mClearA); |
|
618 |
GLES31.glClearDepthf(mClearDepth); |
|
619 |
GLES31.glClearStencil(mClearStencil); |
|
620 |
GLES31.glClear(mClear); |
|
621 |
DistortedRenderState.colorDepthStencilRestore(); |
|
610 |
clear(); |
|
622 | 611 |
} |
623 | 612 |
} |
624 | 613 |
|
src/main/java/org/distorted/library/main/DistortedScreen.java | ||
---|---|---|
34 | 34 |
* <p> |
35 | 35 |
* User is able to render to it just like to a DistortedFramebuffer. |
36 | 36 |
*/ |
37 |
public class DistortedScreen extends DistortedOutputSurface
|
|
37 |
public class DistortedScreen extends DistortedFramebuffer
|
|
38 | 38 |
{ |
39 | 39 |
///// DEBUGGING ONLY ///////////////////////// |
40 |
private boolean mShowFPS; |
|
41 |
|
|
40 | 42 |
private static final int NUM_FRAMES = 100; |
41 | 43 |
|
42 | 44 |
private MeshObject fpsMesh; |
... | ... | |
46 | 48 |
private Bitmap fpsBitmap; |
47 | 49 |
private Paint mPaint; |
48 | 50 |
private int fpsH, fpsW; |
49 |
private String fpsString = "";
|
|
51 |
private String fpsString; |
|
50 | 52 |
private long lastTime=0; |
51 | 53 |
private long[] durations; |
52 | 54 |
private int currDuration; |
53 | 55 |
private static MatrixEffectMove mMoveEffect = new MatrixEffectMove( new Static3D(5,5,0) ); |
54 |
private boolean mInitialized;
|
|
56 |
///// END DEBUGGING //////////////////////////
|
|
55 | 57 |
|
56 | 58 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
57 |
// here we don't manage underlying OpenGL assets ourselves |
|
58 |
|
|
59 |
void create() {} |
|
60 |
void delete() {} |
|
61 |
void recreate() {} |
|
59 |
// PUBLIC API |
|
60 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
61 |
/** |
|
62 |
* Create a new Screen. Initially 1x1 in size. |
|
63 |
* <p> |
|
64 |
* Has to be followed by a 'resize()' to set the size. |
|
65 |
*/ |
|
66 |
public DistortedScreen() |
|
67 |
{ |
|
68 |
super(1,1,1,BOTH_DEPTH_STENCIL); |
|
69 |
mShowFPS = false; |
|
70 |
} |
|
62 | 71 |
|
63 | 72 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
73 |
/** |
|
74 |
* Draws all the attached children to this OutputSurface. |
|
75 |
* <p> |
|
76 |
* Must be called from a thread holding OpenGL Context. |
|
77 |
* |
|
78 |
* @param time Current time, in milliseconds. This will be passed to all the Effects stored in the children Nodes. |
|
79 |
* @return Number of objects rendered. |
|
80 |
*/ |
|
81 |
public int render(long time) |
|
82 |
{ |
|
83 |
if( mShowFPS ) |
|
84 |
{ |
|
85 |
if( lastTime==0 ) lastTime = time; |
|
64 | 86 |
|
65 |
private void initialize() |
|
87 |
currDuration++; |
|
88 |
if (currDuration >= NUM_FRAMES) currDuration = 0; |
|
89 |
durations[NUM_FRAMES] += ((time - lastTime) - durations[currDuration]); |
|
90 |
durations[currDuration] = time - lastTime; |
|
91 |
|
|
92 |
fpsString = "" + ((int)(10000.0f*NUM_FRAMES/durations[NUM_FRAMES]))/10.0f; |
|
93 |
|
|
94 |
mPaint.setColor(0xffffffff); |
|
95 |
fpsCanvas.drawRect(0, 0, fpsW, fpsH, mPaint); |
|
96 |
mPaint.setColor(0xff000000); |
|
97 |
fpsCanvas.drawText(fpsString, fpsW/2, 0.75f*fpsH, mPaint); |
|
98 |
fpsTexture.setTexture(fpsBitmap); |
|
99 |
|
|
100 |
lastTime = time; |
|
101 |
} |
|
102 |
|
|
103 |
int numrender = super.render(time); |
|
104 |
|
|
105 |
GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, 0); |
|
106 |
clear(); |
|
107 |
setAsInput(); |
|
108 |
GLES31.glColorMask(true,true,true,true); |
|
109 |
GLES31.glDepthMask(false); |
|
110 |
GLES31.glDisable(GLES31.GL_STENCIL_TEST); |
|
111 |
GLES31.glDisable(GLES31.GL_DEPTH_TEST); |
|
112 |
DistortedEffects.blitPriv(this); |
|
113 |
|
|
114 |
if( mShowFPS && fpsTexture.setAsInput()) |
|
115 |
{ |
|
116 |
fpsEffects.drawPriv(fpsW / 2.0f, fpsH / 2.0f, fpsMesh, this, time, 0); |
|
117 |
} |
|
118 |
|
|
119 |
return numrender+1; |
|
120 |
} |
|
121 |
|
|
122 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
123 |
/** |
|
124 |
* Make the library show Frames Per Second in the upper-left corner. |
|
125 |
* <p> |
|
126 |
*/ |
|
127 |
public void showFPS() |
|
66 | 128 |
{ |
129 |
mShowFPS = true; |
|
130 |
|
|
67 | 131 |
fpsW = 120; |
68 | 132 |
fpsH = 70; |
69 | 133 |
|
134 |
fpsString = ""; |
|
70 | 135 |
fpsBitmap = Bitmap.createBitmap(fpsW,fpsH, Bitmap.Config.ARGB_8888); |
71 | 136 |
fpsMesh = new MeshFlat(1,1); |
72 | 137 |
fpsTexture = new DistortedTexture(fpsW,fpsH); |
... | ... | |
85 | 150 |
|
86 | 151 |
for(int i=0; i<NUM_FRAMES+1; i++) durations[i]=16; // Assume FPS will be |
87 | 152 |
durations[NUM_FRAMES] = NUM_FRAMES*16; // close to 1000/16 ~ 60 |
88 |
mInitialized=true; |
|
89 |
} |
|
90 |
|
|
91 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
92 |
|
|
93 |
void prepareDebug(long time) |
|
94 |
{ |
|
95 |
if( !mInitialized ) initialize(); |
|
96 |
|
|
97 |
if( lastTime==0 ) lastTime = time; |
|
98 |
|
|
99 |
currDuration++; |
|
100 |
if (currDuration >= NUM_FRAMES) currDuration = 0; |
|
101 |
durations[NUM_FRAMES] += ((time - lastTime) - durations[currDuration]); |
|
102 |
durations[currDuration] = time - lastTime; |
|
103 |
|
|
104 |
fpsString = "" + ((int)(10000.0f*NUM_FRAMES/durations[NUM_FRAMES]))/10.0f; |
|
105 |
|
|
106 |
mPaint.setColor(0xffffffff); |
|
107 |
fpsCanvas.drawRect(0, 0, fpsW, fpsH, mPaint); |
|
108 |
mPaint.setColor(0xff000000); |
|
109 |
fpsCanvas.drawText(fpsString, fpsW/2, 0.75f*fpsH, mPaint); |
|
110 |
fpsTexture.setTexture(fpsBitmap); |
|
111 |
|
|
112 |
lastTime = time; |
|
113 |
} |
|
114 |
|
|
115 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
116 |
|
|
117 |
void renderDebug(long time) |
|
118 |
{ |
|
119 |
if (fpsTexture.setAsInput()) |
|
120 |
{ |
|
121 |
setAsOutput(time); |
|
122 |
GLES31.glColorMask(true,true,true,true); |
|
123 |
GLES31.glDepthMask(false); |
|
124 |
GLES31.glDisable(GLES31.GL_STENCIL_TEST); |
|
125 |
GLES31.glDisable(GLES31.GL_DEPTH_TEST); |
|
126 |
fpsEffects.drawPriv(fpsW/2.0f, fpsH/2.0f, fpsMesh, this, time, 0); |
|
127 |
} |
|
128 |
} |
|
129 |
|
|
130 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
131 |
// PUBLIC API |
|
132 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
133 |
/** |
|
134 |
* Create a new Screen. |
|
135 |
* <p> |
|
136 |
* Has to be followed by a 'resize()' to set the size. |
|
137 |
*/ |
|
138 |
public DistortedScreen() |
|
139 |
{ |
|
140 |
// Screen also has to be created (3rd arg 'NOT_CREATED_YET') because of the SSBO inside OutputSurface. |
|
141 |
super(0,0,NOT_CREATED_YET,1,DEPTH_NO_STENCIL,0,TYPE_USER); |
|
142 |
|
|
143 |
mInitialized = false; |
|
144 | 153 |
} |
145 | 154 |
} |
Also available in: Unified diff
Reengineer DistortedScreen