1 |
c5369f1b
|
leszek
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
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 |
fe82a979
|
Leszek Koltunski
|
package org.distorted.library.main;
|
21 |
c5369f1b
|
leszek
|
|
22 |
e6519ac8
|
Leszek Koltunski
|
import android.opengl.GLES31;
|
23 |
af4cc5db
|
Leszek Koltunski
|
import android.opengl.Matrix;
|
24 |
86e99907
|
leszek
|
|
25 |
3521c6fe
|
leszek
|
import org.distorted.library.effect.EffectQuality;
|
26 |
|
|
|
27 |
12f45260
|
Leszek Koltunski
|
import java.nio.ByteBuffer;
|
28 |
|
|
import java.nio.ByteOrder;
|
29 |
66ace7f5
|
Leszek Koltunski
|
import java.nio.IntBuffer;
|
30 |
a09ada4c
|
Leszek Koltunski
|
import java.util.ArrayList;
|
31 |
af4cc5db
|
Leszek Koltunski
|
|
32 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
33 |
70b6a155
|
Leszek Koltunski
|
/**
|
34 |
|
|
* This is not really part of the public API.
|
35 |
|
|
*
|
36 |
|
|
* @y.exclude
|
37 |
|
|
*/
|
38 |
|
|
public abstract class DistortedOutputSurface extends DistortedSurface implements DistortedMaster.Slave
|
39 |
af4cc5db
|
Leszek Koltunski
|
{
|
40 |
86e99907
|
leszek
|
//////////// DEBUG FLAGS /////////////////////////////////////////////
|
41 |
|
|
/**
|
42 |
|
|
* When rendering a Screen, show FPS in the upper-left corner?
|
43 |
|
|
*/
|
44 |
|
|
public static final int DEBUG_FPS = 1;
|
45 |
|
|
//////////// END DEBUG FLAGS /////////////////////////////////////////
|
46 |
|
|
|
47 |
89de975c
|
leszek
|
/**
|
48 |
|
|
* Do not create DEPTH or STENCIL attachment
|
49 |
|
|
*/
|
50 |
23eecbd9
|
Leszek Koltunski
|
public static final int NO_DEPTH_NO_STENCIL = 0;
|
51 |
89de975c
|
leszek
|
/**
|
52 |
|
|
* Create DEPTH, but not STENCIL
|
53 |
|
|
*/
|
54 |
23eecbd9
|
Leszek Koltunski
|
public static final int DEPTH_NO_STENCIL = 1;
|
55 |
89de975c
|
leszek
|
/**
|
56 |
|
|
* Create both DEPTH and STENCIL
|
57 |
|
|
*/
|
58 |
23eecbd9
|
Leszek Koltunski
|
public static final int BOTH_DEPTH_STENCIL = 2;
|
59 |
89de975c
|
leszek
|
|
60 |
efe3d8fe
|
leszek
|
private static final int ATTACH = 0;
|
61 |
|
|
private static final int DETACH = 1;
|
62 |
|
|
private static final int DETALL = 2;
|
63 |
|
|
private static final int SORT = 3;
|
64 |
|
|
|
65 |
a09ada4c
|
Leszek Koltunski
|
private ArrayList<DistortedNode> mChildren;
|
66 |
|
|
private int mNumChildren; // ==mChildren.length(), but we only create mChildren if the first one gets added
|
67 |
|
|
|
68 |
efe3d8fe
|
leszek
|
private class Job
|
69 |
|
|
{
|
70 |
|
|
int type;
|
71 |
|
|
DistortedNode node;
|
72 |
|
|
|
73 |
ffbe7ecf
|
Leszek Koltunski
|
Job(int t, DistortedNode n)
|
74 |
efe3d8fe
|
leszek
|
{
|
75 |
|
|
type = t;
|
76 |
|
|
node = n;
|
77 |
|
|
}
|
78 |
|
|
}
|
79 |
|
|
|
80 |
|
|
private ArrayList<Job> mJobs = new ArrayList<>();
|
81 |
|
|
|
82 |
ce154014
|
Leszek Koltunski
|
// Global buffers used for postprocessing.
|
83 |
b0f04e72
|
Leszek Koltunski
|
private static DistortedOutputSurface[] mBuffer = new DistortedOutputSurface[EffectQuality.LENGTH];
|
84 |
60c1c622
|
leszek
|
|
85 |
95c441a2
|
leszek
|
private long mTime;
|
86 |
c2c08950
|
leszek
|
private float mFOV;
|
87 |
|
|
float mDistance, mNear;
|
88 |
af4cc5db
|
Leszek Koltunski
|
float[] mProjectionMatrix;
|
89 |
|
|
|
90 |
89de975c
|
leszek
|
int mDepthStencilCreated;
|
91 |
|
|
int mDepthStencil;
|
92 |
|
|
int[] mDepthStencilH = new int[1];
|
93 |
|
|
int[] mFBOH = new int[1];
|
94 |
a436ccc5
|
leszek
|
|
95 |
a9f41fa3
|
leszek
|
private float mClearR, mClearG, mClearB, mClearA;
|
96 |
|
|
private float mClearDepth;
|
97 |
23eecbd9
|
Leszek Koltunski
|
private int mClearStencil;
|
98 |
ad16ed3b
|
Leszek Koltunski
|
private int mClear;
|
99 |
78db8663
|
Leszek Koltunski
|
float mMipmap;
|
100 |
|
|
|
101 |
86e99907
|
leszek
|
private int mDebugLevel;
|
102 |
|
|
|
103 |
61441ce2
|
Leszek Koltunski
|
int mRealWidth; // the Surface can be backed up with a texture that is
|
104 |
|
|
int mRealHeight; // larger than the viewport we have to it.
|
105 |
|
|
// mWidth,mHeight are the sizes of the Viewport, those -
|
106 |
|
|
// sizes of the backing up texture.
|
107 |
a4b182d4
|
Leszek Koltunski
|
|
108 |
12f45260
|
Leszek Koltunski
|
////////////////////////////////////////////////////////////////////////////////
|
109 |
|
|
// section dealing with Shader Storage Buffer Object (for counting transparency)
|
110 |
df4d7edc
|
Leszek Koltunski
|
private static final int FRAME_DELAY = 3;
|
111 |
|
|
private static int mBufferSize =10;
|
112 |
12f45260
|
Leszek Koltunski
|
private static DistortedObjectCounter mSurfaceCounter = new DistortedObjectCounter();
|
113 |
|
|
private static int[] mSSBO = new int[1];
|
114 |
66ace7f5
|
Leszek Koltunski
|
private static IntBuffer mIntBuffer;
|
115 |
12f45260
|
Leszek Koltunski
|
|
116 |
|
|
static
|
117 |
|
|
{
|
118 |
66ace7f5
|
Leszek Koltunski
|
mSSBO[0]= -1;
|
119 |
12f45260
|
Leszek Koltunski
|
}
|
120 |
|
|
|
121 |
|
|
private int mSurfaceID;
|
122 |
|
|
private int mLastIndex;
|
123 |
df4d7edc
|
Leszek Koltunski
|
private int[] mLastValue = new int[FRAME_DELAY];
|
124 |
2faad666
|
Leszek Koltunski
|
private int mLastDiff = -1;
|
125 |
|
|
|
126 |
|
|
private static final int RUNNING_AVERAGE = 10;
|
127 |
|
|
private int[] mRunningAvg = new int[RUNNING_AVERAGE];
|
128 |
|
|
private int mAvgIndex;
|
129 |
|
|
private int mAvgSum;
|
130 |
12f45260
|
Leszek Koltunski
|
// end section
|
131 |
|
|
////////////////////////////////////////////////////////////////////////////////
|
132 |
|
|
|
133 |
86e99907
|
leszek
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
134 |
|
|
|
135 |
|
|
abstract void prepareDebug(long time);
|
136 |
|
|
abstract void renderDebug(long time);
|
137 |
12f45260
|
Leszek Koltunski
|
abstract void createSurface();
|
138 |
|
|
abstract void deleteSurface();
|
139 |
|
|
abstract void recreateSurface();
|
140 |
86e99907
|
leszek
|
|
141 |
af4cc5db
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
142 |
|
|
|
143 |
9ed80185
|
Leszek Koltunski
|
DistortedOutputSurface(int width, int height, int createColor, int numcolors, int depthStencil, int fbo, int type)
|
144 |
af4cc5db
|
Leszek Koltunski
|
{
|
145 |
9ed80185
|
Leszek Koltunski
|
super(width,height,createColor,numcolors,type);
|
146 |
af4cc5db
|
Leszek Koltunski
|
|
147 |
a4b182d4
|
Leszek Koltunski
|
mRealWidth = width;
|
148 |
|
|
mRealHeight= height;
|
149 |
|
|
|
150 |
af4cc5db
|
Leszek Koltunski
|
mProjectionMatrix = new float[16];
|
151 |
|
|
|
152 |
|
|
mFOV = 60.0f;
|
153 |
54fe333a
|
leszek
|
mNear= 0.5f;
|
154 |
af4cc5db
|
Leszek Koltunski
|
|
155 |
23eecbd9
|
Leszek Koltunski
|
mDepthStencilCreated= (depthStencil== NO_DEPTH_NO_STENCIL ? DONT_CREATE:NOT_CREATED_YET);
|
156 |
89de975c
|
leszek
|
mDepthStencil = depthStencil;
|
157 |
|
|
|
158 |
|
|
mFBOH[0] = fbo;
|
159 |
|
|
mDepthStencilH[0]= 0;
|
160 |
a436ccc5
|
leszek
|
|
161 |
95c441a2
|
leszek
|
mTime = 0;
|
162 |
86e99907
|
leszek
|
mDebugLevel = 0;
|
163 |
95c441a2
|
leszek
|
|
164 |
a9f41fa3
|
leszek
|
mClearR = 0.0f;
|
165 |
|
|
mClearG = 0.0f;
|
166 |
|
|
mClearB = 0.0f;
|
167 |
|
|
mClearA = 0.0f;
|
168 |
|
|
|
169 |
|
|
mClearDepth = 1.0f;
|
170 |
23eecbd9
|
Leszek Koltunski
|
mClearStencil = 0;
|
171 |
e6519ac8
|
Leszek Koltunski
|
mClear = GLES31.GL_DEPTH_BUFFER_BIT | GLES31.GL_COLOR_BUFFER_BIT;
|
172 |
a9f41fa3
|
leszek
|
|
173 |
8426bd6a
|
Leszek Koltunski
|
mMipmap = 1.0f;
|
174 |
78db8663
|
Leszek Koltunski
|
|
175 |
af4cc5db
|
Leszek Koltunski
|
createProjection();
|
176 |
|
|
}
|
177 |
|
|
|
178 |
12f45260
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
179 |
|
|
// Must be called from a thread holding OpenGL Context
|
180 |
|
|
|
181 |
|
|
void create()
|
182 |
|
|
{
|
183 |
|
|
if( mSSBO[0]<0 )
|
184 |
|
|
{
|
185 |
|
|
GLES31.glGenBuffers(1,mSSBO,0);
|
186 |
|
|
|
187 |
|
|
// here bind the new SSBO and map it
|
188 |
|
|
GLES31.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, mSSBO[0]);
|
189 |
df4d7edc
|
Leszek Koltunski
|
GLES31.glBufferData(GLES31.GL_SHADER_STORAGE_BUFFER, FRAME_DELAY *mBufferSize*4 , null, GLES31.GL_DYNAMIC_READ);
|
190 |
|
|
ByteBuffer buf = (ByteBuffer) GLES31.glMapBufferRange(GLES31.GL_SHADER_STORAGE_BUFFER, 0, FRAME_DELAY *mBufferSize*4, GLES31.GL_MAP_READ_BIT );
|
191 |
66ace7f5
|
Leszek Koltunski
|
mIntBuffer = buf.order(ByteOrder.nativeOrder()).asIntBuffer();
|
192 |
2ab60f72
|
Leszek Koltunski
|
GLES31.glBindBufferBase(GLES31.GL_SHADER_STORAGE_BUFFER,0, mSSBO[0]);
|
193 |
12f45260
|
Leszek Koltunski
|
}
|
194 |
|
|
|
195 |
|
|
mSurfaceID = mSurfaceCounter.returnNext();
|
196 |
|
|
|
197 |
|
|
if( mSurfaceID>=mBufferSize )
|
198 |
|
|
{
|
199 |
51a3cbab
|
Leszek Koltunski
|
// increase size of the Buffer, copy over old values, remap.
|
200 |
df4d7edc
|
Leszek Koltunski
|
int[] tmp = new int[FRAME_DELAY *mBufferSize];
|
201 |
|
|
for(int i = 0; i< FRAME_DELAY *mBufferSize; i++) tmp[i] = mIntBuffer.get(i);
|
202 |
51a3cbab
|
Leszek Koltunski
|
|
203 |
|
|
mBufferSize*= 2;
|
204 |
|
|
|
205 |
|
|
GLES31.glBindBufferBase(GLES31.GL_SHADER_STORAGE_BUFFER, 0, 0);
|
206 |
|
|
GLES31.glUnmapBuffer(GLES31.GL_SHADER_STORAGE_BUFFER);
|
207 |
|
|
GLES31.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER,0);
|
208 |
|
|
|
209 |
|
|
GLES31.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, mSSBO[0]);
|
210 |
df4d7edc
|
Leszek Koltunski
|
GLES31.glBufferData(GLES31.GL_SHADER_STORAGE_BUFFER, FRAME_DELAY *mBufferSize*4 , null, GLES31.GL_DYNAMIC_READ);
|
211 |
|
|
ByteBuffer buf = (ByteBuffer) GLES31.glMapBufferRange(GLES31.GL_SHADER_STORAGE_BUFFER, 0, FRAME_DELAY *mBufferSize*4, GLES31.GL_MAP_READ_BIT );
|
212 |
51a3cbab
|
Leszek Koltunski
|
mIntBuffer = buf.order(ByteOrder.nativeOrder()).asIntBuffer();
|
213 |
|
|
GLES31.glBindBufferBase(GLES31.GL_SHADER_STORAGE_BUFFER,0, mSSBO[0]);
|
214 |
|
|
|
215 |
|
|
for(int i=0;i<tmp.length;i++) mIntBuffer.put(i,tmp[i]);
|
216 |
12f45260
|
Leszek Koltunski
|
}
|
217 |
|
|
|
218 |
|
|
createSurface();
|
219 |
|
|
}
|
220 |
|
|
|
221 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
222 |
|
|
// Must be called from a thread holding OpenGL Context
|
223 |
|
|
|
224 |
|
|
void delete()
|
225 |
|
|
{
|
226 |
|
|
mSurfaceCounter.release(mSurfaceID);
|
227 |
|
|
deleteSurface();
|
228 |
|
|
}
|
229 |
|
|
|
230 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
231 |
|
|
|
232 |
|
|
void recreate()
|
233 |
|
|
{
|
234 |
2faad666
|
Leszek Koltunski
|
mSSBO[0] = -1;
|
235 |
|
|
mLastDiff = -1;
|
236 |
|
|
|
237 |
df4d7edc
|
Leszek Koltunski
|
for(int i = 0; i< FRAME_DELAY; i++) mLastValue[i] = 0;
|
238 |
12f45260
|
Leszek Koltunski
|
mSurfaceCounter.releaseAll();
|
239 |
2faad666
|
Leszek Koltunski
|
|
240 |
12f45260
|
Leszek Koltunski
|
recreateSurface();
|
241 |
|
|
}
|
242 |
|
|
|
243 |
27cd6b98
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
244 |
|
|
|
245 |
8a57da61
|
Leszek Koltunski
|
int getNewCounter()
|
246 |
27cd6b98
|
Leszek Koltunski
|
{
|
247 |
df4d7edc
|
Leszek Koltunski
|
return FRAME_DELAY*mSurfaceID + mLastIndex;
|
248 |
12f45260
|
Leszek Koltunski
|
}
|
249 |
|
|
|
250 |
c5369f1b
|
leszek
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
251 |
|
|
|
252 |
be60d4ff
|
leszek
|
private void createProjection()
|
253 |
af4cc5db
|
Leszek Koltunski
|
{
|
254 |
|
|
if( mWidth>0 && mHeight>1 )
|
255 |
|
|
{
|
256 |
|
|
if( mFOV>0.0f ) // perspective projection
|
257 |
|
|
{
|
258 |
54fe333a
|
leszek
|
float a = 2.0f*(float)Math.tan(mFOV*Math.PI/360);
|
259 |
8426bd6a
|
Leszek Koltunski
|
float q = mWidth*mNear;
|
260 |
|
|
float c = mHeight*mNear;
|
261 |
54fe333a
|
leszek
|
|
262 |
c2c08950
|
leszek
|
float left = -q/2;
|
263 |
|
|
float right = +q/2;
|
264 |
|
|
float bottom = -c/2;
|
265 |
|
|
float top = +c/2;
|
266 |
|
|
float near = c/a;
|
267 |
54fe333a
|
leszek
|
|
268 |
8426bd6a
|
Leszek Koltunski
|
mDistance = mHeight/a;
|
269 |
af4cc5db
|
Leszek Koltunski
|
float far = 2*mDistance-near;
|
270 |
|
|
|
271 |
|
|
Matrix.frustumM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
|
272 |
|
|
}
|
273 |
|
|
else // parallel projection
|
274 |
|
|
{
|
275 |
8426bd6a
|
Leszek Koltunski
|
float left = -mWidth/2.0f;
|
276 |
|
|
float right = +mWidth/2.0f;
|
277 |
|
|
float bottom = -mHeight/2.0f;
|
278 |
|
|
float top = +mHeight/2.0f;
|
279 |
|
|
float near = mWidth+mHeight-mHeight*(1.0f-mNear);
|
280 |
|
|
mDistance = mWidth+mHeight;
|
281 |
|
|
float far = mWidth+mHeight+mHeight*(1.0f-mNear);
|
282 |
af4cc5db
|
Leszek Koltunski
|
|
283 |
|
|
Matrix.orthoM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
|
284 |
|
|
}
|
285 |
|
|
}
|
286 |
|
|
}
|
287 |
|
|
|
288 |
1d6d261e
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
289 |
|
|
|
290 |
ce154014
|
Leszek Koltunski
|
private static void createBuffers(int width, int height, float near)
|
291 |
1d6d261e
|
Leszek Koltunski
|
{
|
292 |
|
|
float mipmap=1.0f;
|
293 |
|
|
|
294 |
|
|
for(int j=0; j<EffectQuality.LENGTH; j++)
|
295 |
|
|
{
|
296 |
ce154014
|
Leszek Koltunski
|
mBuffer[j] = new DistortedFramebuffer(2,BOTH_DEPTH_STENCIL,TYPE_SYST, (int)(width*mipmap), (int)(height*mipmap) );
|
297 |
0f011027
|
Leszek Koltunski
|
mBuffer[j].mMipmap = mipmap;
|
298 |
ce154014
|
Leszek Koltunski
|
mBuffer[j].mNear = near; // copy mNear as well (for blitting- see PostprocessEffect.apply() )
|
299 |
7ad20cce
|
Leszek Koltunski
|
mBuffer[j].glClearColor(1.0f,1.0f,1.0f,0.0f);
|
300 |
a4b182d4
|
Leszek Koltunski
|
|
301 |
1d6d261e
|
Leszek Koltunski
|
mipmap *= EffectQuality.MULTIPLIER;
|
302 |
|
|
}
|
303 |
|
|
|
304 |
|
|
DistortedObject.toDo(); // create the FBOs immediately. This is safe as we must be holding the OpenGL context now.
|
305 |
|
|
|
306 |
e6519ac8
|
Leszek Koltunski
|
GLES31.glStencilMask(0xff);
|
307 |
|
|
GLES31.glDepthMask(true);
|
308 |
|
|
GLES31.glColorMask(true,true,true,true);
|
309 |
|
|
GLES31.glClearColor(0.0f,0.0f,0.0f,0.0f);
|
310 |
|
|
GLES31.glClearDepthf(1.0f);
|
311 |
|
|
GLES31.glClearStencil(0);
|
312 |
1d6d261e
|
Leszek Koltunski
|
|
313 |
|
|
for(int j=0; j<EffectQuality.LENGTH; j++)
|
314 |
|
|
{
|
315 |
0f011027
|
Leszek Koltunski
|
mBuffer[j].setAsOutput();
|
316 |
e6519ac8
|
Leszek Koltunski
|
GLES31.glFramebufferTexture2D(GLES31.GL_FRAMEBUFFER, GLES31.GL_COLOR_ATTACHMENT0, GLES31.GL_TEXTURE_2D, mBuffer[j].mColorH[1], 0);
|
317 |
|
|
GLES31.glClear(GLES31.GL_COLOR_BUFFER_BIT|GLES31.GL_DEPTH_BUFFER_BIT|GLES31.GL_STENCIL_BUFFER_BIT);
|
318 |
|
|
GLES31.glFramebufferTexture2D(GLES31.GL_FRAMEBUFFER, GLES31.GL_COLOR_ATTACHMENT0, GLES31.GL_TEXTURE_2D, mBuffer[j].mColorH[0], 0);
|
319 |
|
|
GLES31.glClear(GLES31.GL_COLOR_BUFFER_BIT);
|
320 |
1d6d261e
|
Leszek Koltunski
|
}
|
321 |
|
|
}
|
322 |
|
|
|
323 |
a4b182d4
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
324 |
|
|
|
325 |
ce154014
|
Leszek Koltunski
|
static synchronized void onDestroy()
|
326 |
a4b182d4
|
Leszek Koltunski
|
{
|
327 |
b0f04e72
|
Leszek Koltunski
|
for(int j=0; j<EffectQuality.LENGTH; j++)
|
328 |
ce154014
|
Leszek Koltunski
|
{
|
329 |
|
|
mBuffer[j] = null;
|
330 |
|
|
}
|
331 |
|
|
}
|
332 |
a4b182d4
|
Leszek Koltunski
|
|
333 |
ce154014
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
334 |
a4b182d4
|
Leszek Koltunski
|
|
335 |
ce154014
|
Leszek Koltunski
|
private static void cloneViewport(DistortedOutputSurface from)
|
336 |
|
|
{
|
337 |
b0f04e72
|
Leszek Koltunski
|
if( mBuffer[0].mWidth != from.mWidth )
|
338 |
a4b182d4
|
Leszek Koltunski
|
{
|
339 |
ce154014
|
Leszek Koltunski
|
DistortedOutputSurface surface;
|
340 |
|
|
|
341 |
|
|
for(int i=0; i<EffectQuality.LENGTH; i++)
|
342 |
|
|
{
|
343 |
|
|
surface = mBuffer[i];
|
344 |
|
|
|
345 |
|
|
surface.mWidth = (int)(from.mWidth *surface.mMipmap);
|
346 |
|
|
surface.mHeight = (int)(from.mHeight*surface.mMipmap);
|
347 |
|
|
|
348 |
|
|
surface.mNear = from.mNear; // Near plane is independent of the mipmap level
|
349 |
|
|
|
350 |
b0f04e72
|
Leszek Koltunski
|
android.util.Log.e("surface", "viewport "+i+" to ("+from.mWidth+"x"+from.mHeight+")");
|
351 |
|
|
|
352 |
ce154014
|
Leszek Koltunski
|
surface.createProjection();
|
353 |
|
|
|
354 |
|
|
int maxw = surface.mWidth > surface.mRealWidth ? surface.mWidth : surface.mRealWidth;
|
355 |
|
|
int maxh = surface.mHeight > surface.mRealHeight ? surface.mHeight : surface.mRealHeight;
|
356 |
a4b182d4
|
Leszek Koltunski
|
|
357 |
ce154014
|
Leszek Koltunski
|
if (maxw > surface.mRealWidth || maxh > surface.mRealHeight)
|
358 |
|
|
{
|
359 |
|
|
surface.mRealWidth = maxw;
|
360 |
|
|
surface.mRealHeight = maxh;
|
361 |
|
|
|
362 |
|
|
surface.recreateSurface();
|
363 |
|
|
surface.createSurface();
|
364 |
|
|
}
|
365 |
|
|
}
|
366 |
a4b182d4
|
Leszek Koltunski
|
}
|
367 |
|
|
}
|
368 |
|
|
|
369 |
6b962e80
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
370 |
|
|
|
371 |
70b6a155
|
Leszek Koltunski
|
private int blitWithDepth(long currTime, DistortedOutputSurface buffer)
|
372 |
6b962e80
|
Leszek Koltunski
|
{
|
373 |
e6519ac8
|
Leszek Koltunski
|
GLES31.glViewport(0, 0, mWidth, mHeight);
|
374 |
6b962e80
|
Leszek Koltunski
|
setAsOutput(currTime);
|
375 |
e6519ac8
|
Leszek Koltunski
|
GLES31.glActiveTexture(GLES31.GL_TEXTURE0);
|
376 |
|
|
GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, buffer.mColorH[0]);
|
377 |
|
|
GLES31.glActiveTexture(GLES31.GL_TEXTURE1);
|
378 |
|
|
GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, buffer.mDepthStencilH[0]);
|
379 |
6b962e80
|
Leszek Koltunski
|
|
380 |
e6519ac8
|
Leszek Koltunski
|
GLES31.glDisable(GLES31.GL_STENCIL_TEST);
|
381 |
|
|
GLES31.glStencilMask(0x00);
|
382 |
6b962e80
|
Leszek Koltunski
|
|
383 |
|
|
DistortedEffects.blitDepthPriv(this);
|
384 |
e6519ac8
|
Leszek Koltunski
|
GLES31.glActiveTexture(GLES31.GL_TEXTURE0);
|
385 |
|
|
GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, 0);
|
386 |
|
|
GLES31.glActiveTexture(GLES31.GL_TEXTURE1);
|
387 |
|
|
GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, 0);
|
388 |
6b962e80
|
Leszek Koltunski
|
|
389 |
|
|
// clear buffers
|
390 |
e6519ac8
|
Leszek Koltunski
|
GLES31.glStencilMask(0xff);
|
391 |
|
|
GLES31.glDepthMask(true);
|
392 |
|
|
GLES31.glColorMask(true,true,true,true);
|
393 |
|
|
GLES31.glClearColor(0.0f,0.0f,0.0f,0.0f);
|
394 |
|
|
GLES31.glClearDepthf(1.0f);
|
395 |
|
|
GLES31.glClearStencil(0);
|
396 |
6b962e80
|
Leszek Koltunski
|
|
397 |
|
|
buffer.setAsOutput();
|
398 |
e6519ac8
|
Leszek Koltunski
|
GLES31.glFramebufferTexture2D(GLES31.GL_FRAMEBUFFER, GLES31.GL_COLOR_ATTACHMENT0, GLES31.GL_TEXTURE_2D, buffer.mColorH[1], 0);
|
399 |
|
|
GLES31.glClear(GLES31.GL_COLOR_BUFFER_BIT|GLES31.GL_DEPTH_BUFFER_BIT|GLES31.GL_STENCIL_BUFFER_BIT);
|
400 |
|
|
GLES31.glFramebufferTexture2D(GLES31.GL_FRAMEBUFFER, GLES31.GL_COLOR_ATTACHMENT0, GLES31.GL_TEXTURE_2D, buffer.mColorH[0], 0);
|
401 |
|
|
GLES31.glClear(GLES31.GL_COLOR_BUFFER_BIT);
|
402 |
6b962e80
|
Leszek Koltunski
|
|
403 |
|
|
return 1;
|
404 |
|
|
}
|
405 |
|
|
|
406 |
af4cc5db
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
407 |
e02264ff
|
leszek
|
// Render all children, one by one. If there are no postprocessing effects, just render to THIS.
|
408 |
|
|
// Otherwise, render to a buffer and on each change of Postprocessing Bucket, apply the postprocessing
|
409 |
|
|
// to a whole buffer and merge it.
|
410 |
39086ebb
|
leszek
|
|
411 |
8e28b6ff
|
leszek
|
int renderChildren(long time, int numChildren, ArrayList<DistortedNode> children)
|
412 |
b2939df4
|
Leszek Koltunski
|
{
|
413 |
7266d8ef
|
Leszek Koltunski
|
int quality=0, internalQuality = 0, numRenders = 0, bucketChange = 0;
|
414 |
3f44e745
|
Leszek Koltunski
|
DistortedNode child1, child2;
|
415 |
70b6a155
|
Leszek Koltunski
|
EffectQueuePostprocess lastQueue=null, currQueue;
|
416 |
915b7b2b
|
leszek
|
long lastBucket=0, currBucket;
|
417 |
0afc143a
|
leszek
|
|
418 |
8e28b6ff
|
leszek
|
for(int i=0; i<numChildren; i++)
|
419 |
39086ebb
|
leszek
|
{
|
420 |
3f44e745
|
Leszek Koltunski
|
child1 = children.get(i);
|
421 |
70b6a155
|
Leszek Koltunski
|
currQueue = child1.getPostprocessQueue();
|
422 |
|
|
currBucket= currQueue.getID();
|
423 |
b9798977
|
leszek
|
|
424 |
915b7b2b
|
leszek
|
if( currBucket==0 ) numRenders += child1.draw(time,this);
|
425 |
60c1c622
|
leszek
|
else
|
426 |
|
|
{
|
427 |
ce154014
|
Leszek Koltunski
|
if( mBuffer[0]==null ) createBuffers(mWidth,mHeight,mNear);
|
428 |
c9a24bfb
|
Leszek Koltunski
|
|
429 |
915b7b2b
|
leszek
|
if( lastBucket!=currBucket )
|
430 |
c9a24bfb
|
Leszek Koltunski
|
{
|
431 |
915b7b2b
|
leszek
|
if( lastBucket!=0 )
|
432 |
c9a24bfb
|
Leszek Koltunski
|
{
|
433 |
|
|
for(int j=bucketChange; j<i; j++)
|
434 |
|
|
{
|
435 |
3f44e745
|
Leszek Koltunski
|
child2 = children.get(j);
|
436 |
70b6a155
|
Leszek Koltunski
|
numRenders += child2.markStencilAndDepth(time,mBuffer[internalQuality],lastQueue);
|
437 |
c9a24bfb
|
Leszek Koltunski
|
}
|
438 |
|
|
|
439 |
61441ce2
|
Leszek Koltunski
|
numRenders += lastQueue.postprocess(mBuffer);
|
440 |
bed13bea
|
leszek
|
numRenders += blitWithDepth(time, mBuffer[quality]);
|
441 |
c9a24bfb
|
Leszek Koltunski
|
}
|
442 |
660cd468
|
leszek
|
|
443 |
70b6a155
|
Leszek Koltunski
|
internalQuality = currQueue.getInternalQuality();
|
444 |
|
|
quality = currQueue.getQuality();
|
445 |
|
|
bucketChange = i;
|
446 |
ce154014
|
Leszek Koltunski
|
|
447 |
|
|
cloneViewport(this);
|
448 |
c9a24bfb
|
Leszek Koltunski
|
}
|
449 |
|
|
|
450 |
70b6a155
|
Leszek Koltunski
|
child1.draw(time,mBuffer[quality]);
|
451 |
c9a24bfb
|
Leszek Koltunski
|
|
452 |
8e28b6ff
|
leszek
|
if( i==numChildren-1 )
|
453 |
cf7394cc
|
leszek
|
{
|
454 |
8e28b6ff
|
leszek
|
for(int j=bucketChange; j<numChildren; j++)
|
455 |
c9a24bfb
|
Leszek Koltunski
|
{
|
456 |
3f44e745
|
Leszek Koltunski
|
child2 = children.get(j);
|
457 |
70b6a155
|
Leszek Koltunski
|
numRenders += child2.markStencilAndDepth(time,mBuffer[internalQuality],currQueue);
|
458 |
c9a24bfb
|
Leszek Koltunski
|
}
|
459 |
|
|
|
460 |
61441ce2
|
Leszek Koltunski
|
numRenders += currQueue.postprocess(mBuffer);
|
461 |
7266d8ef
|
Leszek Koltunski
|
numRenders += blitWithDepth(time, mBuffer[quality]);
|
462 |
cf7394cc
|
leszek
|
}
|
463 |
60c1c622
|
leszek
|
}
|
464 |
b9798977
|
leszek
|
|
465 |
70b6a155
|
Leszek Koltunski
|
lastQueue = currQueue;
|
466 |
|
|
lastBucket= currBucket;
|
467 |
95c441a2
|
leszek
|
}
|
468 |
270c27bc
|
Leszek Koltunski
|
|
469 |
39086ebb
|
leszek
|
return numRenders;
|
470 |
|
|
}
|
471 |
|
|
|
472 |
be60d4ff
|
leszek
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
473 |
|
|
|
474 |
26a4e5f6
|
leszek
|
ArrayList<DistortedNode> getChildren()
|
475 |
be60d4ff
|
leszek
|
{
|
476 |
26a4e5f6
|
leszek
|
return mChildren;
|
477 |
be60d4ff
|
leszek
|
}
|
478 |
|
|
|
479 |
b2939df4
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
480 |
|
|
// PUBLIC API
|
481 |
39086ebb
|
leszek
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
482 |
86e99907
|
leszek
|
/**
|
483 |
|
|
* Make the library show various debugging information.
|
484 |
|
|
* <p>
|
485 |
|
|
* Currently only DEBUG_FPS - show FPS in the upper-left corner of every Screen - is defined.
|
486 |
|
|
*
|
487 |
|
|
* @param bitmask 0, or a bitmask of DEBUG_** flags to enable (currently only DEBUG_FPS defined)
|
488 |
|
|
*/
|
489 |
|
|
public void setDebug(int bitmask)
|
490 |
|
|
{
|
491 |
26a4e5f6
|
leszek
|
if( this instanceof DistortedScreen )
|
492 |
|
|
mDebugLevel = bitmask;
|
493 |
86e99907
|
leszek
|
}
|
494 |
|
|
|
495 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
496 |
|
|
|
497 |
c5369f1b
|
leszek
|
/**
|
498 |
d9706fd2
|
Leszek Koltunski
|
* Draws all the attached children to this OutputSurface.
|
499 |
af4cc5db
|
Leszek Koltunski
|
* <p>
|
500 |
|
|
* Must be called from a thread holding OpenGL Context.
|
501 |
|
|
*
|
502 |
d9706fd2
|
Leszek Koltunski
|
* @param time Current time, in milliseconds. This will be passed to all the Effects stored in the children Nodes.
|
503 |
7691a39f
|
leszek
|
* @return Number of objects rendered.
|
504 |
c5369f1b
|
leszek
|
*/
|
505 |
b2939df4
|
Leszek Koltunski
|
public int render(long time)
|
506 |
af4cc5db
|
Leszek Koltunski
|
{
|
507 |
26a4e5f6
|
leszek
|
if( mDebugLevel!=0 ) prepareDebug(time);
|
508 |
86e99907
|
leszek
|
|
509 |
c204c69d
|
leszek
|
// change tree topology (attach and detach children)
|
510 |
889cce10
|
Leszek Koltunski
|
/*
|
511 |
42571056
|
Leszek Koltunski
|
boolean changed1 =
|
512 |
889cce10
|
Leszek Koltunski
|
*/
|
513 |
efe3d8fe
|
leszek
|
DistortedMaster.toDo();
|
514 |
eadf0859
|
leszek
|
/*
|
515 |
42571056
|
Leszek Koltunski
|
if( changed1 )
|
516 |
c204c69d
|
leszek
|
{
|
517 |
|
|
for(int i=0; i<mNumChildren; i++)
|
518 |
|
|
{
|
519 |
af27df87
|
leszek
|
mChildren.get(i).debug(0);
|
520 |
c204c69d
|
leszek
|
}
|
521 |
af27df87
|
leszek
|
|
522 |
7691a39f
|
leszek
|
DistortedNode.debugMap();
|
523 |
c204c69d
|
leszek
|
}
|
524 |
eadf0859
|
leszek
|
*/
|
525 |
09ab7524
|
Leszek Koltunski
|
// create and delete all underlying OpenGL resources
|
526 |
|
|
// Watch out: FIRST change topology, only then deal
|
527 |
|
|
// with OpenGL resources. That's because changing Tree
|
528 |
|
|
// can result in additional Framebuffers that would need
|
529 |
|
|
// to be created immediately, before the calls to drawRecursive()
|
530 |
42571056
|
Leszek Koltunski
|
/*
|
531 |
|
|
boolean changed2 =
|
532 |
|
|
*/
|
533 |
f8377ef8
|
leszek
|
toDo();
|
534 |
eadf0859
|
leszek
|
/*
|
535 |
42571056
|
Leszek Koltunski
|
if( changed2 )
|
536 |
af27df87
|
leszek
|
{
|
537 |
226144d0
|
leszek
|
DistortedObject.debugLists();
|
538 |
42571056
|
Leszek Koltunski
|
}
|
539 |
eadf0859
|
leszek
|
*/
|
540 |
c834348d
|
leszek
|
// mark OpenGL state as unknown
|
541 |
|
|
DistortedRenderState.reset();
|
542 |
2ed1c692
|
leszek
|
|
543 |
b2939df4
|
Leszek Koltunski
|
int numRenders=0;
|
544 |
7691a39f
|
leszek
|
|
545 |
d9706fd2
|
Leszek Koltunski
|
for(int i=0; i<mNumChildren; i++)
|
546 |
af4cc5db
|
Leszek Koltunski
|
{
|
547 |
b2939df4
|
Leszek Koltunski
|
numRenders += mChildren.get(i).renderRecursive(time);
|
548 |
af4cc5db
|
Leszek Koltunski
|
}
|
549 |
7691a39f
|
leszek
|
|
550 |
070695a5
|
Leszek Koltunski
|
setAsOutput(time);
|
551 |
b2939df4
|
Leszek Koltunski
|
numRenders += renderChildren(time,mNumChildren,mChildren);
|
552 |
|
|
|
553 |
26a4e5f6
|
leszek
|
if( mDebugLevel != 0 ) renderDebug(time);
|
554 |
b28c6c21
|
leszek
|
|
555 |
7691a39f
|
leszek
|
return numRenders;
|
556 |
af4cc5db
|
Leszek Koltunski
|
}
|
557 |
|
|
|
558 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
559 |
c5369f1b
|
leszek
|
/**
|
560 |
|
|
* Bind this Surface as a Framebuffer we can render to.
|
561 |
02ab6f9d
|
leszek
|
*
|
562 |
|
|
* @param time Present time, in milliseconds. The point: looking at this param the library can figure
|
563 |
|
|
* out if this is the first time during present frame that this FBO is being set as output.
|
564 |
|
|
* If so, the library, in addition to binding the Surface for output, also clears the
|
565 |
|
|
* Surface's color and depth attachments.
|
566 |
c5369f1b
|
leszek
|
*/
|
567 |
95c441a2
|
leszek
|
public void setAsOutput(long time)
|
568 |
a436ccc5
|
leszek
|
{
|
569 |
e6519ac8
|
Leszek Koltunski
|
GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, mFBOH[0]);
|
570 |
95c441a2
|
leszek
|
|
571 |
|
|
if( mTime!=time )
|
572 |
|
|
{
|
573 |
|
|
mTime = time;
|
574 |
23eecbd9
|
Leszek Koltunski
|
DistortedRenderState.colorDepthStencilOn();
|
575 |
e6519ac8
|
Leszek Koltunski
|
GLES31.glClearColor(mClearR, mClearG, mClearB, mClearA);
|
576 |
|
|
GLES31.glClearDepthf(mClearDepth);
|
577 |
|
|
GLES31.glClearStencil(mClearStencil);
|
578 |
|
|
GLES31.glClear(mClear);
|
579 |
144d252c
|
Leszek Koltunski
|
DistortedRenderState.colorDepthStencilRestore();
|
580 |
c14e495b
|
Leszek Koltunski
|
|
581 |
2faad666
|
Leszek Koltunski
|
if( mSSBO[0]>=0 )
|
582 |
df4d7edc
|
Leszek Koltunski
|
{
|
583 |
|
|
// yes, this DOES keep on working when 'value' overflows into negative territory.
|
584 |
2faad666
|
Leszek Koltunski
|
int value = mIntBuffer.get(FRAME_DELAY*mSurfaceID+mLastIndex);
|
585 |
|
|
|
586 |
|
|
if( value-mLastValue[mLastIndex]!=mLastDiff )
|
587 |
|
|
{
|
588 |
|
|
android.util.Log.d("surface", "id " + mSurfaceID +
|
589 |
|
|
(mType == TYPE_USER ? " USER" : (mType == TYPE_SYST ? " SYST" : " TREE")) +
|
590 |
ce154014
|
Leszek Koltunski
|
" viewport: (" + mWidth + "x" + mHeight + ") last frame: " + (value - mLastValue[mLastIndex])
|
591 |
2faad666
|
Leszek Koltunski
|
+ " avg: " + (mAvgSum/RUNNING_AVERAGE)
|
592 |
|
|
);
|
593 |
|
|
}
|
594 |
|
|
|
595 |
|
|
mLastDiff = value-mLastValue[mLastIndex];
|
596 |
|
|
mLastValue[mLastIndex] = value;
|
597 |
|
|
|
598 |
|
|
mAvgSum += (mLastDiff-mRunningAvg[mAvgIndex]);
|
599 |
|
|
mRunningAvg[mAvgIndex] = mLastDiff;
|
600 |
|
|
if( ++mAvgIndex>=RUNNING_AVERAGE ) mAvgIndex =0;
|
601 |
df4d7edc
|
Leszek Koltunski
|
}
|
602 |
2faad666
|
Leszek Koltunski
|
|
603 |
|
|
if( ++mLastIndex >= FRAME_DELAY ) mLastIndex=0;
|
604 |
95c441a2
|
leszek
|
}
|
605 |
a436ccc5
|
leszek
|
}
|
606 |
af4cc5db
|
Leszek Koltunski
|
|
607 |
1dfc9074
|
leszek
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
608 |
|
|
/**
|
609 |
|
|
* Bind this Surface as a Framebuffer we can render to.
|
610 |
|
|
* <p>
|
611 |
|
|
* This version does not attempt to clear anything.
|
612 |
|
|
*/
|
613 |
|
|
|
614 |
|
|
public void setAsOutput()
|
615 |
|
|
{
|
616 |
e6519ac8
|
Leszek Koltunski
|
GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, mFBOH[0]);
|
617 |
1dfc9074
|
leszek
|
}
|
618 |
|
|
|
619 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
620 |
|
|
/**
|
621 |
|
|
* Return the Near plane of the Projection included in the Surface.
|
622 |
|
|
*
|
623 |
|
|
* @return the Near plane.
|
624 |
|
|
*/
|
625 |
|
|
public float getNear()
|
626 |
|
|
{
|
627 |
|
|
return mNear;
|
628 |
|
|
}
|
629 |
|
|
|
630 |
638b5b5c
|
leszek
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
631 |
|
|
/**
|
632 |
|
|
* Set mipmap level.
|
633 |
|
|
* <p>
|
634 |
|
|
* Trick for speeding up your renders - one can create a pyramid of OutputSurface objects, each next
|
635 |
|
|
* one some constant FACTOR smaller than the previous (0.5 is the common value), then set the Mipmap
|
636 |
|
|
* Level of the i-th object to be FACTOR^i (we start counting from 0). When rendering any scene into
|
637 |
|
|
* such prepared OutputSurface, the library will make sure to scale any Effects used so that the end
|
638 |
|
|
* scene will end up looking identical no matter which object we render to. Identical, that is, except
|
639 |
|
|
* for the loss of quality and gain in speed associated with rendering to a smaller Surface.
|
640 |
8426bd6a
|
Leszek Koltunski
|
* <p>
|
641 |
|
|
* Example: if you create two FBOs, one 1000x1000 and another 500x500 in size, and set the second one
|
642 |
|
|
* mipmap to 0.5 (the first one's is 1.0 by default), define Effects to be a single move by (100,100),
|
643 |
|
|
* and render a skinned Mesh into both FBO, the end result will look proportionally the same, because
|
644 |
|
|
* in the second case the move vector (100,100) will be auto-scaled to (50,50).
|
645 |
638b5b5c
|
leszek
|
*
|
646 |
|
|
* @param mipmap The mipmap level. Acceptable range: 0<mipmap<infinity, although mipmap>1
|
647 |
|
|
* does not make any sense (that would result in loss of speed and no gain in quality)
|
648 |
|
|
*/
|
649 |
|
|
public void setMipmap(float mipmap)
|
650 |
|
|
{
|
651 |
|
|
mMipmap = mipmap;
|
652 |
|
|
}
|
653 |
|
|
|
654 |
a9f41fa3
|
leszek
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
655 |
|
|
/**
|
656 |
e6519ac8
|
Leszek Koltunski
|
* Set the (R,G,B,A) values of GLES31.glClearColor() to set up color with which to clear
|
657 |
ad16ed3b
|
Leszek Koltunski
|
* this Surface at the beginning of each frame.
|
658 |
a9f41fa3
|
leszek
|
*
|
659 |
|
|
* @param r the Red component. Default: 0.0f
|
660 |
|
|
* @param g the Green component. Default: 0.0f
|
661 |
|
|
* @param b the Blue component. Default: 0.0f
|
662 |
|
|
* @param a the Alpha component. Default: 0.0f
|
663 |
|
|
*/
|
664 |
|
|
public void glClearColor(float r, float g, float b, float a)
|
665 |
|
|
{
|
666 |
|
|
mClearR = r;
|
667 |
|
|
mClearG = g;
|
668 |
|
|
mClearB = b;
|
669 |
|
|
mClearA = a;
|
670 |
|
|
}
|
671 |
|
|
|
672 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
673 |
|
|
/**
|
674 |
23eecbd9
|
Leszek Koltunski
|
* Uses glClearDepthf() to set up a value with which to clear
|
675 |
|
|
* the Depth buffer of our Surface at the beginning of each frame.
|
676 |
a9f41fa3
|
leszek
|
*
|
677 |
|
|
* @param d the Depth. Default: 1.0f
|
678 |
|
|
*/
|
679 |
|
|
public void glClearDepthf(float d)
|
680 |
|
|
{
|
681 |
|
|
mClearDepth = d;
|
682 |
|
|
}
|
683 |
|
|
|
684 |
23eecbd9
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
685 |
|
|
/**
|
686 |
|
|
* Uses glClearStencil() to set up a value with which to clear the
|
687 |
|
|
* Stencil buffer of our Surface at the beginning of each frame.
|
688 |
|
|
*
|
689 |
|
|
* @param s the Stencil. Default: 0
|
690 |
|
|
*/
|
691 |
|
|
public void glClearStencil(int s)
|
692 |
|
|
{
|
693 |
|
|
mClearStencil = s;
|
694 |
|
|
}
|
695 |
|
|
|
696 |
ad16ed3b
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
697 |
|
|
/**
|
698 |
|
|
* Which buffers to Clear at the beginning of each frame?
|
699 |
|
|
* <p>
|
700 |
|
|
* Valid values: 0, or bitwise OR of one or more values from the set GL_COLOR_BUFFER_BIT,
|
701 |
|
|
* GL_DEPTH_BUFFER_BIT, GL_STENCIL_BUFFER_BIT.
|
702 |
|
|
* Default: GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT.
|
703 |
|
|
*
|
704 |
|
|
* @param mask bitwise OR of BUFFER_BITs to clear.
|
705 |
|
|
*/
|
706 |
|
|
public void glClear(int mask)
|
707 |
|
|
{
|
708 |
|
|
mClear = mask;
|
709 |
|
|
}
|
710 |
|
|
|
711 |
af4cc5db
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
712 |
|
|
/**
|
713 |
|
|
* Create new Projection matrix.
|
714 |
|
|
*
|
715 |
|
|
* @param fov Vertical 'field of view' of the Projection frustrum (in degrees).
|
716 |
54fe333a
|
leszek
|
* Valid values: 0<=fov<180. FOV==0 means 'parallel projection'.
|
717 |
|
|
* @param near Distance between the screen plane and the near plane.
|
718 |
61441ce2
|
Leszek Koltunski
|
* Valid vaules: 0<near<1. When near==0 (illegal!), the Near Plane is exactly at the tip of
|
719 |
|
|
* the pyramid. When near==1 (illegal!) the near plane is equivalent to the screen plane.
|
720 |
af4cc5db
|
Leszek Koltunski
|
*/
|
721 |
54fe333a
|
leszek
|
public void setProjection(float fov, float near)
|
722 |
af4cc5db
|
Leszek Koltunski
|
{
|
723 |
8069e806
|
Leszek Koltunski
|
if( fov < 180.0f && fov >=0.0f )
|
724 |
|
|
{
|
725 |
|
|
mFOV = fov;
|
726 |
|
|
}
|
727 |
|
|
|
728 |
|
|
if( near< 1.0f && near> 0.0f )
|
729 |
|
|
{
|
730 |
|
|
mNear= near;
|
731 |
|
|
}
|
732 |
|
|
else if( near<=0.0f )
|
733 |
|
|
{
|
734 |
|
|
mNear = 0.01f;
|
735 |
|
|
}
|
736 |
|
|
else if( near>=1.0f )
|
737 |
|
|
{
|
738 |
|
|
mNear=0.99f;
|
739 |
|
|
}
|
740 |
af4cc5db
|
Leszek Koltunski
|
|
741 |
1dfc9074
|
leszek
|
if( mBuffer[0]!=null )
|
742 |
|
|
{
|
743 |
|
|
for(int j=0; j<EffectQuality.LENGTH; j++) mBuffer[j].mNear = mNear;
|
744 |
|
|
}
|
745 |
|
|
|
746 |
af4cc5db
|
Leszek Koltunski
|
createProjection();
|
747 |
|
|
}
|
748 |
|
|
|
749 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
750 |
c5369f1b
|
leszek
|
/**
|
751 |
af4cc5db
|
Leszek Koltunski
|
* Resize the underlying Framebuffer.
|
752 |
c7da4e65
|
leszek
|
* <p>
|
753 |
|
|
* This method can be safely called mid-render as it doesn't interfere with rendering.
|
754 |
af4cc5db
|
Leszek Koltunski
|
*
|
755 |
|
|
* @param width The new width.
|
756 |
|
|
* @param height The new height.
|
757 |
c5369f1b
|
leszek
|
*/
|
758 |
af4cc5db
|
Leszek Koltunski
|
public void resize(int width, int height)
|
759 |
|
|
{
|
760 |
|
|
if( mWidth!=width || mHeight!=height )
|
761 |
|
|
{
|
762 |
a4b182d4
|
Leszek Koltunski
|
mWidth = mRealWidth = width;
|
763 |
|
|
mHeight= mRealHeight= height;
|
764 |
f8377ef8
|
leszek
|
|
765 |
|
|
createProjection();
|
766 |
|
|
|
767 |
c7da4e65
|
leszek
|
if( mColorCreated==CREATED )
|
768 |
f8377ef8
|
leszek
|
{
|
769 |
f28fffc2
|
Leszek Koltunski
|
markForCreation();
|
770 |
f8377ef8
|
leszek
|
recreate();
|
771 |
|
|
}
|
772 |
af4cc5db
|
Leszek Koltunski
|
}
|
773 |
|
|
}
|
774 |
a09ada4c
|
Leszek Koltunski
|
|
775 |
a436ccc5
|
leszek
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
776 |
|
|
/**
|
777 |
89de975c
|
leszek
|
* Return true if the Surface contains a DEPTH attachment.
|
778 |
a436ccc5
|
leszek
|
*
|
779 |
89de975c
|
leszek
|
* @return <bold>true</bold> if the Surface contains a DEPTH attachment.
|
780 |
a436ccc5
|
leszek
|
*/
|
781 |
89de975c
|
leszek
|
public boolean hasDepth()
|
782 |
a436ccc5
|
leszek
|
{
|
783 |
89de975c
|
leszek
|
return mDepthStencilCreated==CREATED;
|
784 |
a436ccc5
|
leszek
|
}
|
785 |
|
|
|
786 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
787 |
|
|
/**
|
788 |
89de975c
|
leszek
|
* Return true if the Surface contains a STENCIL attachment.
|
789 |
a436ccc5
|
leszek
|
*
|
790 |
89de975c
|
leszek
|
* @return <bold>true</bold> if the Surface contains a STENCIL attachment.
|
791 |
a436ccc5
|
leszek
|
*/
|
792 |
89de975c
|
leszek
|
public boolean hasStencil()
|
793 |
a436ccc5
|
leszek
|
{
|
794 |
89de975c
|
leszek
|
return (mDepthStencilCreated==CREATED && mDepthStencil==BOTH_DEPTH_STENCIL);
|
795 |
a436ccc5
|
leszek
|
}
|
796 |
|
|
|
797 |
a09ada4c
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
798 |
|
|
/**
|
799 |
|
|
* Adds a new child to the last position in the list of our Surface's children.
|
800 |
c204c69d
|
leszek
|
* <p>
|
801 |
|
|
* We cannot do this mid-render - actual attachment will be done just before the next render, by the
|
802 |
efe3d8fe
|
leszek
|
* DistortedMaster (by calling doWork())
|
803 |
a09ada4c
|
Leszek Koltunski
|
*
|
804 |
|
|
* @param node The new Node to add.
|
805 |
|
|
*/
|
806 |
c204c69d
|
leszek
|
public void attach(DistortedNode node)
|
807 |
a09ada4c
|
Leszek Koltunski
|
{
|
808 |
ffbe7ecf
|
Leszek Koltunski
|
mJobs.add(new Job(ATTACH,node));
|
809 |
efe3d8fe
|
leszek
|
DistortedMaster.newSlave(this);
|
810 |
a09ada4c
|
Leszek Koltunski
|
}
|
811 |
|
|
|
812 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
813 |
|
|
/**
|
814 |
|
|
* Adds a new child to the last position in the list of our Surface's children.
|
815 |
c204c69d
|
leszek
|
* <p>
|
816 |
|
|
* We cannot do this mid-render - actual attachment will be done just before the next render, by the
|
817 |
efe3d8fe
|
leszek
|
* DistortedMaster (by calling doWork())
|
818 |
a09ada4c
|
Leszek Koltunski
|
*
|
819 |
|
|
* @param surface InputSurface to initialize our child Node with.
|
820 |
|
|
* @param effects DistortedEffects to initialize our child Node with.
|
821 |
|
|
* @param mesh MeshObject to initialize our child Node with.
|
822 |
|
|
* @return the newly constructed child Node, or null if we couldn't allocate resources.
|
823 |
|
|
*/
|
824 |
c204c69d
|
leszek
|
public DistortedNode attach(DistortedInputSurface surface, DistortedEffects effects, MeshObject mesh)
|
825 |
a09ada4c
|
Leszek Koltunski
|
{
|
826 |
|
|
DistortedNode node = new DistortedNode(surface,effects,mesh);
|
827 |
ffbe7ecf
|
Leszek Koltunski
|
mJobs.add(new Job(ATTACH,node));
|
828 |
efe3d8fe
|
leszek
|
DistortedMaster.newSlave(this);
|
829 |
c204c69d
|
leszek
|
return node;
|
830 |
|
|
}
|
831 |
|
|
|
832 |
af27df87
|
leszek
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
833 |
|
|
/**
|
834 |
|
|
* Removes the first occurrence of a specified child from the list of children of our Surface.
|
835 |
|
|
* <p>
|
836 |
|
|
* A bit questionable method as there can be many different Nodes attached as children, some
|
837 |
|
|
* of them having the same Effects but - for instance - different Mesh. Use with care.
|
838 |
|
|
* <p>
|
839 |
|
|
* We cannot do this mid-render - actual detachment will be done just before the next render, by the
|
840 |
efe3d8fe
|
leszek
|
* DistortedMaster (by calling doWork())
|
841 |
af27df87
|
leszek
|
*
|
842 |
|
|
* @param effects DistortedEffects to remove.
|
843 |
|
|
*/
|
844 |
|
|
public void detach(DistortedEffects effects)
|
845 |
|
|
{
|
846 |
|
|
long id = effects.getID();
|
847 |
|
|
DistortedNode node;
|
848 |
8baa1fe6
|
Leszek Koltunski
|
boolean detached = false;
|
849 |
af27df87
|
leszek
|
|
850 |
|
|
for(int i=0; i<mNumChildren; i++)
|
851 |
|
|
{
|
852 |
|
|
node = mChildren.get(i);
|
853 |
|
|
|
854 |
|
|
if( node.getEffects().getID()==id )
|
855 |
|
|
{
|
856 |
8baa1fe6
|
Leszek Koltunski
|
detached = true;
|
857 |
ffbe7ecf
|
Leszek Koltunski
|
mJobs.add(new Job(DETACH,node));
|
858 |
efe3d8fe
|
leszek
|
DistortedMaster.newSlave(this);
|
859 |
af27df87
|
leszek
|
break;
|
860 |
|
|
}
|
861 |
|
|
}
|
862 |
8baa1fe6
|
Leszek Koltunski
|
|
863 |
|
|
if( !detached )
|
864 |
|
|
{
|
865 |
|
|
// if we failed to detach any, it still might be the case that
|
866 |
efe3d8fe
|
leszek
|
// there's an ATTACH job that we need to cancel.
|
867 |
|
|
int num = mJobs.size();
|
868 |
|
|
Job job;
|
869 |
|
|
|
870 |
|
|
for(int i=0; i<num; i++)
|
871 |
|
|
{
|
872 |
|
|
job = mJobs.get(i);
|
873 |
|
|
|
874 |
|
|
if( job.type==ATTACH && job.node.getEffects()==effects )
|
875 |
|
|
{
|
876 |
|
|
mJobs.remove(i);
|
877 |
|
|
break;
|
878 |
|
|
}
|
879 |
|
|
}
|
880 |
8baa1fe6
|
Leszek Koltunski
|
}
|
881 |
af27df87
|
leszek
|
}
|
882 |
|
|
|
883 |
a09ada4c
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
884 |
|
|
/**
|
885 |
|
|
* Removes the first occurrence of a specified child from the list of children of our Surface.
|
886 |
c204c69d
|
leszek
|
* <p>
|
887 |
|
|
* We cannot do this mid-render - actual attachment will be done just before the next render, by the
|
888 |
efe3d8fe
|
leszek
|
* DistortedMaster (by calling doWork())
|
889 |
c204c69d
|
leszek
|
*
|
890 |
|
|
* @param node The Node to remove.
|
891 |
|
|
*/
|
892 |
|
|
public void detach(DistortedNode node)
|
893 |
|
|
{
|
894 |
ffbe7ecf
|
Leszek Koltunski
|
mJobs.add(new Job(DETACH,node));
|
895 |
efe3d8fe
|
leszek
|
DistortedMaster.newSlave(this);
|
896 |
a09ada4c
|
Leszek Koltunski
|
}
|
897 |
|
|
|
898 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
899 |
|
|
/**
|
900 |
|
|
* Removes all children Nodes.
|
901 |
c204c69d
|
leszek
|
* <p>
|
902 |
|
|
* We cannot do this mid-render - actual attachment will be done just before the next render, by the
|
903 |
efe3d8fe
|
leszek
|
* DistortedMaster (by calling doWork())
|
904 |
c204c69d
|
leszek
|
*/
|
905 |
|
|
public void detachAll()
|
906 |
|
|
{
|
907 |
ffbe7ecf
|
Leszek Koltunski
|
mJobs.add(new Job(DETALL,null));
|
908 |
efe3d8fe
|
leszek
|
DistortedMaster.newSlave(this);
|
909 |
c204c69d
|
leszek
|
}
|
910 |
|
|
|
911 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
912 |
|
|
/**
|
913 |
|
|
* This is not really part of the public API. Has to be public only because it is a part of the
|
914 |
efe3d8fe
|
leszek
|
* DistortedSlave interface, which should really be a class that we extend here instead but
|
915 |
c204c69d
|
leszek
|
* Java has no multiple inheritance.
|
916 |
43fbf0dd
|
leszek
|
*
|
917 |
|
|
* @y.exclude
|
918 |
a09ada4c
|
Leszek Koltunski
|
*/
|
919 |
efe3d8fe
|
leszek
|
public void doWork()
|
920 |
a09ada4c
|
Leszek Koltunski
|
{
|
921 |
efe3d8fe
|
leszek
|
int num = mJobs.size();
|
922 |
|
|
Job job;
|
923 |
|
|
|
924 |
|
|
for(int i=0; i<num; i++)
|
925 |
|
|
{
|
926 |
|
|
job = mJobs.remove(0);
|
927 |
|
|
|
928 |
|
|
switch(job.type)
|
929 |
|
|
{
|
930 |
be60d4ff
|
leszek
|
case ATTACH: if( mChildren==null ) mChildren = new ArrayList<>(2);
|
931 |
|
|
job.node.setSurfaceParent(this);
|
932 |
|
|
DistortedMaster.addSorted(mChildren,job.node);
|
933 |
efe3d8fe
|
leszek
|
mNumChildren++;
|
934 |
|
|
break;
|
935 |
be60d4ff
|
leszek
|
case DETACH: if( mNumChildren>0 && mChildren.remove(job.node) )
|
936 |
efe3d8fe
|
leszek
|
{
|
937 |
be60d4ff
|
leszek
|
job.node.setSurfaceParent(null);
|
938 |
efe3d8fe
|
leszek
|
mNumChildren--;
|
939 |
|
|
}
|
940 |
|
|
break;
|
941 |
be60d4ff
|
leszek
|
case DETALL: if( mNumChildren>0 )
|
942 |
efe3d8fe
|
leszek
|
{
|
943 |
be60d4ff
|
leszek
|
DistortedNode tmp;
|
944 |
|
|
|
945 |
|
|
for(int j=mNumChildren-1; j>=0; j--)
|
946 |
|
|
{
|
947 |
|
|
tmp = mChildren.remove(j);
|
948 |
|
|
tmp.setSurfaceParent(null);
|
949 |
|
|
}
|
950 |
|
|
|
951 |
efe3d8fe
|
leszek
|
mNumChildren = 0;
|
952 |
|
|
}
|
953 |
|
|
break;
|
954 |
ffbe7ecf
|
Leszek Koltunski
|
case SORT : mChildren.remove(job.node);
|
955 |
be60d4ff
|
leszek
|
DistortedMaster.addSorted(mChildren,job.node);
|
956 |
efe3d8fe
|
leszek
|
break;
|
957 |
|
|
}
|
958 |
|
|
}
|
959 |
a09ada4c
|
Leszek Koltunski
|
}
|
960 |
af4cc5db
|
Leszek Koltunski
|
}
|