Project

General

Profile

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

examples / src / main / java / org / distorted / examples / stencil / StencilRenderer.java @ 77e66c58

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 dc10a48d Leszek Koltunski
import android.app.ActivityManager;
23
import android.content.Context;
24
import android.content.pm.ConfigurationInfo;
25
import android.content.res.Resources;
26 32d08f39 Leszek Koltunski
import android.graphics.Bitmap;
27
import android.graphics.BitmapFactory;
28 41a85bb7 Leszek Koltunski
import android.opengl.GLES31;
29 32d08f39 Leszek Koltunski
import android.opengl.GLSurfaceView;
30
31
import org.distorted.examples.R;
32 fdddb0b2 Leszek Koltunski
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 bdf04acf Leszek Koltunski
import org.distorted.library.effect.VertexEffectScale;
37 e3900503 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
38 01782e85 Leszek Koltunski
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 107e4b72 Leszek Koltunski
import org.distorted.library.mesh.MeshCubes;
44 a4d59c0b Leszek Koltunski
import org.distorted.library.mesh.MeshQuad;
45 32d08f39 Leszek Koltunski
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 dc10a48d Leszek Koltunski
class StencilRenderer implements GLSurfaceView.Renderer, DistortedLibrary.LibraryUser
58 32d08f39 Leszek Koltunski
{
59 dc10a48d Leszek Koltunski
    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 aedd9013 leszek
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 559da65d Leszek Koltunski
        mFBONode.attach(mCube2Node);
84
        mFBONode.enableDepthStencil(DistortedFramebuffer.BOTH_DEPTH_STENCIL);
85 aedd9013 leszek
        }
86
      }
87 32d08f39 Leszek Koltunski
88
///////////////////////////////////////////////////////////////////////////////////////////////////
89
90
    StencilRenderer(GLSurfaceView v)
91
      {
92
      mView = v;
93 dc10a48d Leszek Koltunski
      mResources = v.getResources();
94 32d08f39 Leszek Koltunski
95 698ad0a8 Leszek Koltunski
      MeshCubes cube   = new MeshCubes(1,1,1);
96
      MeshQuad  quaFlo = new MeshQuad();
97
      MeshQuad  quaFBO = new MeshQuad();
98 32d08f39 Leszek Koltunski
99 a4d59c0b Leszek Koltunski
      mScale      = new Static3D(1,1,1);
100 fdddb0b2 Leszek Koltunski
      mFBOScale   = new Static3D(1,1,1);
101
102 687263cc Leszek Koltunski
      mCubeTex   = new DistortedTexture();
103
      mFloorTex  = new DistortedTexture();
104
      mFBOTex    = new DistortedTexture();
105 32d08f39 Leszek Koltunski
106 698ad0a8 Leszek Koltunski
      DistortedEffects cube1Effects = new DistortedEffects();
107
      DistortedEffects cube2Effects = new DistortedEffects();
108
      DistortedEffects floorEffects = new DistortedEffects();
109
      DistortedEffects FBOEffects   = new DistortedEffects();
110 687263cc Leszek Koltunski
111 698ad0a8 Leszek Koltunski
      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 559da65d Leszek Koltunski
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 acb69927 Leszek Koltunski
      // Nothing - i.e. use default OpenGL settings (one difference - in Distorted, Depth Test is on by default).
120 559da65d Leszek Koltunski
      /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
121
      // Stuff to do just before we render the Floor
122 41a85bb7 Leszek Koltunski
      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 aedd9013 leszek
      mFloorNode.glStencilMask(0xFF);                                            // Write to stencil buffer
126
      mFloorNode.glDepthMask(false);                                             // Don't write to depth buffer
127 41a85bb7 Leszek Koltunski
      mFloorNode.glClear(GLES31.GL_STENCIL_BUFFER_BIT);                          // Clear stencil buffer (by default, with a 0)
128 b0568eb5 Leszek Koltunski
129 559da65d Leszek Koltunski
      /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
130
      // Stuff to do just before we render the lower Cube
131 41a85bb7 Leszek Koltunski
      mCube2Node.glEnable(GLES31.GL_STENCIL_TEST);                               // Enable Stencil when rendering this Node
132 864e7b05 Leszek Koltunski
                                                                                 // no, this is not retained; we have to set
133
                                                                                 // this again even though Floor just set it
134 41a85bb7 Leszek Koltunski
      mCube2Node.glStencilFunc(GLES31.GL_EQUAL, 1, 0xFF);                        // Pass test if stencil value is 1
135 aedd9013 leszek
      mCube2Node.glStencilMask(0x00);                                            // Don't write anything to stencil buffer
136
      mCube2Node.glDepthMask(true);                                              // Write to depth buffer
137 559da65d Leszek Koltunski
      /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
138 32d08f39 Leszek Koltunski
139 e4330c89 Leszek Koltunski
      mScreen = new DistortedScreen();
140 32d08f39 Leszek Koltunski
      mScreen.glClearColor(1.0f,1.0f,1.0f,1.0f);
141 aedd9013 leszek
142
      setScreen(true);
143 32d08f39 Leszek Koltunski
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 16b22aab Leszek Koltunski
      Static3D rotCenter  = new Static3D(0,0,0);
153
154 bdf04acf Leszek Koltunski
      VertexEffectScale  vertexScale = new VertexEffectScale( new Static3D(2,2,0) );
155
156 a4d59c0b Leszek Koltunski
      MatrixEffectScale  scale = new MatrixEffectScale(mScale);
157 bdf04acf Leszek Koltunski
      MatrixEffectMove   move1 = new MatrixEffectMove( new Static3D( 0, 0, 0.5f) );
158 16b22aab Leszek Koltunski
      MatrixEffectRotate rotaX = new MatrixEffectRotate(new Static1D(-60.0f), axisX, rotCenter);
159
      MatrixEffectRotate rotaZ = new MatrixEffectRotate(rotDyn, axisZ, rotCenter);
160 fdddb0b2 Leszek Koltunski
161 559da65d Leszek Koltunski
      /////////////////////////////////////////////////////////////////////////////////////////////////////
162 a4d59c0b Leszek Koltunski
      // First move the cube and its reflection to the middle of the floor. Then scale the floor, the cube
163 0ba5de00 Leszek Koltunski
      // and its reflection. Then keep rotating all along the Z axis, tilt all.
164 559da65d Leszek Koltunski
      /////////////////////////////////////////////////////////////////////////////////////////////////////
165 a4d59c0b Leszek Koltunski
      // The cube
166
      cube1Effects.apply( move1 );
167 0ba5de00 Leszek Koltunski
      cube1Effects.apply( scale );
168
      cube1Effects.apply( rotaZ );
169
      cube1Effects.apply( rotaX );
170 559da65d Leszek Koltunski
      /////////////////////////////////////////////////////////////////////////////////////////////////////
171
      // Floor
172 bdf04acf Leszek Koltunski
      floorEffects.apply(vertexScale);
173 a4d59c0b Leszek Koltunski
      floorEffects.apply( scale );
174 0ba5de00 Leszek Koltunski
      floorEffects.apply( rotaZ );
175
      floorEffects.apply( rotaX );
176 559da65d Leszek Koltunski
      /////////////////////////////////////////////////////////////////////////////////////////////////////
177 a4d59c0b Leszek Koltunski
      // Reflection
178 dae661e9 Leszek Koltunski
      cube2Effects.apply( move1 );
179 386fd702 Leszek Koltunski
      cube2Effects.apply( new MatrixEffectScale(new Static3D(1,1,-1)) );
180 0ba5de00 Leszek Koltunski
      cube2Effects.apply( scale );
181
      cube2Effects.apply( rotaZ );
182
      cube2Effects.apply( rotaX );
183 a4d59c0b Leszek Koltunski
      cube2Effects.apply( new FragmentEffectBrightness(new Static1D(0.5f)) );
184 32d08f39 Leszek Koltunski
185 559da65d Leszek Koltunski
      /////////////////////////////////////////////////////////////////////////////////////////////////////
186
      // Framebuffer
187 fdddb0b2 Leszek Koltunski
      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 bdf04acf Leszek Koltunski
      float scale = 0.4f*(Math.min(width,height));
202 fdddb0b2 Leszek Koltunski
203 a4d59c0b Leszek Koltunski
      mScale.set(scale,scale,scale);
204 bdf04acf Leszek Koltunski
      mFBOScale.set(width,height,1);
205 aedd9013 leszek
206 5a683295 Leszek Koltunski
      mFBONode.resizeFBO(width,height);
207 559da65d Leszek Koltunski
      mScreen.resize( width,height);
208 32d08f39 Leszek Koltunski
      }
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 dc10a48d Leszek Koltunski
        catch(IOException ignored) { }
228 32d08f39 Leszek Koltunski
        }
229
230
      mCubeTex.setTexture(bitmap);
231 e50e7ba7 Leszek Koltunski
      mFloorTex.setColorARGB(0xff000000);
232 32d08f39 Leszek Koltunski
233 bdf04acf Leszek Koltunski
      VertexEffectScale.enable();
234 885b9cca leszek
      FragmentEffectBrightness.enable();
235 dc10a48d Leszek Koltunski
      DistortedLibrary.onSurfaceCreated(this);
236 061449ed Leszek Koltunski
      }
237 32d08f39 Leszek Koltunski
238 061449ed Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
239
240
    public void distortedException(Exception ex)
241
      {
242
      android.util.Log.e("Stencil", ex.getMessage() );
243 32d08f39 Leszek Koltunski
      }
244 dc10a48d Leszek Koltunski
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 32d08f39 Leszek Koltunski
}