Project

General

Profile

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

examples / src / main / java / org / distorted / examples / stencil / StencilRenderer.java @ 698ad0a8

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.DistortedLibrary;
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.MeshQuad;
40
import org.distorted.library.type.Dynamic1D;
41
import org.distorted.library.type.Static1D;
42
import org.distorted.library.type.Static3D;
43

    
44
import java.io.IOException;
45
import java.io.InputStream;
46

    
47
import javax.microedition.khronos.egl.EGLConfig;
48
import javax.microedition.khronos.opengles.GL10;
49

    
50
///////////////////////////////////////////////////////////////////////////////////////////////////
51

    
52
class StencilRenderer implements GLSurfaceView.Renderer
53
{
54
    private GLSurfaceView mView;
55
    private DistortedScreen mScreen;
56
    private DistortedTexture mCubeTex, mFloorTex, mFBOTex;
57
    private DistortedNode mCube1Node, mCube2Node, mFloorNode, mFBONode;
58
    private Static3D mMove, mScale, mRotCenter, mFBOScale;
59

    
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61

    
62
    void setScreen(boolean screen)
63
      {
64
      if( screen )
65
        {
66
        mScreen.detachAll();
67
        mScreen.attach(mCube1Node);
68
        mScreen.attach(mFloorNode);
69
        mScreen.attach(mCube2Node);
70
        }
71
      else
72
        {
73
        mScreen.detachAll();
74
        mScreen.attach(mFBONode);
75
        mFBONode.attach(mCube1Node);
76
        mFBONode.attach(mFloorNode);
77
        mFBONode.attach(mCube2Node);
78
        mFBONode.enableDepthStencil(DistortedFramebuffer.BOTH_DEPTH_STENCIL);
79
        }
80
      }
81

    
82
///////////////////////////////////////////////////////////////////////////////////////////////////
83

    
84
    StencilRenderer(GLSurfaceView v)
85
      {
86
      mView = v;
87

    
88
      MeshCubes cube   = new MeshCubes(1,1,1);
89
      MeshQuad  quaFlo = new MeshQuad();
90
      MeshQuad  quaFBO = new MeshQuad();
91

    
92
      mMove       = new Static3D(0,0,0);
93
      mScale      = new Static3D(1,1,1);
94
      mRotCenter  = new Static3D(0,0,0);
95
      mFBOScale   = new Static3D(1,1,1);
96

    
97
      mCubeTex   = new DistortedTexture();
98
      mFloorTex  = new DistortedTexture();
99
      mFBOTex    = new DistortedTexture();
100

    
101
      DistortedEffects cube1Effects = new DistortedEffects();
102
      DistortedEffects cube2Effects = new DistortedEffects();
103
      DistortedEffects floorEffects = new DistortedEffects();
104
      DistortedEffects FBOEffects   = new DistortedEffects();
105

    
106
      quaFlo.setStretch(2,2,0);
107

    
108
      mCube1Node = new DistortedNode(mCubeTex , cube1Effects, cube   );
109
      mCube2Node = new DistortedNode(mCubeTex , cube2Effects, cube   );
110
      mFloorNode = new DistortedNode(mFloorTex, floorEffects, quaFlo );
111
      mFBONode   = new DistortedNode(mFBOTex  , FBOEffects  , quaFBO );
112

    
113
      ///////////////// The Meat of this App - shamelessly ripped off https://open.gl/depthstencils ///////////////////////
114
      /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
115
      // Stuff to do just before we render the upper Cube
116
      // Nothing - i.e. use default OpenGL dialog_settings (one difference - in DistortedLibrary, Depth Test is on by default).
117
      /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
118
      // Stuff to do just before we render the Floor
119
      mFloorNode.glEnable(GLES31.GL_STENCIL_TEST);                               // Enable Stencil when rendering this Node
120
      mFloorNode.glStencilFunc(GLES31.GL_ALWAYS, 1, 0xFF);                       // Set any stencil to 1
121
      mFloorNode.glStencilOp(GLES31.GL_KEEP, GLES31.GL_KEEP, GLES31.GL_REPLACE); // replace with 1 when we fail Depth test
122
      mFloorNode.glStencilMask(0xFF);                                            // Write to stencil buffer
123
      mFloorNode.glDepthMask(false);                                             // Don't write to depth buffer
124
      mFloorNode.glClear(GLES31.GL_STENCIL_BUFFER_BIT);                          // Clear stencil buffer (by default, with a 0)
125

    
126
      /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
127
      // Stuff to do just before we render the lower Cube
128
      mCube2Node.glEnable(GLES31.GL_STENCIL_TEST);                               // Enable Stencil when rendering this Node
129
                                                                                 // no, this is not retained; we have to set
130
                                                                                 // this again even though Floor just set it
131
      mCube2Node.glStencilFunc(GLES31.GL_EQUAL, 1, 0xFF);                        // Pass test if stencil value is 1
132
      mCube2Node.glStencilMask(0x00);                                            // Don't write anything to stencil buffer
133
      mCube2Node.glDepthMask(true);                                              // Write to depth buffer
134
      /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
135

    
136
      mScreen = new DistortedScreen();
137
      mScreen.glClearColor(1.0f,1.0f,1.0f,1.0f);
138

    
139
      setScreen(true);
140

    
141
      float cw = cube.getStretchX();
142
      float ch = cube.getStretchY();
143

    
144
      float fw = quaFlo.getStretchX();
145
      float fh = quaFlo.getStretchY();
146

    
147
      Static3D axisX = new Static3D(1,0,0);
148
      Static3D axisZ = new Static3D(0,0,1);
149

    
150
      Dynamic1D rotDyn = new Dynamic1D(5000,0.0f);
151
      rotDyn.setMode(Dynamic1D.MODE_JUMP);
152
      rotDyn.add(new Static1D(  0));
153
      rotDyn.add(new Static1D(360));
154

    
155
      MatrixEffectScale  scale = new MatrixEffectScale(mScale);
156
      MatrixEffectMove   move1 = new MatrixEffectMove( new Static3D( (fw-cw)/2, (fh-ch)/2, 0) );
157
      MatrixEffectMove   move2 = new MatrixEffectMove( mMove );
158
      MatrixEffectRotate rotaX = new MatrixEffectRotate(new Static1D(-60.0f), axisX, mRotCenter);
159
      MatrixEffectRotate rotaZ = new MatrixEffectRotate(rotDyn, axisZ, mRotCenter);
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, and finally move all to the
164
      // middle of the screen.
165
      /////////////////////////////////////////////////////////////////////////////////////////////////////
166
      // The cube
167
      cube1Effects.apply( move1 );
168
      cube1Effects.apply( scale );
169
      cube1Effects.apply( rotaZ );
170
      cube1Effects.apply( rotaX );
171
      cube1Effects.apply( move2 );
172
      /////////////////////////////////////////////////////////////////////////////////////////////////////
173
      // Floor
174
      floorEffects.apply( scale );
175
      floorEffects.apply( rotaZ );
176
      floorEffects.apply( rotaX );
177
      floorEffects.apply( move2 );
178
      /////////////////////////////////////////////////////////////////////////////////////////////////////
179
      // Reflection
180
      cube2Effects.apply( new MatrixEffectScale(new Static3D(1,1,-1)) );
181
      cube2Effects.apply( move1 );
182
      cube2Effects.apply( scale );
183
      cube2Effects.apply( rotaZ );
184
      cube2Effects.apply( rotaX );
185
      cube2Effects.apply( move2 );
186
      cube2Effects.apply( new FragmentEffectBrightness(new Static1D(0.5f)) );
187

    
188
      /////////////////////////////////////////////////////////////////////////////////////////////////////
189
      // Framebuffer
190
      FBOEffects.apply( new MatrixEffectScale(mFBOScale) );
191
      }
192

    
193
///////////////////////////////////////////////////////////////////////////////////////////////////
194

    
195
    public void onDrawFrame(GL10 glUnused) 
196
      {
197
      mScreen.render(System.currentTimeMillis());
198
      }
199

    
200
///////////////////////////////////////////////////////////////////////////////////////////////////
201
    
202
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
203
      {
204
      float cw = mCube1Node.getMesh().getStretchX();
205
      float ch = mCube1Node.getMesh().getStretchY();
206

    
207
      float fw = mFloorNode.getMesh().getStretchX();
208
      float fh = mFloorNode.getMesh().getStretchY();
209

    
210
      float bw = mFBONode.getMesh().getStretchX();
211
      float bh = mFBONode.getMesh().getStretchY();
212

    
213
      float scale = 0.4f*(width>height ? height/ch:width/cw);
214

    
215
      mRotCenter.set(scale*fw/2,scale*fh/2,0);
216
      mScale.set(scale,scale,scale);
217
      mMove.set((width-scale*fw)/2 , (height-scale*fh)/2, 0 );
218
      mFBOScale.set((float)width/bw, (float)height/bh, 1.0f );
219

    
220
      mFBONode.resize(width,height);
221
      mScreen.resize( width,height);
222
      }
223

    
224
///////////////////////////////////////////////////////////////////////////////////////////////////
225
    
226
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
227
      {
228
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.cat);
229
      Bitmap bitmap;
230

    
231
      try
232
        {
233
        bitmap = BitmapFactory.decodeStream(is);
234
        }
235
      finally
236
        {
237
        try
238
          {
239
          is.close();
240
          }
241
        catch(IOException e) { }
242
        }
243

    
244
      mCubeTex.setTexture(bitmap);
245
      mFloorTex.setColor(0xff000000);  // ARGB
246

    
247
      FragmentEffectBrightness.enable();
248

    
249
      try
250
        {
251
        DistortedLibrary.onCreate(mView.getContext());
252
        }
253
      catch(Exception ex)
254
        {
255
        android.util.Log.e("Stencil", ex.getMessage() );
256
        }
257
      }
258
}
(2-2/3)