Project

General

Profile

Download (12.3 KB) Statistics
| Branch: | Revision:

examples / src / main / java / org / distorted / examples / stencil / StencilRenderer.java @ a5f35220

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.GLES30;
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.main.MeshCubes;
39
import org.distorted.library.main.MeshFlat;
40
import org.distorted.library.main.MeshObject;
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 MeshObject 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(GLES30.GL_STENCIL_TEST);                               // Enable Stencil when rendering this Node
122
      mFloorNode.glStencilFunc(GLES30.GL_ALWAYS, 1, 0xFF);                       // Set any stencil to 1
123
      mFloorNode.glStencilOp(GLES30.GL_KEEP, GLES30.GL_KEEP, GLES30.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(GLES30.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(GLES30.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(GLES30.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
      mView.setEGLConfigChooser(5,6,5,0,16,8);       // Screen: 16 bit depth, 8 bit STENCIL
141

    
142
      setScreen(true);
143

    
144
      float cw = mCubeTex.getWidth();
145
      float ch = mCubeTex.getHeight();
146
      float cd = mCubeTex.getDepth(mCubeMesh);
147

    
148
      float fw = mFloorTex.getWidth();
149
      float fh = mFloorTex.getHeight();
150
      float fd = mFloorTex.getDepth(mQuad);
151

    
152
      Static3D cubeCenter = new Static3D(cw/2,ch  ,-cd/2);
153
      Static3D floorCenter= new Static3D(fw/2,fh/2,-fd/2);
154

    
155
      Static3D axisX = new Static3D(1,0,0);
156
      Static3D axisY = new Static3D(0,1,0);
157
      Static3D axisZ = new Static3D(0,0,1);
158

    
159
      Dynamic1D rotDyn = new Dynamic1D(5000,0.0f);
160
      rotDyn.setMode(Dynamic1D.MODE_JUMP);
161
      rotDyn.add(new Static1D(  0));
162
      rotDyn.add(new Static1D(360));
163

    
164
      float angle = 30.0f;
165

    
166
      Static1D rotSt1 = new Static1D(angle);
167
      Static1D rotSt2 = new Static1D(angle-90.0f);
168

    
169
      MatrixEffectRotate rot1 = new MatrixEffectRotate(rotSt1, axisX, cubeCenter);
170
      MatrixEffectRotate rot2 = new MatrixEffectRotate(rotDyn, axisY, cubeCenter);
171
      MatrixEffectScale  scal = new MatrixEffectScale(mCubeScale);
172
      MatrixEffectMove   move = new MatrixEffectMove(mCubeMove);
173

    
174
      /////////////////////////////////////////////////////////////////////////////////////////////////////
175
      // Those Matrix effects, i.e. correct positioning of Objects on the Screen, is admittedly quite hard.
176
      // Figured out of experimentation.
177
      /////////////////////////////////////////////////////////////////////////////////////////////////////
178
      // Upper cube
179
      cube1Effects.apply( move );
180
      cube1Effects.apply( scal );
181
      cube1Effects.apply( rot1 );
182
      cube1Effects.apply( rot2 );
183

    
184
      /////////////////////////////////////////////////////////////////////////////////////////////////////
185
      // Floor
186
      floorEffects.apply( new MatrixEffectMove(mFloorMove));
187
      floorEffects.apply( new MatrixEffectScale(mFloorScale) );
188
      floorEffects.apply( new MatrixEffectRotate(rotSt2, axisX, floorCenter) );
189
      floorEffects.apply( new MatrixEffectRotate(rotDyn, axisZ, floorCenter) );
190

    
191
      /////////////////////////////////////////////////////////////////////////////////////////////////////
192
      // Lower cube
193
      cube2Effects.apply( move );
194
      cube2Effects.apply( scal );
195
      cube2Effects.apply( rot1 );
196
      cube2Effects.apply( rot2 );
197
      cube2Effects.apply( new MatrixEffectScale(new Static3D(1,-1,1)) );
198
      cube2Effects.apply( new MatrixEffectMove(new Static3D( 0, -2*ch , 0)) );
199

    
200
      /////////////////////////////////////////////////////////////////////////////////////////////////////
201
      // Framebuffer
202
      FBOEffects.apply( new MatrixEffectScale(mFBOScale) );
203
      }
204

    
205
///////////////////////////////////////////////////////////////////////////////////////////////////
206

    
207
    public void onDrawFrame(GL10 glUnused) 
208
      {
209
      mScreen.render(System.currentTimeMillis());
210
      }
211

    
212
///////////////////////////////////////////////////////////////////////////////////////////////////
213
    
214
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
215
      {
216
      float cw = mCubeTex.getWidth();
217
      float ch = mCubeTex.getHeight();
218
      float cd = mCubeTex.getDepth(mCubeMesh);
219

    
220
      float fw = mFloorTex.getWidth();
221
      float fh = mFloorTex.getHeight();
222
      float fd = mFloorTex.getDepth(mQuad);
223

    
224
      float bw = mFBOTex.getWidth();
225
      float bh = mFBOTex.getHeight();
226

    
227
      float cubeScale = 0.4f*(width>height ? height/ch:width/cw);
228
      float floorScale= 0.8f*(width>height ? height/fh:width/fw);
229

    
230
      mCubeMove.set((width-cubeScale*cw)/2 , height/2-cubeScale*ch , cubeScale *cd/2 );
231
      mCubeScale.set(cubeScale,cubeScale,cubeScale);
232
      mFloorMove.set((width-floorScale*fw)/2 ,height/2-floorScale*fh/2, floorScale*fd/2 );
233
      mFloorScale.set(floorScale,floorScale,floorScale);
234
      mFBOScale.set((float)width/bw, (float)height/bh, 1.0f );
235

    
236
      mFBONode.resize( (int)((float)width/bw), (int)((float)height/bh) );
237
      mScreen.resize( width,height);
238
      }
239

    
240
///////////////////////////////////////////////////////////////////////////////////////////////////
241
    
242
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
243
      {
244
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.cat);
245
      Bitmap bitmap;
246

    
247
      try
248
        {
249
        bitmap = BitmapFactory.decodeStream(is);
250
        }
251
      finally
252
        {
253
        try
254
          {
255
          is.close();
256
          }
257
        catch(IOException e) { }
258
        }
259

    
260
      mCubeTex.setTexture(bitmap);
261
      mFloorTex.setColor(0xff000000);  // ARGB
262

    
263
      FragmentEffectBrightness.enable();
264

    
265
      try
266
        {
267
        Distorted.onCreate(mView.getContext());
268
        }
269
      catch(Exception ex)
270
        {
271
        android.util.Log.e("Stencil", ex.getMessage() );
272
        }
273
      }
274
}
(2-2/3)