Project

General

Profile

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

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

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.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
import org.distorted.library.DistortedScreen;
30
import org.distorted.library.DistortedTexture;
31
import org.distorted.library.EffectNames;
32
import org.distorted.library.EffectTypes;
33
import org.distorted.library.MeshCubes;
34
import org.distorted.library.type.Dynamic1D;
35
import org.distorted.library.type.Dynamic3D;
36
import org.distorted.library.type.DynamicQuat;
37
import org.distorted.library.type.Static1D;
38
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
    private static final int OBJ_SIZE    = 100;
65

    
66
    private GLSurfaceView mView;
67
    private DistortedTexture mTex1, mTex2;
68
    private DistortedEffects[] mEffects;
69
    private Static3D[]  mMoveVector;
70
    private Dynamic3D[] mMoveDynamic;
71
    private Static1D  mBlurVector;
72
    private Dynamic1D  mBlurDynamic;
73
    private DistortedScreen mScreen;
74
    private DynamicQuat mQuatInt1, mQuatInt2;
75
    private int mDistance;
76
    private boolean[] mBlurStatus;
77

    
78
    Static4D mQuat1, mQuat2;
79
    int mScreenMin;
80
    
81
///////////////////////////////////////////////////////////////////////////////////////////////////
82

    
83
    MultiblurRenderer(GLSurfaceView v)
84
      {
85
      mView = v;
86
      mDistance = -1;
87

    
88
      mBlurStatus = new boolean[NUM_OBJECTS];
89
      mMoveDynamic= new Dynamic3D[NUM_OBJECTS];
90
      mMoveVector = new Static3D[NUM_OBJECTS];
91
      mEffects    = new DistortedEffects[NUM_OBJECTS];
92

    
93
      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
        mBlurStatus[i] = false;
101
        }
102

    
103
      mBlurDynamic= new Dynamic1D();
104
      mBlurVector = new Static1D(10);
105
      mBlurDynamic.add(mBlurVector);
106

    
107
      mBlurStatus[0] = true;
108
      mEffects[0].blur(mBlurDynamic);
109

    
110
      MeshCubes mesh = new MeshCubes(1,1,false);
111

    
112
      mTex1 = new DistortedTexture(OBJ_SIZE,OBJ_SIZE);
113
      mTex2 = new DistortedTexture(OBJ_SIZE,OBJ_SIZE);
114

    
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
      mScreen = new DistortedScreen();
125

    
126
      for(int i=0; i<NUM_OBJECTS; i++)
127
        mScreen.attach( i<NUM_OBJECTS/2 ? mTex1:mTex2, mEffects[i], mesh);
128
      }
129

    
130
///////////////////////////////////////////////////////////////////////////////////////////////////
131
   
132
    public void onDrawFrame(GL10 glUnused) 
133
      {
134
      mScreen.render( System.currentTimeMillis() );
135
      }
136

    
137
///////////////////////////////////////////////////////////////////////////////////////////////////
138
    
139
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
140
      {
141
      mScreenMin = width<height ? width:height;
142

    
143
    	float factor    = 0.15f*mScreenMin/OBJ_SIZE;
144
      Static3D center = new Static3D( (float)OBJ_SIZE/2, (float)OBJ_SIZE/2, 0.0f );
145
      Static3D moveVec= new Static3D( (width -factor*OBJ_SIZE)/2 ,(height-factor*OBJ_SIZE)/2 ,0);
146

    
147
      for(int i=0; i<NUM_OBJECTS; i++)
148
        {
149
        mEffects[i].abortEffects(EffectTypes.MATRIX);
150

    
151
        mEffects[i].move(moveVec);
152
        mEffects[i].scale(factor);
153
        mEffects[i].quaternion(mQuatInt1, center);
154
        mEffects[i].quaternion(mQuatInt2, center);
155
        mEffects[i].move(mMoveDynamic[i]);
156
        }
157

    
158
      computeMoveVectors();
159

    
160
      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
      DistortedEffects.enableEffect(EffectNames.BLUR);
190

    
191
      try
192
        {
193
        Distorted.onCreate(mView.getContext());
194
        }
195
      catch(Exception ex)
196
        {
197
        android.util.Log.e("Multiblur", ex.getMessage() );
198
        }
199
      }
200

    
201
///////////////////////////////////////////////////////////////////////////////////////////////////
202

    
203
    private void computeMoveVectors()
204
      {
205
      float size= 0.026f*OBJ_SIZE*mDistance;
206

    
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

    
220
      //android.util.Log.d("renderer", "distance: "+distance);
221
      }
222

    
223
///////////////////////////////////////////////////////////////////////////////////////////////////
224

    
225
    void setRange(int range)
226
      {
227
      mBlurVector.set(range);
228

    
229
      //android.util.Log.d("renderer", "range: "+range);
230
      }
231

    
232
///////////////////////////////////////////////////////////////////////////////////////////////////
233

    
234
   boolean[] getChecked()
235
     {
236
     return mBlurStatus;
237
     }
238

    
239
///////////////////////////////////////////////////////////////////////////////////////////////////
240

    
241
   void setChecked(int number, boolean checked)
242
     {
243
     if( number>=0 && number<=7 && mEffects!=null )
244
       {
245
       if( checked && !mBlurStatus[number] )
246
         {
247
         mBlurStatus[number] = true;
248
         mEffects[number].blur(mBlurDynamic);
249
         }
250
       if( !checked && mBlurStatus[number] )
251
         {
252
         mBlurStatus[number] = false;
253
         mEffects[number].abortEffects(EffectNames.BLUR);
254
         }
255
       }
256
     else
257
       {
258
       android.util.Log.e("renderer", "Error, number: "+number+" checked: "+checked+" mEffects="+ (mEffects==null ? "null":"not null") );
259
       }
260

    
261
     //android.util.Log.d("renderer", "setting box "+number+" BLUR state to "+checked);
262
     }
263
}
(2-2/3)