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.app.ActivityManager;
|
23
|
import android.content.Context;
|
24
|
import android.content.pm.ConfigurationInfo;
|
25
|
import android.content.res.Resources;
|
26
|
import android.graphics.Bitmap;
|
27
|
import android.graphics.BitmapFactory;
|
28
|
import android.opengl.GLES31;
|
29
|
import android.opengl.GLSurfaceView;
|
30
|
|
31
|
import org.distorted.examples.R;
|
32
|
import org.distorted.library.effect.FragmentEffectBrightness;
|
33
|
import org.distorted.library.effect.MatrixEffectMove;
|
34
|
import org.distorted.library.effect.MatrixEffectRotate;
|
35
|
import org.distorted.library.effect.MatrixEffectScale;
|
36
|
import org.distorted.library.effect.VertexEffectScale;
|
37
|
import org.distorted.library.main.DistortedLibrary;
|
38
|
import org.distorted.library.main.DistortedEffects;
|
39
|
import org.distorted.library.main.DistortedFramebuffer;
|
40
|
import org.distorted.library.main.DistortedNode;
|
41
|
import org.distorted.library.main.DistortedScreen;
|
42
|
import org.distorted.library.main.DistortedTexture;
|
43
|
import org.distorted.library.mesh.MeshCubes;
|
44
|
import org.distorted.library.mesh.MeshQuad;
|
45
|
import org.distorted.library.type.Dynamic1D;
|
46
|
import org.distorted.library.type.Static1D;
|
47
|
import org.distorted.library.type.Static3D;
|
48
|
|
49
|
import java.io.IOException;
|
50
|
import java.io.InputStream;
|
51
|
|
52
|
import javax.microedition.khronos.egl.EGLConfig;
|
53
|
import javax.microedition.khronos.opengles.GL10;
|
54
|
|
55
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
56
|
|
57
|
class StencilRenderer implements GLSurfaceView.Renderer, DistortedLibrary.LibraryUser
|
58
|
{
|
59
|
private final GLSurfaceView mView;
|
60
|
private final Resources mResources;
|
61
|
private final DistortedScreen mScreen;
|
62
|
private final DistortedTexture mCubeTex, mFloorTex, mFBOTex;
|
63
|
private final DistortedNode mCube1Node, mCube2Node, mFloorNode, mFBONode;
|
64
|
private final Static3D mScale, mFBOScale;
|
65
|
|
66
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
67
|
|
68
|
void setScreen(boolean screen)
|
69
|
{
|
70
|
if( screen )
|
71
|
{
|
72
|
mScreen.detachAll();
|
73
|
mScreen.attach(mCube1Node);
|
74
|
mScreen.attach(mFloorNode);
|
75
|
mScreen.attach(mCube2Node);
|
76
|
}
|
77
|
else
|
78
|
{
|
79
|
mScreen.detachAll();
|
80
|
mScreen.attach(mFBONode);
|
81
|
mFBONode.attach(mCube1Node);
|
82
|
mFBONode.attach(mFloorNode);
|
83
|
mFBONode.attach(mCube2Node);
|
84
|
mFBONode.enableDepthStencil(DistortedFramebuffer.BOTH_DEPTH_STENCIL);
|
85
|
}
|
86
|
}
|
87
|
|
88
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
89
|
|
90
|
StencilRenderer(GLSurfaceView v)
|
91
|
{
|
92
|
mView = v;
|
93
|
mResources = v.getResources();
|
94
|
|
95
|
MeshCubes cube = new MeshCubes(1,1,1);
|
96
|
MeshQuad quaFlo = new MeshQuad();
|
97
|
MeshQuad quaFBO = new MeshQuad();
|
98
|
|
99
|
mScale = new Static3D(1,1,1);
|
100
|
mFBOScale = new Static3D(1,1,1);
|
101
|
|
102
|
mCubeTex = new DistortedTexture();
|
103
|
mFloorTex = new DistortedTexture();
|
104
|
mFBOTex = new DistortedTexture();
|
105
|
|
106
|
DistortedEffects cube1Effects = new DistortedEffects();
|
107
|
DistortedEffects cube2Effects = new DistortedEffects();
|
108
|
DistortedEffects floorEffects = new DistortedEffects();
|
109
|
DistortedEffects FBOEffects = new DistortedEffects();
|
110
|
|
111
|
mCube1Node = new DistortedNode(mCubeTex , cube1Effects, cube );
|
112
|
mCube2Node = new DistortedNode(mCubeTex , cube2Effects, cube );
|
113
|
mFloorNode = new DistortedNode(mFloorTex, floorEffects, quaFlo );
|
114
|
mFBONode = new DistortedNode(mFBOTex , FBOEffects , quaFBO );
|
115
|
|
116
|
///////////////// The Meat of this App - shamelessly ripped off https://open.gl/depthstencils ///////////////////////
|
117
|
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
118
|
// Stuff to do just before we render the upper Cube
|
119
|
// Nothing - i.e. use default OpenGL settings (one difference - in Distorted, Depth Test is on by default).
|
120
|
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
121
|
// Stuff to do just before we render the Floor
|
122
|
mFloorNode.glEnable(GLES31.GL_STENCIL_TEST); // Enable Stencil when rendering this Node
|
123
|
mFloorNode.glStencilFunc(GLES31.GL_ALWAYS, 1, 0xFF); // Set any stencil to 1
|
124
|
mFloorNode.glStencilOp(GLES31.GL_KEEP, GLES31.GL_KEEP, GLES31.GL_REPLACE); // replace with 1 when we fail Depth test
|
125
|
mFloorNode.glStencilMask(0xFF); // Write to stencil buffer
|
126
|
mFloorNode.glDepthMask(false); // Don't write to depth buffer
|
127
|
mFloorNode.glClear(GLES31.GL_STENCIL_BUFFER_BIT); // Clear stencil buffer (by default, with a 0)
|
128
|
|
129
|
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
130
|
// Stuff to do just before we render the lower Cube
|
131
|
mCube2Node.glEnable(GLES31.GL_STENCIL_TEST); // Enable Stencil when rendering this Node
|
132
|
// no, this is not retained; we have to set
|
133
|
// this again even though Floor just set it
|
134
|
mCube2Node.glStencilFunc(GLES31.GL_EQUAL, 1, 0xFF); // Pass test if stencil value is 1
|
135
|
mCube2Node.glStencilMask(0x00); // Don't write anything to stencil buffer
|
136
|
mCube2Node.glDepthMask(true); // Write to depth buffer
|
137
|
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
138
|
|
139
|
mScreen = new DistortedScreen();
|
140
|
mScreen.glClearColor(1.0f,1.0f,1.0f,1.0f);
|
141
|
|
142
|
setScreen(true);
|
143
|
|
144
|
Static3D axisX = new Static3D(1,0,0);
|
145
|
Static3D axisZ = new Static3D(0,0,1);
|
146
|
|
147
|
Dynamic1D rotDyn = new Dynamic1D(5000,0.0f);
|
148
|
rotDyn.setMode(Dynamic1D.MODE_JUMP);
|
149
|
rotDyn.add(new Static1D( 0));
|
150
|
rotDyn.add(new Static1D(360));
|
151
|
|
152
|
Static3D rotCenter = new Static3D(0,0,0);
|
153
|
|
154
|
VertexEffectScale vertexScale = new VertexEffectScale( new Static3D(2,2,0) );
|
155
|
|
156
|
MatrixEffectScale scale = new MatrixEffectScale(mScale);
|
157
|
MatrixEffectMove move1 = new MatrixEffectMove( new Static3D( 0, 0, 0.5f) );
|
158
|
MatrixEffectRotate rotaX = new MatrixEffectRotate(new Static1D(-60.0f), axisX, rotCenter);
|
159
|
MatrixEffectRotate rotaZ = new MatrixEffectRotate(rotDyn, axisZ, rotCenter);
|
160
|
|
161
|
/////////////////////////////////////////////////////////////////////////////////////////////////////
|
162
|
// First move the cube and its reflection to the middle of the floor. Then scale the floor, the cube
|
163
|
// and its reflection. Then keep rotating all along the Z axis, tilt all.
|
164
|
/////////////////////////////////////////////////////////////////////////////////////////////////////
|
165
|
// The cube
|
166
|
cube1Effects.apply( move1 );
|
167
|
cube1Effects.apply( scale );
|
168
|
cube1Effects.apply( rotaZ );
|
169
|
cube1Effects.apply( rotaX );
|
170
|
/////////////////////////////////////////////////////////////////////////////////////////////////////
|
171
|
// Floor
|
172
|
floorEffects.apply(vertexScale);
|
173
|
floorEffects.apply( scale );
|
174
|
floorEffects.apply( rotaZ );
|
175
|
floorEffects.apply( rotaX );
|
176
|
/////////////////////////////////////////////////////////////////////////////////////////////////////
|
177
|
// Reflection
|
178
|
cube2Effects.apply( move1 );
|
179
|
cube2Effects.apply( new MatrixEffectScale(new Static3D(1,1,-1)) );
|
180
|
cube2Effects.apply( scale );
|
181
|
cube2Effects.apply( rotaZ );
|
182
|
cube2Effects.apply( rotaX );
|
183
|
cube2Effects.apply( new FragmentEffectBrightness(new Static1D(0.5f)) );
|
184
|
|
185
|
/////////////////////////////////////////////////////////////////////////////////////////////////////
|
186
|
// Framebuffer
|
187
|
FBOEffects.apply( new MatrixEffectScale(mFBOScale) );
|
188
|
}
|
189
|
|
190
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
191
|
|
192
|
public void onDrawFrame(GL10 glUnused)
|
193
|
{
|
194
|
mScreen.render(System.currentTimeMillis());
|
195
|
}
|
196
|
|
197
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
198
|
|
199
|
public void onSurfaceChanged(GL10 glUnused, int width, int height)
|
200
|
{
|
201
|
float scale = 0.4f*(Math.min(width,height));
|
202
|
|
203
|
mScale.set(scale,scale,scale);
|
204
|
mFBOScale.set(width,height,1);
|
205
|
|
206
|
mFBONode.resizeFBO(width,height);
|
207
|
mScreen.resize( width,height);
|
208
|
}
|
209
|
|
210
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
211
|
|
212
|
public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
|
213
|
{
|
214
|
InputStream is = mView.getContext().getResources().openRawResource(R.raw.cat);
|
215
|
Bitmap bitmap;
|
216
|
|
217
|
try
|
218
|
{
|
219
|
bitmap = BitmapFactory.decodeStream(is);
|
220
|
}
|
221
|
finally
|
222
|
{
|
223
|
try
|
224
|
{
|
225
|
is.close();
|
226
|
}
|
227
|
catch(IOException ignored) { }
|
228
|
}
|
229
|
|
230
|
mCubeTex.setTexture(bitmap);
|
231
|
mFloorTex.setColorARGB(0xff000000);
|
232
|
|
233
|
VertexEffectScale.enable();
|
234
|
FragmentEffectBrightness.enable();
|
235
|
DistortedLibrary.onSurfaceCreated(this);
|
236
|
}
|
237
|
|
238
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
239
|
|
240
|
public void distortedException(Exception ex)
|
241
|
{
|
242
|
android.util.Log.e("Stencil", ex.getMessage() );
|
243
|
}
|
244
|
|
245
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
246
|
|
247
|
public InputStream localFile(int fileID)
|
248
|
{
|
249
|
return mResources.openRawResource(fileID);
|
250
|
}
|
251
|
|
252
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
253
|
|
254
|
public void logMessage(String message)
|
255
|
{
|
256
|
android.util.Log.e("Stencil", message );
|
257
|
}
|
258
|
}
|