Project

General

Profile

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

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

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.effect.VertexEffectScale;
33
import org.distorted.library.main.DistortedLibrary;
34
import org.distorted.library.main.DistortedEffects;
35
import org.distorted.library.main.DistortedFramebuffer;
36
import org.distorted.library.main.DistortedNode;
37
import org.distorted.library.main.DistortedScreen;
38
import org.distorted.library.main.DistortedTexture;
39
import org.distorted.library.mesh.MeshCubes;
40
import org.distorted.library.mesh.MeshQuad;
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 Static3D mScale, mFBOScale;
60

    
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62

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

    
83
///////////////////////////////////////////////////////////////////////////////////////////////////
84

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

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

    
93
      mScale      = new Static3D(1,1,1);
94
      mFBOScale   = new Static3D(1,1,1);
95

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

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

    
105
      mCube1Node = new DistortedNode(mCubeTex , cube1Effects, cube   );
106
      mCube2Node = new DistortedNode(mCubeTex , cube2Effects, cube   );
107
      mFloorNode = new DistortedNode(mFloorTex, floorEffects, quaFlo );
108
      mFBONode   = new DistortedNode(mFBOTex  , FBOEffects  , quaFBO );
109

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

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

    
133
      mScreen = new DistortedScreen();
134
      mScreen.glClearColor(1.0f,1.0f,1.0f,1.0f);
135

    
136
      setScreen(true);
137

    
138
      Static3D axisX = new Static3D(1,0,0);
139
      Static3D axisZ = new Static3D(0,0,1);
140

    
141
      Dynamic1D rotDyn = new Dynamic1D(5000,0.0f);
142
      rotDyn.setMode(Dynamic1D.MODE_JUMP);
143
      rotDyn.add(new Static1D(  0));
144
      rotDyn.add(new Static1D(360));
145

    
146
      Static3D rotCenter  = new Static3D(0,0,0);
147

    
148
      VertexEffectScale  vertexScale = new VertexEffectScale( new Static3D(2,2,0) );
149

    
150
      MatrixEffectScale  scale = new MatrixEffectScale(mScale);
151
      MatrixEffectMove   move1 = new MatrixEffectMove( new Static3D( 0, 0, 0.5f) );
152
      MatrixEffectRotate rotaX = new MatrixEffectRotate(new Static1D(-60.0f), axisX, rotCenter);
153
      MatrixEffectRotate rotaZ = new MatrixEffectRotate(rotDyn, axisZ, rotCenter);
154

    
155
      /////////////////////////////////////////////////////////////////////////////////////////////////////
156
      // First move the cube and its reflection to the middle of the floor. Then scale the floor, the cube
157
      // and its reflection. Then keep rotating all along the Z axis, tilt all.
158
      /////////////////////////////////////////////////////////////////////////////////////////////////////
159
      // The cube
160
      cube1Effects.apply( move1 );
161
      cube1Effects.apply( scale );
162
      cube1Effects.apply( rotaZ );
163
      cube1Effects.apply( rotaX );
164
      /////////////////////////////////////////////////////////////////////////////////////////////////////
165
      // Floor
166
      floorEffects.apply(vertexScale);
167
      floorEffects.apply( scale );
168
      floorEffects.apply( rotaZ );
169
      floorEffects.apply( rotaX );
170
      /////////////////////////////////////////////////////////////////////////////////////////////////////
171
      // Reflection
172
      cube2Effects.apply( move1 );
173
      cube2Effects.apply( new MatrixEffectScale(new Static3D(1,1,-1)) );
174
      cube2Effects.apply( scale );
175
      cube2Effects.apply( rotaZ );
176
      cube2Effects.apply( rotaX );
177
      cube2Effects.apply( new FragmentEffectBrightness(new Static1D(0.5f)) );
178

    
179
      /////////////////////////////////////////////////////////////////////////////////////////////////////
180
      // Framebuffer
181
      FBOEffects.apply( new MatrixEffectScale(mFBOScale) );
182
      }
183

    
184
///////////////////////////////////////////////////////////////////////////////////////////////////
185

    
186
    public void onDrawFrame(GL10 glUnused) 
187
      {
188
      mScreen.render(System.currentTimeMillis());
189
      }
190

    
191
///////////////////////////////////////////////////////////////////////////////////////////////////
192
    
193
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
194
      {
195
      float scale = 0.4f*(Math.min(width,height));
196

    
197
      mScale.set(scale,scale,scale);
198
      mFBOScale.set(width,height,1);
199

    
200
      mFBONode.resizeFBO(width,height);
201
      mScreen.resize( width,height);
202
      }
203

    
204
///////////////////////////////////////////////////////////////////////////////////////////////////
205
    
206
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
207
      {
208
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.cat);
209
      Bitmap bitmap;
210

    
211
      try
212
        {
213
        bitmap = BitmapFactory.decodeStream(is);
214
        }
215
      finally
216
        {
217
        try
218
          {
219
          is.close();
220
          }
221
        catch(IOException e) { }
222
        }
223

    
224
      mCubeTex.setTexture(bitmap);
225
      mFloorTex.setColor(0xff000000);  // ARGB
226

    
227
      VertexEffectScale.enable();
228
      FragmentEffectBrightness.enable();
229

    
230
      try
231
        {
232
        DistortedLibrary.onCreate(mView.getContext());
233
        }
234
      catch(Exception ex)
235
        {
236
        android.util.Log.e("Stencil", ex.getMessage() );
237
        }
238
      }
239
}
(2-2/3)