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