Project

General

Profile

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

examples / src / main / java / org / distorted / examples / movingeffects / MovingEffectsSurfaceView.java @ c7a31368

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.movingeffects;
21

    
22
import android.app.ActivityManager;
23
import android.content.Context;
24
import android.content.pm.ConfigurationInfo;
25
import android.graphics.Canvas;
26
import android.graphics.Paint;
27
import android.graphics.Paint.Style;
28
import android.opengl.GLSurfaceView;
29
import android.view.MotionEvent;
30
import android.util.AttributeSet;
31

    
32
import org.distorted.library.effect.EffectType;
33
import org.distorted.library.effect.FragmentEffectAlpha;
34
import org.distorted.library.effect.FragmentEffectChroma;
35
import org.distorted.library.effect.VertexEffectDeform;
36
import org.distorted.library.effect.VertexEffectSink;
37
import org.distorted.library.effect.VertexEffectSwirl;
38
import org.distorted.library.main.DistortedEffects;
39
import org.distorted.library.type.Dynamic3D;
40
import org.distorted.library.type.Static1D;
41
import org.distorted.library.type.Static3D;
42
import org.distorted.library.type.Static4D;
43

    
44
///////////////////////////////////////////////////////////////////////////////////////////////////
45

    
46
public class MovingEffectsSurfaceView extends GLSurfaceView
47
    {
48
    private static final int LOOP_TIME = 5000;
49
    private static final int NUM_POINTS= 100;
50

    
51
    public static final int EFFECT_POINTS=0;
52
    public static final int EFFECT_BUBBLE=1;
53
    public static final int EFFECT_SINK  =2;
54
    public static final int EFFECT_TRANS =3;
55
    public static final int EFFECT_CHROMA=4;
56
    public static final int EFFECT_SWIRL =5;
57

    
58
    private static final Object lock = new Object();
59

    
60
    private Dynamic3D mCenter;
61
    private Paint mPaint;
62
    private int moving;
63
    private int mScrWidth, mScrHeight;
64
    private long mTime = 0;
65
    
66
    private int mCurrEffect;
67
    private int mSize1, mSize2;
68
    private Static3D mDeform;
69

    
70
    private MovingEffectsRenderer mRenderer;
71

    
72
    private VertexEffectDeform mEffectDeform;
73
    private VertexEffectSink mEffectSink;
74
    private VertexEffectSwirl mEffectSwirl;
75
    private FragmentEffectChroma mEffectChroma;
76
    private FragmentEffectAlpha mEffectAlpha;
77

    
78
///////////////////////////////////////////////////////////////////////////////////////////////////
79
    
80
    public MovingEffectsSurfaceView(Context context, AttributeSet attrs)
81
      {
82
      super(context, attrs);
83

    
84
      mCurrEffect=EFFECT_POINTS;
85
      mPaint = new Paint();
86
      mPaint.setStyle(Style.FILL);
87
      moving = -1;
88
      
89
      mCenter = new Dynamic3D(LOOP_TIME,0.0f);
90
      mDeform = new Static3D(0,0,0.15f);
91

    
92
      if(!isInEditMode())
93
        {
94
        mRenderer = new MovingEffectsRenderer(this);
95

    
96
        setFocusable(true);
97
        setFocusableInTouchMode(true);
98
        final ActivityManager activityManager     = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
99
        final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
100
        setEGLContextClientVersion( (configurationInfo.reqGlEsVersion>>16) >= 3 ? 3:2 );
101
        setRenderer(mRenderer);
102

    
103
        Static3D regionFrag = new Static3D(0.15f,0.15f,0.15f);
104
        Static4D regionVert = new Static4D(0,0,0,0.15f);
105

    
106
        mEffectDeform  = new VertexEffectDeform  ( mDeform          , mCenter, regionVert);
107
        mEffectSink    = new VertexEffectSink    (new Static1D(10)  , mCenter, regionVert);
108
        mEffectSwirl   = new VertexEffectSwirl   (new Static1D(30)  , mCenter, regionVert);
109
        mEffectAlpha   = new FragmentEffectAlpha (new Static1D(0.5f), mCenter, regionFrag, true);
110
        mEffectChroma  = new FragmentEffectChroma(new Static1D(0.5f), new Static3D(1,0,0), mCenter, regionFrag, true);
111
        }
112
      }
113

    
114
///////////////////////////////////////////////////////////////////////////////////////////////////
115

    
116
    public void onSurfaceChanged(int width,int height)
117
      {
118
      int max = width>height ? width:height;
119

    
120
      mScrWidth  = width;
121
      mScrHeight = height;
122

    
123
      mSize1 = max/200;
124
      mSize2 = max/80;
125
      }
126

    
127
///////////////////////////////////////////////////////////////////////////////////////////////////
128

    
129
    public int getCurrentEffect()
130
      {
131
      return mCurrEffect;
132
      }
133
    
134
///////////////////////////////////////////////////////////////////////////////////////////////////
135
    
136
    public void Bubble()
137
      {
138
      if( mCurrEffect==EFFECT_BUBBLE ) return;     
139
      
140
      synchronized(lock)
141
        {
142
        DistortedEffects q = mRenderer.getEffects();
143
        q.abortByType(EffectType.VERTEX);
144
        q.abortByType(EffectType.FRAGMENT);
145
        q.apply(mEffectDeform);
146
        mCurrEffect = EFFECT_BUBBLE;
147
        }
148
      }
149

    
150
///////////////////////////////////////////////////////////////////////////////////////////////////
151

    
152
    public void Sink()
153
      {
154
      if( mCurrEffect==EFFECT_SINK ) return; 
155
         
156
      synchronized(lock)
157
        {
158
        DistortedEffects q = mRenderer.getEffects();
159
        q.abortByType(EffectType.VERTEX);
160
        q.abortByType(EffectType.FRAGMENT);
161
        q.apply(mEffectSink);
162
        mCurrEffect = EFFECT_SINK;
163
        }
164
      }
165

    
166
///////////////////////////////////////////////////////////////////////////////////////////////////
167

    
168
    public void Chroma()
169
      {
170
      if( mCurrEffect==EFFECT_CHROMA ) return;
171
         
172
      synchronized(lock)
173
        {
174
        DistortedEffects q = mRenderer.getEffects();
175
        q.abortByType(EffectType.VERTEX);
176
        q.abortByType(EffectType.FRAGMENT);
177
        q.apply(mEffectChroma);
178
        mCurrEffect = EFFECT_CHROMA;
179
        }
180
      }
181

    
182
///////////////////////////////////////////////////////////////////////////////////////////////////
183

    
184
    public void Transparency()
185
      {
186
      if( mCurrEffect==EFFECT_TRANS ) return;   
187
      
188
      synchronized(lock)
189
        {
190
        DistortedEffects q = mRenderer.getEffects();
191
        q.abortByType(EffectType.VERTEX);
192
        q.abortByType(EffectType.FRAGMENT);
193
        q.apply(mEffectAlpha);
194
        mCurrEffect = EFFECT_TRANS;
195
        }
196
      }
197

    
198
///////////////////////////////////////////////////////////////////////////////////////////////////
199

    
200
    public void Swirl()
201
      {
202
      if( mCurrEffect==EFFECT_SWIRL ) return;   
203
      
204
      synchronized(lock)
205
        {
206
        DistortedEffects q = mRenderer.getEffects();
207
        q.abortByType(EffectType.VERTEX);
208
        q.abortByType(EffectType.FRAGMENT);
209
        q.apply(mEffectSwirl);
210
        mCurrEffect = EFFECT_SWIRL;
211
        }
212
      }
213
    
214
///////////////////////////////////////////////////////////////////////////////////////////////////
215

    
216
    public void Abort()
217
      {
218
      synchronized(lock)
219
        {
220
        DistortedEffects q = mRenderer.getEffects();
221
        q.abortByType(EffectType.VERTEX);
222
        q.abortByType(EffectType.FRAGMENT);
223
        mCenter.removeAll();
224
        mCurrEffect = EFFECT_POINTS;
225
        mRenderer.setRefresh();
226
        }
227
      }
228

    
229
///////////////////////////////////////////////////////////////////////////////////////////////////
230
 
231
    public void drawCurve(Canvas c, long time)
232
      {
233
      synchronized(lock)
234
        {  
235
        int len = mCenter.getNumPoints();
236

    
237
        float[] drawCoord = new float[3];
238
        Static3D cu;
239

    
240
        if( len>=2 )
241
          {
242
          final float step = (float)LOOP_TIME/(NUM_POINTS+1);
243

    
244
          for(int i=0; i<NUM_POINTS; i++)
245
            {
246
            mPaint.setColor( 0xffffffff );
247
            mCenter.get( drawCoord, 0, (long)(i*step) );
248
            c.drawCircle( (drawCoord[0]+0.5f)*mScrWidth, mScrHeight*(0.5f-drawCoord[1]), mSize1, mPaint );
249
            }
250
          }
251
     
252
        mPaint.setColor(0xffff0000);
253
      
254
        for(int curr=0; curr<len; curr++)
255
          {       
256
          cu = mCenter.getPoint(curr);
257
          c.drawCircle( (cu.get0()+0.5f)*mScrWidth, mScrHeight*(0.5f-cu.get1()), mSize2, mPaint);
258
          }
259
        
260
        if( time-mTime > LOOP_TIME ) mTime = time;
261
        }
262
      }
263
       
264
///////////////////////////////////////////////////////////////////////////////////////////////////
265
    
266
    @Override
267
    public boolean onTouchEvent(MotionEvent event)
268
      {
269
      if( mCurrEffect!=EFFECT_POINTS ) return true;   
270

    
271
      float xDown = event.getX()/mScrWidth - 0.5f;
272
      float yDown = 0.5f - event.getY()/mScrHeight;
273

    
274
      switch(event.getAction())
275
        {
276
        case MotionEvent.ACTION_DOWN: float gx, gy;
277
                                      Static3D dv;
278
                                      int len = mCenter.getNumPoints();
279
                                 
280
                                      for(int g=0; g<len; g++)
281
                                        {
282
                                        dv = mCenter.getPoint(g);
283
                                        gx = dv.get0();
284
                                        gy = dv.get1();
285

    
286
                                        float Z = mDeform.get2()/7;
287

    
288
                                        if( (xDown-gx)*(xDown-gx) + (yDown-gy)*(yDown-gy) < Z*Z )
289
                                          {
290
                                          moving = g;
291
                                          break;
292
                                          }
293
                                        }
294
                                      if( moving<0 )
295
                                        {
296
                                        synchronized(lock)
297
                                          {
298
                                          mCenter.add(new Static3D(xDown,yDown,0));
299
                                          }
300
                                        }
301
                                      mRenderer.setRefresh();
302
                                      break;
303
        case MotionEvent.ACTION_MOVE: if( moving>=0 )
304
                                        {
305
                                        mCenter.setPoint(moving, xDown, yDown, 0);
306
                                        }
307
                                      mRenderer.setRefresh();
308
                                      break;
309
        case MotionEvent.ACTION_UP  : moving = -1;
310
                                      break;
311
        }
312
      return true;
313
      }
314
  }
(3-3/3)