Project

General

Profile

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

examples / src / main / java / org / distorted / examples / dynamic / DynamicSurfaceView.java @ fe59d375

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

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

    
32
import org.distorted.library.type.Dynamic;
33
import org.distorted.library.type.Dynamic1D;
34
import org.distorted.library.type.Dynamic2D;
35
import org.distorted.library.type.Dynamic3D;
36
import org.distorted.library.type.Static1D;
37
import org.distorted.library.type.Static2D;
38
import org.distorted.library.type.Static3D;
39

    
40
///////////////////////////////////////////////////////////////////
41

    
42
public class DynamicSurfaceView extends GLSurfaceView
43
    {
44
    public static final int DIM_1D   = 0; 
45
    public static final int DIM_2D   = 1; 
46
    public static final int DIM_3DXY = 2; 
47
    public static final int DIM_3DXZ = 3; 
48

    
49
    private static final int NUM_POINTS = 250;
50
    private static final int MAX_POINTS =   6;
51

    
52
    private static final Object lock = new Object();
53

    
54
    private Dynamic1D di1D;
55
    private Dynamic2D di2D;
56
    private Dynamic3D di3D;
57
    
58
    private Paint mPaint;
59
    private int moving;
60
    private int mDuration;
61
    private int mPosition;
62
    private long mDiffTime, mLastTime;
63
    private float mNoise0, mNoise1, mNoise2;
64

    
65
    private int mSize1, mSize2, mSizeT, mAvg;
66

    
67
    private int currentDim= DIM_2D;
68
    
69
    private Static1D p1D;
70
    private Static2D p2D;
71
    private Static3D p3D;
72

    
73
    private Static1D p1N;
74
    private Static2D p2N;
75
    private Static3D p3N;
76

    
77
    private float[] mPoints = new float[3*NUM_POINTS];
78
      
79
///////////////////////////////////////////////////////////////////
80
    
81
    public DynamicSurfaceView(Context context, AttributeSet attrs)
82
      {
83
      super(context, attrs);
84
      
85
      mPaint = new Paint();
86
      mPaint.setStyle(Style.FILL);
87
      mPaint.setAntiAlias(true);
88

    
89
      moving    = -1;
90
      mDuration = 10000;
91
      mPosition = 0;
92
      mNoise0   = 0.0f;
93
      mNoise1   = 0.0f;
94
      mNoise2   = 0.0f;
95
      mDiffTime = -1;
96
      mLastTime = -1;
97

    
98
      di1D = new Dynamic1D(mDuration,0.0f);
99
      p1N  = new Static1D(mNoise0);
100
      di2D = new Dynamic2D(mDuration,0.0f);
101
      p2N  = new Static2D(mNoise0,mNoise1);
102
      di3D = new Dynamic3D(mDuration,0.0f);
103
      p3N  = new Static3D(mNoise0,mNoise1,mNoise2);
104

    
105
      di1D.setAccessMode(Dynamic.ACCESS_SEQUENTIAL);
106
      di2D.setAccessMode(Dynamic.ACCESS_SEQUENTIAL);
107
      di3D.setAccessMode(Dynamic.ACCESS_SEQUENTIAL);
108

    
109
      if(!isInEditMode())
110
        {
111
        setFocusable(true);
112
        setFocusableInTouchMode(true);
113
        final ActivityManager activityManager     = (android.app.ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
114
        final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
115
        android.util.Log.e("View", "Using OpenGL ES "+configurationInfo.getGlEsVersion());
116
        setEGLContextClientVersion( (configurationInfo.reqGlEsVersion>>16) >= 3 ? 3:2 );
117
        DynamicRenderer mRenderer = new DynamicRenderer(this);
118
        setRenderer(mRenderer);
119
        }
120
      }
121

    
122
///////////////////////////////////////////////////////////////////////////////////////////////////
123

    
124
    public void onSurfaceChanged(int width,int height)
125
      {
126
      mAvg = (width+height)/2;
127

    
128
      mSize1 = mAvg/150;
129
      mSize2 = mAvg/60;
130
      mSizeT = mAvg/30;
131

    
132
      mPaint.setTextSize(mSizeT);
133

    
134
      clearPoints();
135
      }
136

    
137
///////////////////////////////////////////////////////////////////
138

    
139
    public void setMode(int mode)
140
      {
141
      di1D.setMode(mode);  
142
      di2D.setMode(mode);
143
      di3D.setMode(mode);
144
      }
145

    
146
///////////////////////////////////////////////////////////////////
147

    
148
    public void setDuration(int duration)
149
      {
150
      mDuration = duration;
151
      
152
      di1D.setDuration(duration);
153
      di2D.setDuration(duration);
154
      di3D.setDuration(duration);
155
      }
156

    
157
///////////////////////////////////////////////////////////////////
158

    
159
    public void setNoise(float noise0, float noise1, float noise2)
160
      {
161
      mNoise0 = noise0;
162
      mNoise1 = noise1;
163
      mNoise2 = noise2;
164

    
165
      p1N.set(mNoise0);
166
      p2N.set(mNoise0,mNoise1);
167
      p3N.set(mNoise0,mNoise1,mNoise2);
168

    
169
      di1D.setNoise(p1N);
170
      di2D.setNoise(p2N);
171
      di3D.setNoise(p3N);
172
      }
173
    
174
///////////////////////////////////////////////////////////////////
175

    
176
    public void setDimension(int dim)
177
      {
178
      if( currentDim != dim )
179
        {
180
        if( !(currentDim==DIM_3DXY && dim==DIM_3DXZ) && !(currentDim==DIM_3DXZ && dim==DIM_3DXY) )
181
          {
182
          synchronized(lock)
183
            {
184
            di1D.removeAll();
185
            di2D.removeAll();
186
            di3D.removeAll();
187

    
188
            clearPoints();
189
            }
190
          }
191
      
192
        currentDim = dim;
193
        }
194
      }
195
    
196
///////////////////////////////////////////////////////////////////
197
    
198
    public void drawCurve(Canvas c, long time)
199
      {
200
      if ( ++mPosition >= NUM_POINTS ) mPosition=0;
201

    
202
      if( mLastTime<0 )
203
        {
204
        mLastTime = time;
205
        }
206
      else
207
        {
208
        mDiffTime = time - mLastTime;
209
        }
210

    
211
      synchronized(lock)
212
        {
213
        switch(currentDim)
214
          {
215
          case DIM_1D: drawCurve1D(c,time); break;
216
          case DIM_2D: drawCurve2D(c,time); break;
217
          default    : drawCurve3D(c,time); break;
218
          }
219
        }
220

    
221
      mLastTime = time;
222
      }
223

    
224
///////////////////////////////////////////////////////////////////
225

    
226
    private void clearPoints()
227
      {
228
      for(int i=0; i<3*NUM_POINTS; i++)
229
         {
230
         mPoints[i] = -10.0f;
231
         }
232
      }
233

    
234
///////////////////////////////////////////////////////////////////
235

    
236
    private void drawCurve1D(Canvas c, long time)
237
      {
238
      int len = di1D.getNumPoints();   
239
      mPaint.setColor(0xff000000);
240
      
241
      c.drawLine(0, DynamicRenderer.texH/2, DynamicRenderer.texW, DynamicRenderer.texH/2, mPaint);
242
      c.drawText("x", 0.95f*DynamicRenderer.texW, DynamicRenderer.texH /2 + mSizeT , mPaint);
243

    
244
      if( len>=2 )
245
        {
246
        di1D.interpolateMain(mPoints,3*mPosition, time, mDiffTime);
247

    
248
        for(int i=0; i<NUM_POINTS; i++)
249
          {
250
          int color = i<=mPosition ? 0xff - (mPosition           -i)*0xff/(NUM_POINTS-1)
251
                                   : 0xff - (mPosition+NUM_POINTS-i)*0xff/(NUM_POINTS-1);
252
         
253
          mPaint.setColor( 0xffffff + ((color&0xff)<<24) ); 
254
          c.drawCircle(mPoints[3*i], DynamicRenderer.texH/2 , mSize1, mPaint );
255
          }
256
        }
257
     
258
      mPaint.setColor(0xffff0000);
259
      
260
      for(int curr=0; curr<len; curr++)
261
        {      
262
        p1D = di1D.getPoint(curr);
263
        c.drawCircle(p1D.getX(), DynamicRenderer.texH/2 , mSize2, mPaint);
264
        }   
265
      }
266
    
267
///////////////////////////////////////////////////////////////////
268
      
269
    private void drawCurve2D(Canvas c, long time)
270
      {
271
      int len = di2D.getNumPoints();   
272
      mPaint.setColor(0xff000000);
273
      
274
      c.drawLine(0, DynamicRenderer.texH/2, DynamicRenderer.texW, DynamicRenderer.texH/2, mPaint);
275
      c.drawLine(DynamicRenderer.texW/2, 0, DynamicRenderer.texW/2, DynamicRenderer.texH, mPaint);
276
      
277
      c.drawText("x", 0.95f* DynamicRenderer.texW    , DynamicRenderer.texH/2+mSizeT , mPaint);
278
      c.drawText("y", DynamicRenderer.texW/2 + mSizeT,                        mSizeT , mPaint);
279
      
280
      if( len>=2 )
281
        {
282
        di2D.interpolateMain(mPoints,3*mPosition, time, mDiffTime);
283

    
284
        for(int i=0; i<NUM_POINTS; i++)
285
          {
286
          int color = i<=mPosition ? 0xff - (mPosition           -i)*0xff/(NUM_POINTS-1)
287
                                   : 0xff - (mPosition+NUM_POINTS-i)*0xff/(NUM_POINTS-1);
288
         
289
          mPaint.setColor( 0xffffff + ((color&0xff)<<24) );
290
          c.drawCircle(mPoints[3*i], mPoints[3*i+1], mSize1, mPaint );
291
          }
292
        }
293
     
294
      mPaint.setColor(0xffff0000);
295
      
296
      for(int curr=0; curr<len; curr++)
297
        {      
298
        p2D = di2D.getPoint(curr);
299
        c.drawCircle(p2D.getX(),p2D.getY(), mSize2, mPaint);
300
        }
301
      }
302

    
303
///////////////////////////////////////////////////////////////////
304
      
305
    private void drawCurve3D(Canvas c, long time)
306
      {
307
      int len = di3D.getNumPoints();   
308
      mPaint.setColor(0xff000000);
309
      
310
      c.drawLine(0, DynamicRenderer.texH/2, DynamicRenderer.texW  , DynamicRenderer.texH/2, mPaint);
311
      c.drawLine(DynamicRenderer.texW/2, 0, DynamicRenderer.texW/2, DynamicRenderer.texH  , mPaint);
312
      
313
      c.drawText( "x"                             , 0.95f* DynamicRenderer.texW    , DynamicRenderer.texH/2 + mSizeT , mPaint);
314
      c.drawText( currentDim==DIM_3DXY ? "y" : "z", DynamicRenderer.texW/2 + mSizeT,                          mSizeT , mPaint);
315
      
316
      if( len>=2 )
317
        {
318
        di3D.interpolateMain(mPoints, 3*mPosition, time, mDiffTime);
319

    
320
        for(int i=0; i<NUM_POINTS; i++)
321
          {
322
          int color = i<=mPosition ? 0xff - (mPosition           -i)*0xff/(NUM_POINTS-1)
323
                                   : 0xff - (mPosition+NUM_POINTS-i)*0xff/(NUM_POINTS-1);
324
         
325
          mPaint.setColor( 0xffffff + ((color&0xff)<<24) ); 
326
          c.drawCircle(mPoints[3*i], mPoints[3*i + (currentDim==DIM_3DXY ? 1:2) ], mSize1, mPaint );
327
          }
328
        }
329
     
330
      mPaint.setColor(0xffff0000);
331
      
332
      for(int curr=0; curr<len; curr++)
333
        {      
334
        p3D = di3D.getPoint(curr);
335
        c.drawCircle(p3D.getX(), currentDim==DIM_3DXY ? p3D.getY():p3D.getZ(), mSize2, mPaint);
336
        }   
337
      }
338
    
339
///////////////////////////////////////////////////////////////////
340

    
341
    private void addNewPoint(int x, int y)
342
      {
343
      float gx,gy,gz;
344
      int len;
345
      
346
      switch(currentDim)
347
        {
348
        case DIM_1D: len = di1D.getNumPoints();
349
                
350
                     for(int g=0; g<len; g++)
351
                       {
352
                       p1D = di1D.getPoint(g);  
353
                       gx = p1D.getX();
354
                                    
355
                       if( (x-gx)*(x-gx) < (mAvg*mAvg/100) )
356
                         {
357
                         moving = g;
358
                         break;
359
                         }
360
                       }
361
                     if( moving<0 )
362
                       {
363
                       synchronized(lock)
364
                         {
365
                         if( len>=MAX_POINTS )
366
                           {
367
                           di1D.removeAll();
368
                           clearPoints();
369
                           }
370
                         di1D.add(new Static1D(x));
371
                         }
372
                       }
373
                     break;
374
        case DIM_2D: len = di2D.getNumPoints();
375
                                 
376
                     for(int g=0; g<len; g++)
377
                       {
378
                       p2D = di2D.getPoint(g);  
379
                       gx = p2D.getX();
380
                       gy = p2D.getY();
381
                                    
382
                       if( (x-gx)*(x-gx) + (y-gy)*(y-gy) < (mAvg*mAvg/100) )
383
                         {
384
                         moving = g;
385
                         break;
386
                         }
387
                       }
388
                     if( moving<0 )
389
                       {
390
                       synchronized(lock)
391
                         {
392
                         if( len>=MAX_POINTS )
393
                           {
394
                           di2D.removeAll();
395
                           clearPoints();
396
                           }
397
                         di2D.add(new Static2D(x,y));
398
                         }
399
                       }
400
                     break;
401
        default    : len = di3D.getNumPoints();
402
                                 
403
                     for(int g=0; g<len; g++)
404
                       {
405
                       p3D = di3D.getPoint(g);  
406
                       gx = p3D.getX();
407
                       gy = p3D.getY();
408
                       gz = p3D.getZ();
409
                               
410
                     if( currentDim==DIM_3DXY )
411
                       {
412
                       if( (x-gx)*(x-gx) + (y-gy)*(y-gy) < (mAvg*mAvg/100) )
413
                         {
414
                         moving = g;
415
                         break;
416
                         }
417
                       }
418
                     if( currentDim==DIM_3DXZ )
419
                       {
420
                       if( (x-gx)*(x-gx) + (y-gz)*(y-gz) < (mAvg*mAvg/100) )
421
                         {
422
                         moving = g;
423
                         break;
424
                         }
425
                       }
426
                     }
427
                   if( moving<0 )
428
                     { 
429
                     synchronized(lock)
430
                       {
431
                       if( len>=MAX_POINTS )
432
                         {
433
                         di3D.removeAll();
434
                         clearPoints();
435
                         }
436
                    
437
                       if( currentDim==DIM_3DXY )
438
                         {
439
                         di3D.add(new Static3D(x,y, DynamicRenderer.texH/2));
440
                         }
441
                       if( currentDim==DIM_3DXZ )
442
                         {
443
                         di3D.add(new Static3D(x, DynamicRenderer.texH/2,y));
444
                         }
445
                       }
446
                     }
447
                   break; 
448
        }
449
      }
450
    
451
///////////////////////////////////////////////////////////////////
452
    
453
    @Override public boolean onTouchEvent(MotionEvent event) 
454
      {
455
      int action = event.getAction();
456
      int xDown, yDown;
457

    
458
      switch(action)
459
        {
460
        case MotionEvent.ACTION_DOWN: xDown = (int)event.getX();
461
                                      yDown = (int)event.getY();
462
                                      
463
                                      addNewPoint(xDown,yDown);
464
                                    
465
                                      break;
466
        case MotionEvent.ACTION_MOVE: if( moving>=0 )
467
                                        {
468
                                        xDown = (int)event.getX();
469
                                        yDown = (int)event.getY();
470
                                        
471
                                        switch(currentDim)
472
                                          {
473
                                          case DIM_1D  : di1D.setPoint(moving, xDown); 
474
                                                         break;
475
                                          case DIM_2D  : di2D.setPoint(moving, xDown, yDown);
476
                                                         break;
477
                                          case DIM_3DXY: di3D.setPoint(moving, xDown, yDown, (int)di3D.getPoint(moving).getZ());
478
                                                         break;
479
                                          case DIM_3DXZ: di3D.setPoint(moving, xDown, (int)di3D.getPoint(moving).getY(), yDown);
480
                                                         break;
481
                                          }
482
                                        }                           
483
                                      break;
484
        case MotionEvent.ACTION_UP  : moving = -1;
485
                                      break;
486
        }
487
            
488
      return true;
489
      }
490
 
491
///////////////////////////////////////////////////////////////////
492
  }
(3-3/3)