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.opengl.GLES30;
|
23
|
import android.opengl.Matrix;
|
24
|
import java.util.ArrayList;
|
25
|
|
26
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
27
|
|
28
|
abstract class DistortedOutputSurface extends DistortedSurface implements DistortedSlave
|
29
|
{
|
30
|
/**
|
31
|
* Do not create DEPTH or STENCIL attachment
|
32
|
*/
|
33
|
public static final int NO_DEPTH_NO_STENCIL = 0;
|
34
|
/**
|
35
|
* Create DEPTH, but not STENCIL
|
36
|
*/
|
37
|
public static final int DEPTH_NO_STENCIL = 1;
|
38
|
/**
|
39
|
* Create both DEPTH and STENCIL
|
40
|
*/
|
41
|
public static final int BOTH_DEPTH_STENCIL = 2;
|
42
|
|
43
|
private static final int ATTACH = 0;
|
44
|
private static final int DETACH = 1;
|
45
|
private static final int DETALL = 2;
|
46
|
private static final int SORT = 3;
|
47
|
|
48
|
private ArrayList<DistortedNode> mChildren;
|
49
|
private int mNumChildren; // ==mChildren.length(), but we only create mChildren if the first one gets added
|
50
|
|
51
|
private class Job
|
52
|
{
|
53
|
int type;
|
54
|
DistortedNode node;
|
55
|
DistortedEffectsPostprocess dep;
|
56
|
|
57
|
Job(int t, DistortedNode n, DistortedEffectsPostprocess d)
|
58
|
{
|
59
|
type = t;
|
60
|
node = n;
|
61
|
dep = d;
|
62
|
}
|
63
|
}
|
64
|
|
65
|
private ArrayList<Job> mJobs = new ArrayList<>();
|
66
|
|
67
|
DistortedFramebuffer[] mBuffer;
|
68
|
|
69
|
private long mTime;
|
70
|
private float mFOV;
|
71
|
float mDistance, mNear;
|
72
|
float[] mProjectionMatrix;
|
73
|
|
74
|
int mDepthStencilCreated;
|
75
|
int mDepthStencil;
|
76
|
int[] mDepthStencilH = new int[1];
|
77
|
int[] mFBOH = new int[1];
|
78
|
|
79
|
private float mClearR, mClearG, mClearB, mClearA;
|
80
|
private float mClearDepth;
|
81
|
private int mClearStencil;
|
82
|
private int mClear;
|
83
|
float mMipmap;
|
84
|
|
85
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
86
|
|
87
|
DistortedOutputSurface(int width, int height, int createColor, int numcolors, int depthStencil, int fbo, int type)
|
88
|
{
|
89
|
super(width,height,createColor,numcolors,type);
|
90
|
|
91
|
mProjectionMatrix = new float[16];
|
92
|
|
93
|
mFOV = 60.0f;
|
94
|
mNear= 0.5f;
|
95
|
|
96
|
mDepthStencilCreated= (depthStencil== NO_DEPTH_NO_STENCIL ? DONT_CREATE:NOT_CREATED_YET);
|
97
|
mDepthStencil = depthStencil;
|
98
|
|
99
|
mFBOH[0] = fbo;
|
100
|
mDepthStencilH[0]= 0;
|
101
|
|
102
|
mTime = 0;
|
103
|
|
104
|
mClearR = 0.0f;
|
105
|
mClearG = 0.0f;
|
106
|
mClearB = 0.0f;
|
107
|
mClearA = 0.0f;
|
108
|
|
109
|
mClearDepth = 1.0f;
|
110
|
mClearStencil = 0;
|
111
|
mClear = GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_COLOR_BUFFER_BIT;
|
112
|
|
113
|
mBuffer = new DistortedFramebuffer[EffectQuality.LENGTH];
|
114
|
|
115
|
mMipmap = 1.0f;
|
116
|
|
117
|
createProjection();
|
118
|
}
|
119
|
|
120
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
121
|
|
122
|
private void createProjection()
|
123
|
{
|
124
|
if( mWidth>0 && mHeight>1 )
|
125
|
{
|
126
|
if( mFOV>0.0f ) // perspective projection
|
127
|
{
|
128
|
float a = 2.0f*(float)Math.tan(mFOV*Math.PI/360);
|
129
|
float q = mWidth*mNear;
|
130
|
float c = mHeight*mNear;
|
131
|
|
132
|
float left = -q/2;
|
133
|
float right = +q/2;
|
134
|
float bottom = -c/2;
|
135
|
float top = +c/2;
|
136
|
float near = c/a;
|
137
|
|
138
|
mDistance = mHeight/a;
|
139
|
float far = 2*mDistance-near;
|
140
|
|
141
|
Matrix.frustumM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
|
142
|
}
|
143
|
else // parallel projection
|
144
|
{
|
145
|
float left = -mWidth/2.0f;
|
146
|
float right = +mWidth/2.0f;
|
147
|
float bottom = -mHeight/2.0f;
|
148
|
float top = +mHeight/2.0f;
|
149
|
float near = mWidth+mHeight-mHeight*(1.0f-mNear);
|
150
|
mDistance = mWidth+mHeight;
|
151
|
float far = mWidth+mHeight+mHeight*(1.0f-mNear);
|
152
|
|
153
|
Matrix.orthoM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
|
154
|
}
|
155
|
}
|
156
|
}
|
157
|
|
158
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
159
|
|
160
|
void createBuffers()
|
161
|
{
|
162
|
float mipmap=1.0f;
|
163
|
|
164
|
for(int j=0; j<EffectQuality.LENGTH; j++)
|
165
|
{
|
166
|
mBuffer[j] = new DistortedFramebuffer(2,BOTH_DEPTH_STENCIL,TYPE_SYST, (int)(mWidth*mipmap), (int)(mHeight*mipmap) );
|
167
|
mBuffer[j].mMipmap = mipmap;
|
168
|
mipmap *= EffectQuality.MULTIPLIER;
|
169
|
}
|
170
|
|
171
|
DistortedObject.toDo(); // create the FBOs immediately. This is safe as we must be holding the OpenGL context now.
|
172
|
|
173
|
GLES30.glStencilMask(0xff);
|
174
|
GLES30.glDepthMask(true);
|
175
|
GLES30.glColorMask(true,true,true,true);
|
176
|
GLES30.glClearColor(0.0f,0.0f,0.0f,0.0f);
|
177
|
GLES30.glClearDepthf(1.0f);
|
178
|
GLES30.glClearStencil(0);
|
179
|
|
180
|
for(int j=0; j<EffectQuality.LENGTH; j++)
|
181
|
{
|
182
|
mBuffer[j].setAsOutput();
|
183
|
GLES30.glFramebufferTexture2D(GLES30.GL_FRAMEBUFFER, GLES30.GL_COLOR_ATTACHMENT0, GLES30.GL_TEXTURE_2D, mBuffer[j].mColorH[1], 0);
|
184
|
GLES30.glClear(GLES30.GL_COLOR_BUFFER_BIT|GLES30.GL_DEPTH_BUFFER_BIT|GLES30.GL_STENCIL_BUFFER_BIT);
|
185
|
GLES30.glFramebufferTexture2D(GLES30.GL_FRAMEBUFFER, GLES30.GL_COLOR_ATTACHMENT0, GLES30.GL_TEXTURE_2D, mBuffer[j].mColorH[0], 0);
|
186
|
GLES30.glClear(GLES30.GL_COLOR_BUFFER_BIT);
|
187
|
}
|
188
|
}
|
189
|
|
190
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
191
|
// Render all children, one by one. If there are no postprocessing effects, just render to THIS.
|
192
|
// Otherwise, render to a buffer and on each change of Postprocessing Bucket, apply the postprocessing
|
193
|
// to a whole buffer and merge it.
|
194
|
|
195
|
int renderChildren(long time, int num, ArrayList<DistortedNode> children)
|
196
|
{
|
197
|
int numRenders = 0;
|
198
|
DistortedNode child;
|
199
|
DistortedEffectsPostprocess lastP=null, currP;
|
200
|
long lastB=0, currB;
|
201
|
int quality;
|
202
|
|
203
|
for(int i=0; i<num; i++)
|
204
|
{
|
205
|
child = children.get(i);
|
206
|
currP = child.getEffectsPostprocess();
|
207
|
currB = currP==null ? 0 : currP.getBucket();
|
208
|
|
209
|
if( lastB!=currB && lastB!=0 )
|
210
|
{
|
211
|
quality = lastP.getQuality();
|
212
|
numRenders += lastP.postprocess(time, this);
|
213
|
numRenders += blitWithDepth(quality,time);
|
214
|
}
|
215
|
|
216
|
if( currB==0 ) numRenders += child.draw(time,this);
|
217
|
else
|
218
|
{
|
219
|
if( mBuffer[0]==null ) createBuffers();
|
220
|
numRenders += child.markStencilAndDraw(time,this,currP);
|
221
|
if( i==num-1 )
|
222
|
{
|
223
|
quality = currP.getQuality();
|
224
|
numRenders += currP.postprocess(time,this);
|
225
|
numRenders += blitWithDepth(quality,time);
|
226
|
}
|
227
|
}
|
228
|
|
229
|
lastP = currP;
|
230
|
lastB = currB;
|
231
|
}
|
232
|
|
233
|
return numRenders;
|
234
|
}
|
235
|
|
236
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
237
|
|
238
|
int blitWithDepth(int quality, long currTime)
|
239
|
{
|
240
|
DistortedFramebuffer buffer = mBuffer[quality];
|
241
|
|
242
|
GLES30.glViewport(0, 0, mWidth, mHeight);
|
243
|
setAsOutput(currTime);
|
244
|
GLES30.glActiveTexture(GLES30.GL_TEXTURE0);
|
245
|
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, buffer.mColorH[0]);
|
246
|
GLES30.glActiveTexture(GLES30.GL_TEXTURE1);
|
247
|
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, buffer.mDepthStencilH[0]);
|
248
|
|
249
|
GLES30.glDisable(GLES30.GL_STENCIL_TEST);
|
250
|
GLES30.glStencilMask(0x00);
|
251
|
|
252
|
DistortedEffects.blitDepthPriv(this);
|
253
|
GLES30.glActiveTexture(GLES30.GL_TEXTURE0);
|
254
|
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, 0);
|
255
|
GLES30.glActiveTexture(GLES30.GL_TEXTURE1);
|
256
|
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, 0);
|
257
|
|
258
|
// clear buffers
|
259
|
GLES30.glStencilMask(0xff);
|
260
|
GLES30.glDepthMask(true);
|
261
|
GLES30.glColorMask(true,true,true,true);
|
262
|
GLES30.glClearColor(0.0f,0.0f,0.0f,0.0f);
|
263
|
GLES30.glClearDepthf(1.0f);
|
264
|
GLES30.glClearStencil(0);
|
265
|
|
266
|
buffer.setAsOutput();
|
267
|
GLES30.glFramebufferTexture2D(GLES30.GL_FRAMEBUFFER, GLES30.GL_COLOR_ATTACHMENT0, GLES30.GL_TEXTURE_2D, buffer.mColorH[1], 0);
|
268
|
GLES30.glClear(GLES30.GL_COLOR_BUFFER_BIT|GLES30.GL_DEPTH_BUFFER_BIT|GLES30.GL_STENCIL_BUFFER_BIT);
|
269
|
GLES30.glFramebufferTexture2D(GLES30.GL_FRAMEBUFFER, GLES30.GL_COLOR_ATTACHMENT0, GLES30.GL_TEXTURE_2D, buffer.mColorH[0], 0);
|
270
|
GLES30.glClear(GLES30.GL_COLOR_BUFFER_BIT);
|
271
|
|
272
|
return 1;
|
273
|
}
|
274
|
|
275
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
276
|
|
277
|
void newJob(int t, DistortedNode n, DistortedEffectsPostprocess d)
|
278
|
{
|
279
|
mJobs.add(new Job(t,n,d));
|
280
|
}
|
281
|
|
282
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
283
|
|
284
|
void setAsOutput()
|
285
|
{
|
286
|
GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, mFBOH[0]);
|
287
|
}
|
288
|
|
289
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
290
|
// PUBLIC API
|
291
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
292
|
/**
|
293
|
* Draws all the attached children to this OutputSurface.
|
294
|
* <p>
|
295
|
* Must be called from a thread holding OpenGL Context.
|
296
|
*
|
297
|
* @param time Current time, in milliseconds. This will be passed to all the Effects stored in the children Nodes.
|
298
|
* @return Number of objects rendered.
|
299
|
*/
|
300
|
public int render(long time)
|
301
|
{
|
302
|
// change tree topology (attach and detach children)
|
303
|
/*
|
304
|
boolean changed1 =
|
305
|
*/
|
306
|
DistortedMaster.toDo();
|
307
|
/*
|
308
|
if( changed1 )
|
309
|
{
|
310
|
for(int i=0; i<mNumChildren; i++)
|
311
|
{
|
312
|
mChildren.get(i).debug(0);
|
313
|
}
|
314
|
|
315
|
DistortedNode.debugMap();
|
316
|
}
|
317
|
*/
|
318
|
// create and delete all underlying OpenGL resources
|
319
|
// Watch out: FIRST change topology, only then deal
|
320
|
// with OpenGL resources. That's because changing Tree
|
321
|
// can result in additional Framebuffers that would need
|
322
|
// to be created immediately, before the calls to drawRecursive()
|
323
|
/*
|
324
|
boolean changed2 =
|
325
|
*/
|
326
|
toDo();
|
327
|
/*
|
328
|
if( changed2 )
|
329
|
{
|
330
|
DistortedObject.debugLists();
|
331
|
}
|
332
|
*/
|
333
|
// mark OpenGL state as unknown
|
334
|
DistortedRenderState.reset();
|
335
|
|
336
|
int numRenders=0;
|
337
|
|
338
|
for(int i=0; i<mNumChildren; i++)
|
339
|
{
|
340
|
numRenders += mChildren.get(i).renderRecursive(time);
|
341
|
}
|
342
|
|
343
|
setAsOutput(time);
|
344
|
numRenders += renderChildren(time,mNumChildren,mChildren);
|
345
|
|
346
|
return numRenders;
|
347
|
}
|
348
|
|
349
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
350
|
/**
|
351
|
* Bind this Surface as a Framebuffer we can render to.
|
352
|
*
|
353
|
* @param time Present time, in milliseconds. The point: looking at this param the library can figure
|
354
|
* out if this is the first time during present frame that this FBO is being set as output.
|
355
|
* If so, the library, in addition to binding the Surface for output, also clears the
|
356
|
* Surface's color and depth attachments.
|
357
|
*/
|
358
|
public void setAsOutput(long time)
|
359
|
{
|
360
|
GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, mFBOH[0]);
|
361
|
|
362
|
if( mTime!=time )
|
363
|
{
|
364
|
mTime = time;
|
365
|
DistortedRenderState.colorDepthStencilOn();
|
366
|
GLES30.glClearColor(mClearR, mClearG, mClearB, mClearA);
|
367
|
GLES30.glClearDepthf(mClearDepth);
|
368
|
GLES30.glClearStencil(mClearStencil);
|
369
|
GLES30.glClear(mClear);
|
370
|
}
|
371
|
}
|
372
|
|
373
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
374
|
/**
|
375
|
* Set mipmap level.
|
376
|
* <p>
|
377
|
* Trick for speeding up your renders - one can create a pyramid of OutputSurface objects, each next
|
378
|
* one some constant FACTOR smaller than the previous (0.5 is the common value), then set the Mipmap
|
379
|
* Level of the i-th object to be FACTOR^i (we start counting from 0). When rendering any scene into
|
380
|
* such prepared OutputSurface, the library will make sure to scale any Effects used so that the end
|
381
|
* scene will end up looking identical no matter which object we render to. Identical, that is, except
|
382
|
* for the loss of quality and gain in speed associated with rendering to a smaller Surface.
|
383
|
* <p>
|
384
|
* Example: if you create two FBOs, one 1000x1000 and another 500x500 in size, and set the second one
|
385
|
* mipmap to 0.5 (the first one's is 1.0 by default), define Effects to be a single move by (100,100),
|
386
|
* and render a skinned Mesh into both FBO, the end result will look proportionally the same, because
|
387
|
* in the second case the move vector (100,100) will be auto-scaled to (50,50).
|
388
|
*
|
389
|
* @param mipmap The mipmap level. Acceptable range: 0<mipmap<infinity, although mipmap>1
|
390
|
* does not make any sense (that would result in loss of speed and no gain in quality)
|
391
|
*/
|
392
|
public void setMipmap(float mipmap)
|
393
|
{
|
394
|
mMipmap = mipmap;
|
395
|
}
|
396
|
|
397
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
398
|
/**
|
399
|
* Set the (R,G,B,A) values of GLES30.glClearColor() to set up color with which to clear
|
400
|
* this Surface at the beginning of each frame.
|
401
|
*
|
402
|
* @param r the Red component. Default: 0.0f
|
403
|
* @param g the Green component. Default: 0.0f
|
404
|
* @param b the Blue component. Default: 0.0f
|
405
|
* @param a the Alpha component. Default: 0.0f
|
406
|
*/
|
407
|
public void glClearColor(float r, float g, float b, float a)
|
408
|
{
|
409
|
mClearR = r;
|
410
|
mClearG = g;
|
411
|
mClearB = b;
|
412
|
mClearA = a;
|
413
|
}
|
414
|
|
415
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
416
|
/**
|
417
|
* Uses glClearDepthf() to set up a value with which to clear
|
418
|
* the Depth buffer of our Surface at the beginning of each frame.
|
419
|
*
|
420
|
* @param d the Depth. Default: 1.0f
|
421
|
*/
|
422
|
public void glClearDepthf(float d)
|
423
|
{
|
424
|
mClearDepth = d;
|
425
|
}
|
426
|
|
427
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
428
|
/**
|
429
|
* Uses glClearStencil() to set up a value with which to clear the
|
430
|
* Stencil buffer of our Surface at the beginning of each frame.
|
431
|
*
|
432
|
* @param s the Stencil. Default: 0
|
433
|
*/
|
434
|
public void glClearStencil(int s)
|
435
|
{
|
436
|
mClearStencil = s;
|
437
|
}
|
438
|
|
439
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
440
|
/**
|
441
|
* Which buffers to Clear at the beginning of each frame?
|
442
|
* <p>
|
443
|
* Valid values: 0, or bitwise OR of one or more values from the set GL_COLOR_BUFFER_BIT,
|
444
|
* GL_DEPTH_BUFFER_BIT, GL_STENCIL_BUFFER_BIT.
|
445
|
* Default: GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT.
|
446
|
*
|
447
|
* @param mask bitwise OR of BUFFER_BITs to clear.
|
448
|
*/
|
449
|
public void glClear(int mask)
|
450
|
{
|
451
|
mClear = mask;
|
452
|
}
|
453
|
|
454
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
455
|
/**
|
456
|
* Create new Projection matrix.
|
457
|
*
|
458
|
* @param fov Vertical 'field of view' of the Projection frustrum (in degrees).
|
459
|
* Valid values: 0<=fov<180. FOV==0 means 'parallel projection'.
|
460
|
* @param near Distance between the screen plane and the near plane.
|
461
|
* Valid vaules: 0<near<1. When near==0, the Near Plane is exactly at the tip of the
|
462
|
* pyramid. When near==1 (illegal!) the near plane is equivalent to the screen plane.
|
463
|
*/
|
464
|
public void setProjection(float fov, float near)
|
465
|
{
|
466
|
if( fov < 180.0f && fov >=0.0f )
|
467
|
{
|
468
|
mFOV = fov;
|
469
|
}
|
470
|
|
471
|
if( near< 1.0f && near> 0.0f )
|
472
|
{
|
473
|
mNear= near;
|
474
|
}
|
475
|
else if( near<=0.0f )
|
476
|
{
|
477
|
mNear = 0.01f;
|
478
|
}
|
479
|
else if( near>=1.0f )
|
480
|
{
|
481
|
mNear=0.99f;
|
482
|
}
|
483
|
|
484
|
createProjection();
|
485
|
}
|
486
|
|
487
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
488
|
/**
|
489
|
* Resize the underlying Framebuffer.
|
490
|
* <p>
|
491
|
* This method can be safely called mid-render as it doesn't interfere with rendering.
|
492
|
*
|
493
|
* @param width The new width.
|
494
|
* @param height The new height.
|
495
|
*/
|
496
|
public void resize(int width, int height)
|
497
|
{
|
498
|
if( mWidth!=width || mHeight!=height )
|
499
|
{
|
500
|
mWidth = width;
|
501
|
mHeight= height;
|
502
|
|
503
|
createProjection();
|
504
|
|
505
|
if( mColorCreated==CREATED )
|
506
|
{
|
507
|
markForCreation();
|
508
|
recreate();
|
509
|
}
|
510
|
}
|
511
|
}
|
512
|
|
513
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
514
|
/**
|
515
|
* Return true if the Surface contains a DEPTH attachment.
|
516
|
*
|
517
|
* @return <bold>true</bold> if the Surface contains a DEPTH attachment.
|
518
|
*/
|
519
|
public boolean hasDepth()
|
520
|
{
|
521
|
return mDepthStencilCreated==CREATED;
|
522
|
}
|
523
|
|
524
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
525
|
/**
|
526
|
* Return true if the Surface contains a STENCIL attachment.
|
527
|
*
|
528
|
* @return <bold>true</bold> if the Surface contains a STENCIL attachment.
|
529
|
*/
|
530
|
public boolean hasStencil()
|
531
|
{
|
532
|
return (mDepthStencilCreated==CREATED && mDepthStencil==BOTH_DEPTH_STENCIL);
|
533
|
}
|
534
|
|
535
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
536
|
/**
|
537
|
* Adds a new child to the last position in the list of our Surface's children.
|
538
|
* <p>
|
539
|
* We cannot do this mid-render - actual attachment will be done just before the next render, by the
|
540
|
* DistortedMaster (by calling doWork())
|
541
|
*
|
542
|
* @param node The new Node to add.
|
543
|
*/
|
544
|
public void attach(DistortedNode node)
|
545
|
{
|
546
|
mJobs.add(new Job(ATTACH,node,null));
|
547
|
DistortedMaster.newSlave(this);
|
548
|
}
|
549
|
|
550
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
551
|
/**
|
552
|
* Adds a new child to the last position in the list of our Surface's children.
|
553
|
* <p>
|
554
|
* We cannot do this mid-render - actual attachment will be done just before the next render, by the
|
555
|
* DistortedMaster (by calling doWork())
|
556
|
*
|
557
|
* @param surface InputSurface to initialize our child Node with.
|
558
|
* @param effects DistortedEffects to initialize our child Node with.
|
559
|
* @param mesh MeshObject to initialize our child Node with.
|
560
|
* @return the newly constructed child Node, or null if we couldn't allocate resources.
|
561
|
*/
|
562
|
public DistortedNode attach(DistortedInputSurface surface, DistortedEffects effects, MeshObject mesh)
|
563
|
{
|
564
|
DistortedNode node = new DistortedNode(surface,effects,mesh);
|
565
|
mJobs.add(new Job(ATTACH,node,null));
|
566
|
DistortedMaster.newSlave(this);
|
567
|
return node;
|
568
|
}
|
569
|
|
570
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
571
|
/**
|
572
|
* Removes the first occurrence of a specified child from the list of children of our Surface.
|
573
|
* <p>
|
574
|
* A bit questionable method as there can be many different Nodes attached as children, some
|
575
|
* of them having the same Effects but - for instance - different Mesh. Use with care.
|
576
|
* <p>
|
577
|
* We cannot do this mid-render - actual detachment will be done just before the next render, by the
|
578
|
* DistortedMaster (by calling doWork())
|
579
|
*
|
580
|
* @param effects DistortedEffects to remove.
|
581
|
*/
|
582
|
public void detach(DistortedEffects effects)
|
583
|
{
|
584
|
long id = effects.getID();
|
585
|
DistortedNode node;
|
586
|
boolean detached = false;
|
587
|
|
588
|
for(int i=0; i<mNumChildren; i++)
|
589
|
{
|
590
|
node = mChildren.get(i);
|
591
|
|
592
|
if( node.getEffects().getID()==id )
|
593
|
{
|
594
|
detached = true;
|
595
|
mJobs.add(new Job(DETACH,node,null));
|
596
|
DistortedMaster.newSlave(this);
|
597
|
break;
|
598
|
}
|
599
|
}
|
600
|
|
601
|
if( !detached )
|
602
|
{
|
603
|
// if we failed to detach any, it still might be the case that
|
604
|
// there's an ATTACH job that we need to cancel.
|
605
|
int num = mJobs.size();
|
606
|
Job job;
|
607
|
|
608
|
for(int i=0; i<num; i++)
|
609
|
{
|
610
|
job = mJobs.get(i);
|
611
|
|
612
|
if( job.type==ATTACH && job.node.getEffects()==effects )
|
613
|
{
|
614
|
mJobs.remove(i);
|
615
|
break;
|
616
|
}
|
617
|
}
|
618
|
}
|
619
|
}
|
620
|
|
621
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
622
|
/**
|
623
|
* Removes the first occurrence of a specified child from the list of children of our Surface.
|
624
|
* <p>
|
625
|
* We cannot do this mid-render - actual attachment will be done just before the next render, by the
|
626
|
* DistortedMaster (by calling doWork())
|
627
|
*
|
628
|
* @param node The Node to remove.
|
629
|
*/
|
630
|
public void detach(DistortedNode node)
|
631
|
{
|
632
|
mJobs.add(new Job(DETACH,node,null));
|
633
|
DistortedMaster.newSlave(this);
|
634
|
}
|
635
|
|
636
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
637
|
/**
|
638
|
* Removes all children Nodes.
|
639
|
* <p>
|
640
|
* We cannot do this mid-render - actual attachment will be done just before the next render, by the
|
641
|
* DistortedMaster (by calling doWork())
|
642
|
*/
|
643
|
public void detachAll()
|
644
|
{
|
645
|
mJobs.add(new Job(DETALL,null,null));
|
646
|
DistortedMaster.newSlave(this);
|
647
|
}
|
648
|
|
649
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
650
|
/**
|
651
|
* This is not really part of the public API. Has to be public only because it is a part of the
|
652
|
* DistortedSlave interface, which should really be a class that we extend here instead but
|
653
|
* Java has no multiple inheritance.
|
654
|
*
|
655
|
* @y.exclude
|
656
|
*/
|
657
|
public void doWork()
|
658
|
{
|
659
|
int num = mJobs.size();
|
660
|
Job job;
|
661
|
|
662
|
for(int i=0; i<num; i++)
|
663
|
{
|
664
|
job = mJobs.remove(0);
|
665
|
|
666
|
switch(job.type)
|
667
|
{
|
668
|
case ATTACH: if( mChildren==null ) mChildren = new ArrayList<>(2);
|
669
|
job.node.setSurfaceParent(this);
|
670
|
DistortedMaster.addSorted(mChildren,job.node);
|
671
|
mNumChildren++;
|
672
|
break;
|
673
|
case DETACH: if( mNumChildren>0 && mChildren.remove(job.node) )
|
674
|
{
|
675
|
job.node.setSurfaceParent(null);
|
676
|
mNumChildren--;
|
677
|
}
|
678
|
break;
|
679
|
case DETALL: if( mNumChildren>0 )
|
680
|
{
|
681
|
DistortedNode tmp;
|
682
|
|
683
|
for(int j=mNumChildren-1; j>=0; j--)
|
684
|
{
|
685
|
tmp = mChildren.remove(j);
|
686
|
tmp.setSurfaceParent(null);
|
687
|
}
|
688
|
|
689
|
mNumChildren = 0;
|
690
|
}
|
691
|
break;
|
692
|
case SORT : job.node.setPost(job.dep);
|
693
|
mChildren.remove(job.node);
|
694
|
DistortedMaster.addSorted(mChildren,job.node);
|
695
|
break;
|
696
|
}
|
697
|
}
|
698
|
}
|
699
|
}
|