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