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
|
private static final int ATTACH = 0;
|
31
|
private static final int DETACH = 1;
|
32
|
private static final int DETALL = 2;
|
33
|
private static final int SORT = 3;
|
34
|
|
35
|
private ArrayList<DistortedNode> mChildren;
|
36
|
private int mNumChildren; // ==mChildren.length(), but we only create mChildren if the first one gets added
|
37
|
|
38
|
private class Job
|
39
|
{
|
40
|
int type;
|
41
|
DistortedNode node;
|
42
|
|
43
|
Job(int t, DistortedNode n)
|
44
|
{
|
45
|
type = t;
|
46
|
node = n;
|
47
|
}
|
48
|
}
|
49
|
|
50
|
private ArrayList<Job> mJobs = new ArrayList<>();
|
51
|
|
52
|
DistortedFramebuffer mBuffer1, mBuffer2;
|
53
|
|
54
|
private long mTime;
|
55
|
private float mFOV;
|
56
|
int mWidth,mHeight,mDepth;
|
57
|
float mDistance, mNear;
|
58
|
float[] mProjectionMatrix;
|
59
|
|
60
|
int mDepthCreated;
|
61
|
int[] mDepthH = new int[1];
|
62
|
int[] mFBOH = new int[1];
|
63
|
|
64
|
private float mClearR, mClearG, mClearB, mClearA;
|
65
|
private float mClearDepth;
|
66
|
|
67
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
68
|
|
69
|
DistortedOutputSurface(int width, int height, int createColor, int createDepth, int fbo, int type)
|
70
|
{
|
71
|
super(width,height,createColor,type);
|
72
|
|
73
|
mProjectionMatrix = new float[16];
|
74
|
|
75
|
mWidth = width;
|
76
|
mHeight= height;
|
77
|
|
78
|
mFOV = 60.0f;
|
79
|
mNear= 0.5f;
|
80
|
|
81
|
mDepthCreated= createDepth;
|
82
|
mFBOH[0] = fbo;
|
83
|
mDepthH[0] = 0;
|
84
|
|
85
|
mTime = 0;
|
86
|
|
87
|
mClearR = 0.0f;
|
88
|
mClearG = 0.0f;
|
89
|
mClearB = 0.0f;
|
90
|
mClearA = 0.0f;
|
91
|
|
92
|
mClearDepth = 1.0f;
|
93
|
|
94
|
createProjection();
|
95
|
}
|
96
|
|
97
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
98
|
|
99
|
void createProjection()
|
100
|
{
|
101
|
if( mWidth>0 && mHeight>1 )
|
102
|
{
|
103
|
if( mFOV>0.0f ) // perspective projection
|
104
|
{
|
105
|
float a = 2.0f*(float)Math.tan(mFOV*Math.PI/360);
|
106
|
float q = mWidth*mNear;
|
107
|
float c = mHeight*mNear;
|
108
|
|
109
|
float left = -q/2;
|
110
|
float right = +q/2;
|
111
|
float bottom = -c/2;
|
112
|
float top = +c/2;
|
113
|
float near = c/a;
|
114
|
|
115
|
mDistance = mHeight/a;
|
116
|
float far = 2*mDistance-near;
|
117
|
mDepth = (int)((far-near)/2);
|
118
|
|
119
|
Matrix.frustumM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
|
120
|
}
|
121
|
else // parallel projection
|
122
|
{
|
123
|
float left = -mWidth /2.0f;
|
124
|
float right = +mWidth /2.0f;
|
125
|
float bottom = -mHeight/2.0f;
|
126
|
float top = +mHeight/2.0f;
|
127
|
float near = mWidth+mHeight-mHeight*(1.0f-mNear);
|
128
|
mDistance = mWidth+mHeight;
|
129
|
float far = mWidth+mHeight+mHeight*(1.0f-mNear);
|
130
|
mDepth = (int)((far-near)/2);
|
131
|
|
132
|
Matrix.orthoM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
|
133
|
}
|
134
|
}
|
135
|
}
|
136
|
|
137
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
138
|
// Render all children, one by one. If there are no postprocessing effects, just render to THIS.
|
139
|
// Otherwise, render to a buffer and on each change of Postprocessing Bucket, apply the postprocessing
|
140
|
// to a whole buffer and merge it.
|
141
|
|
142
|
int renderChildren(long time, int num, ArrayList<DistortedNode> children)
|
143
|
{
|
144
|
int numRenders = 0;
|
145
|
DistortedNode child;
|
146
|
DistortedEffectsPostprocess lastP=null, currP;
|
147
|
long lastB=0, currB;
|
148
|
|
149
|
for(int i=0; i<num; i++)
|
150
|
{
|
151
|
child = children.get(i);
|
152
|
currP = child.getEffectsPostprocess();
|
153
|
currB = currP==null ? 0 : currP.getBucket();
|
154
|
|
155
|
if( lastB!=currB && lastB!=0 )
|
156
|
{
|
157
|
numRenders += lastP.postprocess(time,this);
|
158
|
}
|
159
|
|
160
|
if( currB==0 )
|
161
|
{
|
162
|
numRenders += child.draw(time,this);
|
163
|
}
|
164
|
else
|
165
|
{
|
166
|
if( mBuffer1==null )
|
167
|
{
|
168
|
mBuffer1 = new DistortedFramebuffer(mDepthCreated!=DONT_CREATE, DistortedSurface.TYPE_TREE, mWidth, mHeight);
|
169
|
mBuffer2 = new DistortedFramebuffer(false , DistortedSurface.TYPE_TREE, mWidth, mHeight);
|
170
|
}
|
171
|
|
172
|
numRenders += child.draw(time,mBuffer1);
|
173
|
if( i==num-1 )
|
174
|
{
|
175
|
numRenders += currP.postprocess(time,this);
|
176
|
}
|
177
|
}
|
178
|
|
179
|
lastP = currP;
|
180
|
lastB = currB;
|
181
|
}
|
182
|
|
183
|
return numRenders;
|
184
|
}
|
185
|
|
186
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
187
|
// PUBLIC API
|
188
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
189
|
/**
|
190
|
* Draws all the attached children to this OutputSurface.
|
191
|
* <p>
|
192
|
* Must be called from a thread holding OpenGL Context.
|
193
|
*
|
194
|
* @param time Current time, in milliseconds. This will be passed to all the Effects stored in the children Nodes.
|
195
|
* @return Number of objects rendered.
|
196
|
*/
|
197
|
public int render(long time)
|
198
|
{
|
199
|
// change tree topology (attach and detach children)
|
200
|
/*
|
201
|
boolean changed =
|
202
|
*/
|
203
|
DistortedMaster.toDo();
|
204
|
/*
|
205
|
// debugging only
|
206
|
if( changed )
|
207
|
{
|
208
|
for(int i=0; i<mNumChildren; i++)
|
209
|
{
|
210
|
mChildren.get(i).debug(0);
|
211
|
}
|
212
|
|
213
|
DistortedNode.debugMap();
|
214
|
}
|
215
|
*/
|
216
|
// create and delete all underlying OpenGL resources
|
217
|
// Watch out: FIRST change topology, only then deal
|
218
|
// with OpenGL resources. That's because changing Tree
|
219
|
// can result in additional Framebuffers that would need
|
220
|
// to be created immediately, before the calls to drawRecursive()
|
221
|
toDo();
|
222
|
/*
|
223
|
// debugging only
|
224
|
if( changed )
|
225
|
{
|
226
|
DistortedSurface.debugLists();
|
227
|
}
|
228
|
*/
|
229
|
// mark OpenGL state as unknown
|
230
|
DistortedRenderState.reset();
|
231
|
|
232
|
int numRenders=0;
|
233
|
|
234
|
for(int i=0; i<mNumChildren; i++)
|
235
|
{
|
236
|
numRenders += mChildren.get(i).renderRecursive(time);
|
237
|
}
|
238
|
|
239
|
setAsOutput(time);
|
240
|
numRenders += renderChildren(time,mNumChildren,mChildren);
|
241
|
|
242
|
return numRenders;
|
243
|
}
|
244
|
|
245
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
246
|
/**
|
247
|
* Bind this Surface as a Framebuffer we can render to.
|
248
|
*
|
249
|
* @param time Present time, in milliseconds. The point: looking at this param the library can figure
|
250
|
* out if this is the first time during present frame that this FBO is being set as output.
|
251
|
* If so, the library, in addition to binding the Surface for output, also clears the
|
252
|
* Surface's color and depth attachments.
|
253
|
*/
|
254
|
public void setAsOutput(long time)
|
255
|
{
|
256
|
GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, mFBOH[0]);
|
257
|
|
258
|
if( mTime!=time )
|
259
|
{
|
260
|
mTime = time;
|
261
|
DistortedRenderState.colorDepthOn();
|
262
|
GLES30.glClearColor(mClearR, mClearG, mClearB, mClearA);
|
263
|
GLES30.glClearDepthf(mClearDepth);
|
264
|
GLES30.glClear( GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_COLOR_BUFFER_BIT);
|
265
|
}
|
266
|
}
|
267
|
|
268
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
269
|
/**
|
270
|
* Set the (R,G,B,A) values of GLES30.glClearColor() to set up color with which to clear
|
271
|
* this Surface before each render.
|
272
|
*
|
273
|
* @param r the Red component. Default: 0.0f
|
274
|
* @param g the Green component. Default: 0.0f
|
275
|
* @param b the Blue component. Default: 0.0f
|
276
|
* @param a the Alpha component. Default: 0.0f
|
277
|
*/
|
278
|
public void glClearColor(float r, float g, float b, float a)
|
279
|
{
|
280
|
mClearR = r;
|
281
|
mClearG = g;
|
282
|
mClearB = b;
|
283
|
mClearA = a;
|
284
|
}
|
285
|
|
286
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
287
|
/**
|
288
|
* Set the Depth value of GLES30.glClearDepthf() to set up depth with which to clear
|
289
|
* the Depth buffer of Surface before each render.
|
290
|
*
|
291
|
* @param d the Depth. Default: 1.0f
|
292
|
*/
|
293
|
public void glClearDepthf(float d)
|
294
|
{
|
295
|
mClearDepth = d;
|
296
|
}
|
297
|
|
298
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
299
|
/**
|
300
|
* Create new Projection matrix.
|
301
|
*
|
302
|
* @param fov Vertical 'field of view' of the Projection frustrum (in degrees).
|
303
|
* Valid values: 0<=fov<180. FOV==0 means 'parallel projection'.
|
304
|
* @param near Distance between the screen plane and the near plane.
|
305
|
* Valid vaules: 0<near<1. When near==0, the Near Plane is exactly at the tip of the
|
306
|
* pyramid. When near==1 (illegal!) the near plane is equivalent to the screen plane.
|
307
|
*/
|
308
|
public void setProjection(float fov, float near)
|
309
|
{
|
310
|
if( fov < 180.0f && fov >=0.0f )
|
311
|
{
|
312
|
mFOV = fov;
|
313
|
}
|
314
|
|
315
|
if( near< 1.0f && near> 0.0f )
|
316
|
{
|
317
|
mNear= near;
|
318
|
}
|
319
|
else if( near<=0.0f )
|
320
|
{
|
321
|
mNear = 0.01f;
|
322
|
}
|
323
|
else if( near>=1.0f )
|
324
|
{
|
325
|
mNear=0.99f;
|
326
|
}
|
327
|
|
328
|
createProjection();
|
329
|
}
|
330
|
|
331
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
332
|
/**
|
333
|
* Resize the underlying Framebuffer.
|
334
|
* <p>
|
335
|
* This method can be safely called mid-render as it doesn't interfere with rendering.
|
336
|
*
|
337
|
* @param width The new width.
|
338
|
* @param height The new height.
|
339
|
*/
|
340
|
public void resize(int width, int height)
|
341
|
{
|
342
|
if( mWidth!=width || mHeight!=height )
|
343
|
{
|
344
|
mWidth = width;
|
345
|
mHeight= height;
|
346
|
mSizeX = width;
|
347
|
mSizeY = height;
|
348
|
|
349
|
createProjection();
|
350
|
|
351
|
if( mColorCreated==CREATED )
|
352
|
{
|
353
|
markForCreation();
|
354
|
recreate();
|
355
|
}
|
356
|
}
|
357
|
}
|
358
|
|
359
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
360
|
/**
|
361
|
* Create a new DEPTH buffer and attach it or (param=false) detach an existing DEPTH attachment and recreate it.
|
362
|
*
|
363
|
* @param enable <bold>true</bold> if we want to attach a new DEPTH buffer to the FBO.<br>
|
364
|
* <bold>false</bold> if we want to detach the DEPTH attachment.
|
365
|
*/
|
366
|
public void enableDepth(boolean enable)
|
367
|
{
|
368
|
if( enable && mDepthCreated==DONT_CREATE )
|
369
|
{
|
370
|
mDepthCreated = NOT_CREATED_YET;
|
371
|
markForCreation();
|
372
|
}
|
373
|
if( !enable && mDepthCreated!=DONT_CREATE )
|
374
|
{
|
375
|
mDepthCreated = DONT_CREATE;
|
376
|
markForCreation();
|
377
|
}
|
378
|
}
|
379
|
|
380
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
381
|
/**
|
382
|
* Return true if the Surface contains a DEPTH attachment.
|
383
|
*
|
384
|
* @return <bold>true</bold> if the FBO contains a DEPTH attachment.
|
385
|
*/
|
386
|
public boolean hasDepth()
|
387
|
{
|
388
|
return mDepthCreated==CREATED;
|
389
|
}
|
390
|
|
391
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
392
|
/**
|
393
|
* Adds a new child to the last position in the list of our Surface's children.
|
394
|
* <p>
|
395
|
* We cannot do this mid-render - actual attachment will be done just before the next render, by the
|
396
|
* DistortedMaster (by calling doWork())
|
397
|
*
|
398
|
* @param node The new Node to add.
|
399
|
*/
|
400
|
public void attach(DistortedNode node)
|
401
|
{
|
402
|
mJobs.add(new Job(ATTACH,node));
|
403
|
DistortedMaster.newSlave(this);
|
404
|
}
|
405
|
|
406
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
407
|
/**
|
408
|
* Adds a new child to the last position in the list of our Surface's children.
|
409
|
* <p>
|
410
|
* We cannot do this mid-render - actual attachment will be done just before the next render, by the
|
411
|
* DistortedMaster (by calling doWork())
|
412
|
*
|
413
|
* @param surface InputSurface to initialize our child Node with.
|
414
|
* @param effects DistortedEffects to initialize our child Node with.
|
415
|
* @param mesh MeshObject to initialize our child Node with.
|
416
|
* @return the newly constructed child Node, or null if we couldn't allocate resources.
|
417
|
*/
|
418
|
public DistortedNode attach(DistortedInputSurface surface, DistortedEffects effects, MeshObject mesh)
|
419
|
{
|
420
|
DistortedNode node = new DistortedNode(surface,effects,mesh);
|
421
|
mJobs.add(new Job(ATTACH,node));
|
422
|
DistortedMaster.newSlave(this);
|
423
|
return node;
|
424
|
}
|
425
|
|
426
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
427
|
/**
|
428
|
* Removes the first occurrence of a specified child from the list of children of our Surface.
|
429
|
* <p>
|
430
|
* A bit questionable method as there can be many different Nodes attached as children, some
|
431
|
* of them having the same Effects but - for instance - different Mesh. Use with care.
|
432
|
* <p>
|
433
|
* We cannot do this mid-render - actual detachment will be done just before the next render, by the
|
434
|
* DistortedMaster (by calling doWork())
|
435
|
*
|
436
|
* @param effects DistortedEffects to remove.
|
437
|
*/
|
438
|
public void detach(DistortedEffects effects)
|
439
|
{
|
440
|
long id = effects.getID();
|
441
|
DistortedNode node;
|
442
|
boolean detached = false;
|
443
|
|
444
|
for(int i=0; i<mNumChildren; i++)
|
445
|
{
|
446
|
node = mChildren.get(i);
|
447
|
|
448
|
if( node.getEffects().getID()==id )
|
449
|
{
|
450
|
detached = true;
|
451
|
mJobs.add(new Job(DETACH,node));
|
452
|
DistortedMaster.newSlave(this);
|
453
|
break;
|
454
|
}
|
455
|
}
|
456
|
|
457
|
if( !detached )
|
458
|
{
|
459
|
// if we failed to detach any, it still might be the case that
|
460
|
// there's an ATTACH job that we need to cancel.
|
461
|
int num = mJobs.size();
|
462
|
Job job;
|
463
|
|
464
|
for(int i=0; i<num; i++)
|
465
|
{
|
466
|
job = mJobs.get(i);
|
467
|
|
468
|
if( job.type==ATTACH && job.node.getEffects()==effects )
|
469
|
{
|
470
|
mJobs.remove(i);
|
471
|
break;
|
472
|
}
|
473
|
}
|
474
|
}
|
475
|
}
|
476
|
|
477
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
478
|
/**
|
479
|
* Removes the first occurrence of a specified child from the list of children of our Surface.
|
480
|
* <p>
|
481
|
* We cannot do this mid-render - actual attachment will be done just before the next render, by the
|
482
|
* DistortedMaster (by calling doWork())
|
483
|
*
|
484
|
* @param node The Node to remove.
|
485
|
*/
|
486
|
public void detach(DistortedNode node)
|
487
|
{
|
488
|
mJobs.add(new Job(DETACH,node));
|
489
|
DistortedMaster.newSlave(this);
|
490
|
}
|
491
|
|
492
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
493
|
/**
|
494
|
* Removes all children Nodes.
|
495
|
* <p>
|
496
|
* We cannot do this mid-render - actual attachment will be done just before the next render, by the
|
497
|
* DistortedMaster (by calling doWork())
|
498
|
*/
|
499
|
public void detachAll()
|
500
|
{
|
501
|
mJobs.add(new Job(DETALL,null));
|
502
|
DistortedMaster.newSlave(this);
|
503
|
}
|
504
|
|
505
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
506
|
/**
|
507
|
* This is not really part of the public API. Has to be public only because it is a part of the
|
508
|
* DistortedSlave interface, which should really be a class that we extend here instead but
|
509
|
* Java has no multiple inheritance.
|
510
|
*
|
511
|
* @y.exclude
|
512
|
*/
|
513
|
public void doWork()
|
514
|
{
|
515
|
int num = mJobs.size();
|
516
|
Job job;
|
517
|
|
518
|
int numChanges=0;
|
519
|
int numSorts =0;
|
520
|
|
521
|
for(int i=0; i<num; i++)
|
522
|
{
|
523
|
job = mJobs.remove(0);
|
524
|
|
525
|
switch(job.type)
|
526
|
{
|
527
|
case ATTACH: numChanges++;
|
528
|
if( mChildren==null ) mChildren = new ArrayList<>(2);
|
529
|
mChildren.add(job.node);
|
530
|
mNumChildren++;
|
531
|
break;
|
532
|
case DETACH: numChanges++;
|
533
|
if( mNumChildren>0 && mChildren.remove(job.node) )
|
534
|
{
|
535
|
mNumChildren--;
|
536
|
}
|
537
|
break;
|
538
|
case DETALL: numChanges++;
|
539
|
if( mNumChildren>0 )
|
540
|
{
|
541
|
mNumChildren = 0;
|
542
|
mChildren.clear();
|
543
|
}
|
544
|
break;
|
545
|
case SORT : numSorts++;
|
546
|
// TODO: sort
|
547
|
break;
|
548
|
}
|
549
|
}
|
550
|
|
551
|
if( numChanges>0 && numSorts==0 )
|
552
|
{
|
553
|
// TODO: sort
|
554
|
}
|
555
|
}
|
556
|
}
|