Project

General

Profile

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

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

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.Distorted;
29
import org.distorted.library.DistortedEffects;
30
import org.distorted.library.DistortedFramebuffer;
31
import org.distorted.library.DistortedNode;
32
import org.distorted.library.DistortedScreen;
33
import org.distorted.library.DistortedTexture;
34
import org.distorted.library.EffectNames;
35
import org.distorted.library.EffectTypes;
36
import org.distorted.library.MeshCubes;
37
import org.distorted.library.MeshFlat;
38
import org.distorted.library.MeshObject;
39
import org.distorted.library.type.Dynamic1D;
40
import org.distorted.library.type.Static1D;
41
import org.distorted.library.type.Static3D;
42

    
43
import java.io.IOException;
44
import java.io.InputStream;
45

    
46
import javax.microedition.khronos.egl.EGLConfig;
47
import javax.microedition.khronos.opengles.GL10;
48

    
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50

    
51
class StencilRenderer implements GLSurfaceView.Renderer
52
{
53
    private GLSurfaceView mView;
54
    private DistortedScreen mScreen;
55
    private DistortedTexture mCubeTex, mFloorTex, mFBOTex;
56
    private DistortedEffects mCube1Effects, mCube2Effects, mFloorEffects, mFBOEffects;
57
    private DistortedNode mCube1Node, mCube2Node, mFloorNode, mFBONode;
58
    private MeshObject mCubeMesh, mQuad;
59

    
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
        if( mFBONode==null ) mFBONode = new DistortedNode(mFBOTex,mFBOEffects,mQuad);
74

    
75
        mScreen.detachAll();
76
        mScreen.attach(mFBONode);
77
        mFBONode.attach(mCube1Node);
78
        mFBONode.attach(mCube2Node);
79
        mFBONode.attach(mFloorNode);
80

    
81
        DistortedFramebuffer fbo = mFBONode.getFramebuffer();
82
        if( fbo!=null ) fbo.enableDepthStencil(DistortedFramebuffer.BOTH_DEPTH_STENCIL);
83
        else
84
          {
85
          android.util.Log.e("stencil", "failed to enable STENCIL!");
86
          }
87
        }
88
      }
89

    
90
///////////////////////////////////////////////////////////////////////////////////////////////////
91

    
92
    StencilRenderer(GLSurfaceView v)
93
      {
94
      mView = v;
95

    
96
      mCubeMesh = new MeshCubes(1,1,false);
97
      mQuad     = new MeshFlat(1,1);
98

    
99
      mCubeTex   = new DistortedTexture(1,1);
100
      mFloorTex  = new DistortedTexture(1,1);
101
      mFBOTex    = new DistortedTexture(1,1);
102

    
103
      mCube1Effects = new DistortedEffects();
104
      mCube2Effects = new DistortedEffects();
105
      mFloorEffects = new DistortedEffects();
106
      mFBOEffects   = new DistortedEffects();
107

    
108
      mCube2Effects.brightness(new Static1D(0.5f));
109

    
110
      mCube1Node = new DistortedNode(mCubeTex ,mCube1Effects,mCubeMesh );
111
      mCube2Node = new DistortedNode(mCubeTex ,mCube2Effects,mCubeMesh );
112
      mFloorNode = new DistortedNode(mFloorTex,mFloorEffects,mQuad     );
113

    
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 (0 by default)
120

    
121
      mCube2Node.glEnable(GLES30.GL_STENCIL_TEST);                               // Enable Stencil when rendering this Node
122
      mCube2Node.glStencilFunc(GLES30.GL_EQUAL, 1, 0xFF);                        // Pass test if stencil value is 1
123
      mCube2Node.glStencilMask(0x00);                                            // Don't write anything to stencil buffer
124
      mCube2Node.glDepthMask(true);                                              // Write to depth buffer
125

    
126
      mScreen = new DistortedScreen(mView);
127
      mScreen.glClearColor(1.0f,1.0f,1.0f,1.0f);
128
      mView.setEGLConfigChooser(5,6,5,0,16,8);       // Screen: 16 bit depth, 8 bit STENCIL
129

    
130
      setScreen(true);
131
      }
132

    
133
///////////////////////////////////////////////////////////////////////////////////////////////////
134

    
135
    public void onDrawFrame(GL10 glUnused) 
136
      {
137
      mScreen.render(System.currentTimeMillis());
138
      }
139

    
140
///////////////////////////////////////////////////////////////////////////////////////////////////
141
    
142
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
143
      {
144
      float cw = mCubeTex.getWidth();
145
      float ch = mCubeTex.getHeight();
146
      float cd = mCubeTex.getDepth(mCubeMesh);
147

    
148
      float fw = mFloorTex.getWidth();
149
      float fh = mFloorTex.getHeight();
150
      float fd = mFloorTex.getDepth(mQuad);
151

    
152
      float cubeScale = 0.4f*(width>height ? height/ch:width/cw);
153
      float floorScale= 0.8f*(width>height ? height/fh:width/fw);
154

    
155
      Static3D cubeCenter = new Static3D(cw/2,ch  ,-cd/2);
156
      Static3D floorCenter= new Static3D(fw/2,fh/2,-fd/2);
157

    
158
      Static3D axisX = new Static3D(1,0,0);
159
      Static3D axisY = new Static3D(0,1,0);
160
      Static3D axisZ = new Static3D(0,0,1);
161

    
162
      Dynamic1D rotDyn = new Dynamic1D(5000,0.0f);
163
      rotDyn.setMode(Dynamic1D.MODE_JUMP);
164
      rotDyn.add(new Static1D(  0));
165
      rotDyn.add(new Static1D(360));
166

    
167
      float angle = 30.0f;
168

    
169
      Static1D rotSt1 = new Static1D(angle);
170
      Static1D rotSt2 = new Static1D(angle-90.0f);
171

    
172
      mCube1Effects.abortEffects(EffectTypes.MATRIX);
173
      mCube1Effects.move( new Static3D( (width-cubeScale*cw)/2 , height/2-cubeScale*ch , cubeScale *cd/2) );
174
      mCube1Effects.scale(cubeScale);
175
      mCube1Effects.rotate(rotSt1, axisX, cubeCenter);
176
      mCube1Effects.rotate(rotDyn, axisY, cubeCenter);
177

    
178
      mCube2Effects.abortEffects(EffectTypes.MATRIX);
179
      mCube2Effects.move( new Static3D( (width-cubeScale*cw)/2 , height/2-cubeScale*ch , cubeScale *cd/2) );
180
      mCube2Effects.scale(cubeScale);
181
      mCube2Effects.rotate(rotSt1, axisX, cubeCenter);
182
      mCube2Effects.rotate(rotDyn, axisY, cubeCenter);
183
      mCube2Effects.scale(new Static3D(1,-1,1) );
184
      mCube2Effects.move( new Static3D( 0, -2*ch , 0) );
185

    
186
      mFloorEffects.abortEffects(EffectTypes.MATRIX);
187
      mFloorEffects.move( new Static3D( (width-floorScale*fw)/2 ,height/2-floorScale*fh/2, floorScale*fd/2) );
188
      mFloorEffects.scale(floorScale);
189
      mFloorEffects.rotate(rotSt2, axisX, floorCenter);
190
      mFloorEffects.rotate(rotDyn, axisZ, floorCenter);
191

    
192
      if( mFBONode==null ) mFBONode = new DistortedNode(mFBOTex,mFBOEffects,mQuad);
193

    
194
      DistortedFramebuffer fbo = mFBONode.getFramebuffer();
195
      if( fbo!=null ) fbo.resize(width,height);
196
      else
197
        {
198
        android.util.Log.e("stencil", "failed to resize FBO!");
199
        }
200

    
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
      DistortedEffects.enableEffect(EffectNames.BRIGHTNESS);
228

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