1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2016 Leszek Koltunski //
|
3
|
// //
|
4
|
// This file is part of Distorted. //
|
5
|
// //
|
6
|
// Distorted is free software: you can redistribute it and/or modify //
|
7
|
// it under the terms of the GNU General Public License as published by //
|
8
|
// the Free Software Foundation, either version 2 of the License, or //
|
9
|
// (at your option) any later version. //
|
10
|
// //
|
11
|
// Distorted is distributed in the hope that it will be useful, //
|
12
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
|
13
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
|
14
|
// GNU General Public License for more details. //
|
15
|
// //
|
16
|
// You should have received a copy of the GNU General Public License //
|
17
|
// along with Distorted. If not, see <http://www.gnu.org/licenses/>. //
|
18
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
19
|
|
20
|
package org.distorted.library;
|
21
|
|
22
|
import android.opengl.GLES30;
|
23
|
|
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
|
private static int sBlend;
|
39
|
private static int sBlendSrc, sBlendDst;
|
40
|
|
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
|
private int mBlend;
|
50
|
private int mBlendSrc, mBlendDst;
|
51
|
|
52
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
53
|
// default: color writes on, depth test and writes on, blending on, stencil off.
|
54
|
|
55
|
DistortedRenderState()
|
56
|
{
|
57
|
mColorMaskR = 1;
|
58
|
mColorMaskG = 1;
|
59
|
mColorMaskB = 1;
|
60
|
mColorMaskA = 1;
|
61
|
|
62
|
mDepthTest = 1;
|
63
|
mDepthMask = 1;
|
64
|
mDepthFunc = GLES30.GL_LEQUAL;
|
65
|
|
66
|
mBlend = 1;
|
67
|
mBlendSrc = GLES30.GL_SRC_ALPHA;
|
68
|
mBlendDst = GLES30.GL_ONE_MINUS_SRC_ALPHA;
|
69
|
|
70
|
mStencilTest = 0;
|
71
|
mStencilMask = 0x11111111;
|
72
|
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
|
// reset state of everything to 'unknown'
|
82
|
|
83
|
static void reset()
|
84
|
{
|
85
|
sColorMaskR = -1;
|
86
|
sColorMaskG = -1;
|
87
|
sColorMaskB = -1;
|
88
|
sColorMaskA = -1;
|
89
|
|
90
|
sDepthTest = -1;
|
91
|
sDepthMask = -1;
|
92
|
sDepthFunc = -1;
|
93
|
|
94
|
sBlend = -1;
|
95
|
sBlendSrc = -1;
|
96
|
sBlendDst = -1;
|
97
|
|
98
|
sStencilTest = -1;
|
99
|
sStencilMask = -1;
|
100
|
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
|
if( mColorMaskR!=sColorMaskR || mColorMaskG!=sColorMaskG || mColorMaskB!=sColorMaskB || mColorMaskA!=sColorMaskA)
|
113
|
{
|
114
|
sColorMaskR = mColorMaskR;
|
115
|
sColorMaskG = mColorMaskG;
|
116
|
sColorMaskB = mColorMaskB;
|
117
|
sColorMaskA = mColorMaskA;
|
118
|
GLES30.glColorMask(sColorMaskR==1,sColorMaskG==1,sColorMaskB==1,sColorMaskA==1);
|
119
|
}
|
120
|
|
121
|
if( mDepthTest!=sDepthTest )
|
122
|
{
|
123
|
sDepthTest = mDepthTest;
|
124
|
if( sDepthTest==0 ) GLES30.glDisable(GLES30.GL_DEPTH_TEST);
|
125
|
else
|
126
|
{
|
127
|
GLES30.glEnable (GLES30.GL_DEPTH_TEST);
|
128
|
|
129
|
if( mDepthMask!=sDepthMask )
|
130
|
{
|
131
|
sDepthMask = mDepthMask;
|
132
|
GLES30.glDepthMask(sDepthMask==1);
|
133
|
}
|
134
|
|
135
|
if( mDepthFunc!=sDepthFunc )
|
136
|
{
|
137
|
sDepthFunc = mDepthFunc;
|
138
|
GLES30.glDepthFunc(sDepthFunc);
|
139
|
}
|
140
|
}
|
141
|
}
|
142
|
|
143
|
if( mBlend!=sBlend )
|
144
|
{
|
145
|
sBlend = mBlend;
|
146
|
if( sBlend==0 ) GLES30.glDisable(GLES30.GL_BLEND);
|
147
|
else
|
148
|
{
|
149
|
GLES30.glEnable(GLES30.GL_BLEND);
|
150
|
|
151
|
if( mBlendSrc!=sBlendSrc || mBlendDst!=sBlendDst )
|
152
|
{
|
153
|
sBlendSrc = mBlendSrc;
|
154
|
sBlendDst = mBlendDst;
|
155
|
GLES30.glBlendFunc(sBlendSrc,sBlendDst);
|
156
|
}
|
157
|
}
|
158
|
}
|
159
|
|
160
|
if( mStencilTest!=sStencilTest )
|
161
|
{
|
162
|
sStencilTest = mStencilTest;
|
163
|
if( sStencilTest==0 ) GLES30.glDisable(GLES30.GL_STENCIL_TEST);
|
164
|
else
|
165
|
{
|
166
|
GLES30.glEnable(GLES30.GL_STENCIL_TEST);
|
167
|
|
168
|
if( mStencilMask!=sStencilMask )
|
169
|
{
|
170
|
sStencilMask = mStencilMask;
|
171
|
GLES30.glStencilMask(sStencilMask);
|
172
|
}
|
173
|
|
174
|
if( mStencilFuncFunc!=sStencilFuncFunc || mStencilFuncRef!=sStencilFuncRef || mStencilFuncMask!=sStencilFuncMask )
|
175
|
{
|
176
|
sStencilFuncFunc = mStencilFuncFunc;
|
177
|
sStencilFuncRef = mStencilFuncRef ;
|
178
|
sStencilFuncMask = mStencilFuncMask;
|
179
|
GLES30.glStencilFunc(sStencilFuncFunc,sStencilFuncRef,sStencilFuncMask);
|
180
|
}
|
181
|
|
182
|
if( mStencilOpSfail!=sStencilOpSfail || mStencilOpDpfail!=sStencilOpDpfail || mStencilOpDppass!=sStencilOpDppass )
|
183
|
{
|
184
|
sStencilOpSfail = mStencilOpSfail;
|
185
|
sStencilOpDpfail= mStencilOpDpfail;
|
186
|
sStencilOpDppass= mStencilOpDppass;
|
187
|
GLES30.glStencilOp(sStencilOpSfail,sStencilOpDpfail,sStencilOpDppass);
|
188
|
}
|
189
|
}
|
190
|
}
|
191
|
}
|
192
|
|
193
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
194
|
|
195
|
void glColorMask(boolean r, boolean g, boolean b, boolean a)
|
196
|
{
|
197
|
mColorMaskR = (r ? 1:0);
|
198
|
mColorMaskG = (g ? 1:0);
|
199
|
mColorMaskB = (b ? 1:0);
|
200
|
mColorMaskA = (a ? 1:0);
|
201
|
}
|
202
|
|
203
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
204
|
|
205
|
void glDepthMask(boolean mask)
|
206
|
{
|
207
|
mDepthMask = (mask ? 1:0);
|
208
|
}
|
209
|
|
210
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
211
|
|
212
|
void glStencilMask(int mask)
|
213
|
{
|
214
|
mStencilMask = mask;
|
215
|
}
|
216
|
|
217
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
218
|
|
219
|
void glEnable(int test)
|
220
|
{
|
221
|
if( test==GLES30.GL_DEPTH_TEST ) mDepthTest = 1;
|
222
|
else if( test==GLES30.GL_STENCIL_TEST ) mStencilTest = 1;
|
223
|
else if( test==GLES30.GL_BLEND ) mBlend = 1;
|
224
|
}
|
225
|
|
226
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
227
|
|
228
|
void glDisable(int test)
|
229
|
{
|
230
|
if( test==GLES30.GL_DEPTH_TEST ) mDepthTest = 0;
|
231
|
else if( test==GLES30.GL_STENCIL_TEST ) mStencilTest = 0;
|
232
|
else if( test==GLES30.GL_BLEND ) mBlend = 0;
|
233
|
}
|
234
|
|
235
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
236
|
|
237
|
void glStencilFunc(int func, int ref, int mask)
|
238
|
{
|
239
|
mStencilFuncFunc = func;
|
240
|
mStencilFuncRef = ref;
|
241
|
mStencilFuncMask = mask;
|
242
|
}
|
243
|
|
244
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
245
|
|
246
|
void glStencilOp(int sfail, int dpfail, int dppass)
|
247
|
{
|
248
|
mStencilOpSfail = sfail;
|
249
|
mStencilOpDpfail= dpfail;
|
250
|
mStencilOpDppass= dppass;
|
251
|
}
|
252
|
|
253
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
254
|
|
255
|
void glDepthFunc(int func)
|
256
|
{
|
257
|
mDepthFunc = func;
|
258
|
}
|
259
|
|
260
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
261
|
|
262
|
void glBlendFunc(int src, int dst)
|
263
|
{
|
264
|
mBlendSrc = src;
|
265
|
mBlendDst = dst;
|
266
|
}
|
267
|
|
268
|
}
|