Project

General

Profile

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

examples / src / main / java / org / distorted / examples / dynamic / DynamicSurfaceView.java @ 27e12007

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.content.Context;
23
import android.opengl.GLSurfaceView;
24
import android.os.Build;
25
import android.view.MotionEvent;
26
import android.util.AttributeSet;
27
import android.graphics.Canvas;
28
import android.graphics.Paint.Style;
29
import android.graphics.Paint;
30

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

    
39
///////////////////////////////////////////////////////////////////
40

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

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

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

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

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

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

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

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

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

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

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

    
108
      if(!isInEditMode())
109
        {
110
        setFocusable(true);
111
        setFocusableInTouchMode(true);
112
        
113
        setEGLContextClientVersion(2);
114
        
115
        if( Build.FINGERPRINT.startsWith("generic") ) // when running on the emulator, insert a magic line that is
116
          {                                           // supposed to cure the 'no config chosen' crash on emulator startup
117
          setEGLConfigChooser(8, 8, 8, 8, 16, 0);   
118
          }
119
        
120
        DynamicRenderer mRenderer = new DynamicRenderer(this);
121
        setRenderer(mRenderer);
122
        }
123
      }
124

    
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126

    
127
    public void onSurfaceChanged(int width,int height)
128
      {
129
      mAvg = (width+height)/2;
130

    
131
      mSize1 = mAvg/150;
132
      mSize2 = mAvg/60;
133
      mSizeT = mAvg/30;
134

    
135
      mPaint.setTextSize(mSizeT);
136

    
137
      clearPoints();
138
      }
139

    
140
///////////////////////////////////////////////////////////////////
141

    
142
    public void setMode(int mode)
143
      {
144
      di1D.setMode(mode);  
145
      di2D.setMode(mode);
146
      di3D.setMode(mode);
147
      }
148

    
149
///////////////////////////////////////////////////////////////////
150

    
151
    public void setDuration(int duration)
152
      {
153
      mDuration = duration;
154
      
155
      di1D.setDuration(duration);
156
      di2D.setDuration(duration);
157
      di3D.setDuration(duration);
158
      }
159

    
160
///////////////////////////////////////////////////////////////////
161

    
162
    public void setNoise(float noise0, float noise1, float noise2)
163
      {
164
      mNoise0 = noise0;
165
      mNoise1 = noise1;
166
      mNoise2 = noise2;
167

    
168
      p1N.set(mNoise0);
169
      p2N.set(mNoise0,mNoise1);
170
      p3N.set(mNoise0,mNoise1,mNoise2);
171

    
172
      di1D.setNoise(p1N);
173
      di2D.setNoise(p2N);
174
      di3D.setNoise(p3N);
175
      }
176
    
177
///////////////////////////////////////////////////////////////////
178

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

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

    
205
      if( mLastTime<0 )
206
        {
207
        mLastTime = time;
208
        }
209
      else
210
        {
211
        mDiffTime = time - mLastTime;
212
        }
213

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

    
224
      mLastTime = time;
225
      }
226

    
227
///////////////////////////////////////////////////////////////////
228

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

    
237
///////////////////////////////////////////////////////////////////
238

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

    
247
      if( len>=2 )
248
        {
249
        di1D.interpolateMain(mPoints,3*mPosition, time, mDiffTime);
250

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

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

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

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

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

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