Project

General

Profile

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

examples / src / main / java / org / distorted / examples / multiblur / MultiblurRenderer.java @ dea953f4

1 58059374 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.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
import org.distorted.library.Distorted;
28
import org.distorted.library.DistortedEffects;
29 d218d64e leszek
import org.distorted.library.DistortedScreen;
30 58059374 Leszek Koltunski
import org.distorted.library.DistortedTexture;
31 6637d0f2 Leszek Koltunski
import org.distorted.library.EffectNames;
32 58059374 Leszek Koltunski
import org.distorted.library.EffectTypes;
33
import org.distorted.library.MeshCubes;
34 fc6f1299 Leszek Koltunski
import org.distorted.library.type.Dynamic1D;
35 3e673c74 Leszek Koltunski
import org.distorted.library.type.Dynamic3D;
36 58059374 Leszek Koltunski
import org.distorted.library.type.DynamicQuat;
37 9bea2f59 Leszek Koltunski
import org.distorted.library.type.Static1D;
38 58059374 Leszek Koltunski
import org.distorted.library.type.Static3D;
39
import org.distorted.library.type.Static4D;
40
41
import java.io.IOException;
42
import java.io.InputStream;
43
44
import javax.microedition.khronos.egl.EGLConfig;
45
import javax.microedition.khronos.opengles.GL10;
46
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48
49
class MultiblurRenderer implements GLSurfaceView.Renderer
50
{
51
    private static final int[] MOVE_VEC =
52
        {
53
        +1,+1,+1,
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
        };
62
63
    private static final int NUM_OBJECTS = MOVE_VEC.length/3;
64 e11b37f4 Leszek Koltunski
    private static final int OBJ_SIZE    = 100;
65 58059374 Leszek Koltunski
66
    private GLSurfaceView mView;
67
    private DistortedTexture mTex1, mTex2;
68
    private DistortedEffects[] mEffects;
69 3e673c74 Leszek Koltunski
    private Static3D[]  mMoveVector;
70
    private Dynamic3D[] mMoveDynamic;
71 fc6f1299 Leszek Koltunski
    private Static1D  mBlurVector;
72 febb61ba Leszek Koltunski
    private Dynamic1D  mBlurDynamic;
73 d218d64e leszek
    private DistortedScreen mScreen;
74 58059374 Leszek Koltunski
    private DynamicQuat mQuatInt1, mQuatInt2;
75 3e673c74 Leszek Koltunski
    private int mDistance;
76 46ab4363 Leszek Koltunski
    private boolean[] mBlurStatus;
77 58059374 Leszek Koltunski
78
    Static4D mQuat1, mQuat2;
79
    int mScreenMin;
80
    
81
///////////////////////////////////////////////////////////////////////////////////////////////////
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 3e673c74 Leszek Koltunski
      mMoveDynamic= new Dynamic3D[NUM_OBJECTS];
90 58059374 Leszek Koltunski
      mMoveVector = new Static3D[NUM_OBJECTS];
91 3e673c74 Leszek Koltunski
      mEffects    = new DistortedEffects[NUM_OBJECTS];
92 58059374 Leszek Koltunski
93 3e673c74 Leszek Koltunski
      for(int i=0; i<NUM_OBJECTS; i++)
94
        {
95
        mMoveVector[i]  = new Static3D(0,0,0);
96
        mEffects[i]     = new DistortedEffects();
97
        mMoveDynamic[i] = new Dynamic3D();
98
99
        mMoveDynamic[i].add(mMoveVector[i]);
100 46ab4363 Leszek Koltunski
        mBlurStatus[i] = false;
101 3e673c74 Leszek Koltunski
        }
102 58059374 Leszek Koltunski
103 febb61ba Leszek Koltunski
      mBlurDynamic= new Dynamic1D();
104 fc6f1299 Leszek Koltunski
      mBlurVector = new Static1D(10);
105 febb61ba Leszek Koltunski
      mBlurDynamic.add(mBlurVector);
106 fc6f1299 Leszek Koltunski
107 46ab4363 Leszek Koltunski
      mBlurStatus[0] = true;
108 febb61ba Leszek Koltunski
      mEffects[0].blur(mBlurDynamic);
109 9bea2f59 Leszek Koltunski
110 fe59d375 Leszek Koltunski
      MeshCubes mesh = new MeshCubes(1,1,false);
111 58059374 Leszek Koltunski
112 e11b37f4 Leszek Koltunski
      mTex1 = new DistortedTexture(OBJ_SIZE,OBJ_SIZE);
113
      mTex2 = new DistortedTexture(OBJ_SIZE,OBJ_SIZE);
114 58059374 Leszek Koltunski
115
      mQuat1 = new Static4D(0,0,0,1);  // unity
116
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
117
      
118
      mQuatInt1 = new DynamicQuat(0,0.5f);
119
      mQuatInt2 = new DynamicQuat(0,0.5f);
120
121
      mQuatInt1.add(mQuat1);
122
      mQuatInt2.add(mQuat2);
123
124 d218d64e leszek
      mScreen = new DistortedScreen();
125 fe59d375 Leszek Koltunski
126
      for(int i=0; i<NUM_OBJECTS; i++)
127
        mScreen.attach( i<NUM_OBJECTS/2 ? mTex1:mTex2, mEffects[i], mesh);
128 58059374 Leszek Koltunski
      }
129
130
///////////////////////////////////////////////////////////////////////////////////////////////////
131
   
132
    public void onDrawFrame(GL10 glUnused) 
133
      {
134 fe59d375 Leszek Koltunski
      mScreen.render( System.currentTimeMillis() );
135 58059374 Leszek Koltunski
      }
136
137
///////////////////////////////////////////////////////////////////////////////////////////////////
138
    
139
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
140
      {
141
      mScreenMin = width<height ? width:height;
142 3e673c74 Leszek Koltunski
143
    	float factor    = 0.15f*mScreenMin/OBJ_SIZE;
144 e11b37f4 Leszek Koltunski
      Static3D center = new Static3D( (float)OBJ_SIZE/2, (float)OBJ_SIZE/2, 0.0f );
145 3e673c74 Leszek Koltunski
      Static3D moveVec= new Static3D( (width -factor*OBJ_SIZE)/2 ,(height-factor*OBJ_SIZE)/2 ,0);
146 58059374 Leszek Koltunski
147
      for(int i=0; i<NUM_OBJECTS; i++)
148
        {
149
        mEffects[i].abortEffects(EffectTypes.MATRIX);
150
151 3e673c74 Leszek Koltunski
        mEffects[i].move(moveVec);
152 58059374 Leszek Koltunski
        mEffects[i].scale(factor);
153
        mEffects[i].quaternion(mQuatInt1, center);
154
        mEffects[i].quaternion(mQuatInt2, center);
155 3e673c74 Leszek Koltunski
        mEffects[i].move(mMoveDynamic[i]);
156 58059374 Leszek Koltunski
        }
157
158 3e673c74 Leszek Koltunski
      computeMoveVectors();
159
160 58059374 Leszek Koltunski
      mScreen.resize(width, height);
161
      }
162
163
///////////////////////////////////////////////////////////////////////////////////////////////////
164
    
165
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
166
      {
167
      InputStream is1 = mView.getContext().getResources().openRawResource(R.raw.cat);
168
      InputStream is2 = mView.getContext().getResources().openRawResource(R.raw.dog);
169
      Bitmap bitmap1,bitmap2;
170
        
171
      try 
172
        {
173
        bitmap1 = BitmapFactory.decodeStream(is1);
174
        bitmap2 = BitmapFactory.decodeStream(is2);
175
        }
176
      finally 
177
        {
178
        try 
179
          {
180
          is1.close();
181
          is2.close();
182
          }
183
        catch(IOException e) { }
184
        }  
185
186
      mTex1.setTexture(bitmap1);
187
      mTex2.setTexture(bitmap2);
188
189 6637d0f2 Leszek Koltunski
      DistortedEffects.enableEffect(EffectNames.BLUR);
190
191 58059374 Leszek Koltunski
      try
192
        {
193
        Distorted.onCreate(mView.getContext());
194
        }
195
      catch(Exception ex)
196
        {
197
        android.util.Log.e("Multiblur", ex.getMessage() );
198
        }
199
      }
200 3e673c74 Leszek Koltunski
201
///////////////////////////////////////////////////////////////////////////////////////////////////
202
203
    private void computeMoveVectors()
204
      {
205 dea953f4 leszek
      float size= 0.026f*OBJ_SIZE*mDistance;
206 3e673c74 Leszek Koltunski
207
      for(int i=0; i<NUM_OBJECTS; i++)
208
        {
209
        mMoveVector[i].set(size*MOVE_VEC[3*i], size*MOVE_VEC[3*i+1], size*MOVE_VEC[3*i+2]);
210
        }
211
      }
212
213
///////////////////////////////////////////////////////////////////////////////////////////////////
214
215
    void setDistance(int distance)
216
      {
217
      mDistance = distance;
218
      computeMoveVectors();
219 fc6f1299 Leszek Koltunski
220 dea953f4 leszek
      //android.util.Log.d("renderer", "distance: "+distance);
221 46ab4363 Leszek Koltunski
      }
222
223 fc6f1299 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
224
225
    void setRange(int range)
226
      {
227
      mBlurVector.set(range);
228 dea953f4 leszek
229
      //android.util.Log.d("renderer", "range: "+range);
230 fc6f1299 Leszek Koltunski
      }
231 febb61ba Leszek Koltunski
232
///////////////////////////////////////////////////////////////////////////////////////////////////
233
234 46ab4363 Leszek Koltunski
   boolean[] getChecked()
235
     {
236
     return mBlurStatus;
237
     }
238
239
///////////////////////////////////////////////////////////////////////////////////////////////////
240
241
   void setChecked(int number, boolean checked)
242 febb61ba Leszek Koltunski
     {
243
     if( number>=0 && number<=7 && mEffects!=null )
244
       {
245 dea953f4 leszek
       if( checked && !mBlurStatus[number] )
246 febb61ba Leszek Koltunski
         {
247 dea953f4 leszek
         mBlurStatus[number] = true;
248
         mEffects[number].blur(mBlurDynamic);
249 febb61ba Leszek Koltunski
         }
250 dea953f4 leszek
       if( !checked && mBlurStatus[number] )
251 febb61ba Leszek Koltunski
         {
252 dea953f4 leszek
         mBlurStatus[number] = false;
253
         mEffects[number].abortEffects(EffectNames.BLUR);
254 febb61ba Leszek Koltunski
         }
255
       }
256
     else
257
       {
258
       android.util.Log.e("renderer", "Error, number: "+number+" checked: "+checked+" mEffects="+ (mEffects==null ? "null":"not null") );
259
       }
260 dea953f4 leszek
261
     //android.util.Log.d("renderer", "setting box "+number+" BLUR state to "+checked);
262 febb61ba Leszek Koltunski
     }
263 58059374 Leszek Koltunski
}