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 |
|
|
*/
|
28 |
|
|
class DistortedRenderState
|
29 |
|
|
{
|
30 |
|
|
private static int sColorMaskR, sColorMaskG, sColorMaskB, sColorMaskA;
|
31 |
|
|
private static int sDepthMask;
|
32 |
|
|
private static int sStencilMask;
|
33 |
|
|
private static int sDepthTest;
|
34 |
|
|
private static int sStencilTest;
|
35 |
|
|
private static int sStencilFuncFunc, sStencilFuncRef, sStencilFuncMask;
|
36 |
|
|
private static int sStencilOpSfail, sStencilOpDpfail, sStencilOpDppass;
|
37 |
|
|
private static int sDepthFunc;
|
38 |
c834348d
|
leszek
|
private static int sBlend;
|
39 |
|
|
private static int sBlendSrc, sBlendDst;
|
40 |
9dba4df1
|
leszek
|
|
41 |
|
|
private int mColorMaskR, mColorMaskG, mColorMaskB, mColorMaskA;
|
42 |
|
|
private int mDepthMask;
|
43 |
|
|
private int mStencilMask;
|
44 |
|
|
private int mDepthTest;
|
45 |
|
|
private int mStencilTest;
|
46 |
|
|
private int mStencilFuncFunc, mStencilFuncRef, mStencilFuncMask;
|
47 |
|
|
private int mStencilOpSfail, mStencilOpDpfail, mStencilOpDppass;
|
48 |
|
|
private int mDepthFunc;
|
49 |
c834348d
|
leszek
|
private int mBlend;
|
50 |
|
|
private int mBlendSrc, mBlendDst;
|
51 |
9dba4df1
|
leszek
|
|
52 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
53 |
c834348d
|
leszek
|
// default: color writes on, depth test and writes on, blending on, stencil off.
|
54 |
9dba4df1
|
leszek
|
|
55 |
|
|
DistortedRenderState()
|
56 |
|
|
{
|
57 |
|
|
mColorMaskR = 1;
|
58 |
|
|
mColorMaskG = 1;
|
59 |
|
|
mColorMaskB = 1;
|
60 |
|
|
mColorMaskA = 1;
|
61 |
c834348d
|
leszek
|
|
62 |
9dba4df1
|
leszek
|
mDepthTest = 1;
|
63 |
c834348d
|
leszek
|
mDepthMask = 1;
|
64 |
|
|
mDepthFunc = GLES30.GL_LEQUAL;
|
65 |
9dba4df1
|
leszek
|
|
66 |
c834348d
|
leszek
|
mBlend = 1;
|
67 |
|
|
mBlendSrc = GLES30.GL_SRC_ALPHA;
|
68 |
|
|
mBlendDst = GLES30.GL_ONE_MINUS_SRC_ALPHA;
|
69 |
|
|
|
70 |
|
|
mStencilTest = 0;
|
71 |
|
|
mStencilMask = 0x11111111;
|
72 |
9dba4df1
|
leszek
|
mStencilFuncFunc = GLES30.GL_NEVER;
|
73 |
|
|
mStencilFuncRef = 0;
|
74 |
|
|
mStencilFuncMask = 0x11111111;
|
75 |
|
|
mStencilOpSfail = GLES30.GL_KEEP;
|
76 |
|
|
mStencilOpDpfail = GLES30.GL_KEEP;
|
77 |
|
|
mStencilOpDppass = GLES30.GL_KEEP;
|
78 |
|
|
}
|
79 |
|
|
|
80 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
81 |
c834348d
|
leszek
|
// reset state of everything to 'unknown'
|
82 |
9dba4df1
|
leszek
|
|
83 |
|
|
static void reset()
|
84 |
|
|
{
|
85 |
|
|
sColorMaskR = -1;
|
86 |
|
|
sColorMaskG = -1;
|
87 |
|
|
sColorMaskB = -1;
|
88 |
|
|
sColorMaskA = -1;
|
89 |
c834348d
|
leszek
|
|
90 |
9dba4df1
|
leszek
|
sDepthTest = -1;
|
91 |
c834348d
|
leszek
|
sDepthMask = -1;
|
92 |
|
|
sDepthFunc = -1;
|
93 |
|
|
|
94 |
|
|
sBlend = -1;
|
95 |
|
|
sBlendSrc = -1;
|
96 |
|
|
sBlendDst = -1;
|
97 |
9dba4df1
|
leszek
|
|
98 |
c834348d
|
leszek
|
sStencilTest = -1;
|
99 |
|
|
sStencilMask = -1;
|
100 |
9dba4df1
|
leszek
|
sStencilFuncFunc = -1;
|
101 |
|
|
sStencilFuncRef = -1;
|
102 |
|
|
sStencilFuncMask = -1;
|
103 |
|
|
sStencilOpSfail = -1;
|
104 |
|
|
sStencilOpDpfail = -1;
|
105 |
|
|
sStencilOpDppass = -1;
|
106 |
|
|
}
|
107 |
|
|
|
108 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
109 |
|
|
|
110 |
|
|
void apply()
|
111 |
|
|
{
|
112 |
d582c116
|
Leszek Koltunski
|
// 1. Write to color buffer?
|
113 |
|
|
|
114 |
9dba4df1
|
leszek
|
if( mColorMaskR!=sColorMaskR || mColorMaskG!=sColorMaskG || mColorMaskB!=sColorMaskB || mColorMaskA!=sColorMaskA)
|
115 |
|
|
{
|
116 |
|
|
sColorMaskR = mColorMaskR;
|
117 |
|
|
sColorMaskG = mColorMaskG;
|
118 |
|
|
sColorMaskB = mColorMaskB;
|
119 |
|
|
sColorMaskA = mColorMaskA;
|
120 |
|
|
GLES30.glColorMask(sColorMaskR==1,sColorMaskG==1,sColorMaskB==1,sColorMaskA==1);
|
121 |
|
|
}
|
122 |
|
|
|
123 |
d582c116
|
Leszek Koltunski
|
// 2. Enable Depth test?
|
124 |
|
|
|
125 |
9dba4df1
|
leszek
|
if( mDepthTest!=sDepthTest )
|
126 |
|
|
{
|
127 |
|
|
sDepthTest = mDepthTest;
|
128 |
d582c116
|
Leszek Koltunski
|
|
129 |
9dba4df1
|
leszek
|
if( sDepthTest==0 ) GLES30.glDisable(GLES30.GL_DEPTH_TEST);
|
130 |
|
|
else
|
131 |
|
|
{
|
132 |
|
|
GLES30.glEnable (GLES30.GL_DEPTH_TEST);
|
133 |
|
|
|
134 |
|
|
if( mDepthFunc!=sDepthFunc )
|
135 |
|
|
{
|
136 |
|
|
sDepthFunc = mDepthFunc;
|
137 |
|
|
GLES30.glDepthFunc(sDepthFunc);
|
138 |
|
|
}
|
139 |
|
|
}
|
140 |
|
|
}
|
141 |
|
|
|
142 |
d582c116
|
Leszek Koltunski
|
// 3. Write to Depth buffer?
|
143 |
|
|
|
144 |
|
|
if( mDepthMask!=sDepthMask )
|
145 |
|
|
{
|
146 |
|
|
sDepthMask = mDepthMask;
|
147 |
|
|
GLES30.glDepthMask(sDepthMask==1);
|
148 |
|
|
}
|
149 |
|
|
|
150 |
|
|
// 4. Enable Blending?
|
151 |
|
|
|
152 |
c834348d
|
leszek
|
if( mBlend!=sBlend )
|
153 |
|
|
{
|
154 |
|
|
sBlend = mBlend;
|
155 |
d582c116
|
Leszek Koltunski
|
|
156 |
c834348d
|
leszek
|
if( sBlend==0 ) GLES30.glDisable(GLES30.GL_BLEND);
|
157 |
|
|
else
|
158 |
|
|
{
|
159 |
|
|
GLES30.glEnable(GLES30.GL_BLEND);
|
160 |
|
|
|
161 |
|
|
if( mBlendSrc!=sBlendSrc || mBlendDst!=sBlendDst )
|
162 |
|
|
{
|
163 |
|
|
sBlendSrc = mBlendSrc;
|
164 |
|
|
sBlendDst = mBlendDst;
|
165 |
|
|
GLES30.glBlendFunc(sBlendSrc,sBlendDst);
|
166 |
|
|
}
|
167 |
|
|
}
|
168 |
|
|
}
|
169 |
|
|
|
170 |
d582c116
|
Leszek Koltunski
|
// 5. Enable Stencil Test?
|
171 |
|
|
|
172 |
9dba4df1
|
leszek
|
if( mStencilTest!=sStencilTest )
|
173 |
|
|
{
|
174 |
|
|
sStencilTest = mStencilTest;
|
175 |
d582c116
|
Leszek Koltunski
|
|
176 |
9dba4df1
|
leszek
|
if( sStencilTest==0 ) GLES30.glDisable(GLES30.GL_STENCIL_TEST);
|
177 |
|
|
else
|
178 |
|
|
{
|
179 |
|
|
GLES30.glEnable(GLES30.GL_STENCIL_TEST);
|
180 |
|
|
|
181 |
|
|
if( mStencilFuncFunc!=sStencilFuncFunc || mStencilFuncRef!=sStencilFuncRef || mStencilFuncMask!=sStencilFuncMask )
|
182 |
|
|
{
|
183 |
|
|
sStencilFuncFunc = mStencilFuncFunc;
|
184 |
|
|
sStencilFuncRef = mStencilFuncRef ;
|
185 |
|
|
sStencilFuncMask = mStencilFuncMask;
|
186 |
|
|
GLES30.glStencilFunc(sStencilFuncFunc,sStencilFuncRef,sStencilFuncMask);
|
187 |
|
|
}
|
188 |
|
|
|
189 |
|
|
if( mStencilOpSfail!=sStencilOpSfail || mStencilOpDpfail!=sStencilOpDpfail || mStencilOpDppass!=sStencilOpDppass )
|
190 |
|
|
{
|
191 |
|
|
sStencilOpSfail = mStencilOpSfail;
|
192 |
|
|
sStencilOpDpfail= mStencilOpDpfail;
|
193 |
|
|
sStencilOpDppass= mStencilOpDppass;
|
194 |
|
|
GLES30.glStencilOp(sStencilOpSfail,sStencilOpDpfail,sStencilOpDppass);
|
195 |
|
|
}
|
196 |
|
|
}
|
197 |
|
|
}
|
198 |
d582c116
|
Leszek Koltunski
|
|
199 |
|
|
// 6. Write to Stencil buffer?
|
200 |
|
|
|
201 |
|
|
if( mStencilMask!=sStencilMask )
|
202 |
|
|
{
|
203 |
|
|
sStencilMask = mStencilMask;
|
204 |
|
|
GLES30.glStencilMask(sStencilMask);
|
205 |
|
|
}
|
206 |
9dba4df1
|
leszek
|
}
|
207 |
|
|
|
208 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
209 |
|
|
|
210 |
|
|
void glColorMask(boolean r, boolean g, boolean b, boolean a)
|
211 |
|
|
{
|
212 |
|
|
mColorMaskR = (r ? 1:0);
|
213 |
|
|
mColorMaskG = (g ? 1:0);
|
214 |
|
|
mColorMaskB = (b ? 1:0);
|
215 |
|
|
mColorMaskA = (a ? 1:0);
|
216 |
|
|
}
|
217 |
|
|
|
218 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
219 |
|
|
|
220 |
|
|
void glDepthMask(boolean mask)
|
221 |
|
|
{
|
222 |
|
|
mDepthMask = (mask ? 1:0);
|
223 |
|
|
}
|
224 |
|
|
|
225 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
226 |
|
|
|
227 |
|
|
void glStencilMask(int mask)
|
228 |
|
|
{
|
229 |
|
|
mStencilMask = mask;
|
230 |
|
|
}
|
231 |
|
|
|
232 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
233 |
|
|
|
234 |
|
|
void glEnable(int test)
|
235 |
|
|
{
|
236 |
|
|
if( test==GLES30.GL_DEPTH_TEST ) mDepthTest = 1;
|
237 |
|
|
else if( test==GLES30.GL_STENCIL_TEST ) mStencilTest = 1;
|
238 |
c834348d
|
leszek
|
else if( test==GLES30.GL_BLEND ) mBlend = 1;
|
239 |
9dba4df1
|
leszek
|
}
|
240 |
|
|
|
241 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
242 |
|
|
|
243 |
|
|
void glDisable(int test)
|
244 |
|
|
{
|
245 |
|
|
if( test==GLES30.GL_DEPTH_TEST ) mDepthTest = 0;
|
246 |
|
|
else if( test==GLES30.GL_STENCIL_TEST ) mStencilTest = 0;
|
247 |
c834348d
|
leszek
|
else if( test==GLES30.GL_BLEND ) mBlend = 0;
|
248 |
9dba4df1
|
leszek
|
}
|
249 |
|
|
|
250 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
251 |
|
|
|
252 |
|
|
void glStencilFunc(int func, int ref, int mask)
|
253 |
|
|
{
|
254 |
|
|
mStencilFuncFunc = func;
|
255 |
|
|
mStencilFuncRef = ref;
|
256 |
|
|
mStencilFuncMask = mask;
|
257 |
|
|
}
|
258 |
|
|
|
259 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
260 |
|
|
|
261 |
|
|
void glStencilOp(int sfail, int dpfail, int dppass)
|
262 |
|
|
{
|
263 |
|
|
mStencilOpSfail = sfail;
|
264 |
|
|
mStencilOpDpfail= dpfail;
|
265 |
|
|
mStencilOpDppass= dppass;
|
266 |
|
|
}
|
267 |
|
|
|
268 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
269 |
|
|
|
270 |
|
|
void glDepthFunc(int func)
|
271 |
|
|
{
|
272 |
|
|
mDepthFunc = func;
|
273 |
|
|
}
|
274 |
c834348d
|
leszek
|
|
275 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
276 |
|
|
|
277 |
|
|
void glBlendFunc(int src, int dst)
|
278 |
|
|
{
|
279 |
|
|
mBlendSrc = src;
|
280 |
|
|
mBlendDst = dst;
|
281 |
|
|
}
|
282 |
|
|
|
283 |
9dba4df1
|
leszek
|
}
|