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