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.main;
|
21
|
|
22
|
import android.opengl.GLES31;
|
23
|
import android.opengl.Matrix;
|
24
|
|
25
|
import org.distorted.library.effect.EffectQuality;
|
26
|
|
27
|
import java.util.ArrayList;
|
28
|
|
29
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
30
|
/**
|
31
|
* This is not really part of the public API.
|
32
|
*
|
33
|
* @y.exclude
|
34
|
*/
|
35
|
public abstract class DistortedOutputSurface extends DistortedSurface implements DistortedMaster.Slave
|
36
|
{
|
37
|
/**
|
38
|
* Do not create DEPTH or STENCIL attachment
|
39
|
*/
|
40
|
public static final int NO_DEPTH_NO_STENCIL = 0;
|
41
|
/**
|
42
|
* Create DEPTH, but not STENCIL
|
43
|
*/
|
44
|
public static final int DEPTH_NO_STENCIL = 1;
|
45
|
/**
|
46
|
* Create both DEPTH and STENCIL
|
47
|
*/
|
48
|
public static final int BOTH_DEPTH_STENCIL = 2;
|
49
|
|
50
|
private static final int ATTACH = 0;
|
51
|
private static final int DETACH = 1;
|
52
|
private static final int DETALL = 2;
|
53
|
private static final int SORT = 3;
|
54
|
|
55
|
private ArrayList<DistortedNode> mChildren;
|
56
|
private int mNumChildren; // ==mChildren.length(), but we only create mChildren if the first one gets added
|
57
|
|
58
|
private class Job
|
59
|
{
|
60
|
int type;
|
61
|
DistortedNode node;
|
62
|
|
63
|
Job(int t, DistortedNode n)
|
64
|
{
|
65
|
type = t;
|
66
|
node = n;
|
67
|
}
|
68
|
}
|
69
|
|
70
|
private ArrayList<Job> mJobs = new ArrayList<>();
|
71
|
|
72
|
// Global buffers used for postprocessing. MipmapLevels x PING_PONG of each for postprocessing ping-pong.
|
73
|
private static final int PING_PONG = 2;
|
74
|
private static DistortedOutputSurface[][] mBuffer = null;
|
75
|
|
76
|
private long mTime;
|
77
|
private float mFOV;
|
78
|
float mDistance, mNear;
|
79
|
float[] mProjectionMatrix;
|
80
|
|
81
|
int mDepthStencilCreated;
|
82
|
int mDepthStencil;
|
83
|
int[] mDepthStencilH = new int[1];
|
84
|
int[] mFBOH = new int[1];
|
85
|
|
86
|
private float mClearR, mClearG, mClearB, mClearA;
|
87
|
private float mClearDepth;
|
88
|
private int mClearStencil;
|
89
|
private int mClear;
|
90
|
float mMipmap;
|
91
|
|
92
|
int mRealWidth; // the Surface can be backed up with a texture that is
|
93
|
int mRealHeight; // larger than the viewport we have to it.
|
94
|
// mWidth,mHeight are the sizes of the Viewport, those -
|
95
|
// sizes of the backing up texture.
|
96
|
|
97
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
98
|
|
99
|
DistortedOutputSurface(int width, int height, int createColor, int numcolors, int depthStencil, int fbo, int type)
|
100
|
{
|
101
|
super(width,height,createColor,numcolors,type);
|
102
|
|
103
|
mRealWidth = width;
|
104
|
mRealHeight= height;
|
105
|
|
106
|
mProjectionMatrix = new float[16];
|
107
|
|
108
|
mFOV = 60.0f;
|
109
|
mNear= 0.5f;
|
110
|
|
111
|
mDepthStencilCreated= (depthStencil== NO_DEPTH_NO_STENCIL ? DONT_CREATE:NOT_CREATED_YET);
|
112
|
mDepthStencil = depthStencil;
|
113
|
|
114
|
mFBOH[0] = fbo;
|
115
|
mDepthStencilH[0]= 0;
|
116
|
|
117
|
mTime = 0;
|
118
|
|
119
|
mClearR = 0.0f;
|
120
|
mClearG = 0.0f;
|
121
|
mClearB = 0.0f;
|
122
|
mClearA = 0.0f;
|
123
|
|
124
|
mClearDepth = 1.0f;
|
125
|
mClearStencil = 0;
|
126
|
mClear = GLES31.GL_DEPTH_BUFFER_BIT | GLES31.GL_COLOR_BUFFER_BIT;
|
127
|
|
128
|
mMipmap = 1.0f;
|
129
|
|
130
|
createProjection();
|
131
|
}
|
132
|
|
133
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
134
|
|
135
|
private void createProjection()
|
136
|
{
|
137
|
if( mWidth>0 && mHeight>1 )
|
138
|
{
|
139
|
if( mFOV>0.0f ) // perspective projection
|
140
|
{
|
141
|
float a = 2.0f*(float)Math.tan(mFOV*Math.PI/360);
|
142
|
float q = mWidth*mNear;
|
143
|
float c = mHeight*mNear;
|
144
|
|
145
|
float left = -q/2;
|
146
|
float right = +q/2;
|
147
|
float bottom = -c/2;
|
148
|
float top = +c/2;
|
149
|
float near = c/a;
|
150
|
|
151
|
mDistance = mHeight/a;
|
152
|
float far = 2*mDistance-near;
|
153
|
|
154
|
Matrix.frustumM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
|
155
|
}
|
156
|
else // parallel projection
|
157
|
{
|
158
|
float left = -mWidth/2.0f;
|
159
|
float right = +mWidth/2.0f;
|
160
|
float bottom = -mHeight/2.0f;
|
161
|
float top = +mHeight/2.0f;
|
162
|
float near = mWidth+mHeight-mHeight*(1.0f-mNear);
|
163
|
mDistance = mWidth+mHeight;
|
164
|
float far = mWidth+mHeight+mHeight*(1.0f-mNear);
|
165
|
|
166
|
Matrix.orthoM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
|
167
|
}
|
168
|
}
|
169
|
}
|
170
|
|
171
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
172
|
|
173
|
private static void createPostprocessingBuffers(int width, int height, float near)
|
174
|
{
|
175
|
mBuffer = new DistortedOutputSurface[EffectQuality.LENGTH][PING_PONG];
|
176
|
float mipmap=1.0f;
|
177
|
|
178
|
for (int i=0; i<EffectQuality.LENGTH; i++)
|
179
|
{
|
180
|
mBuffer[i][0] = new DistortedFramebuffer(1, BOTH_DEPTH_STENCIL, TYPE_SYST, (int) (width * mipmap), (int) (height * mipmap));
|
181
|
mBuffer[i][0].mMipmap = mipmap;
|
182
|
mBuffer[i][0].mNear = near; // copy mNear as well (for blitting- see PostprocessEffect.apply() )
|
183
|
mBuffer[i][0].glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
|
184
|
|
185
|
for(int j=1; j<PING_PONG; j++)
|
186
|
{
|
187
|
mBuffer[i][j] = new DistortedFramebuffer(1, NO_DEPTH_NO_STENCIL, TYPE_SYST, (int) (width * mipmap), (int) (height * mipmap));
|
188
|
mBuffer[i][j].mMipmap = mipmap;
|
189
|
mBuffer[i][j].mNear = near;
|
190
|
mBuffer[i][j].glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
|
191
|
}
|
192
|
|
193
|
mipmap *= EffectQuality.MULTIPLIER;
|
194
|
}
|
195
|
|
196
|
DistortedObject.toDo(); // create the FBOs immediately. This is safe as we must be holding the OpenGL context now.
|
197
|
|
198
|
for (int i=0; i<EffectQuality.LENGTH; i++)
|
199
|
{
|
200
|
for(int j=1; j<PING_PONG; j++)
|
201
|
{
|
202
|
( (DistortedFramebuffer)mBuffer[i][j]).copyDepthAndStencil( (DistortedFramebuffer)mBuffer[i][0] );
|
203
|
}
|
204
|
}
|
205
|
|
206
|
GLES31.glStencilMask(0xff);
|
207
|
GLES31.glDepthMask(true);
|
208
|
GLES31.glColorMask(true, true, true, true);
|
209
|
GLES31.glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
|
210
|
GLES31.glClearDepthf(1.0f);
|
211
|
GLES31.glClearStencil(0);
|
212
|
|
213
|
for (int i=0; i<EffectQuality.LENGTH; i++)
|
214
|
{
|
215
|
mBuffer[i][0].setAsOutput();
|
216
|
GLES31.glClear(GLES31.GL_COLOR_BUFFER_BIT | GLES31.GL_DEPTH_BUFFER_BIT | GLES31.GL_STENCIL_BUFFER_BIT);
|
217
|
|
218
|
for(int j=1; j<PING_PONG; j++)
|
219
|
{
|
220
|
mBuffer[i][j].setAsOutput();
|
221
|
GLES31.glClear(GLES31.GL_COLOR_BUFFER_BIT);
|
222
|
}
|
223
|
}
|
224
|
}
|
225
|
|
226
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
227
|
|
228
|
static synchronized void onDestroy()
|
229
|
{
|
230
|
if( mBuffer!=null )
|
231
|
{
|
232
|
for (int i=0; i<EffectQuality.LENGTH; i++)
|
233
|
{
|
234
|
for (int j=1; j<PING_PONG; j++)
|
235
|
{
|
236
|
mBuffer[i][j] = null;
|
237
|
}
|
238
|
}
|
239
|
|
240
|
mBuffer = null;
|
241
|
}
|
242
|
}
|
243
|
|
244
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
245
|
// The postprocessing buffers mBuffer[] are generally speaking too large (there's just one static
|
246
|
// set of them) so before we use them for output, we need to adjust the Viewport as if they were
|
247
|
// smaller. That takes care of outputting pixels to them. When we use them as input, we have to
|
248
|
// adjust the texture coords - see the get{Width|Height}Correction functions.
|
249
|
|
250
|
private static void clonePostprocessingViewport(DistortedOutputSurface from)
|
251
|
{
|
252
|
if( mBuffer[0][0].mWidth != from.mWidth || mBuffer[0][0].mHeight != from.mHeight )
|
253
|
{
|
254
|
DistortedOutputSurface surface;
|
255
|
|
256
|
for(int i=0; i<EffectQuality.LENGTH; i++)
|
257
|
{
|
258
|
for(int j=0; j<PING_PONG; j++)
|
259
|
{
|
260
|
surface = mBuffer[i][j];
|
261
|
|
262
|
surface.mWidth = (int) (from.mWidth * surface.mMipmap);
|
263
|
surface.mHeight = (int) (from.mHeight * surface.mMipmap);
|
264
|
|
265
|
surface.mNear = from.mNear; // Near plane is independent of the mipmap level
|
266
|
|
267
|
//android.util.Log.e("surface", "viewport "+i+" to ("+from.mWidth+"x"+from.mHeight+")");
|
268
|
|
269
|
surface.createProjection();
|
270
|
|
271
|
int maxw = surface.mWidth > surface.mRealWidth ? surface.mWidth : surface.mRealWidth;
|
272
|
int maxh = surface.mHeight > surface.mRealHeight ? surface.mHeight : surface.mRealHeight;
|
273
|
|
274
|
if (maxw > surface.mRealWidth || maxh > surface.mRealHeight)
|
275
|
{
|
276
|
surface.mRealWidth = maxw;
|
277
|
surface.mRealHeight = maxh;
|
278
|
|
279
|
surface.recreate();
|
280
|
surface.create();
|
281
|
}
|
282
|
}
|
283
|
}
|
284
|
}
|
285
|
}
|
286
|
|
287
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
288
|
|
289
|
private static void oitClear(DistortedOutputSurface buffer)
|
290
|
{
|
291
|
DistortedEffects.zeroOutAtomic();
|
292
|
DistortedEffects.oitClear(buffer);
|
293
|
GLES31.glMemoryBarrier(GLES31.GL_SHADER_STORAGE_BARRIER_BIT|GLES31.GL_ATOMIC_COUNTER_BARRIER_BIT);
|
294
|
}
|
295
|
|
296
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
297
|
|
298
|
private int oitBuild(DistortedOutputSurface buffer)
|
299
|
{
|
300
|
GLES31.glViewport(0, 0, mWidth, mHeight);
|
301
|
setAsOutput();
|
302
|
GLES31.glActiveTexture(GLES31.GL_TEXTURE0);
|
303
|
GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, buffer.mColorH[0]);
|
304
|
GLES31.glActiveTexture(GLES31.GL_TEXTURE1);
|
305
|
GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, buffer.mDepthStencilH[0]);
|
306
|
|
307
|
DistortedRenderState.colorDepthStencilOn();
|
308
|
DistortedRenderState.enableDepthTest();
|
309
|
|
310
|
DistortedEffects.oitBuild(this, buffer.getWidthCorrection(), buffer.getHeightCorrection() );
|
311
|
GLES31.glActiveTexture(GLES31.GL_TEXTURE0);
|
312
|
GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, 0);
|
313
|
GLES31.glActiveTexture(GLES31.GL_TEXTURE1);
|
314
|
GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, 0);
|
315
|
|
316
|
DistortedRenderState.colorDepthStencilRestore();
|
317
|
DistortedRenderState.restoreDepthTest();
|
318
|
|
319
|
return 1;
|
320
|
}
|
321
|
|
322
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
323
|
// two phases: 1. collapse the SSBO 2. blend the SSBO's color
|
324
|
|
325
|
private int oitRender(long currTime)
|
326
|
{
|
327
|
float corrW = getWidthCorrection();
|
328
|
float corrH = getHeightCorrection();
|
329
|
|
330
|
// Do the Collapse Pass only if we do have a Depth attachment.
|
331
|
// Otherwise there's no point (in fact we then would create a feedback loop!)
|
332
|
|
333
|
if( mDepthStencilH[0] != 0 )
|
334
|
{
|
335
|
GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, 0);
|
336
|
GLES31.glActiveTexture(GLES31.GL_TEXTURE1);
|
337
|
GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, mDepthStencilH[0]);
|
338
|
DistortedRenderState.switchOffColorDepthStencil();
|
339
|
DistortedEffects.oitCollapse(this, corrW, corrH);
|
340
|
GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, 0);
|
341
|
}
|
342
|
|
343
|
setAsOutput(currTime);
|
344
|
DistortedRenderState.switchColorDepthOnStencilOff();
|
345
|
DistortedEffects.oitRender(this, corrW, corrH);
|
346
|
DistortedRenderState.restoreColorDepthStencil();
|
347
|
|
348
|
return 1;
|
349
|
}
|
350
|
|
351
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
352
|
|
353
|
private static void clearPostprocessingBuffer(int quality)
|
354
|
{
|
355
|
GLES31.glStencilMask(0xff);
|
356
|
GLES31.glDepthMask(true);
|
357
|
GLES31.glColorMask(true,true,true,true);
|
358
|
GLES31.glClearColor(1.0f,1.0f,1.0f,0.0f);
|
359
|
GLES31.glClearDepthf(1.0f);
|
360
|
GLES31.glClearStencil(0);
|
361
|
|
362
|
mBuffer[quality][0].setAsOutput();
|
363
|
GLES31.glClear(GLES31.GL_COLOR_BUFFER_BIT|GLES31.GL_DEPTH_BUFFER_BIT|GLES31.GL_STENCIL_BUFFER_BIT);
|
364
|
|
365
|
for(int j=1; j<PING_PONG; j++)
|
366
|
{
|
367
|
mBuffer[quality][j].setAsOutput();
|
368
|
GLES31.glClear(GLES31.GL_COLOR_BUFFER_BIT);
|
369
|
}
|
370
|
}
|
371
|
|
372
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
373
|
|
374
|
void clear()
|
375
|
{
|
376
|
DistortedRenderState.colorDepthStencilOn();
|
377
|
GLES31.glClearColor(mClearR, mClearG, mClearB, mClearA);
|
378
|
GLES31.glClearDepthf(mClearDepth);
|
379
|
GLES31.glClearStencil(mClearStencil);
|
380
|
GLES31.glClear(mClear);
|
381
|
DistortedRenderState.colorDepthStencilRestore();
|
382
|
}
|
383
|
|
384
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
385
|
// Render all children, one by one. If there are no postprocessing effects, just render to THIS.
|
386
|
// Otherwise, render to a buffer and on each change of Postprocessing Bucket, apply the postprocessing
|
387
|
// to a whole buffer (lastQueue.postprocess) and merge it (this.oitBuild).
|
388
|
|
389
|
int renderChildren(long time, int numChildren, ArrayList<DistortedNode> children)
|
390
|
{
|
391
|
int quality=0, internalQuality = 0, numRenders = 0, bucketChange = 0;
|
392
|
DistortedNode child1, child2;
|
393
|
EffectQueuePostprocess lastQueue=null, currQueue;
|
394
|
long lastBucket=0, currBucket;
|
395
|
|
396
|
for(int i=0; i<numChildren; i++)
|
397
|
{
|
398
|
child1 = children.get(i);
|
399
|
currQueue = child1.getPostprocessQueue();
|
400
|
currBucket= currQueue.getID();
|
401
|
|
402
|
if( currBucket==0 )
|
403
|
{
|
404
|
GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, mFBOH[0]);
|
405
|
numRenders += child1.draw(time, this);
|
406
|
}
|
407
|
else
|
408
|
{
|
409
|
if( mBuffer==null ) createPostprocessingBuffers(mWidth,mHeight,mNear);
|
410
|
|
411
|
if( lastBucket!=currBucket )
|
412
|
{
|
413
|
if( lastBucket==0 )
|
414
|
{
|
415
|
clonePostprocessingViewport(this);
|
416
|
oitClear(this);
|
417
|
}
|
418
|
else
|
419
|
{
|
420
|
for(int j=bucketChange; j<i; j++)
|
421
|
{
|
422
|
child2 = children.get(j);
|
423
|
numRenders += child2.markStencilAndDepth(time,mBuffer[internalQuality][0],lastQueue);
|
424
|
}
|
425
|
|
426
|
numRenders += lastQueue.postprocess(mBuffer);
|
427
|
numRenders += oitBuild(mBuffer[quality][0]);
|
428
|
GLES31.glMemoryBarrier(GLES31.GL_SHADER_STORAGE_BARRIER_BIT|GLES31.GL_ATOMIC_COUNTER_BARRIER_BIT);
|
429
|
clearPostprocessingBuffer(quality);
|
430
|
}
|
431
|
|
432
|
internalQuality = currQueue.getInternalQuality();
|
433
|
quality = currQueue.getQuality();
|
434
|
bucketChange = i;
|
435
|
}
|
436
|
|
437
|
mBuffer[quality][0].setAsOutput(time);
|
438
|
child1.drawNoBlend(time,mBuffer[quality][0]);
|
439
|
|
440
|
if( i==numChildren-1 )
|
441
|
{
|
442
|
for(int j=bucketChange; j<numChildren; j++)
|
443
|
{
|
444
|
child2 = children.get(j);
|
445
|
numRenders += child2.markStencilAndDepth(time,mBuffer[internalQuality][0],currQueue);
|
446
|
}
|
447
|
|
448
|
numRenders += currQueue.postprocess(mBuffer);
|
449
|
numRenders += oitBuild(mBuffer[quality][0]);
|
450
|
GLES31.glMemoryBarrier(GLES31.GL_SHADER_STORAGE_BARRIER_BIT|GLES31.GL_ATOMIC_COUNTER_BARRIER_BIT);
|
451
|
numRenders += oitRender(time); // merge the OIT linked list
|
452
|
clearPostprocessingBuffer(quality);
|
453
|
}
|
454
|
} // end else (postprocessed child)
|
455
|
|
456
|
lastQueue = currQueue;
|
457
|
lastBucket= currBucket;
|
458
|
} // end main for loop
|
459
|
|
460
|
return numRenders;
|
461
|
}
|
462
|
|
463
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
464
|
|
465
|
ArrayList<DistortedNode> getChildren()
|
466
|
{
|
467
|
return mChildren;
|
468
|
}
|
469
|
|
470
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
471
|
/**
|
472
|
* Not part of the Public API.
|
473
|
*
|
474
|
* @y.exclude
|
475
|
*/
|
476
|
public float getWidthCorrection()
|
477
|
{
|
478
|
return (float)mWidth/mRealWidth;
|
479
|
}
|
480
|
|
481
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
482
|
/**
|
483
|
* Not part of the Public API.
|
484
|
*
|
485
|
* @y.exclude
|
486
|
*/
|
487
|
public float getHeightCorrection()
|
488
|
{
|
489
|
return (float)mHeight/mRealHeight;
|
490
|
}
|
491
|
|
492
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
493
|
// PUBLIC API
|
494
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
495
|
/**
|
496
|
* Draws all the attached children to this OutputSurface.
|
497
|
* <p>
|
498
|
* Must be called from a thread holding OpenGL Context.
|
499
|
*
|
500
|
* @param time Current time, in milliseconds. This will be passed to all the Effects stored in the children Nodes.
|
501
|
* @return Number of objects rendered.
|
502
|
*/
|
503
|
public int render(long time)
|
504
|
{
|
505
|
// change tree topology (attach and detach children)
|
506
|
/*
|
507
|
boolean changed1 =
|
508
|
*/
|
509
|
DistortedMaster.toDo();
|
510
|
/*
|
511
|
if( changed1 )
|
512
|
{
|
513
|
for(int i=0; i<mNumChildren; i++)
|
514
|
{
|
515
|
mChildren.get(i).debug(0);
|
516
|
}
|
517
|
|
518
|
DistortedNode.debugMap();
|
519
|
}
|
520
|
*/
|
521
|
// create and delete all underlying OpenGL resources
|
522
|
// Watch out: FIRST change topology, only then deal
|
523
|
// with OpenGL resources. That's because changing Tree
|
524
|
// can result in additional Framebuffers that would need
|
525
|
// to be created immediately, before the calls to drawRecursive()
|
526
|
/*
|
527
|
boolean changed2 =
|
528
|
*/
|
529
|
toDo();
|
530
|
/*
|
531
|
if( changed2 )
|
532
|
{
|
533
|
DistortedObject.debugLists();
|
534
|
}
|
535
|
*/
|
536
|
// mark OpenGL state as unknown
|
537
|
DistortedRenderState.reset();
|
538
|
|
539
|
int numRenders=0;
|
540
|
|
541
|
for(int i=0; i<mNumChildren; i++)
|
542
|
{
|
543
|
numRenders += mChildren.get(i).renderRecursive(time);
|
544
|
}
|
545
|
|
546
|
setAsOutput(time);
|
547
|
numRenders += renderChildren(time,mNumChildren,mChildren);
|
548
|
|
549
|
return numRenders;
|
550
|
}
|
551
|
|
552
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
553
|
/**
|
554
|
* Bind this Surface as a Framebuffer we can render to.
|
555
|
*
|
556
|
* @param time Present time, in milliseconds. The point: looking at this param the library can figure
|
557
|
* out if this is the first time during present frame that this FBO is being set as output.
|
558
|
* If so, the library, in addition to binding the Surface for output, also clears the
|
559
|
* Surface's color and depth attachments.
|
560
|
*/
|
561
|
public void setAsOutput(long time)
|
562
|
{
|
563
|
GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, mFBOH[0]);
|
564
|
|
565
|
if( mTime!=time )
|
566
|
{
|
567
|
mTime = time;
|
568
|
clear();
|
569
|
}
|
570
|
}
|
571
|
|
572
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
573
|
/**
|
574
|
* Bind this Surface as a Framebuffer we can render to.
|
575
|
* <p>
|
576
|
* This version does not attempt to clear anything.
|
577
|
*/
|
578
|
|
579
|
public void setAsOutput()
|
580
|
{
|
581
|
GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, mFBOH[0]);
|
582
|
}
|
583
|
|
584
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
585
|
/**
|
586
|
* Return the Near plane of the Projection included in the Surface.
|
587
|
*
|
588
|
* @return the Near plane.
|
589
|
*/
|
590
|
public float getNear()
|
591
|
{
|
592
|
return mNear;
|
593
|
}
|
594
|
|
595
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
596
|
/**
|
597
|
* Set mipmap level.
|
598
|
* <p>
|
599
|
* Trick for speeding up your renders - one can create a pyramid of OutputSurface objects, each next
|
600
|
* one some constant FACTOR smaller than the previous (0.5 is the common value), then set the Mipmap
|
601
|
* Level of the i-th object to be FACTOR^i (we start counting from 0). When rendering any scene into
|
602
|
* such prepared OutputSurface, the library will make sure to scale any Effects used so that the end
|
603
|
* scene will end up looking identical no matter which object we render to. Identical, that is, except
|
604
|
* for the loss of quality and gain in speed associated with rendering to a smaller Surface.
|
605
|
* <p>
|
606
|
* Example: if you create two FBOs, one 1000x1000 and another 500x500 in size, and set the second one
|
607
|
* mipmap to 0.5 (the first one's is 1.0 by default), define Effects to be a single move by (100,100),
|
608
|
* and render a skinned Mesh into both FBO, the end result will look proportionally the same, because
|
609
|
* in the second case the move vector (100,100) will be auto-scaled to (50,50).
|
610
|
*
|
611
|
* @param mipmap The mipmap level. Acceptable range: 0<mipmap<infinity, although mipmap>1
|
612
|
* does not make any sense (that would result in loss of speed and no gain in quality)
|
613
|
*/
|
614
|
public void setMipmap(float mipmap)
|
615
|
{
|
616
|
mMipmap = mipmap;
|
617
|
}
|
618
|
|
619
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
620
|
/**
|
621
|
* Set the (R,G,B,A) values of GLES31.glClearColor() to set up color with which to clear
|
622
|
* this Surface at the beginning of each frame.
|
623
|
*
|
624
|
* @param r the Red component. Default: 0.0f
|
625
|
* @param g the Green component. Default: 0.0f
|
626
|
* @param b the Blue component. Default: 0.0f
|
627
|
* @param a the Alpha component. Default: 0.0f
|
628
|
*/
|
629
|
public void glClearColor(float r, float g, float b, float a)
|
630
|
{
|
631
|
mClearR = r;
|
632
|
mClearG = g;
|
633
|
mClearB = b;
|
634
|
mClearA = a;
|
635
|
}
|
636
|
|
637
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
638
|
/**
|
639
|
* Uses glClearDepthf() to set up a value with which to clear
|
640
|
* the Depth buffer of our Surface at the beginning of each frame.
|
641
|
*
|
642
|
* @param d the Depth. Default: 1.0f
|
643
|
*/
|
644
|
public void glClearDepthf(float d)
|
645
|
{
|
646
|
mClearDepth = d;
|
647
|
}
|
648
|
|
649
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
650
|
/**
|
651
|
* Uses glClearStencil() to set up a value with which to clear the
|
652
|
* Stencil buffer of our Surface at the beginning of each frame.
|
653
|
*
|
654
|
* @param s the Stencil. Default: 0
|
655
|
*/
|
656
|
public void glClearStencil(int s)
|
657
|
{
|
658
|
mClearStencil = s;
|
659
|
}
|
660
|
|
661
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
662
|
/**
|
663
|
* Which buffers to Clear at the beginning of each frame?
|
664
|
* <p>
|
665
|
* Valid values: 0, or bitwise OR of one or more values from the set GL_COLOR_BUFFER_BIT,
|
666
|
* GL_DEPTH_BUFFER_BIT, GL_STENCIL_BUFFER_BIT.
|
667
|
* Default: GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT.
|
668
|
*
|
669
|
* @param mask bitwise OR of BUFFER_BITs to clear.
|
670
|
*/
|
671
|
public void glClear(int mask)
|
672
|
{
|
673
|
mClear = mask;
|
674
|
}
|
675
|
|
676
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
677
|
/**
|
678
|
* Create new Projection matrix.
|
679
|
*
|
680
|
* @param fov Vertical 'field of view' of the Projection frustrum (in degrees).
|
681
|
* Valid values: 0<=fov<180. FOV==0 means 'parallel projection'.
|
682
|
* @param near Distance between the screen plane and the near plane.
|
683
|
* Valid vaules: 0<near<1. When near==0 (illegal!), the Near Plane is exactly at the tip of
|
684
|
* the pyramid. When near==1 (illegal!) the near plane is equivalent to the screen plane.
|
685
|
*/
|
686
|
public void setProjection(float fov, float near)
|
687
|
{
|
688
|
if( fov < 180.0f && fov >=0.0f )
|
689
|
{
|
690
|
mFOV = fov;
|
691
|
}
|
692
|
|
693
|
if( near< 1.0f && near> 0.0f )
|
694
|
{
|
695
|
mNear= near;
|
696
|
}
|
697
|
else if( near<=0.0f )
|
698
|
{
|
699
|
mNear = 0.01f;
|
700
|
}
|
701
|
else if( near>=1.0f )
|
702
|
{
|
703
|
mNear=0.99f;
|
704
|
}
|
705
|
|
706
|
createProjection();
|
707
|
}
|
708
|
|
709
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
710
|
/**
|
711
|
* Resize the underlying Framebuffer.
|
712
|
* <p>
|
713
|
* This method can be safely called mid-render as it doesn't interfere with rendering.
|
714
|
*
|
715
|
* @param width The new width.
|
716
|
* @param height The new height.
|
717
|
*/
|
718
|
public void resize(int width, int height)
|
719
|
{
|
720
|
if( mWidth!=width || mHeight!=height )
|
721
|
{
|
722
|
mWidth = mRealWidth = width;
|
723
|
mHeight= mRealHeight= height;
|
724
|
|
725
|
createProjection();
|
726
|
|
727
|
if( mColorCreated==CREATED )
|
728
|
{
|
729
|
markForCreation();
|
730
|
recreate();
|
731
|
}
|
732
|
}
|
733
|
}
|
734
|
|
735
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
736
|
/**
|
737
|
* Return true if the Surface contains a DEPTH attachment.
|
738
|
*
|
739
|
* @return <bold>true</bold> if the Surface contains a DEPTH attachment.
|
740
|
*/
|
741
|
public boolean hasDepth()
|
742
|
{
|
743
|
return mDepthStencilCreated==CREATED;
|
744
|
}
|
745
|
|
746
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
747
|
/**
|
748
|
* Return true if the Surface contains a STENCIL attachment.
|
749
|
*
|
750
|
* @return <bold>true</bold> if the Surface contains a STENCIL attachment.
|
751
|
*/
|
752
|
public boolean hasStencil()
|
753
|
{
|
754
|
return (mDepthStencilCreated==CREATED && mDepthStencil==BOTH_DEPTH_STENCIL);
|
755
|
}
|
756
|
|
757
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
758
|
/**
|
759
|
* Adds a new child to the last position in the list of our Surface's children.
|
760
|
* <p>
|
761
|
* We cannot do this mid-render - actual attachment will be done just before the next render, by the
|
762
|
* DistortedMaster (by calling doWork())
|
763
|
*
|
764
|
* @param node The new Node to add.
|
765
|
*/
|
766
|
public void attach(DistortedNode node)
|
767
|
{
|
768
|
mJobs.add(new Job(ATTACH,node));
|
769
|
DistortedMaster.newSlave(this);
|
770
|
}
|
771
|
|
772
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
773
|
/**
|
774
|
* Adds a new child to the last position in the list of our Surface's children.
|
775
|
* <p>
|
776
|
* We cannot do this mid-render - actual attachment will be done just before the next render, by the
|
777
|
* DistortedMaster (by calling doWork())
|
778
|
*
|
779
|
* @param surface InputSurface to initialize our child Node with.
|
780
|
* @param effects DistortedEffects to initialize our child Node with.
|
781
|
* @param mesh MeshObject to initialize our child Node with.
|
782
|
* @return the newly constructed child Node, or null if we couldn't allocate resources.
|
783
|
*/
|
784
|
public DistortedNode attach(DistortedInputSurface surface, DistortedEffects effects, MeshObject mesh)
|
785
|
{
|
786
|
DistortedNode node = new DistortedNode(surface,effects,mesh);
|
787
|
mJobs.add(new Job(ATTACH,node));
|
788
|
DistortedMaster.newSlave(this);
|
789
|
return node;
|
790
|
}
|
791
|
|
792
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
793
|
/**
|
794
|
* Removes the first occurrence of a specified child from the list of children of our Surface.
|
795
|
* <p>
|
796
|
* A bit questionable method as there can be many different Nodes attached as children, some
|
797
|
* of them having the same Effects but - for instance - different Mesh. Use with care.
|
798
|
* <p>
|
799
|
* We cannot do this mid-render - actual detachment will be done just before the next render, by the
|
800
|
* DistortedMaster (by calling doWork())
|
801
|
*
|
802
|
* @param effects DistortedEffects to remove.
|
803
|
*/
|
804
|
public void detach(DistortedEffects effects)
|
805
|
{
|
806
|
long id = effects.getID();
|
807
|
DistortedNode node;
|
808
|
boolean detached = false;
|
809
|
|
810
|
for(int i=0; i<mNumChildren; i++)
|
811
|
{
|
812
|
node = mChildren.get(i);
|
813
|
|
814
|
if( node.getEffects().getID()==id )
|
815
|
{
|
816
|
detached = true;
|
817
|
mJobs.add(new Job(DETACH,node));
|
818
|
DistortedMaster.newSlave(this);
|
819
|
break;
|
820
|
}
|
821
|
}
|
822
|
|
823
|
if( !detached )
|
824
|
{
|
825
|
// if we failed to detach any, it still might be the case that
|
826
|
// there's an ATTACH job that we need to cancel.
|
827
|
int num = mJobs.size();
|
828
|
Job job;
|
829
|
|
830
|
for(int i=0; i<num; i++)
|
831
|
{
|
832
|
job = mJobs.get(i);
|
833
|
|
834
|
if( job.type==ATTACH && job.node.getEffects()==effects )
|
835
|
{
|
836
|
mJobs.remove(i);
|
837
|
break;
|
838
|
}
|
839
|
}
|
840
|
}
|
841
|
}
|
842
|
|
843
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
844
|
/**
|
845
|
* Removes the first occurrence of a specified child from the list of children of our Surface.
|
846
|
* <p>
|
847
|
* We cannot do this mid-render - actual attachment will be done just before the next render, by the
|
848
|
* DistortedMaster (by calling doWork())
|
849
|
*
|
850
|
* @param node The Node to remove.
|
851
|
*/
|
852
|
public void detach(DistortedNode node)
|
853
|
{
|
854
|
mJobs.add(new Job(DETACH,node));
|
855
|
DistortedMaster.newSlave(this);
|
856
|
}
|
857
|
|
858
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
859
|
/**
|
860
|
* Removes all children Nodes.
|
861
|
* <p>
|
862
|
* We cannot do this mid-render - actual attachment will be done just before the next render, by the
|
863
|
* DistortedMaster (by calling doWork())
|
864
|
*/
|
865
|
public void detachAll()
|
866
|
{
|
867
|
mJobs.add(new Job(DETALL,null));
|
868
|
DistortedMaster.newSlave(this);
|
869
|
}
|
870
|
|
871
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
872
|
/**
|
873
|
* This is not really part of the public API. Has to be public only because it is a part of the
|
874
|
* DistortedSlave interface, which should really be a class that we extend here instead but
|
875
|
* Java has no multiple inheritance.
|
876
|
*
|
877
|
* @y.exclude
|
878
|
*/
|
879
|
public void doWork()
|
880
|
{
|
881
|
int num = mJobs.size();
|
882
|
Job job;
|
883
|
|
884
|
for(int i=0; i<num; i++)
|
885
|
{
|
886
|
job = mJobs.remove(0);
|
887
|
|
888
|
switch(job.type)
|
889
|
{
|
890
|
case ATTACH: if( mChildren==null ) mChildren = new ArrayList<>(2);
|
891
|
job.node.setSurfaceParent(this);
|
892
|
DistortedMaster.addSortingByBuckets(mChildren,job.node);
|
893
|
mNumChildren++;
|
894
|
break;
|
895
|
case DETACH: if( mNumChildren>0 && mChildren.remove(job.node) )
|
896
|
{
|
897
|
job.node.setSurfaceParent(null);
|
898
|
mNumChildren--;
|
899
|
}
|
900
|
break;
|
901
|
case DETALL: if( mNumChildren>0 )
|
902
|
{
|
903
|
DistortedNode tmp;
|
904
|
|
905
|
for(int j=mNumChildren-1; j>=0; j--)
|
906
|
{
|
907
|
tmp = mChildren.remove(j);
|
908
|
tmp.setSurfaceParent(null);
|
909
|
}
|
910
|
|
911
|
mNumChildren = 0;
|
912
|
}
|
913
|
break;
|
914
|
case SORT : mChildren.remove(job.node);
|
915
|
DistortedMaster.addSortingByBuckets(mChildren,job.node);
|
916
|
break;
|
917
|
}
|
918
|
}
|
919
|
}
|
920
|
}
|