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.examples.stencil;
|
21
|
|
22
|
import android.graphics.Bitmap;
|
23
|
import android.graphics.BitmapFactory;
|
24
|
import android.opengl.GLES31;
|
25
|
import android.opengl.GLSurfaceView;
|
26
|
|
27
|
import org.distorted.examples.R;
|
28
|
import org.distorted.library.effect.FragmentEffectBrightness;
|
29
|
import org.distorted.library.effect.MatrixEffectMove;
|
30
|
import org.distorted.library.effect.MatrixEffectRotate;
|
31
|
import org.distorted.library.effect.MatrixEffectScale;
|
32
|
import org.distorted.library.main.Distorted;
|
33
|
import org.distorted.library.main.DistortedEffects;
|
34
|
import org.distorted.library.main.DistortedFramebuffer;
|
35
|
import org.distorted.library.main.DistortedNode;
|
36
|
import org.distorted.library.main.DistortedScreen;
|
37
|
import org.distorted.library.main.DistortedTexture;
|
38
|
import org.distorted.library.mesh.MeshCubes;
|
39
|
import org.distorted.library.mesh.MeshFlat;
|
40
|
import org.distorted.library.mesh.MeshBase;
|
41
|
import org.distorted.library.type.Dynamic1D;
|
42
|
import org.distorted.library.type.Static1D;
|
43
|
import org.distorted.library.type.Static3D;
|
44
|
|
45
|
import java.io.IOException;
|
46
|
import java.io.InputStream;
|
47
|
|
48
|
import javax.microedition.khronos.egl.EGLConfig;
|
49
|
import javax.microedition.khronos.opengles.GL10;
|
50
|
|
51
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
52
|
|
53
|
class StencilRenderer implements GLSurfaceView.Renderer
|
54
|
{
|
55
|
private GLSurfaceView mView;
|
56
|
private DistortedScreen mScreen;
|
57
|
private DistortedTexture mCubeTex, mFloorTex, mFBOTex;
|
58
|
private DistortedNode mCube1Node, mCube2Node, mFloorNode, mFBONode;
|
59
|
private MeshBase mCubeMesh, mQuad;
|
60
|
private Static3D mCubeMove, mCubeScale, mFloorScale, mFloorMove, mFBOScale;
|
61
|
|
62
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
63
|
|
64
|
void setScreen(boolean screen)
|
65
|
{
|
66
|
if( screen )
|
67
|
{
|
68
|
mScreen.detachAll();
|
69
|
mScreen.attach(mCube1Node);
|
70
|
mScreen.attach(mFloorNode);
|
71
|
mScreen.attach(mCube2Node);
|
72
|
}
|
73
|
else
|
74
|
{
|
75
|
mScreen.detachAll();
|
76
|
mScreen.attach(mFBONode);
|
77
|
mFBONode.attach(mCube1Node);
|
78
|
mFBONode.attach(mFloorNode);
|
79
|
mFBONode.attach(mCube2Node);
|
80
|
mFBONode.enableDepthStencil(DistortedFramebuffer.BOTH_DEPTH_STENCIL);
|
81
|
}
|
82
|
}
|
83
|
|
84
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
85
|
|
86
|
StencilRenderer(GLSurfaceView v)
|
87
|
{
|
88
|
mView = v;
|
89
|
|
90
|
mCubeMesh = new MeshCubes(1,1,1);
|
91
|
mQuad = new MeshFlat(1,1);
|
92
|
|
93
|
mCubeMove = new Static3D(0,0,0);
|
94
|
mCubeScale = new Static3D(1,1,1);
|
95
|
mFloorScale = new Static3D(1,1,1);
|
96
|
mFloorMove = new Static3D(0,0,0);
|
97
|
mFBOScale = new Static3D(1,1,1);
|
98
|
|
99
|
mCubeTex = new DistortedTexture(1,1);
|
100
|
mFloorTex = new DistortedTexture(1,1);
|
101
|
mFBOTex = new DistortedTexture(1,1);
|
102
|
|
103
|
DistortedEffects cube1Effects = new DistortedEffects();
|
104
|
DistortedEffects cube2Effects = new DistortedEffects();
|
105
|
DistortedEffects floorEffects = new DistortedEffects();
|
106
|
DistortedEffects FBOEffects = new DistortedEffects();
|
107
|
|
108
|
cube2Effects.apply( new FragmentEffectBrightness(new Static1D(0.5f)) );
|
109
|
|
110
|
mCube1Node = new DistortedNode(mCubeTex ,cube1Effects,mCubeMesh );
|
111
|
mCube2Node = new DistortedNode(mCubeTex ,cube2Effects,mCubeMesh );
|
112
|
mFloorNode = new DistortedNode(mFloorTex,floorEffects,mQuad );
|
113
|
mFBONode = new DistortedNode(mFBOTex ,FBOEffects ,mQuad );
|
114
|
|
115
|
///////////////// The Meat of this App - shamelessly ripped off https://open.gl/depthstencils ///////////////////////
|
116
|
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
117
|
// Stuff to do just before we render the upper Cube
|
118
|
// Nothing - i.e. use default OpenGL settings (one difference - in Distorted, Depth Test is on by default).
|
119
|
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
120
|
// Stuff to do just before we render the Floor
|
121
|
mFloorNode.glEnable(GLES31.GL_STENCIL_TEST); // Enable Stencil when rendering this Node
|
122
|
mFloorNode.glStencilFunc(GLES31.GL_ALWAYS, 1, 0xFF); // Set any stencil to 1
|
123
|
mFloorNode.glStencilOp(GLES31.GL_KEEP, GLES31.GL_KEEP, GLES31.GL_REPLACE); // replace with 1 when we fail Depth test
|
124
|
mFloorNode.glStencilMask(0xFF); // Write to stencil buffer
|
125
|
mFloorNode.glDepthMask(false); // Don't write to depth buffer
|
126
|
mFloorNode.glClear(GLES31.GL_STENCIL_BUFFER_BIT); // Clear stencil buffer (by default, with a 0)
|
127
|
|
128
|
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
129
|
// Stuff to do just before we render the lower Cube
|
130
|
mCube2Node.glEnable(GLES31.GL_STENCIL_TEST); // Enable Stencil when rendering this Node
|
131
|
// no, this is not retained; we have to set
|
132
|
// this again even though Floor just set it
|
133
|
mCube2Node.glStencilFunc(GLES31.GL_EQUAL, 1, 0xFF); // Pass test if stencil value is 1
|
134
|
mCube2Node.glStencilMask(0x00); // Don't write anything to stencil buffer
|
135
|
mCube2Node.glDepthMask(true); // Write to depth buffer
|
136
|
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
137
|
|
138
|
mScreen = new DistortedScreen();
|
139
|
mScreen.glClearColor(1.0f,1.0f,1.0f,1.0f);
|
140
|
|
141
|
setScreen(true);
|
142
|
|
143
|
float cw = mCubeTex.getWidth();
|
144
|
float ch = mCubeTex.getHeight();
|
145
|
float cd = mCubeTex.getDepth(mCubeMesh);
|
146
|
|
147
|
float fw = mFloorTex.getWidth();
|
148
|
float fh = mFloorTex.getHeight();
|
149
|
float fd = mFloorTex.getDepth(mQuad);
|
150
|
|
151
|
Static3D cubeCenter = new Static3D(cw/2,ch ,-cd/2);
|
152
|
Static3D floorCenter= new Static3D(fw/2,fh/2,-fd/2);
|
153
|
|
154
|
Static3D axisX = new Static3D(1,0,0);
|
155
|
Static3D axisY = new Static3D(0,1,0);
|
156
|
Static3D axisZ = new Static3D(0,0,1);
|
157
|
|
158
|
Dynamic1D rotDyn = new Dynamic1D(5000,0.0f);
|
159
|
rotDyn.setMode(Dynamic1D.MODE_JUMP);
|
160
|
rotDyn.add(new Static1D( 0));
|
161
|
rotDyn.add(new Static1D(360));
|
162
|
|
163
|
float angle = 30.0f;
|
164
|
|
165
|
Static1D rotSt1 = new Static1D(angle);
|
166
|
Static1D rotSt2 = new Static1D(angle-90.0f);
|
167
|
|
168
|
MatrixEffectRotate rot1 = new MatrixEffectRotate(rotSt1, axisX, cubeCenter);
|
169
|
MatrixEffectRotate rot2 = new MatrixEffectRotate(rotDyn, axisY, cubeCenter);
|
170
|
MatrixEffectScale scal = new MatrixEffectScale(mCubeScale);
|
171
|
MatrixEffectMove move = new MatrixEffectMove(mCubeMove);
|
172
|
|
173
|
/////////////////////////////////////////////////////////////////////////////////////////////////////
|
174
|
// Those Matrix effects, i.e. correct positioning of Objects on the Screen, is admittedly quite hard.
|
175
|
// Figured out of experimentation.
|
176
|
/////////////////////////////////////////////////////////////////////////////////////////////////////
|
177
|
// Upper cube
|
178
|
cube1Effects.apply( move );
|
179
|
cube1Effects.apply( scal );
|
180
|
cube1Effects.apply( rot1 );
|
181
|
cube1Effects.apply( rot2 );
|
182
|
|
183
|
/////////////////////////////////////////////////////////////////////////////////////////////////////
|
184
|
// Floor
|
185
|
floorEffects.apply( new MatrixEffectMove(mFloorMove));
|
186
|
floorEffects.apply( new MatrixEffectScale(mFloorScale) );
|
187
|
floorEffects.apply( new MatrixEffectRotate(rotSt2, axisX, floorCenter) );
|
188
|
floorEffects.apply( new MatrixEffectRotate(rotDyn, axisZ, floorCenter) );
|
189
|
|
190
|
/////////////////////////////////////////////////////////////////////////////////////////////////////
|
191
|
// Lower cube
|
192
|
cube2Effects.apply( move );
|
193
|
cube2Effects.apply( scal );
|
194
|
cube2Effects.apply( rot1 );
|
195
|
cube2Effects.apply( rot2 );
|
196
|
cube2Effects.apply( new MatrixEffectScale(new Static3D(1,-1,1)) );
|
197
|
cube2Effects.apply( new MatrixEffectMove(new Static3D( 0, -2*ch , 0)) );
|
198
|
|
199
|
/////////////////////////////////////////////////////////////////////////////////////////////////////
|
200
|
// Framebuffer
|
201
|
FBOEffects.apply( new MatrixEffectScale(mFBOScale) );
|
202
|
}
|
203
|
|
204
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
205
|
|
206
|
public void onDrawFrame(GL10 glUnused)
|
207
|
{
|
208
|
mScreen.render(System.currentTimeMillis());
|
209
|
}
|
210
|
|
211
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
212
|
|
213
|
public void onSurfaceChanged(GL10 glUnused, int width, int height)
|
214
|
{
|
215
|
float cw = mCubeTex.getWidth();
|
216
|
float ch = mCubeTex.getHeight();
|
217
|
float cd = mCubeTex.getDepth(mCubeMesh);
|
218
|
|
219
|
float fw = mFloorTex.getWidth();
|
220
|
float fh = mFloorTex.getHeight();
|
221
|
float fd = mFloorTex.getDepth(mQuad);
|
222
|
|
223
|
float bw = mFBOTex.getWidth();
|
224
|
float bh = mFBOTex.getHeight();
|
225
|
|
226
|
float cubeScale = 0.4f*(width>height ? height/ch:width/cw);
|
227
|
float floorScale= 0.8f*(width>height ? height/fh:width/fw);
|
228
|
|
229
|
mCubeMove.set((width-cubeScale*cw)/2 , height/2-cubeScale*ch , cubeScale *cd/2 );
|
230
|
mCubeScale.set(cubeScale,cubeScale,cubeScale);
|
231
|
mFloorMove.set((width-floorScale*fw)/2 ,height/2-floorScale*fh/2, floorScale*fd/2 );
|
232
|
mFloorScale.set(floorScale,floorScale,floorScale);
|
233
|
mFBOScale.set((float)width/bw, (float)height/bh, 1.0f );
|
234
|
|
235
|
mFBONode.resize( (int)((float)width/bw), (int)((float)height/bh) );
|
236
|
mScreen.resize( width,height);
|
237
|
}
|
238
|
|
239
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
240
|
|
241
|
public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
|
242
|
{
|
243
|
InputStream is = mView.getContext().getResources().openRawResource(R.raw.cat);
|
244
|
Bitmap bitmap;
|
245
|
|
246
|
try
|
247
|
{
|
248
|
bitmap = BitmapFactory.decodeStream(is);
|
249
|
}
|
250
|
finally
|
251
|
{
|
252
|
try
|
253
|
{
|
254
|
is.close();
|
255
|
}
|
256
|
catch(IOException e) { }
|
257
|
}
|
258
|
|
259
|
mCubeTex.setTexture(bitmap);
|
260
|
mFloorTex.setColor(0xff000000); // ARGB
|
261
|
|
262
|
FragmentEffectBrightness.enable();
|
263
|
|
264
|
try
|
265
|
{
|
266
|
Distorted.onCreate(mView.getContext());
|
267
|
}
|
268
|
catch(Exception ex)
|
269
|
{
|
270
|
android.util.Log.e("Stencil", ex.getMessage() );
|
271
|
}
|
272
|
}
|
273
|
}
|