Project

General

Profile

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

examples / src / main / java / org / distorted / examples / multiblur / MultiblurRenderer.java @ 698ad0a8

1 58059374 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4 71c8884f Leszek Koltunski
// This file is part of Distorted.                                                               //
5 58059374 Leszek Koltunski
//                                                                                               //
6 71c8884f Leszek Koltunski
// Distorted is free software: you can redistribute it and/or modify                             //
7 58059374 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 58059374 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 58059374 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
19
20
package org.distorted.examples.multiblur;
21
22
import android.graphics.Bitmap;
23
import android.graphics.BitmapFactory;
24
import android.opengl.GLSurfaceView;
25
26
import org.distorted.examples.R;
27 d38672b1 Leszek Koltunski
import org.distorted.library.effect.MatrixEffectMove;
28
import org.distorted.library.effect.MatrixEffectQuaternion;
29
import org.distorted.library.effect.MatrixEffectScale;
30
import org.distorted.library.effect.PostprocessEffectBlur;
31 e3900503 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
32 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedEffects;
33
import org.distorted.library.main.DistortedNode;
34
import org.distorted.library.main.DistortedScreen;
35
import org.distorted.library.main.DistortedTexture;
36 8843b69b leszek
import org.distorted.library.effect.EffectQuality;
37 107e4b72 Leszek Koltunski
import org.distorted.library.mesh.MeshCubes;
38 9bea2f59 Leszek Koltunski
import org.distorted.library.type.Static1D;
39 58059374 Leszek Koltunski
import org.distorted.library.type.Static3D;
40
import org.distorted.library.type.Static4D;
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 MultiblurRenderer implements GLSurfaceView.Renderer
51
{
52
    private static final int[] MOVE_VEC =
53
        {
54
        +1,+1,+1,
55
        +1,-1,+1,
56
        +1,+1,-1,
57
        +1,-1,-1,
58
        -1,+1,+1,
59
        -1,-1,+1,
60
        -1,+1,-1,
61
        -1,-1,-1
62
        };
63
64
    private static final int NUM_OBJECTS = MOVE_VEC.length/3;
65 8c1caf83 leszek
    private static final int OBJ_SIZE    = 100;
66 58059374 Leszek Koltunski
67
    private GLSurfaceView mView;
68
    private DistortedTexture mTex1, mTex2;
69 895d2f4f leszek
    private DistortedNode[] mNode;
70 3e673c74 Leszek Koltunski
    private Static3D[]  mMoveVector;
71 fc6f1299 Leszek Koltunski
    private Static1D  mBlurVector;
72 d218d64e leszek
    private DistortedScreen mScreen;
73 66d31749 Leszek Koltunski
    private PostprocessEffectBlur mBlur;
74 3e673c74 Leszek Koltunski
    private int mDistance;
75 46ab4363 Leszek Koltunski
    private boolean[] mBlurStatus;
76 d38672b1 Leszek Koltunski
    private Static3D mMove, mScale, mCenter;
77 58059374 Leszek Koltunski
78
    Static4D mQuat1, mQuat2;
79
    int mScreenMin;
80 28fe91ae leszek
81 58059374 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
82
83
    MultiblurRenderer(GLSurfaceView v)
84
      {
85
      mView = v;
86 3e673c74 Leszek Koltunski
      mDistance = -1;
87 58059374 Leszek Koltunski
88 46ab4363 Leszek Koltunski
      mBlurStatus = new boolean[NUM_OBJECTS];
89 58059374 Leszek Koltunski
      mMoveVector = new Static3D[NUM_OBJECTS];
90 895d2f4f leszek
      mNode       = new DistortedNode[NUM_OBJECTS];
91 58059374 Leszek Koltunski
92 d38672b1 Leszek Koltunski
      DistortedEffects[] effects= new DistortedEffects[NUM_OBJECTS];
93
94 3e673c74 Leszek Koltunski
      for(int i=0; i<NUM_OBJECTS; i++)
95
        {
96 d38672b1 Leszek Koltunski
        mMoveVector[i] = new Static3D(0,0,0);
97 698ad0a8 Leszek Koltunski
        effects[i]     = new DistortedEffects();
98 46ab4363 Leszek Koltunski
        mBlurStatus[i] = false;
99 3e673c74 Leszek Koltunski
        }
100 58059374 Leszek Koltunski
101 fc6f1299 Leszek Koltunski
      mBlurVector = new Static1D(10);
102 9bea2f59 Leszek Koltunski
103 58e9e190 leszek
      MeshCubes mesh = new MeshCubes(1,1,1);
104 698ad0a8 Leszek Koltunski
      mesh.setStretch(OBJ_SIZE,OBJ_SIZE,OBJ_SIZE);
105 58059374 Leszek Koltunski
106 687263cc Leszek Koltunski
      mTex1 = new DistortedTexture();
107
      mTex2 = new DistortedTexture();
108 58059374 Leszek Koltunski
109
      mQuat1 = new Static4D(0,0,0,1);  // unity
110
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
111
112 e4330c89 Leszek Koltunski
      mScreen = new DistortedScreen();
113 e72c53e5 Leszek Koltunski
      mScreen.showFPS();
114 fe59d375 Leszek Koltunski
115
      for(int i=0; i<NUM_OBJECTS; i++)
116 895d2f4f leszek
        {
117 d38672b1 Leszek Koltunski
        mNode[i] = new DistortedNode(i < NUM_OBJECTS / 2 ? mTex1 : mTex2, effects[i], mesh);
118 895d2f4f leszek
        mScreen.attach(mNode[i]);
119
        }
120
121 66d31749 Leszek Koltunski
      mBlur = new PostprocessEffectBlur(mBlurVector);
122 895d2f4f leszek
      mBlurStatus[0] = true;
123 66d31749 Leszek Koltunski
      effects[0].apply(mBlur);
124 d38672b1 Leszek Koltunski
125
      mMove   = new Static3D(0,0,0);
126
      mScale  = new Static3D(1,1,1);
127
      mCenter = new Static3D(0,0,0);
128
129
      MatrixEffectMove moveEffect = new MatrixEffectMove(mMove);
130
      MatrixEffectScale scaleEffect = new MatrixEffectScale(mScale);
131
      MatrixEffectQuaternion quatEffect1 = new MatrixEffectQuaternion(mQuat1, mCenter);
132
      MatrixEffectQuaternion quatEffect2 = new MatrixEffectQuaternion(mQuat2, mCenter);
133
134
      for(int i=0; i<NUM_OBJECTS; i++)
135
        {
136
        effects[i].apply(new MatrixEffectMove(mMoveVector[i]));
137 dae661e9 Leszek Koltunski
        effects[i].apply(quatEffect2);
138
        effects[i].apply(quatEffect1);
139
        effects[i].apply(scaleEffect);
140
        effects[i].apply(moveEffect);
141 d38672b1 Leszek Koltunski
        }
142 58059374 Leszek Koltunski
      }
143
144
///////////////////////////////////////////////////////////////////////////////////////////////////
145 bf0bc90f Leszek Koltunski
146
    void setQuality(EffectQuality quality)
147
      {
148 6155c308 Leszek Koltunski
      mBlur.setQuality(quality);
149 bf0bc90f Leszek Koltunski
      }
150
151 a935e9db Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
152
153
    void setRenderModeToOIT(boolean oit)
154
      {
155
      mScreen.setOrderIndependentTransparency(oit);
156
      }
157
158 bf0bc90f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
159
160 58059374 Leszek Koltunski
    public void onDrawFrame(GL10 glUnused) 
161
      {
162 24ab1cf8 leszek
      mScreen.render(System.currentTimeMillis());
163 58059374 Leszek Koltunski
      }
164
165
///////////////////////////////////////////////////////////////////////////////////////////////////
166
    
167
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
168
      {
169
      mScreenMin = width<height ? width:height;
170 3e673c74 Leszek Koltunski
171 8c1caf83 leszek
    	float factor    = 0.15f*mScreenMin/OBJ_SIZE;
172 d38672b1 Leszek Koltunski
    	mScale.set(factor,factor,factor);
173 b424b062 Leszek Koltunski
      mCenter.set((float)OBJ_SIZE/2, (float)OBJ_SIZE/2, (float)OBJ_SIZE/2 );
174 a4d59c0b Leszek Koltunski
      mMove.set( (width -factor*OBJ_SIZE)/2 ,(height-factor*OBJ_SIZE)/2 , -factor*OBJ_SIZE);
175 3e673c74 Leszek Koltunski
      computeMoveVectors();
176 58059374 Leszek Koltunski
      mScreen.resize(width, height);
177
      }
178
179
///////////////////////////////////////////////////////////////////////////////////////////////////
180
    
181
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
182
      {
183
      InputStream is1 = mView.getContext().getResources().openRawResource(R.raw.cat);
184
      InputStream is2 = mView.getContext().getResources().openRawResource(R.raw.dog);
185
      Bitmap bitmap1,bitmap2;
186
        
187
      try 
188
        {
189
        bitmap1 = BitmapFactory.decodeStream(is1);
190
        bitmap2 = BitmapFactory.decodeStream(is2);
191
        }
192
      finally 
193
        {
194
        try 
195
          {
196
          is1.close();
197
          is2.close();
198
          }
199
        catch(IOException e) { }
200
        }  
201
202
      mTex1.setTexture(bitmap1);
203
      mTex2.setTexture(bitmap2);
204
205 885b9cca leszek
      PostprocessEffectBlur.enable();
206 6637d0f2 Leszek Koltunski
207 58059374 Leszek Koltunski
      try
208
        {
209 e3900503 Leszek Koltunski
        DistortedLibrary.onCreate(mView.getContext());
210 58059374 Leszek Koltunski
        }
211
      catch(Exception ex)
212
        {
213
        android.util.Log.e("Multiblur", ex.getMessage() );
214
        }
215
      }
216 3e673c74 Leszek Koltunski
217
///////////////////////////////////////////////////////////////////////////////////////////////////
218
219
    private void computeMoveVectors()
220
      {
221 8c1caf83 leszek
      float size= 0.026f*OBJ_SIZE*mDistance;
222 3e673c74 Leszek Koltunski
223
      for(int i=0; i<NUM_OBJECTS; i++)
224
        {
225 8c1caf83 leszek
        mMoveVector[i].set(size*MOVE_VEC[3*i], size*MOVE_VEC[3*i+1], size*MOVE_VEC[3*i+2]);
226 3e673c74 Leszek Koltunski
        }
227
      }
228
229
///////////////////////////////////////////////////////////////////////////////////////////////////
230
231
    void setDistance(int distance)
232
      {
233
      mDistance = distance;
234
      computeMoveVectors();
235 46ab4363 Leszek Koltunski
      }
236
237 fc6f1299 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
238
239
    void setRange(int range)
240
      {
241 86e9b10a Leszek Koltunski
      mBlurVector.set(range/2);
242 fc6f1299 Leszek Koltunski
      }
243 febb61ba Leszek Koltunski
244
///////////////////////////////////////////////////////////////////////////////////////////////////
245
246 66d31749 Leszek Koltunski
    boolean[] getChecked()
247 46ab4363 Leszek Koltunski
     {
248
     return mBlurStatus;
249
     }
250
251
///////////////////////////////////////////////////////////////////////////////////////////////////
252
253 66d31749 Leszek Koltunski
    void setChecked(int number, boolean checked)
254
      {
255 d4a46952 leszek
      if( number>=0 && number<NUM_OBJECTS )
256 66d31749 Leszek Koltunski
        {
257
        if( checked && !mBlurStatus[number] )
258
          {
259
          mBlurStatus[number] = true;
260
          mNode[number].getEffects().apply(mBlur);
261
          }
262
        if( !checked && mBlurStatus[number] )
263
          {
264
          mBlurStatus[number] = false;
265
          mNode[number].getEffects().abortEffect(mBlur);
266
          }
267
        }
268
      else
269
        {
270
        android.util.Log.e("renderer", "Error, number: "+number+" checked: "+checked );
271
        }
272
273
      //android.util.Log.d("renderer", "setting box "+number+" BLUR state to "+checked);
274
      }
275 58059374 Leszek Koltunski
}