Project

General

Profile

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

examples / src / main / java / org / distorted / examples / stencil / StencilRenderer.java @ 5a683295

1 32d08f39 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4 71c8884f Leszek Koltunski
// This file is part of Distorted.                                                               //
5 32d08f39 Leszek Koltunski
//                                                                                               //
6 71c8884f Leszek Koltunski
// Distorted is free software: you can redistribute it and/or modify                             //
7 32d08f39 Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// Distorted is distributed in the hope that it will be useful,                                  //
12 32d08f39 Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18 32d08f39 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
19
20
package org.distorted.examples.stencil;
21
22
import android.graphics.Bitmap;
23
import android.graphics.BitmapFactory;
24 41a85bb7 Leszek Koltunski
import android.opengl.GLES31;
25 32d08f39 Leszek Koltunski
import android.opengl.GLSurfaceView;
26
27
import org.distorted.examples.R;
28 fdddb0b2 Leszek Koltunski
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 e3900503 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
33 01782e85 Leszek Koltunski
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 107e4b72 Leszek Koltunski
import org.distorted.library.mesh.MeshCubes;
39 a4d59c0b Leszek Koltunski
import org.distorted.library.mesh.MeshQuad;
40 32d08f39 Leszek Koltunski
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 aedd9013 leszek
    private DistortedTexture mCubeTex, mFloorTex, mFBOTex;
57
    private DistortedNode mCube1Node, mCube2Node, mFloorNode, mFBONode;
58 16b22aab Leszek Koltunski
    private Static3D mScale, mFBOScale;
59 aedd9013 leszek
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 559da65d Leszek Koltunski
        mFBONode.attach(mCube2Node);
78
        mFBONode.enableDepthStencil(DistortedFramebuffer.BOTH_DEPTH_STENCIL);
79 aedd9013 leszek
        }
80
      }
81 32d08f39 Leszek Koltunski
82
///////////////////////////////////////////////////////////////////////////////////////////////////
83
84
    StencilRenderer(GLSurfaceView v)
85
      {
86
      mView = v;
87
88 698ad0a8 Leszek Koltunski
      MeshCubes cube   = new MeshCubes(1,1,1);
89
      MeshQuad  quaFlo = new MeshQuad();
90
      MeshQuad  quaFBO = new MeshQuad();
91 32d08f39 Leszek Koltunski
92 a4d59c0b Leszek Koltunski
      mScale      = new Static3D(1,1,1);
93 fdddb0b2 Leszek Koltunski
      mFBOScale   = new Static3D(1,1,1);
94
95 687263cc Leszek Koltunski
      mCubeTex   = new DistortedTexture();
96
      mFloorTex  = new DistortedTexture();
97
      mFBOTex    = new DistortedTexture();
98 32d08f39 Leszek Koltunski
99 698ad0a8 Leszek Koltunski
      DistortedEffects cube1Effects = new DistortedEffects();
100
      DistortedEffects cube2Effects = new DistortedEffects();
101
      DistortedEffects floorEffects = new DistortedEffects();
102
      DistortedEffects FBOEffects   = new DistortedEffects();
103 687263cc Leszek Koltunski
104 698ad0a8 Leszek Koltunski
      quaFlo.setStretch(2,2,0);
105 32d08f39 Leszek Koltunski
106 698ad0a8 Leszek Koltunski
      mCube1Node = new DistortedNode(mCubeTex , cube1Effects, cube   );
107
      mCube2Node = new DistortedNode(mCubeTex , cube2Effects, cube   );
108
      mFloorNode = new DistortedNode(mFloorTex, floorEffects, quaFlo );
109
      mFBONode   = new DistortedNode(mFBOTex  , FBOEffects  , quaFBO );
110 559da65d Leszek Koltunski
111
      ///////////////// The Meat of this App - shamelessly ripped off https://open.gl/depthstencils ///////////////////////
112
      /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
113
      // Stuff to do just before we render the upper Cube
114 acb69927 Leszek Koltunski
      // Nothing - i.e. use default OpenGL settings (one difference - in Distorted, Depth Test is on by default).
115 559da65d Leszek Koltunski
      /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
116
      // Stuff to do just before we render the Floor
117 41a85bb7 Leszek Koltunski
      mFloorNode.glEnable(GLES31.GL_STENCIL_TEST);                               // Enable Stencil when rendering this Node
118
      mFloorNode.glStencilFunc(GLES31.GL_ALWAYS, 1, 0xFF);                       // Set any stencil to 1
119
      mFloorNode.glStencilOp(GLES31.GL_KEEP, GLES31.GL_KEEP, GLES31.GL_REPLACE); // replace with 1 when we fail Depth test
120 aedd9013 leszek
      mFloorNode.glStencilMask(0xFF);                                            // Write to stencil buffer
121
      mFloorNode.glDepthMask(false);                                             // Don't write to depth buffer
122 41a85bb7 Leszek Koltunski
      mFloorNode.glClear(GLES31.GL_STENCIL_BUFFER_BIT);                          // Clear stencil buffer (by default, with a 0)
123 b0568eb5 Leszek Koltunski
124 559da65d Leszek Koltunski
      /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
125
      // Stuff to do just before we render the lower Cube
126 41a85bb7 Leszek Koltunski
      mCube2Node.glEnable(GLES31.GL_STENCIL_TEST);                               // Enable Stencil when rendering this Node
127 864e7b05 Leszek Koltunski
                                                                                 // no, this is not retained; we have to set
128
                                                                                 // this again even though Floor just set it
129 41a85bb7 Leszek Koltunski
      mCube2Node.glStencilFunc(GLES31.GL_EQUAL, 1, 0xFF);                        // Pass test if stencil value is 1
130 aedd9013 leszek
      mCube2Node.glStencilMask(0x00);                                            // Don't write anything to stencil buffer
131
      mCube2Node.glDepthMask(true);                                              // Write to depth buffer
132 559da65d Leszek Koltunski
      /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
133 32d08f39 Leszek Koltunski
134 e4330c89 Leszek Koltunski
      mScreen = new DistortedScreen();
135 32d08f39 Leszek Koltunski
      mScreen.glClearColor(1.0f,1.0f,1.0f,1.0f);
136 aedd9013 leszek
137
      setScreen(true);
138 32d08f39 Leszek Koltunski
139
      Static3D axisX = new Static3D(1,0,0);
140
      Static3D axisZ = new Static3D(0,0,1);
141
142
      Dynamic1D rotDyn = new Dynamic1D(5000,0.0f);
143
      rotDyn.setMode(Dynamic1D.MODE_JUMP);
144
      rotDyn.add(new Static1D(  0));
145
      rotDyn.add(new Static1D(360));
146
147 16b22aab Leszek Koltunski
      Static3D rotCenter  = new Static3D(0,0,0);
148
149 a4d59c0b Leszek Koltunski
      MatrixEffectScale  scale = new MatrixEffectScale(mScale);
150 0ba5de00 Leszek Koltunski
      MatrixEffectMove   move1 = new MatrixEffectMove( new Static3D( 0, 0, cube.getStretchX()/2) );
151 16b22aab Leszek Koltunski
      MatrixEffectRotate rotaX = new MatrixEffectRotate(new Static1D(-60.0f), axisX, rotCenter);
152
      MatrixEffectRotate rotaZ = new MatrixEffectRotate(rotDyn, axisZ, rotCenter);
153 fdddb0b2 Leszek Koltunski
154 559da65d Leszek Koltunski
      /////////////////////////////////////////////////////////////////////////////////////////////////////
155 a4d59c0b Leszek Koltunski
      // First move the cube and its reflection to the middle of the floor. Then scale the floor, the cube
156 0ba5de00 Leszek Koltunski
      // and its reflection. Then keep rotating all along the Z axis, tilt all.
157 559da65d Leszek Koltunski
      /////////////////////////////////////////////////////////////////////////////////////////////////////
158 a4d59c0b Leszek Koltunski
      // The cube
159
      cube1Effects.apply( move1 );
160 0ba5de00 Leszek Koltunski
      cube1Effects.apply( scale );
161
      cube1Effects.apply( rotaZ );
162
      cube1Effects.apply( rotaX );
163 559da65d Leszek Koltunski
      /////////////////////////////////////////////////////////////////////////////////////////////////////
164
      // Floor
165 a4d59c0b Leszek Koltunski
      floorEffects.apply( scale );
166 0ba5de00 Leszek Koltunski
      floorEffects.apply( rotaZ );
167
      floorEffects.apply( rotaX );
168 559da65d Leszek Koltunski
      /////////////////////////////////////////////////////////////////////////////////////////////////////
169 a4d59c0b Leszek Koltunski
      // Reflection
170 dae661e9 Leszek Koltunski
      cube2Effects.apply( move1 );
171 386fd702 Leszek Koltunski
      cube2Effects.apply( new MatrixEffectScale(new Static3D(1,1,-1)) );
172 0ba5de00 Leszek Koltunski
      cube2Effects.apply( scale );
173
      cube2Effects.apply( rotaZ );
174
      cube2Effects.apply( rotaX );
175 a4d59c0b Leszek Koltunski
      cube2Effects.apply( new FragmentEffectBrightness(new Static1D(0.5f)) );
176 32d08f39 Leszek Koltunski
177 559da65d Leszek Koltunski
      /////////////////////////////////////////////////////////////////////////////////////////////////////
178
      // Framebuffer
179 fdddb0b2 Leszek Koltunski
      FBOEffects.apply( new MatrixEffectScale(mFBOScale) );
180
      }
181
182
///////////////////////////////////////////////////////////////////////////////////////////////////
183
184
    public void onDrawFrame(GL10 glUnused) 
185
      {
186
      mScreen.render(System.currentTimeMillis());
187
      }
188
189
///////////////////////////////////////////////////////////////////////////////////////////////////
190
    
191
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
192
      {
193 698ad0a8 Leszek Koltunski
      float cw = mCube1Node.getMesh().getStretchX();
194
      float ch = mCube1Node.getMesh().getStretchY();
195 fdddb0b2 Leszek Koltunski
196 698ad0a8 Leszek Koltunski
      float bw = mFBONode.getMesh().getStretchX();
197
      float bh = mFBONode.getMesh().getStretchY();
198 fdddb0b2 Leszek Koltunski
199 a4d59c0b Leszek Koltunski
      float scale = 0.4f*(width>height ? height/ch:width/cw);
200 fdddb0b2 Leszek Koltunski
201 a4d59c0b Leszek Koltunski
      mScale.set(scale,scale,scale);
202 fdddb0b2 Leszek Koltunski
      mFBOScale.set((float)width/bw, (float)height/bh, 1.0f );
203 aedd9013 leszek
204 5a683295 Leszek Koltunski
      mFBONode.resizeFBO(width,height);
205 559da65d Leszek Koltunski
      mScreen.resize( width,height);
206 32d08f39 Leszek Koltunski
      }
207
208
///////////////////////////////////////////////////////////////////////////////////////////////////
209
    
210
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
211
      {
212
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.cat);
213
      Bitmap bitmap;
214
215
      try
216
        {
217
        bitmap = BitmapFactory.decodeStream(is);
218
        }
219
      finally
220
        {
221
        try
222
          {
223
          is.close();
224
          }
225
        catch(IOException e) { }
226
        }
227
228
      mCubeTex.setTexture(bitmap);
229
      mFloorTex.setColor(0xff000000);  // ARGB
230
231 885b9cca leszek
      FragmentEffectBrightness.enable();
232 32d08f39 Leszek Koltunski
233
      try
234
        {
235 e3900503 Leszek Koltunski
        DistortedLibrary.onCreate(mView.getContext());
236 32d08f39 Leszek Koltunski
        }
237
      catch(Exception ex)
238
        {
239
        android.util.Log.e("Stencil", ex.getMessage() );
240
        }
241
      }
242
}