Project

General

Profile

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

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

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.GLES30;
25
import android.opengl.GLSurfaceView;
26

    
27
import org.distorted.examples.R;
28
import org.distorted.library.effect.EffectName;
29
import org.distorted.library.main.Distorted;
30
import org.distorted.library.main.DistortedEffects;
31
import org.distorted.library.main.DistortedFramebuffer;
32
import org.distorted.library.main.DistortedNode;
33
import org.distorted.library.main.DistortedScreen;
34
import org.distorted.library.main.DistortedTexture;
35
import org.distorted.library.EffectNames;
36
import org.distorted.library.EffectTypes;
37
import org.distorted.library.main.MeshCubes;
38
import org.distorted.library.main.MeshFlat;
39
import org.distorted.library.main.MeshObject;
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 DistortedEffects mCube1Effects, mCube2Effects, mFloorEffects, mFBOEffects;
58
    private DistortedNode mCube1Node, mCube2Node, mFloorNode, mFBONode;
59
    private MeshObject mCubeMesh, mQuad;
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
      mCubeMesh = new MeshCubes(1,1,1);
90
      mQuad     = new MeshFlat(1,1);
91

    
92
      mCubeTex   = new DistortedTexture(1,1);
93
      mFloorTex  = new DistortedTexture(1,1);
94
      mFBOTex    = new DistortedTexture(1,1);
95

    
96
      mCube1Effects = new DistortedEffects();
97
      mCube2Effects = new DistortedEffects();
98
      mFloorEffects = new DistortedEffects();
99
      mFBOEffects   = new DistortedEffects();
100

    
101
      mCube2Effects.brightness(new Static1D(0.5f));
102

    
103
      mCube1Node = new DistortedNode(mCubeTex ,mCube1Effects,mCubeMesh );
104
      mCube2Node = new DistortedNode(mCubeTex ,mCube2Effects,mCubeMesh );
105
      mFloorNode = new DistortedNode(mFloorTex,mFloorEffects,mQuad     );
106
      mFBONode   = new DistortedNode(mFBOTex  ,mFBOEffects  ,mQuad     );
107

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

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

    
131
      mScreen = new DistortedScreen(mView);
132
      mScreen.glClearColor(1.0f,1.0f,1.0f,1.0f);
133
      mView.setEGLConfigChooser(5,6,5,0,16,8);       // Screen: 16 bit depth, 8 bit STENCIL
134

    
135
      setScreen(true);
136
      }
137

    
138
///////////////////////////////////////////////////////////////////////////////////////////////////
139

    
140
    public void onDrawFrame(GL10 glUnused) 
141
      {
142
      mScreen.render(System.currentTimeMillis());
143
      }
144

    
145
///////////////////////////////////////////////////////////////////////////////////////////////////
146
    
147
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
148
      {
149
      float cw = mCubeTex.getWidth();
150
      float ch = mCubeTex.getHeight();
151
      float cd = mCubeTex.getDepth(mCubeMesh);
152

    
153
      float fw = mFloorTex.getWidth();
154
      float fh = mFloorTex.getHeight();
155
      float fd = mFloorTex.getDepth(mQuad);
156

    
157
      float bw = mFBOTex.getWidth();
158
      float bh = mFBOTex.getHeight();
159

    
160
      float cubeScale = 0.4f*(width>height ? height/ch:width/cw);
161
      float floorScale= 0.8f*(width>height ? height/fh:width/fw);
162

    
163
      Static3D cubeCenter = new Static3D(cw/2,ch  ,-cd/2);
164
      Static3D floorCenter= new Static3D(fw/2,fh/2,-fd/2);
165

    
166
      Static3D axisX = new Static3D(1,0,0);
167
      Static3D axisY = new Static3D(0,1,0);
168
      Static3D axisZ = new Static3D(0,0,1);
169

    
170
      Dynamic1D rotDyn = new Dynamic1D(5000,0.0f);
171
      rotDyn.setMode(Dynamic1D.MODE_JUMP);
172
      rotDyn.add(new Static1D(  0));
173
      rotDyn.add(new Static1D(360));
174

    
175
      float angle = 30.0f;
176

    
177
      Static1D rotSt1 = new Static1D(angle);
178
      Static1D rotSt2 = new Static1D(angle-90.0f);
179

    
180
      /////////////////////////////////////////////////////////////////////////////////////////////////////
181
      // Those Matrix effects, i.e. correct positioning of Objects on the Screen, is admittedly quite hard.
182
      // Figured out of experimentation.
183
      /////////////////////////////////////////////////////////////////////////////////////////////////////
184
      // Upper cube
185
      mCube1Effects.abortEffects(EffectTypes.MATRIX);
186
      mCube1Effects.move( new Static3D( (width-cubeScale*cw)/2 , height/2-cubeScale*ch , cubeScale *cd/2) );
187
      mCube1Effects.scale(cubeScale);
188
      mCube1Effects.rotate(rotSt1, axisX, cubeCenter);
189
      mCube1Effects.rotate(rotDyn, axisY, cubeCenter);
190

    
191
      /////////////////////////////////////////////////////////////////////////////////////////////////////
192
      // Floor
193
      mFloorEffects.abortEffects(EffectTypes.MATRIX);
194
      mFloorEffects.move( new Static3D( (width-floorScale*fw)/2 ,height/2-floorScale*fh/2, floorScale*fd/2) );
195
      mFloorEffects.scale(floorScale);
196
      mFloorEffects.rotate(rotSt2, axisX, floorCenter);
197
      mFloorEffects.rotate(rotDyn, axisZ, floorCenter);
198

    
199
      /////////////////////////////////////////////////////////////////////////////////////////////////////
200
      // Lower cube
201
      mCube2Effects.abortEffects(EffectTypes.MATRIX);
202
      mCube2Effects.move( new Static3D( (width-cubeScale*cw)/2 , height/2-cubeScale*ch , cubeScale *cd/2) );
203
      mCube2Effects.scale(cubeScale);
204
      mCube2Effects.rotate(rotSt1, axisX, cubeCenter);
205
      mCube2Effects.rotate(rotDyn, axisY, cubeCenter);
206
      mCube2Effects.scale(new Static3D(1,-1,1) );
207
      mCube2Effects.move( new Static3D( 0, -2*ch , 0) );
208

    
209
      /////////////////////////////////////////////////////////////////////////////////////////////////////
210
      // Framebuffer
211
      mFBOEffects.abortEffects(EffectTypes.MATRIX);
212
      mFBOEffects.scale( new Static3D( (float)width/bw, (float)height/bh, 1.0f) );
213

    
214
      mFBONode.resize( (int)((float)width/bw), (int)((float)height/bh) );
215
      mScreen.resize( width,height);
216
      }
217

    
218
///////////////////////////////////////////////////////////////////////////////////////////////////
219
    
220
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
221
      {
222
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.cat);
223
      Bitmap bitmap;
224

    
225
      try
226
        {
227
        bitmap = BitmapFactory.decodeStream(is);
228
        }
229
      finally
230
        {
231
        try
232
          {
233
          is.close();
234
          }
235
        catch(IOException e) { }
236
        }
237

    
238
      mCubeTex.setTexture(bitmap);
239
      mFloorTex.setColor(0xff000000);  // ARGB
240

    
241
      DistortedEffects.enableEffect(EffectName.BRIGHTNESS);
242

    
243
      try
244
        {
245
        Distorted.onCreate(mView.getContext());
246
        }
247
      catch(Exception ex)
248
        {
249
        android.util.Log.e("Stencil", ex.getMessage() );
250
        }
251
      }
252
}
(2-2/3)