1 |
9dba4df1
|
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 |
|
|
package org.distorted.library;
|
21 |
|
|
|
22 |
|
|
import android.opengl.GLES30;
|
23 |
|
|
|
24 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
25 |
|
|
/**
|
26 |
|
|
* Remember the OpenGL state.
|
27 |
ad16ed3b
|
Leszek Koltunski
|
* <p>
|
28 |
|
|
* This is a member of DistortedNode. Remembers the OpenGL state we want to set just before rendering
|
29 |
|
|
* the Node.
|
30 |
9dba4df1
|
leszek
|
*/
|
31 |
|
|
class DistortedRenderState
|
32 |
|
|
{
|
33 |
ad16ed3b
|
Leszek Koltunski
|
private static int sColorMaskR, sColorMaskG, sColorMaskB, sColorMaskA; //
|
34 |
|
|
private static int sDepthMask; //
|
35 |
|
|
private static int sStencilMask; //
|
36 |
|
|
private static int sDepthTest; //
|
37 |
|
|
private static int sStencilTest; //
|
38 |
|
|
private static int sStencilFuncFunc, sStencilFuncRef, sStencilFuncMask; // current OpenGL state
|
39 |
|
|
private static int sStencilOpSfail, sStencilOpDpfail, sStencilOpDppass; //
|
40 |
|
|
private static int sDepthFunc; //
|
41 |
|
|
private static int sBlend; //
|
42 |
|
|
private static int sBlendSrc, sBlendDst; //
|
43 |
|
|
|
44 |
|
|
private int mColorMaskR, mColorMaskG, mColorMaskB, mColorMaskA; //
|
45 |
|
|
private int mDepthMask; //
|
46 |
|
|
private int mStencilMask; //
|
47 |
|
|
private int mDepthTest; //
|
48 |
|
|
private int mStencilTest; //
|
49 |
|
|
private int mStencilFuncFunc, mStencilFuncRef, mStencilFuncMask; // The state we want to have
|
50 |
|
|
private int mStencilOpSfail, mStencilOpDpfail, mStencilOpDppass; //
|
51 |
|
|
private int mDepthFunc; //
|
52 |
|
|
private int mBlend; //
|
53 |
|
|
private int mBlendSrc, mBlendDst; //
|
54 |
|
|
private int mClear; // This does not have a 'static' compatriot
|
55 |
9dba4df1
|
leszek
|
|
56 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
57 |
c834348d
|
leszek
|
// default: color writes on, depth test and writes on, blending on, stencil off.
|
58 |
9dba4df1
|
leszek
|
|
59 |
|
|
DistortedRenderState()
|
60 |
|
|
{
|
61 |
|
|
mColorMaskR = 1;
|
62 |
|
|
mColorMaskG = 1;
|
63 |
|
|
mColorMaskB = 1;
|
64 |
|
|
mColorMaskA = 1;
|
65 |
c834348d
|
leszek
|
|
66 |
9dba4df1
|
leszek
|
mDepthTest = 1;
|
67 |
c834348d
|
leszek
|
mDepthMask = 1;
|
68 |
|
|
mDepthFunc = GLES30.GL_LEQUAL;
|
69 |
9dba4df1
|
leszek
|
|
70 |
c834348d
|
leszek
|
mBlend = 1;
|
71 |
|
|
mBlendSrc = GLES30.GL_SRC_ALPHA;
|
72 |
|
|
mBlendDst = GLES30.GL_ONE_MINUS_SRC_ALPHA;
|
73 |
|
|
|
74 |
|
|
mStencilTest = 0;
|
75 |
|
|
mStencilMask = 0x11111111;
|
76 |
9dba4df1
|
leszek
|
mStencilFuncFunc = GLES30.GL_NEVER;
|
77 |
|
|
mStencilFuncRef = 0;
|
78 |
|
|
mStencilFuncMask = 0x11111111;
|
79 |
|
|
mStencilOpSfail = GLES30.GL_KEEP;
|
80 |
|
|
mStencilOpDpfail = GLES30.GL_KEEP;
|
81 |
|
|
mStencilOpDppass = GLES30.GL_KEEP;
|
82 |
ad16ed3b
|
Leszek Koltunski
|
|
83 |
|
|
mClear = 0;
|
84 |
9dba4df1
|
leszek
|
}
|
85 |
|
|
|
86 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
87 |
c834348d
|
leszek
|
// reset state of everything to 'unknown'
|
88 |
9dba4df1
|
leszek
|
|
89 |
|
|
static void reset()
|
90 |
|
|
{
|
91 |
|
|
sColorMaskR = -1;
|
92 |
|
|
sColorMaskG = -1;
|
93 |
|
|
sColorMaskB = -1;
|
94 |
|
|
sColorMaskA = -1;
|
95 |
c834348d
|
leszek
|
|
96 |
9dba4df1
|
leszek
|
sDepthTest = -1;
|
97 |
c834348d
|
leszek
|
sDepthMask = -1;
|
98 |
|
|
sDepthFunc = -1;
|
99 |
|
|
|
100 |
|
|
sBlend = -1;
|
101 |
|
|
sBlendSrc = -1;
|
102 |
|
|
sBlendDst = -1;
|
103 |
9dba4df1
|
leszek
|
|
104 |
c834348d
|
leszek
|
sStencilTest = -1;
|
105 |
|
|
sStencilMask = -1;
|
106 |
9dba4df1
|
leszek
|
sStencilFuncFunc = -1;
|
107 |
|
|
sStencilFuncRef = -1;
|
108 |
|
|
sStencilFuncMask = -1;
|
109 |
|
|
sStencilOpSfail = -1;
|
110 |
|
|
sStencilOpDpfail = -1;
|
111 |
|
|
sStencilOpDppass = -1;
|
112 |
|
|
}
|
113 |
|
|
|
114 |
580f7d10
|
leszek
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
115 |
|
|
|
116 |
|
|
static void colorDepthOn()
|
117 |
|
|
{
|
118 |
|
|
if( sColorMaskR!=1 || sColorMaskG!=1 || sColorMaskB!=1 || sColorMaskA!=1 )
|
119 |
|
|
{
|
120 |
|
|
sColorMaskR = 1;
|
121 |
|
|
sColorMaskG = 1;
|
122 |
|
|
sColorMaskB = 1;
|
123 |
|
|
sColorMaskA = 1;
|
124 |
|
|
GLES30.glColorMask(true,true,true,true);
|
125 |
|
|
}
|
126 |
|
|
if( sDepthMask!=1 )
|
127 |
|
|
{
|
128 |
|
|
sDepthMask = 1;
|
129 |
|
|
GLES30.glDepthMask(true);
|
130 |
|
|
}
|
131 |
|
|
}
|
132 |
|
|
|
133 |
cab7c165
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
134 |
|
|
|
135 |
|
|
static void switchOffDrawing()
|
136 |
|
|
{
|
137 |
|
|
GLES30.glEnable(GLES30.GL_SCISSOR_TEST);
|
138 |
|
|
GLES30.glScissor(0,0,0,0);
|
139 |
|
|
}
|
140 |
|
|
|
141 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
142 |
|
|
|
143 |
|
|
static void restoreDrawing()
|
144 |
|
|
{
|
145 |
|
|
GLES30.glDisable(GLES30.GL_SCISSOR_TEST);
|
146 |
|
|
}
|
147 |
|
|
|
148 |
9dba4df1
|
leszek
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
149 |
|
|
|
150 |
|
|
void apply()
|
151 |
|
|
{
|
152 |
cc8151c4
|
leszek
|
//android.util.Log.e("State", "APPLYING STATE");
|
153 |
d582c116
|
Leszek Koltunski
|
|
154 |
cc8151c4
|
leszek
|
/////////////////////////////////////////////////////
|
155 |
|
|
// 1. Write to color buffer?
|
156 |
9dba4df1
|
leszek
|
if( mColorMaskR!=sColorMaskR || mColorMaskG!=sColorMaskG || mColorMaskB!=sColorMaskB || mColorMaskA!=sColorMaskA)
|
157 |
|
|
{
|
158 |
cc8151c4
|
leszek
|
//android.util.Log.d("State", "setting color mask");
|
159 |
9dba4df1
|
leszek
|
sColorMaskR = mColorMaskR;
|
160 |
|
|
sColorMaskG = mColorMaskG;
|
161 |
|
|
sColorMaskB = mColorMaskB;
|
162 |
|
|
sColorMaskA = mColorMaskA;
|
163 |
|
|
GLES30.glColorMask(sColorMaskR==1,sColorMaskG==1,sColorMaskB==1,sColorMaskA==1);
|
164 |
|
|
}
|
165 |
|
|
|
166 |
cc8151c4
|
leszek
|
/////////////////////////////////////////////////////
|
167 |
d582c116
|
Leszek Koltunski
|
// 2. Enable Depth test?
|
168 |
9dba4df1
|
leszek
|
if( mDepthTest!=sDepthTest )
|
169 |
|
|
{
|
170 |
|
|
sDepthTest = mDepthTest;
|
171 |
d582c116
|
Leszek Koltunski
|
|
172 |
cc8151c4
|
leszek
|
if (sDepthTest == 0)
|
173 |
|
|
{
|
174 |
|
|
//android.util.Log.d("State", "disabling depth test");
|
175 |
|
|
GLES30.glDisable(GLES30.GL_DEPTH_TEST);
|
176 |
|
|
}
|
177 |
9dba4df1
|
leszek
|
else
|
178 |
|
|
{
|
179 |
cc8151c4
|
leszek
|
//android.util.Log.d("State", "enable depth test");
|
180 |
|
|
GLES30.glEnable(GLES30.GL_DEPTH_TEST);
|
181 |
9dba4df1
|
leszek
|
}
|
182 |
|
|
}
|
183 |
|
|
|
184 |
cc8151c4
|
leszek
|
/////////////////////////////////////////////////////
|
185 |
|
|
// 3. Change Depth Function?
|
186 |
|
|
if( mDepthFunc!=sDepthFunc )
|
187 |
|
|
{
|
188 |
|
|
//android.util.Log.d("State", "setting depth func");
|
189 |
|
|
sDepthFunc = mDepthFunc;
|
190 |
|
|
GLES30.glDepthFunc(sDepthFunc);
|
191 |
|
|
}
|
192 |
d582c116
|
Leszek Koltunski
|
|
193 |
cc8151c4
|
leszek
|
/////////////////////////////////////////////////////
|
194 |
|
|
// 4. Write to Depth buffer?
|
195 |
d582c116
|
Leszek Koltunski
|
if( mDepthMask!=sDepthMask )
|
196 |
|
|
{
|
197 |
cc8151c4
|
leszek
|
//android.util.Log.d("State", "setting depth mask");
|
198 |
d582c116
|
Leszek Koltunski
|
sDepthMask = mDepthMask;
|
199 |
|
|
GLES30.glDepthMask(sDepthMask==1);
|
200 |
|
|
}
|
201 |
|
|
|
202 |
cc8151c4
|
leszek
|
/////////////////////////////////////////////////////
|
203 |
|
|
// 5. Enable Blending?
|
204 |
c834348d
|
leszek
|
if( mBlend!=sBlend )
|
205 |
|
|
{
|
206 |
|
|
sBlend = mBlend;
|
207 |
d582c116
|
Leszek Koltunski
|
|
208 |
cc8151c4
|
leszek
|
if (sBlend == 0)
|
209 |
|
|
{
|
210 |
|
|
//android.util.Log.d("State", "disabling blending");
|
211 |
|
|
GLES30.glDisable(GLES30.GL_BLEND);
|
212 |
|
|
}
|
213 |
c834348d
|
leszek
|
else
|
214 |
|
|
{
|
215 |
cc8151c4
|
leszek
|
//android.util.Log.d("State", "enabling blending");
|
216 |
c834348d
|
leszek
|
GLES30.glEnable(GLES30.GL_BLEND);
|
217 |
|
|
}
|
218 |
|
|
}
|
219 |
|
|
|
220 |
cc8151c4
|
leszek
|
/////////////////////////////////////////////////////
|
221 |
|
|
// 6. Change Blend function?
|
222 |
|
|
if( mBlendSrc!=sBlendSrc || mBlendDst!=sBlendDst )
|
223 |
|
|
{
|
224 |
|
|
//android.util.Log.d("State", "setting blend function");
|
225 |
|
|
sBlendSrc = mBlendSrc;
|
226 |
|
|
sBlendDst = mBlendDst;
|
227 |
|
|
GLES30.glBlendFunc(sBlendSrc,sBlendDst);
|
228 |
|
|
}
|
229 |
d582c116
|
Leszek Koltunski
|
|
230 |
cc8151c4
|
leszek
|
/////////////////////////////////////////////////////
|
231 |
|
|
// 7. Enable/Disable Stencil Test?
|
232 |
9dba4df1
|
leszek
|
if( mStencilTest!=sStencilTest )
|
233 |
|
|
{
|
234 |
|
|
sStencilTest = mStencilTest;
|
235 |
d582c116
|
Leszek Koltunski
|
|
236 |
cc8151c4
|
leszek
|
if (sStencilTest == 0)
|
237 |
|
|
{
|
238 |
|
|
//android.util.Log.d("State", "disabling stencil test");
|
239 |
|
|
GLES30.glDisable(GLES30.GL_STENCIL_TEST);
|
240 |
|
|
}
|
241 |
9dba4df1
|
leszek
|
else
|
242 |
|
|
{
|
243 |
cc8151c4
|
leszek
|
//android.util.Log.d("State", "enabling stencil test");
|
244 |
9dba4df1
|
leszek
|
GLES30.glEnable(GLES30.GL_STENCIL_TEST);
|
245 |
|
|
}
|
246 |
|
|
}
|
247 |
d582c116
|
Leszek Koltunski
|
|
248 |
cc8151c4
|
leszek
|
// android.util.Log.d("State", "mFunc="+mStencilFuncFunc+" sFunc="+sStencilFuncFunc);
|
249 |
|
|
// android.util.Log.d("State", "GL_ALWAYS="+GLES30.GL_ALWAYS+" GL_EQUAL="+GLES30.GL_EQUAL);
|
250 |
|
|
|
251 |
|
|
/////////////////////////////////////////////////////
|
252 |
|
|
// 8. Adjust Stencil function?
|
253 |
|
|
if( mStencilFuncFunc!=sStencilFuncFunc || mStencilFuncRef!=sStencilFuncRef || mStencilFuncMask!=sStencilFuncMask )
|
254 |
|
|
{
|
255 |
|
|
//android.util.Log.d("State", "setting stencil function");
|
256 |
|
|
sStencilFuncFunc = mStencilFuncFunc;
|
257 |
|
|
sStencilFuncRef = mStencilFuncRef ;
|
258 |
|
|
sStencilFuncMask = mStencilFuncMask;
|
259 |
|
|
GLES30.glStencilFunc(sStencilFuncFunc,sStencilFuncRef,sStencilFuncMask);
|
260 |
|
|
}
|
261 |
|
|
|
262 |
|
|
/////////////////////////////////////////////////////
|
263 |
|
|
// 9. Adjust Stencil operation?
|
264 |
|
|
if( mStencilOpSfail!=sStencilOpSfail || mStencilOpDpfail!=sStencilOpDpfail || mStencilOpDppass!=sStencilOpDppass )
|
265 |
|
|
{
|
266 |
|
|
//android.util.Log.d("State", "setting stencil op");
|
267 |
|
|
sStencilOpSfail = mStencilOpSfail;
|
268 |
|
|
sStencilOpDpfail= mStencilOpDpfail;
|
269 |
|
|
sStencilOpDppass= mStencilOpDppass;
|
270 |
|
|
GLES30.glStencilOp(sStencilOpSfail,sStencilOpDpfail,sStencilOpDppass);
|
271 |
|
|
}
|
272 |
d582c116
|
Leszek Koltunski
|
|
273 |
cc8151c4
|
leszek
|
/////////////////////////////////////////////////////
|
274 |
|
|
// 10. Write to Stencil buffer?
|
275 |
d582c116
|
Leszek Koltunski
|
if( mStencilMask!=sStencilMask )
|
276 |
|
|
{
|
277 |
cc8151c4
|
leszek
|
//android.util.Log.d("State", "setting stencil mask");
|
278 |
d582c116
|
Leszek Koltunski
|
sStencilMask = mStencilMask;
|
279 |
|
|
GLES30.glStencilMask(sStencilMask);
|
280 |
|
|
}
|
281 |
ad16ed3b
|
Leszek Koltunski
|
|
282 |
cc8151c4
|
leszek
|
/////////////////////////////////////////////////////
|
283 |
|
|
// 11. Clear buffers?
|
284 |
ad16ed3b
|
Leszek Koltunski
|
if( mClear!=0 )
|
285 |
|
|
{
|
286 |
cc8151c4
|
leszek
|
//android.util.Log.d("State", "clearing buffer");
|
287 |
ad16ed3b
|
Leszek Koltunski
|
GLES30.glClear(mClear);
|
288 |
|
|
}
|
289 |
9dba4df1
|
leszek
|
}
|
290 |
|
|
|
291 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
292 |
|
|
|
293 |
|
|
void glColorMask(boolean r, boolean g, boolean b, boolean a)
|
294 |
|
|
{
|
295 |
|
|
mColorMaskR = (r ? 1:0);
|
296 |
|
|
mColorMaskG = (g ? 1:0);
|
297 |
|
|
mColorMaskB = (b ? 1:0);
|
298 |
|
|
mColorMaskA = (a ? 1:0);
|
299 |
|
|
}
|
300 |
|
|
|
301 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
302 |
|
|
|
303 |
|
|
void glDepthMask(boolean mask)
|
304 |
|
|
{
|
305 |
|
|
mDepthMask = (mask ? 1:0);
|
306 |
|
|
}
|
307 |
|
|
|
308 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
309 |
|
|
|
310 |
|
|
void glStencilMask(int mask)
|
311 |
|
|
{
|
312 |
|
|
mStencilMask = mask;
|
313 |
|
|
}
|
314 |
|
|
|
315 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
316 |
|
|
|
317 |
|
|
void glEnable(int test)
|
318 |
|
|
{
|
319 |
|
|
if( test==GLES30.GL_DEPTH_TEST ) mDepthTest = 1;
|
320 |
|
|
else if( test==GLES30.GL_STENCIL_TEST ) mStencilTest = 1;
|
321 |
c834348d
|
leszek
|
else if( test==GLES30.GL_BLEND ) mBlend = 1;
|
322 |
9dba4df1
|
leszek
|
}
|
323 |
|
|
|
324 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
325 |
|
|
|
326 |
|
|
void glDisable(int test)
|
327 |
|
|
{
|
328 |
|
|
if( test==GLES30.GL_DEPTH_TEST ) mDepthTest = 0;
|
329 |
|
|
else if( test==GLES30.GL_STENCIL_TEST ) mStencilTest = 0;
|
330 |
c834348d
|
leszek
|
else if( test==GLES30.GL_BLEND ) mBlend = 0;
|
331 |
9dba4df1
|
leszek
|
}
|
332 |
|
|
|
333 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
334 |
|
|
|
335 |
|
|
void glStencilFunc(int func, int ref, int mask)
|
336 |
|
|
{
|
337 |
|
|
mStencilFuncFunc = func;
|
338 |
|
|
mStencilFuncRef = ref;
|
339 |
|
|
mStencilFuncMask = mask;
|
340 |
|
|
}
|
341 |
|
|
|
342 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
343 |
|
|
|
344 |
|
|
void glStencilOp(int sfail, int dpfail, int dppass)
|
345 |
|
|
{
|
346 |
|
|
mStencilOpSfail = sfail;
|
347 |
|
|
mStencilOpDpfail= dpfail;
|
348 |
|
|
mStencilOpDppass= dppass;
|
349 |
|
|
}
|
350 |
|
|
|
351 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
352 |
|
|
|
353 |
|
|
void glDepthFunc(int func)
|
354 |
|
|
{
|
355 |
|
|
mDepthFunc = func;
|
356 |
|
|
}
|
357 |
c834348d
|
leszek
|
|
358 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
359 |
|
|
|
360 |
|
|
void glBlendFunc(int src, int dst)
|
361 |
|
|
{
|
362 |
|
|
mBlendSrc = src;
|
363 |
|
|
mBlendDst = dst;
|
364 |
|
|
}
|
365 |
|
|
|
366 |
ad16ed3b
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
367 |
|
|
|
368 |
|
|
void glClear(int mask)
|
369 |
|
|
{
|
370 |
|
|
mClear = mask;
|
371 |
|
|
}
|
372 |
9dba4df1
|
leszek
|
}
|