Project

General

Profile

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

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

1 32d08f39 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 b0568eb5 Leszek Koltunski
import android.opengl.GLES30;
25 32d08f39 Leszek Koltunski
import android.opengl.GLSurfaceView;
26
27
import org.distorted.examples.R;
28 c6526577 Leszek Koltunski
import org.distorted.library.effect.EffectName;
29 fdddb0b2 Leszek Koltunski
import org.distorted.library.effect.FragmentEffectBrightness;
30
import org.distorted.library.effect.MatrixEffectMove;
31
import org.distorted.library.effect.MatrixEffectRotate;
32
import org.distorted.library.effect.MatrixEffectScale;
33 01782e85 Leszek Koltunski
import org.distorted.library.main.Distorted;
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.main.MeshCubes;
40
import org.distorted.library.main.MeshFlat;
41
import org.distorted.library.main.MeshObject;
42 32d08f39 Leszek Koltunski
import org.distorted.library.type.Dynamic1D;
43
import org.distorted.library.type.Static1D;
44
import org.distorted.library.type.Static3D;
45
46
import java.io.IOException;
47
import java.io.InputStream;
48
49
import javax.microedition.khronos.egl.EGLConfig;
50
import javax.microedition.khronos.opengles.GL10;
51
52
///////////////////////////////////////////////////////////////////////////////////////////////////
53
54
class StencilRenderer implements GLSurfaceView.Renderer
55
{
56
    private GLSurfaceView mView;
57
    private DistortedScreen mScreen;
58 aedd9013 leszek
    private DistortedTexture mCubeTex, mFloorTex, mFBOTex;
59
    private DistortedNode mCube1Node, mCube2Node, mFloorNode, mFBONode;
60
    private MeshObject mCubeMesh, mQuad;
61 fdddb0b2 Leszek Koltunski
    private Static3D mCubeMove, mCubeScale, mFloorScale, mFloorMove, mFBOScale;
62 aedd9013 leszek
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64
65
    void setScreen(boolean screen)
66
      {
67
      if( screen )
68
        {
69
        mScreen.detachAll();
70
        mScreen.attach(mCube1Node);
71
        mScreen.attach(mFloorNode);
72
        mScreen.attach(mCube2Node);
73
        }
74
      else
75
        {
76
        mScreen.detachAll();
77
        mScreen.attach(mFBONode);
78
        mFBONode.attach(mCube1Node);
79
        mFBONode.attach(mFloorNode);
80 559da65d Leszek Koltunski
        mFBONode.attach(mCube2Node);
81
        mFBONode.enableDepthStencil(DistortedFramebuffer.BOTH_DEPTH_STENCIL);
82 aedd9013 leszek
        }
83
      }
84 32d08f39 Leszek Koltunski
85
///////////////////////////////////////////////////////////////////////////////////////////////////
86
87
    StencilRenderer(GLSurfaceView v)
88
      {
89
      mView = v;
90
91 58e9e190 leszek
      mCubeMesh = new MeshCubes(1,1,1);
92 aedd9013 leszek
      mQuad     = new MeshFlat(1,1);
93 32d08f39 Leszek Koltunski
94 fdddb0b2 Leszek Koltunski
      mCubeMove   = new Static3D(0,0,0);
95
      mCubeScale  = new Static3D(1,1,1);
96
      mFloorScale = new Static3D(1,1,1);
97
      mFloorMove  = new Static3D(0,0,0);
98
      mFBOScale   = new Static3D(1,1,1);
99
100 32d08f39 Leszek Koltunski
      mCubeTex   = new DistortedTexture(1,1);
101
      mFloorTex  = new DistortedTexture(1,1);
102 aedd9013 leszek
      mFBOTex    = new DistortedTexture(1,1);
103 32d08f39 Leszek Koltunski
104 fdddb0b2 Leszek Koltunski
      DistortedEffects cube1Effects = new DistortedEffects();
105
      DistortedEffects cube2Effects = new DistortedEffects();
106
      DistortedEffects floorEffects = new DistortedEffects();
107
      DistortedEffects FBOEffects   = new DistortedEffects();
108 32d08f39 Leszek Koltunski
109 fdddb0b2 Leszek Koltunski
      cube2Effects.apply( new FragmentEffectBrightness(new Static1D(0.5f)) );
110 32d08f39 Leszek Koltunski
111 fdddb0b2 Leszek Koltunski
      mCube1Node = new DistortedNode(mCubeTex ,cube1Effects,mCubeMesh );
112
      mCube2Node = new DistortedNode(mCubeTex ,cube2Effects,mCubeMesh );
113
      mFloorNode = new DistortedNode(mFloorTex,floorEffects,mQuad     );
114
      mFBONode   = new DistortedNode(mFBOTex  ,FBOEffects  ,mQuad     );
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 aedd9013 leszek
      mFloorNode.glEnable(GLES30.GL_STENCIL_TEST);                               // Enable Stencil when rendering this Node
123
      mFloorNode.glStencilFunc(GLES30.GL_ALWAYS, 1, 0xFF);                       // Set any stencil to 1
124
      mFloorNode.glStencilOp(GLES30.GL_KEEP, GLES30.GL_KEEP, GLES30.GL_REPLACE); // replace with 1 when we fail Depth test
125
      mFloorNode.glStencilMask(0xFF);                                            // Write to stencil buffer
126
      mFloorNode.glDepthMask(false);                                             // Don't write to depth buffer
127 559da65d Leszek Koltunski
      mFloorNode.glClear(GLES30.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 aedd9013 leszek
      mCube2Node.glEnable(GLES30.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 aedd9013 leszek
      mCube2Node.glStencilFunc(GLES30.GL_EQUAL, 1, 0xFF);                        // Pass test if stencil value is 1
135
      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 1f9a52f1 leszek
      mView.setEGLConfigChooser(5,6,5,0,16,8);       // Screen: 16 bit depth, 8 bit STENCIL
142 aedd9013 leszek
143
      setScreen(true);
144 32d08f39 Leszek Koltunski
145
      float cw = mCubeTex.getWidth();
146
      float ch = mCubeTex.getHeight();
147
      float cd = mCubeTex.getDepth(mCubeMesh);
148
149
      float fw = mFloorTex.getWidth();
150
      float fh = mFloorTex.getHeight();
151 aedd9013 leszek
      float fd = mFloorTex.getDepth(mQuad);
152 32d08f39 Leszek Koltunski
153
      Static3D cubeCenter = new Static3D(cw/2,ch  ,-cd/2);
154
      Static3D floorCenter= new Static3D(fw/2,fh/2,-fd/2);
155
156
      Static3D axisX = new Static3D(1,0,0);
157
      Static3D axisY = new Static3D(0,1,0);
158
      Static3D axisZ = new Static3D(0,0,1);
159
160
      Dynamic1D rotDyn = new Dynamic1D(5000,0.0f);
161
      rotDyn.setMode(Dynamic1D.MODE_JUMP);
162
      rotDyn.add(new Static1D(  0));
163
      rotDyn.add(new Static1D(360));
164
165
      float angle = 30.0f;
166
167
      Static1D rotSt1 = new Static1D(angle);
168
      Static1D rotSt2 = new Static1D(angle-90.0f);
169
170 fdddb0b2 Leszek Koltunski
      MatrixEffectRotate rot1 = new MatrixEffectRotate(rotSt1, axisX, cubeCenter);
171
      MatrixEffectRotate rot2 = new MatrixEffectRotate(rotDyn, axisY, cubeCenter);
172
      MatrixEffectScale  scal = new MatrixEffectScale(mCubeScale);
173
      MatrixEffectMove   move = new MatrixEffectMove(mCubeMove);
174
175 559da65d Leszek Koltunski
      /////////////////////////////////////////////////////////////////////////////////////////////////////
176
      // Those Matrix effects, i.e. correct positioning of Objects on the Screen, is admittedly quite hard.
177
      // Figured out of experimentation.
178
      /////////////////////////////////////////////////////////////////////////////////////////////////////
179
      // Upper cube
180 fdddb0b2 Leszek Koltunski
      cube1Effects.apply( move );
181
      cube1Effects.apply( scal );
182
      cube1Effects.apply( rot1 );
183
      cube1Effects.apply( rot2 );
184 32d08f39 Leszek Koltunski
185 559da65d Leszek Koltunski
      /////////////////////////////////////////////////////////////////////////////////////////////////////
186
      // Floor
187 fdddb0b2 Leszek Koltunski
      floorEffects.apply( new MatrixEffectMove(mFloorMove));
188
      floorEffects.apply( new MatrixEffectScale(mFloorScale) );
189
      floorEffects.apply( new MatrixEffectRotate(rotSt2, axisX, floorCenter) );
190
      floorEffects.apply( new MatrixEffectRotate(rotDyn, axisZ, floorCenter) );
191 559da65d Leszek Koltunski
192
      /////////////////////////////////////////////////////////////////////////////////////////////////////
193
      // Lower cube
194 fdddb0b2 Leszek Koltunski
      cube2Effects.apply( move );
195
      cube2Effects.apply( scal );
196
      cube2Effects.apply( rot1 );
197
      cube2Effects.apply( rot2 );
198
      cube2Effects.apply( new MatrixEffectScale(new Static3D(1,-1,1)) );
199
      cube2Effects.apply( new MatrixEffectMove(new Static3D( 0, -2*ch , 0)) );
200 32d08f39 Leszek Koltunski
201 559da65d Leszek Koltunski
      /////////////////////////////////////////////////////////////////////////////////////////////////////
202
      // Framebuffer
203 fdddb0b2 Leszek Koltunski
      FBOEffects.apply( new MatrixEffectScale(mFBOScale) );
204
      }
205
206
///////////////////////////////////////////////////////////////////////////////////////////////////
207
208
    public void onDrawFrame(GL10 glUnused) 
209
      {
210
      mScreen.render(System.currentTimeMillis());
211
      }
212
213
///////////////////////////////////////////////////////////////////////////////////////////////////
214
    
215
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
216
      {
217
      float cw = mCubeTex.getWidth();
218
      float ch = mCubeTex.getHeight();
219
      float cd = mCubeTex.getDepth(mCubeMesh);
220
221
      float fw = mFloorTex.getWidth();
222
      float fh = mFloorTex.getHeight();
223
      float fd = mFloorTex.getDepth(mQuad);
224
225
      float bw = mFBOTex.getWidth();
226
      float bh = mFBOTex.getHeight();
227
228
      float cubeScale = 0.4f*(width>height ? height/ch:width/cw);
229
      float floorScale= 0.8f*(width>height ? height/fh:width/fw);
230
231
      mCubeMove.set((width-cubeScale*cw)/2 , height/2-cubeScale*ch , cubeScale *cd/2 );
232
      mCubeScale.set(cubeScale,cubeScale,cubeScale);
233
      mFloorMove.set((width-floorScale*fw)/2 ,height/2-floorScale*fh/2, floorScale*fd/2 );
234
      mFloorScale.set(floorScale,floorScale,floorScale);
235
      mFBOScale.set((float)width/bw, (float)height/bh, 1.0f );
236 aedd9013 leszek
237 559da65d Leszek Koltunski
      mFBONode.resize( (int)((float)width/bw), (int)((float)height/bh) );
238
      mScreen.resize( width,height);
239 32d08f39 Leszek Koltunski
      }
240
241
///////////////////////////////////////////////////////////////////////////////////////////////////
242
    
243
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
244
      {
245
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.cat);
246
      Bitmap bitmap;
247
248
      try
249
        {
250
        bitmap = BitmapFactory.decodeStream(is);
251
        }
252
      finally
253
        {
254
        try
255
          {
256
          is.close();
257
          }
258
        catch(IOException e) { }
259
        }
260
261
      mCubeTex.setTexture(bitmap);
262
      mFloorTex.setColor(0xff000000);  // ARGB
263
264 c6526577 Leszek Koltunski
      DistortedEffects.enableEffect(EffectName.BRIGHTNESS);
265 32d08f39 Leszek Koltunski
266
      try
267
        {
268
        Distorted.onCreate(mView.getContext());
269
        }
270
      catch(Exception ex)
271
        {
272
        android.util.Log.e("Stencil", ex.getMessage() );
273
        }
274
      }
275
}