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
|
private float mFOV;
|
34
|
int mWidth,mHeight,mDepth;
|
35
|
float mDistance, mNear;
|
36
|
float[] mProjectionMatrix;
|
37
|
|
38
|
int mDepthCreated;
|
39
|
int[] mDepthH = new int[1];
|
40
|
int[] mFBOH = new int[1];
|
41
|
|
42
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
43
|
|
44
|
DistortedOutputSurface(int width, int height, int createColor, int createDepth, int fbo, int type)
|
45
|
{
|
46
|
super(width,height,createColor,type);
|
47
|
|
48
|
mProjectionMatrix = new float[16];
|
49
|
|
50
|
mWidth = width;
|
51
|
mHeight= height;
|
52
|
|
53
|
mFOV = 60.0f;
|
54
|
mNear= 0.5f;
|
55
|
|
56
|
mDepthCreated= createDepth;
|
57
|
mFBOH[0] = fbo;
|
58
|
mDepthH[0] = 0;
|
59
|
|
60
|
createProjection();
|
61
|
}
|
62
|
|
63
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
64
|
|
65
|
void createProjection()
|
66
|
{
|
67
|
if( mWidth>0 && mHeight>1 )
|
68
|
{
|
69
|
if( mFOV>0.0f ) // perspective projection
|
70
|
{
|
71
|
float a = 2.0f*(float)Math.tan(mFOV*Math.PI/360);
|
72
|
float q = mWidth*mNear;
|
73
|
float c = mHeight*mNear;
|
74
|
|
75
|
float left = -q/2;
|
76
|
float right = +q/2;
|
77
|
float bottom = -c/2;
|
78
|
float top = +c/2;
|
79
|
float near = c/a;
|
80
|
|
81
|
mDistance = mHeight/a;
|
82
|
float far = 2*mDistance-near;
|
83
|
mDepth = (int)((far-near)/2);
|
84
|
|
85
|
Matrix.frustumM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
|
86
|
}
|
87
|
else // parallel projection
|
88
|
{
|
89
|
float left = -mWidth /2.0f;
|
90
|
float right = +mWidth /2.0f;
|
91
|
float bottom = -mHeight/2.0f;
|
92
|
float top = +mHeight/2.0f;
|
93
|
float near = mWidth+mHeight-mHeight*(1.0f-mNear);
|
94
|
mDistance = mWidth+mHeight;
|
95
|
float far = mWidth+mHeight+mHeight*(1.0f-mNear);
|
96
|
mDepth = (int)((far-near)/2);
|
97
|
|
98
|
Matrix.orthoM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
|
99
|
}
|
100
|
}
|
101
|
}
|
102
|
|
103
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
104
|
// PUBLIC API
|
105
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
106
|
|
107
|
public int render(long time)
|
108
|
{
|
109
|
DistortedAttachDaemon.toDo();
|
110
|
toDo();
|
111
|
DistortedRenderState.reset();
|
112
|
|
113
|
int numRenders=0;
|
114
|
|
115
|
for(int i=0; i<mNumChildren; i++)
|
116
|
{
|
117
|
numRenders += mChildren.get(i).renderRecursive(time);
|
118
|
}
|
119
|
|
120
|
setAsOutput();
|
121
|
|
122
|
// 'renderChildren'
|
123
|
for(int i=0; i<mNumChildren; i++)
|
124
|
{
|
125
|
numRenders += mChildren.get(i).draw(time,this);
|
126
|
}
|
127
|
|
128
|
return numRenders;
|
129
|
}
|
130
|
|
131
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
132
|
|
133
|
/**
|
134
|
* Draws all the attached children to this OutputSurface.
|
135
|
* <p>
|
136
|
* Must be called from a thread holding OpenGL Context.
|
137
|
*
|
138
|
* @param time Current time, in milliseconds. This will be passed to all the Effects stored in the children Nodes.
|
139
|
* @return Number of objects rendered.
|
140
|
*/
|
141
|
public int renderOld(long time)
|
142
|
{
|
143
|
// change tree topology (attach and detach children)
|
144
|
/*
|
145
|
boolean changed =
|
146
|
*/
|
147
|
DistortedAttachDaemon.toDo();
|
148
|
/*
|
149
|
// debugging only
|
150
|
if( changed )
|
151
|
{
|
152
|
for(int i=0; i<mNumChildren; i++)
|
153
|
{
|
154
|
mChildren.get(i).debug(0);
|
155
|
}
|
156
|
|
157
|
DistortedNode.debugMap();
|
158
|
}
|
159
|
*/
|
160
|
// create and delete all underlying OpenGL resources
|
161
|
// Watch out: FIRST change topology, only then deal
|
162
|
// with OpenGL resources. That's because changing Tree
|
163
|
// can result in additional Framebuffers that would need
|
164
|
// to be created immediately, before the calls to drawRecursive()
|
165
|
toDo();
|
166
|
/*
|
167
|
// debugging only
|
168
|
if( changed )
|
169
|
{
|
170
|
DistortedSurface.debugLists();
|
171
|
}
|
172
|
*/
|
173
|
// mark OpenGL state as unknown
|
174
|
DistortedRenderState.reset();
|
175
|
|
176
|
int numRenders = 0;
|
177
|
|
178
|
for(int i=0; i<mNumChildren; i++)
|
179
|
{
|
180
|
numRenders += mChildren.get(i).drawRecursive(time,this);
|
181
|
}
|
182
|
|
183
|
return numRenders;
|
184
|
}
|
185
|
|
186
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
187
|
/**
|
188
|
* Bind this Surface as a Framebuffer we can render to.
|
189
|
*/
|
190
|
public void setAsOutput()
|
191
|
{
|
192
|
GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, mFBOH[0]);
|
193
|
}
|
194
|
|
195
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
196
|
/**
|
197
|
* Create new Projection matrix.
|
198
|
*
|
199
|
* @param fov Vertical 'field of view' of the Projection frustrum (in degrees).
|
200
|
* Valid values: 0<=fov<180. FOV==0 means 'parallel projection'.
|
201
|
* @param near Distance between the screen plane and the near plane.
|
202
|
* Valid vaules: 0<near<1. When near==0, the Near Plane is exactly at the tip of the
|
203
|
* pyramid. When near==1 (illegal!) the near plane is equivalent to the screen plane.
|
204
|
*/
|
205
|
public void setProjection(float fov, float near)
|
206
|
{
|
207
|
if( fov < 180.0f && fov >=0.0f ) mFOV = fov;
|
208
|
if( near< 1.0f && near> 0.0f ) mNear= near;
|
209
|
|
210
|
createProjection();
|
211
|
}
|
212
|
|
213
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
214
|
/**
|
215
|
* Resize the underlying Framebuffer.
|
216
|
* <p>
|
217
|
* This method can be safely called mid-render as it doesn't interfere with rendering.
|
218
|
*
|
219
|
* @param width The new width.
|
220
|
* @param height The new height.
|
221
|
*/
|
222
|
public void resize(int width, int height)
|
223
|
{
|
224
|
if( mWidth!=width || mHeight!=height )
|
225
|
{
|
226
|
mWidth = width;
|
227
|
mHeight= height;
|
228
|
mSizeX = width;
|
229
|
mSizeY = height;
|
230
|
|
231
|
createProjection();
|
232
|
|
233
|
if( mColorCreated==CREATED )
|
234
|
{
|
235
|
markForCreation();
|
236
|
recreate();
|
237
|
}
|
238
|
}
|
239
|
}
|
240
|
|
241
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
242
|
/**
|
243
|
* Create a new DEPTH buffer and attach it or (param=false) detach an existing DEPTH attachment and recreate it.
|
244
|
*
|
245
|
* @param enable <bold>true</bold> if we want to attach a new DEPTH buffer to the FBO.<br>
|
246
|
* <bold>false</bold> if we want to detach the DEPTH attachment.
|
247
|
*/
|
248
|
public void enableDepth(boolean enable)
|
249
|
{
|
250
|
if( enable && mDepthCreated==DONT_CREATE )
|
251
|
{
|
252
|
mDepthCreated = NOT_CREATED_YET;
|
253
|
markForCreation();
|
254
|
}
|
255
|
if( !enable && mDepthCreated!=DONT_CREATE )
|
256
|
{
|
257
|
mDepthCreated = DONT_CREATE;
|
258
|
markForCreation();
|
259
|
}
|
260
|
}
|
261
|
|
262
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
263
|
/**
|
264
|
* Return true if the Surface contains a DEPTH attachment.
|
265
|
*
|
266
|
* @return <bold>true</bold> if the FBO contains a DEPTH attachment.
|
267
|
*/
|
268
|
public boolean hasDepth()
|
269
|
{
|
270
|
return mDepthCreated==CREATED;
|
271
|
}
|
272
|
|
273
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
274
|
/**
|
275
|
* Adds a new child to the last position in the list of our Surface's children.
|
276
|
* <p>
|
277
|
* We cannot do this mid-render - actual attachment will be done just before the next render, by the
|
278
|
* DistortedAttachDeamon (by calling attachNow())
|
279
|
*
|
280
|
* @param node The new Node to add.
|
281
|
*/
|
282
|
public void attach(DistortedNode node)
|
283
|
{
|
284
|
DistortedAttachDaemon.attach(this,node);
|
285
|
}
|
286
|
|
287
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
288
|
/**
|
289
|
* Adds a new child to the last position in the list of our Surface's children.
|
290
|
* <p>
|
291
|
* We cannot do this mid-render - actual attachment will be done just before the next render, by the
|
292
|
* DistortedAttachDeamon (by calling attachNow())
|
293
|
*
|
294
|
* @param surface InputSurface to initialize our child Node with.
|
295
|
* @param effects DistortedEffects to initialize our child Node with.
|
296
|
* @param mesh MeshObject to initialize our child Node with.
|
297
|
* @return the newly constructed child Node, or null if we couldn't allocate resources.
|
298
|
*/
|
299
|
public DistortedNode attach(DistortedInputSurface surface, DistortedEffects effects, MeshObject mesh)
|
300
|
{
|
301
|
DistortedNode node = new DistortedNode(surface,effects,mesh);
|
302
|
DistortedAttachDaemon.attach(this,node);
|
303
|
return node;
|
304
|
}
|
305
|
|
306
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
307
|
/**
|
308
|
* This is not really part of the public API. Has to be public only because it is a part of the
|
309
|
* DistortedAttacheable interface, which should really be a class that we extend here instead but
|
310
|
* Java has no multiple inheritance.
|
311
|
*
|
312
|
* @y.exclude
|
313
|
* @param node new Node to add.
|
314
|
*/
|
315
|
public void attachNow(DistortedNode node)
|
316
|
{
|
317
|
if( mChildren==null ) mChildren = new ArrayList<>(2);
|
318
|
mChildren.add(node);
|
319
|
mNumChildren++;
|
320
|
}
|
321
|
|
322
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
323
|
/**
|
324
|
* Removes the first occurrence of a specified child from the list of children of our Surface.
|
325
|
* <p>
|
326
|
* A bit questionable method as there can be many different Nodes attached as children, some
|
327
|
* of them having the same Effects but - for instance - different Mesh. Use with care.
|
328
|
* <p>
|
329
|
* We cannot do this mid-render - actual detachment will be done just before the next render, by the
|
330
|
* DistortedAttachDeamon (by calling detachNow())
|
331
|
*
|
332
|
* @param effects DistortedEffects to remove.
|
333
|
*/
|
334
|
public void detach(DistortedEffects effects)
|
335
|
{
|
336
|
long id = effects.getID();
|
337
|
DistortedNode node;
|
338
|
|
339
|
for(int i=0; i<mNumChildren; i++)
|
340
|
{
|
341
|
node = mChildren.get(i);
|
342
|
|
343
|
if( node.getEffects().getID()==id )
|
344
|
{
|
345
|
DistortedAttachDaemon.detach(this,node);
|
346
|
break;
|
347
|
}
|
348
|
}
|
349
|
}
|
350
|
|
351
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
352
|
/**
|
353
|
* Removes the first occurrence of a specified child from the list of children of our Surface.
|
354
|
* <p>
|
355
|
* We cannot do this mid-render - actual attachment will be done just before the next render, by the
|
356
|
* DistortedAttachDeamon (by calling detachNow())
|
357
|
*
|
358
|
* @param node The Node to remove.
|
359
|
*/
|
360
|
public void detach(DistortedNode node)
|
361
|
{
|
362
|
DistortedAttachDaemon.detach(this,node);
|
363
|
}
|
364
|
|
365
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
366
|
/**
|
367
|
* This is not really part of the public API. Has to be public only because it is a part of the
|
368
|
* DistortedAttacheable interface, which should really be a class that we extend here instead but
|
369
|
* Java has no multiple inheritance.
|
370
|
*
|
371
|
* @y.exclude
|
372
|
* @param node The Node to remove.
|
373
|
*/
|
374
|
public void detachNow(DistortedNode node)
|
375
|
{
|
376
|
if( mNumChildren>0 && mChildren.remove(node) )
|
377
|
{
|
378
|
mNumChildren--;
|
379
|
}
|
380
|
}
|
381
|
|
382
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
383
|
/**
|
384
|
* Removes all children Nodes.
|
385
|
* <p>
|
386
|
* We cannot do this mid-render - actual attachment will be done just before the next render, by the
|
387
|
* DistortedAttachDeamon (by calling detachAllNow())
|
388
|
*/
|
389
|
public void detachAll()
|
390
|
{
|
391
|
DistortedAttachDaemon.detachAll(this);
|
392
|
}
|
393
|
|
394
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
395
|
/**
|
396
|
* This is not really part of the public API. Has to be public only because it is a part of the
|
397
|
* DistortedAttacheable interface, which should really be a class that we extend here instead but
|
398
|
* Java has no multiple inheritance.
|
399
|
*
|
400
|
* @y.exclude
|
401
|
*/
|
402
|
public void detachAllNow()
|
403
|
{
|
404
|
if( mNumChildren>0 )
|
405
|
{
|
406
|
mNumChildren = 0;
|
407
|
mChildren.clear();
|
408
|
}
|
409
|
}
|
410
|
}
|