Project

General

Profile

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

examples / src / main / java / org / distorted / examples / stencil / StencilRenderer.java @ 1f9a52f1

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

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

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

    
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49

    
50
class StencilRenderer implements GLSurfaceView.Renderer
51
{
52
    private GLSurfaceView mView;
53
    private DistortedScreen mScreen;
54
    private DistortedTexture mCubeTex, mFloorTex;
55
    private DistortedEffects mCube1Effects, mCube2Effects, mFloorEffects;
56
    private MeshObject mCubeMesh, mFloorMesh;
57

    
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59

    
60
    StencilRenderer(GLSurfaceView v)
61
      {
62
      mView = v;
63

    
64
      mCubeMesh  = new MeshCubes(1,1,false);
65
      mFloorMesh = new MeshFlat(1,1);
66

    
67
      mCubeTex   = new DistortedTexture(1,1);
68
      mFloorTex  = new DistortedTexture(1,1);
69

    
70
      mCube1Effects = new DistortedEffects();
71
      mCube2Effects = new DistortedEffects();
72
      mFloorEffects = new DistortedEffects();
73

    
74
      mCube2Effects.brightness(new Static1D(0.5f));
75

    
76
      DistortedNode cube1Node = new DistortedNode(mCubeTex ,mCube1Effects,mCubeMesh );
77
      DistortedNode cube2Node = new DistortedNode(mCubeTex ,mCube2Effects,mCubeMesh );
78
      DistortedNode floorNode = new DistortedNode(mFloorTex,mFloorEffects,mFloorMesh);
79

    
80
      floorNode.glEnable(GLES30.GL_STENCIL_TEST);                               // Enable Stencil when rendering this Node
81
      floorNode.glStencilFunc(GLES30.GL_ALWAYS, 1, 0xFF);                       // Set any stencil to 1
82
      floorNode.glStencilOp(GLES30.GL_KEEP, GLES30.GL_KEEP, GLES30.GL_REPLACE); // replace with 1 when we fail Depth test
83
      floorNode.glStencilMask(0xFF);                                            // Write to stencil buffer
84
      floorNode.glDepthMask(false);                                             // Don't write to depth buffer
85
      floorNode.glClear(GLES30.GL_STENCIL_BUFFER_BIT);                          // Clear stencil buffer (0 by default)
86

    
87
      cube2Node.glEnable(GLES30.GL_STENCIL_TEST);                               // Enable Stencil when rendering this Node
88
      cube2Node.glStencilFunc(GLES30.GL_EQUAL, 1, 0xFF);                        // Pass test if stencil value is 1
89
      cube2Node.glStencilMask(0x00);                                            // Don't write anything to stencil buffer
90
      cube2Node.glDepthMask(true);                                              // Write to depth buffer
91

    
92
      mScreen = new DistortedScreen(mView);
93
      mScreen.attach(cube1Node);
94
      mScreen.attach(floorNode);
95
      mScreen.attach(cube2Node);
96
      mScreen.glClearColor(1.0f,1.0f,1.0f,1.0f);
97

    
98
      mView.setEGLConfigChooser(5,6,5,0,16,8);       // Screen: 16 bit depth, 8 bit STENCIL
99
      }
100

    
101
///////////////////////////////////////////////////////////////////////////////////////////////////
102
   
103
    public void onDrawFrame(GL10 glUnused) 
104
      {
105
      mScreen.render(System.currentTimeMillis());
106
      }
107

    
108
///////////////////////////////////////////////////////////////////////////////////////////////////
109
    
110
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
111
      {
112
      float cw = mCubeTex.getWidth();
113
      float ch = mCubeTex.getHeight();
114
      float cd = mCubeTex.getDepth(mCubeMesh);
115

    
116
      float fw = mFloorTex.getWidth();
117
      float fh = mFloorTex.getHeight();
118
      float fd = mFloorTex.getDepth(mFloorMesh);
119

    
120
      float cubeScale = 0.4f*(width>height ? height/ch:width/cw);
121
      float floorScale= 0.8f*(width>height ? height/fh:width/fw);
122

    
123
      Static3D cubeCenter = new Static3D(cw/2,ch  ,-cd/2);
124
      Static3D floorCenter= new Static3D(fw/2,fh/2,-fd/2);
125

    
126
      Static3D axisX = new Static3D(1,0,0);
127
      Static3D axisY = new Static3D(0,1,0);
128
      Static3D axisZ = new Static3D(0,0,1);
129

    
130
      Dynamic1D rotDyn = new Dynamic1D(5000,0.0f);
131
      rotDyn.setMode(Dynamic1D.MODE_JUMP);
132
      rotDyn.add(new Static1D(  0));
133
      rotDyn.add(new Static1D(360));
134

    
135
      float angle = 30.0f;
136

    
137
      Static1D rotSt1 = new Static1D(angle);
138
      Static1D rotSt2 = new Static1D(angle-90.0f);
139

    
140
      mCube1Effects.abortEffects(EffectTypes.MATRIX);
141
      mCube1Effects.move( new Static3D( (width-cubeScale*cw)/2 , height/2-cubeScale*ch , cubeScale *cd/2) );
142
      mCube1Effects.scale(cubeScale);
143
      mCube1Effects.rotate(rotSt1, axisX, cubeCenter);
144
      mCube1Effects.rotate(rotDyn, axisY, cubeCenter);
145

    
146
      mCube2Effects.abortEffects(EffectTypes.MATRIX);
147
      mCube2Effects.move( new Static3D( (width-cubeScale*cw)/2 , height/2-cubeScale*ch , cubeScale *cd/2) );
148
      mCube2Effects.scale(cubeScale);
149
      mCube2Effects.rotate(rotSt1, axisX, cubeCenter);
150
      mCube2Effects.rotate(rotDyn, axisY, cubeCenter);
151
      mCube2Effects.scale(new Static3D(1,-1,1) );
152
      mCube2Effects.move( new Static3D( 0, -2*ch , 0) );
153

    
154
      mFloorEffects.abortEffects(EffectTypes.MATRIX);
155
      mFloorEffects.move( new Static3D( (width-floorScale*fw)/2 ,height/2-floorScale*fh/2, floorScale*fd/2) );
156
      mFloorEffects.scale(floorScale);
157
      mFloorEffects.rotate(rotSt2, axisX, floorCenter);
158
      mFloorEffects.rotate(rotDyn, axisZ, floorCenter);
159

    
160
      mScreen.resize(width, height);
161
      }
162

    
163
///////////////////////////////////////////////////////////////////////////////////////////////////
164
    
165
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
166
      {
167
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.cat);
168
      Bitmap bitmap;
169

    
170
      try
171
        {
172
        bitmap = BitmapFactory.decodeStream(is);
173
        }
174
      finally
175
        {
176
        try
177
          {
178
          is.close();
179
          }
180
        catch(IOException e) { }
181
        }
182

    
183
      mCubeTex.setTexture(bitmap);
184
      mFloorTex.setColor(0xff000000);  // ARGB
185

    
186
      DistortedEffects.enableEffect(EffectNames.BRIGHTNESS);
187

    
188
      try
189
        {
190
        Distorted.onCreate(mView.getContext());
191
        }
192
      catch(Exception ex)
193
        {
194
        android.util.Log.e("Stencil", ex.getMessage() );
195
        }
196
      }
197
}
(2-2/3)