Project

General

Profile

« Previous | Next » 

Revision 76a81b6a

Added by Leszek Koltunski over 7 years ago

Combine Fragment3D and Vertex3D apps into one.

View differences:

src/main/AndroidManifest.xml
35 35
        <activity android:name=".cubes.CubesActivity" />       
36 36
        <activity android:name=".quaternion.QuaternionActivity" />          
37 37
        <activity android:name=".matrix3d.Matrix3DActivity" />
38
        <activity android:name=".vertex3d.Vertex3DActivity" />
39
        <activity android:name=".fragment3d.Fragment3DActivity" />
38
        <activity android:name=".effects3d.Effects3DActivity" />
40 39
        <activity android:name=".plainmonalisa.PlainMonaLisaActivity" />
41 40
        <activity android:name=".save.SaveActivity"/>
42 41
    </application>
src/main/java/org/distorted/examples/TableOfContents.java
54 54
import org.distorted.examples.cubes.CubesActivity;
55 55
import org.distorted.examples.quaternion.QuaternionActivity;
56 56
import org.distorted.examples.matrix3d.Matrix3DActivity;
57
import org.distorted.examples.vertex3d.Vertex3DActivity;
58
import org.distorted.examples.fragment3d.Fragment3DActivity;
57
import org.distorted.examples.effects3d.Effects3DActivity;
59 58
import org.distorted.examples.plainmonalisa.PlainMonaLisaActivity;
60 59
import org.distorted.examples.save.SaveActivity;
61 60

  
......
262 261

  
263 262
   {
264 263
      final Map<String, Object> item = new HashMap<>();
265
      item.put(ITEM_IMAGE, R.drawable.icon_example_vertex3d);
266
      item.put(ITEM_TITLE, (i+1)+". "+getText(R.string.example_vertex3d));
267
      item.put(ITEM_SUBTITLE, getText(R.string.example_vertex3d_subtitle));
264
      item.put(ITEM_IMAGE, R.drawable.icon_example_effects3d);
265
      item.put(ITEM_TITLE, (i+1)+". "+getText(R.string.example_effects3d));
266
      item.put(ITEM_SUBTITLE, getText(R.string.example_effects3d_subtitle));
268 267
      data.add(item);
269
      activityMapping.put(i++, Vertex3DActivity.class);
270
   }
271

  
272
   {
273
      final Map<String, Object> item = new HashMap<>();
274
      item.put(ITEM_IMAGE, R.drawable.icon_example_fragment3d);
275
      item.put(ITEM_TITLE, (i+1)+". "+getText(R.string.example_fragment3d));
276
      item.put(ITEM_SUBTITLE, getText(R.string.example_fragment3d_subtitle));
277
      data.add(item);
278
      activityMapping.put(i++, Fragment3DActivity.class);
268
      activityMapping.put(i++, Effects3DActivity.class);
279 269
   }
280 270

  
281 271
   {
src/main/java/org/distorted/examples/effects3d/Effects3DActivity.java
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.effects3d;
21

  
22
import android.app.Activity;
23
import android.graphics.Bitmap;
24
import android.graphics.BitmapFactory;
25
import android.graphics.Canvas;
26
import android.graphics.Paint;
27
import android.opengl.GLSurfaceView;
28
import android.os.Bundle;
29
import android.view.Gravity;
30
import android.view.View;
31
import android.widget.AdapterView;
32
import android.widget.ArrayAdapter;
33
import android.widget.Button;
34
import android.widget.LinearLayout;
35
import android.widget.NumberPicker;
36
import android.widget.Spinner;
37
import android.widget.TableRow;
38

  
39
import org.distorted.examples.R;
40
import org.distorted.library.Distorted;
41
import org.distorted.library.DistortedBitmap;
42
import org.distorted.library.DistortedCubes;
43
import org.distorted.library.DistortedObject;
44
import org.distorted.library.EffectNames;
45
import org.distorted.library.EffectTypes;
46

  
47
import java.io.IOException;
48
import java.io.InputStream;
49
import java.util.ArrayList;
50

  
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52

  
53
public class Effects3DActivity extends Activity
54
                              implements View.OnClickListener,
55
                                         AdapterView.OnItemSelectedListener
56
  {
57
  private static final int COLOR_OFF = 0xffffe81f;
58
  private static final int COLOR_ON  = 0xff0000ff;
59

  
60
  private boolean firstScreen;
61

  
62
  // fields needed for the first 'pick-a-shape' screen
63
  //
64
  private int mNumCols = 10;
65
  private int mNumRows = 10;
66
  private NumberPicker mColsPicker, mRowsPicker;
67
  private boolean[] mShape;
68
  private DistortedObject mObject;
69
  private int mObjectType;
70
  private int mBitmap;
71

  
72
  private ArrayList<Effects3DEffect> mEffects;
73
  private int mEffectAdd;
74
  private float mCenterX, mCenterY;
75
  private float mRegionX, mRegionY, mRegionR;
76

  
77
  private EffectNames[] mEffectNames;
78

  
79
  private static boolean mSupportsRegion = true;
80
  private static boolean mTypeVertex     = false;
81

  
82
///////////////////////////////////////////////////////////////////////////////////////////////////
83

  
84
  @Override
85
  protected void onCreate(Bundle savedState)
86
    {
87
    super.onCreate(savedState);
88

  
89
    mEffects = new ArrayList<>();
90

  
91
    createEffectNames();
92

  
93
    setContentView(R.layout.objectpickerlayout);
94

  
95
    mColsPicker = (NumberPicker)findViewById(R.id.objectpicker_cols);
96
    mRowsPicker = (NumberPicker)findViewById(R.id.objectpicker_rows);
97

  
98
    mColsPicker.setMaxValue(15);
99
    mColsPicker.setMinValue( 0);
100
    mRowsPicker.setMaxValue(15);
101
    mRowsPicker.setMinValue( 0);
102

  
103
    mColsPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener()
104
      {
105
      @Override
106
      public void onValueChange(NumberPicker picker, int oldVal, int newVal)
107
        {
108
        mNumCols = mColsPicker.getValue();
109
        }
110
      });
111

  
112
    mRowsPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener()
113
      {
114
      @Override
115
      public void onValueChange(NumberPicker picker, int oldVal, int newVal)
116
        {
117
        mNumRows = mRowsPicker.getValue();
118
        }
119
      });
120

  
121
    firstScreen = true;
122

  
123
    mObjectType = 0;
124

  
125
    Spinner typeSpinner  = (Spinner)findViewById(R.id.objectpicker_spinnerType);
126
    typeSpinner.setOnItemSelectedListener(this);
127

  
128
    String[] objectType = new String[] {"DistortedCubes", "DistortedBitmap"};
129

  
130
    ArrayAdapter<String> adapterType = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, objectType);
131
    adapterType.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
132
    typeSpinner.setAdapter(adapterType);
133

  
134
    Spinner bitmapSpinner  = (Spinner)findViewById(R.id.objectpicker_spinnerBitmap);
135
    bitmapSpinner.setOnItemSelectedListener(this);
136

  
137
    String[] objectBitmap = new String[] { "Grid", "Girl", "Dog", "Cat", "Squares"};
138

  
139
    ArrayAdapter<String> adapterBitmap = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, objectBitmap);
140
    adapterBitmap.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
141
    bitmapSpinner.setAdapter(adapterBitmap);
142
    }
143

  
144
///////////////////////////////////////////////////////////////////////////////////////////////////
145

  
146
  private void createEffectNames()
147
    {
148
    EffectTypes type1 = EffectTypes.FRAGMENT;
149
    EffectTypes type2 = EffectTypes.VERTEX;
150

  
151
    EffectNames[] names = EffectNames.values();
152

  
153
    int numEffects=0;
154

  
155
    for(int i=0; i<names.length; i++)
156
      if( names[i].getType() == type1 || names[i].getType() == type2 ) numEffects++;
157

  
158
    mEffectNames = new EffectNames[numEffects];
159

  
160
    numEffects=0;
161

  
162
    for(int i=0; i<names.length; i++)
163
      if( names[i].getType() == type1 || names[i].getType() == type2 )
164
        {
165
        mEffectNames[numEffects++] = names[i];
166
        }
167
    }
168

  
169
///////////////////////////////////////////////////////////////////////////////////////////////////
170

  
171
  private void setGrid()
172
    {
173
    LinearLayout lay = (LinearLayout)findViewById(R.id.objectpicker_buttongrid);
174

  
175
    int width = lay.getWidth();
176
    int height= lay.getHeight();
177
    int w = mNumCols>0 ? (width / mNumCols) -10 : 0;
178
    int h = mNumRows>0 ? (height/ mNumRows) -10 : 0;
179
    int size= w<h ? w:h;
180
    int pad = size/20;
181

  
182
    lay.removeAllViews();
183

  
184
    mShape = new boolean[mNumRows*mNumCols];
185

  
186
    TableRow.LayoutParams p = new TableRow.LayoutParams();
187

  
188
    p.rightMargin  = pad;
189
    p.leftMargin   = pad;
190
    p.topMargin    = pad;
191
    p.bottomMargin = pad;
192
    p.height       = size;
193
    p.width        = size;
194

  
195
    for (int rows=0; rows<mNumRows; rows++)
196
      {
197
      TableRow tr = new TableRow(this);
198
      tr.setGravity(Gravity.CENTER);
199

  
200
      for(int cols=0; cols<mNumCols; cols++)
201
        {
202
        Button b = new Button(this);
203
        b.setOnClickListener(this);
204
        b.setId(rows*mNumCols+cols);
205
        b.setLayoutParams(p);
206
        b.setBackgroundColor(COLOR_ON);
207
        tr.addView(b, p);
208
        mShape[rows*mNumCols+cols] = true;
209
        }
210

  
211
      lay.addView(tr);
212
      }
213
    }
214

  
215
///////////////////////////////////////////////////////////////////////////////////////////////////
216

  
217
  public DistortedObject getObject()
218
    {
219
    return mObject;
220
    }
221

  
222
///////////////////////////////////////////////////////////////////////////////////////////////////
223

  
224
  public static void setSupportsRegion(boolean supports)
225
    {
226
    mSupportsRegion = supports;
227
    }
228

  
229
///////////////////////////////////////////////////////////////////////////////////////////////////
230

  
231
  public static boolean supportsRegion()
232
    {
233
    return mSupportsRegion;
234
    }
235

  
236
///////////////////////////////////////////////////////////////////////////////////////////////////
237

  
238
  public static void setTypeVertex(boolean type)
239
    {
240
    mTypeVertex = type;
241
    }
242

  
243
///////////////////////////////////////////////////////////////////////////////////////////////////
244

  
245
  public static boolean isTypeVertex()
246
    {
247
    return mTypeVertex;
248
    }
249

  
250
///////////////////////////////////////////////////////////////////////////////////////////////////
251

  
252
  public Bitmap getBitmap()
253
    {
254
    Bitmap bmp;
255

  
256
    if( mBitmap!=-1)
257
      {
258
      InputStream is = getResources().openRawResource(mBitmap);
259

  
260
      try
261
        {
262
        bmp = BitmapFactory.decodeStream(is);
263
        }
264
      finally
265
        {
266
        try
267
          {
268
          is.close();
269
          }
270
        catch(IOException e) { }
271
        }
272
      }
273
    else
274
      {
275
      final int W = 640;
276
      final int H = 640;
277

  
278
      Paint paint = new Paint();
279
      bmp = Bitmap.createBitmap(W,H, Bitmap.Config.ARGB_8888);
280
      Canvas canvas = new Canvas(bmp);
281

  
282
      paint.setAntiAlias(true);
283
      paint.setTextAlign(Paint.Align.CENTER);
284
      paint.setColor(0xff008800);
285
      paint.setStyle(Paint.Style.FILL);
286
      canvas.drawRect(0, 0, W, H, paint);
287
      paint.setColor(0xffffffff);
288

  
289
      for(int i=0; i<=mNumCols ; i++ )
290
        {
291
        canvas.drawRect(W*i/mNumCols - 1,                0,  W*i/mNumCols + 1,  H               , paint);
292
        canvas.drawRect(               0, H *i/mNumCols -1,  W               ,  H*i/mNumCols + 1, paint);
293
        }
294
      }
295

  
296
    return bmp;
297
    }
298

  
299
///////////////////////////////////////////////////////////////////////////////////////////////////
300

  
301
  public void onClick(View view)
302
    {
303
    Button tmp = (Button)view;
304
    int id = tmp.getId();
305
    mShape[id] = !mShape[id];
306
    tmp.setBackgroundColor(mShape[id] ? COLOR_ON:COLOR_OFF);
307
    }
308

  
309
///////////////////////////////////////////////////////////////////////////////////////////////////
310

  
311
  public void setRegion(float x, float y, float r)
312
    {
313
    mRegionX = x;
314
    mRegionY =-y;
315
    mRegionR = r;
316

  
317
    Effects3DSurfaceView view = (Effects3DSurfaceView)findViewById(R.id.effects3dSurfaceView);
318

  
319
    view.getRenderer().setRegion(mRegionX, mRegionY, mRegionR);
320
    }
321

  
322
///////////////////////////////////////////////////////////////////////////////////////////////////
323

  
324
  public void setCenter(float x, float y)
325
    {
326
    mCenterX = x;
327
    mCenterY = y;
328

  
329
    Effects3DSurfaceView view = (Effects3DSurfaceView)findViewById(R.id.effects3dSurfaceView);
330

  
331
    view.getRenderer().setCenter( mCenterX, mCenterY );
332
    }
333

  
334
///////////////////////////////////////////////////////////////////////////////////////////////////
335

  
336
  public float getCenterX()
337
    {
338
    return mCenterX;
339
    }
340

  
341
///////////////////////////////////////////////////////////////////////////////////////////////////
342

  
343
  public float getCenterY()
344
    {
345
    return mCenterY;
346
    }
347

  
348
///////////////////////////////////////////////////////////////////////////////////////////////////
349

  
350
  public float getRegionX()
351
    {
352
    return mRegionX;
353
    }
354

  
355
///////////////////////////////////////////////////////////////////////////////////////////////////
356

  
357
  public float getRegionY()
358
    {
359
    return mRegionY;
360
    }
361

  
362
///////////////////////////////////////////////////////////////////////////////////////////////////
363

  
364
  public float getRegionR()
365
    {
366
    return mRegionR;
367
    }
368

  
369
///////////////////////////////////////////////////////////////////////////////////////////////////
370

  
371
  public void Continue(View v)
372
    {
373
    firstScreen = false;
374

  
375
    if( mObjectType==1 )
376
      {
377
      mObject = new DistortedBitmap(100,100,mNumCols);
378
      setEffectView();
379
      }
380
    else
381
      {
382
      View view = getLayoutInflater().inflate(R.layout.objectpicker2layout, null);
383

  
384
      setContentView(view);
385

  
386
      view.post(new Runnable() {
387
            @Override
388
            public void run() {
389
              setGrid();
390
            }
391
        });
392
      }
393
    }
394

  
395
///////////////////////////////////////////////////////////////////////////////////////////////////
396

  
397
  public void Create(View v)
398
    {
399
    firstScreen = false;
400

  
401
    String str = "";
402

  
403
    for(int i=0; i<mNumRows*mNumCols; i++)
404
      str += mShape[i] ? "1" : "0";
405

  
406
    mObject = new DistortedCubes(mNumCols, str, 10);
407

  
408
    setEffectView();
409
    }
410

  
411
///////////////////////////////////////////////////////////////////////////////////////////////////
412

  
413
  public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
414
    {
415
    switch(parent.getId())
416
      {
417
      case R.id.objectpicker_spinnerType  : mObjectType = pos;
418
                                            break;
419
      case R.id.objectpicker_spinnerBitmap: switch(pos)
420
                                              {
421
                                              case 0: mBitmap = -1        ; break;
422
                                              case 1: mBitmap = R.raw.face; break;
423
                                              case 2: mBitmap = R.raw.dog;  break;
424
                                              case 3: mBitmap = R.raw.cat;  break;
425
                                              case 4: mBitmap = R.raw.grid; break;
426
                                              }
427
                                            break;
428
      case R.id.effects3dspinner          : mEffectAdd = pos;
429
                                            break;
430
      }
431
    }
432

  
433
///////////////////////////////////////////////////////////////////////////////////////////////////
434

  
435
  public void onNothingSelected(AdapterView<?> parent)
436
    {
437
    }
438

  
439
///////////////////////////////////////////////////////////////////////////////////////////////////
440

  
441
  public int getWidth()
442
    {
443
    return mObject==null ? 0: mObject.getWidth();
444
    }
445

  
446
///////////////////////////////////////////////////////////////////////////////////////////////////
447

  
448
  public int getHeight()
449
    {
450
    return mObject==null ? 0: mObject.getHeight();
451
    }
452

  
453
///////////////////////////////////////////////////////////////////////////////////////////////////
454

  
455
  private void resetData()
456
    {
457
    mCenterX = 0.5f*getWidth();
458
    mCenterY = 0.5f*getHeight();
459
    mRegionX = 0;
460
    mRegionY = 0;
461
    mRegionR = getWidth()/2;
462
    }
463

  
464
///////////////////////////////////////////////////////////////////////////////////////////////////
465
// 'second screen' methods
466

  
467
  private void setEffectView()
468
    {
469
    resetData();
470

  
471
    final View view = getLayoutInflater().inflate(R.layout.effects3dlayout, null);
472

  
473
    setContentView(view);
474

  
475
    String[] effects = new String[mEffectNames.length];
476

  
477
    for(int i=0; i<mEffectNames.length; i++) effects[i] = mEffectNames[i].name();
478

  
479
    Spinner effectSpinner = (Spinner)findViewById(R.id.effects3dspinner );
480
    effectSpinner.setOnItemSelectedListener(this);
481

  
482
    ArrayAdapter<String> adapterEffect = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, effects);
483
    adapterEffect.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
484
    effectSpinner.setAdapter(adapterEffect);
485

  
486
    mEffectAdd = 0;
487
    }
488

  
489
///////////////////////////////////////////////////////////////////////////////////////////////////
490

  
491
  public void newEffect(View v)
492
    {
493
    Effects3DEffect eff = new Effects3DEffect(mEffectNames[mEffectAdd], this);
494
    mEffects.add(eff);
495

  
496
    LinearLayout layout = (LinearLayout)findViewById(R.id.effects3dlayout);
497
    View view = eff.createView();
498
    layout.addView(view);
499

  
500
    if( mTypeVertex )
501
      {
502
      View center = eff.createCenter();
503
      layout.addView(center);
504
      }
505

  
506
    if( mSupportsRegion )
507
      {
508
      View region = eff.createRegion();
509
      layout.addView(region);
510
      }
511

  
512
    eff.apply(mObject);
513
    }
514

  
515
///////////////////////////////////////////////////////////////////////////////////////////////////
516

  
517
  public void removeAll(View v)
518
    {
519
    mEffects.clear();
520
    LinearLayout layout = (LinearLayout)findViewById(R.id.effects3dlayout);
521
    layout.removeAllViews();
522
    mObject.abortEffects(EffectTypes.VERTEX);
523
    mObject.abortEffects(EffectTypes.FRAGMENT);
524

  
525
    resetData();
526

  
527
    Effects3DSurfaceView view = (Effects3DSurfaceView)findViewById(R.id.effects3dSurfaceView);
528
    Effects3DRenderer renderer= view.getRenderer();
529

  
530
    renderer.setCenter( mCenterX, mCenterY );
531
    renderer.setRegion( mRegionX, mRegionY, mRegionR );
532
    renderer.mQuat1.set(0,0,0,1);
533
    renderer.mQuat2.set(0,0,0,1);
534
    }
535

  
536
///////////////////////////////////////////////////////////////////////////////////////////////////
537
// Overrides
538

  
539
  @Override
540
  protected void onPause()
541
    {
542
    GLSurfaceView mView = (GLSurfaceView)findViewById(R.id.effects3dSurfaceView);
543
    if( mView!=null ) mView.onPause();
544
    super.onPause();
545
    }
546

  
547
///////////////////////////////////////////////////////////////////////////////////////////////////
548
    
549
  @Override
550
  protected void onResume()
551
    {
552
    super.onResume();
553
    GLSurfaceView mView = (GLSurfaceView)findViewById(R.id.effects3dSurfaceView);
554
    if( mView!=null ) mView.onResume();
555
    }
556

  
557
///////////////////////////////////////////////////////////////////////////////////////////////////
558
    
559
  @Override
560
  public void onWindowFocusChanged(boolean hasFocus)
561
    {
562
    super.onWindowFocusChanged(hasFocus);
563

  
564
    if( firstScreen )
565
      {
566
      mColsPicker.setValue(mNumCols);
567
      mRowsPicker.setValue(mNumRows);
568
      }
569
    }
570

  
571
///////////////////////////////////////////////////////////////////////////////////////////////////
572
    
573
  @Override
574
  protected void onDestroy()
575
    {
576
    Distorted.onDestroy();
577
    super.onDestroy();
578
    }
579

  
580
  }
src/main/java/org/distorted/examples/effects3d/Effects3DEffect.java
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.effects3d;
21

  
22
import android.view.View;
23
import android.widget.SeekBar;
24
import android.widget.TextView;
25

  
26
import org.distorted.examples.R;
27
import org.distorted.library.DistortedObject;
28
import org.distorted.library.EffectNames;
29
import org.distorted.library.EffectTypes;
30
import org.distorted.library.type.Dynamic1D;
31
import org.distorted.library.type.Dynamic2D;
32
import org.distorted.library.type.Dynamic3D;
33
import org.distorted.library.type.Dynamic4D;
34
import org.distorted.library.type.Static1D;
35
import org.distorted.library.type.Static2D;
36
import org.distorted.library.type.Static3D;
37
import org.distorted.library.type.Static4D;
38

  
39
import java.lang.ref.WeakReference;
40

  
41
///////////////////////////////////////////////////////////////////////////////////////////////////
42

  
43
public class Effects3DEffect implements SeekBar.OnSeekBarChangeListener
44
  {
45
  private WeakReference<Effects3DActivity> mAct;
46

  
47
  private EffectNames mName;
48
  private int[] mInter;
49
  private int[] mInterRegion;
50
  private int[] mInterCenter;
51
  private int[] mSeekID;
52
  private int[] mSeekRegionID;
53
  private int[] mSeekCenterID;
54
  private int mDimension;
55
  private TextView mText,mTextRegion,mTextCenter;
56

  
57
  private Dynamic1D mDyn1;
58
  private Dynamic3D mDyn3;
59
  private Static1D  mSta1;
60
  private Static3D  mSta3;
61
  private Dynamic4D mRegionDyn;
62
  private Static4D  mRegionSta;
63
  private Dynamic2D mCenterDyn;
64
  private Static2D  mCenterSta;
65

  
66
///////////////////////////////////////////////////////////////////////////////////////////////////
67
// requires knowledge about effect nature
68

  
69
  private int getDimension()
70
    {
71
    switch(mName)
72
      {
73
      case DISTORT      :
74
      case DEFORM       : return 3;
75

  
76
      case CHROMA       :
77
      case SMOOTH_CHROMA: return 4;
78

  
79
      default           : return 1;
80
      }
81
    }
82

  
83
///////////////////////////////////////////////////////////////////////////////////////////////////
84
// requires knowledge about effect nature
85

  
86
  public void apply(DistortedObject object)
87
    {
88
    switch(mName)
89
      {
90
      case DISTORT: object.distort(mDyn3, mCenterDyn, mRegionDyn); break;
91
      case DEFORM : object.deform (mDyn3, mCenterDyn            ); break;
92
      case SINK   : object.sink   (mDyn1, mCenterDyn, mRegionDyn); break;
93
      case SWIRL  : object.swirl  (mDyn1, mCenterDyn, mRegionDyn); break;
94

  
95
      case ALPHA            : object.alpha     (mDyn1,        mRegionDyn, false); break;
96
      case SMOOTH_ALPHA     : object.alpha     (mDyn1,        mRegionDyn, true ); break;
97
      case CHROMA           : object.chroma    (mDyn1, mDyn3, mRegionDyn, false); break;
98
      case SMOOTH_CHROMA    : object.chroma    (mDyn1, mDyn3, mRegionDyn, true ); break;
99
      case BRIGHTNESS       : object.brightness(mDyn1,        mRegionDyn, false); break;
100
      case SMOOTH_BRIGHTNESS: object.brightness(mDyn1,        mRegionDyn, true ); break;
101
      case SATURATION       : object.saturation(mDyn1,        mRegionDyn, false); break;
102
      case SMOOTH_SATURATION: object.saturation(mDyn1,        mRegionDyn, true ); break;
103
      case CONTRAST         : object.contrast  (mDyn1,        mRegionDyn, false); break;
104
      case SMOOTH_CONTRAST  : object.contrast  (mDyn1,        mRegionDyn, true ); break;
105
      }
106
    }
107

  
108
///////////////////////////////////////////////////////////////////////////////////////////////////
109
// requires knowledge about effect nature
110

  
111
  private boolean supportsRegion()
112
    {
113
    return mName == EffectNames.DEFORM ? false : true;
114
    }
115

  
116
///////////////////////////////////////////////////////////////////////////////////////////////////
117
// requires knowledge about effect nature
118

  
119
  private void fillStatics()
120
    {
121
    switch(mName)
122
      {
123
      case DISTORT          :
124
      case DEFORM           : float l = mAct.get().getWidth()/50.0f;
125
                              float x = (mInter[0]-50)*l;
126
                              float y = (mInter[1]-50)*l;
127
                              float z = (mInter[2]-50)*l;
128
                              mSta3.set(x,y,z);
129
                              break;
130
      case SINK             : mSta1.set(mInter[0] > 50 ? 50.0f/(100.01f-mInter[0]) : mInter[0] / 50.0f);
131
                              break;
132
      case SWIRL            : mSta1.set( 3.6f*(mInter[0]-50) );
133
                              break;
134

  
135
      case ALPHA            :
136
      case SMOOTH_ALPHA     :
137
      case BRIGHTNESS       :
138
      case SMOOTH_BRIGHTNESS:
139
      case SATURATION       :
140
      case SMOOTH_SATURATION:
141
      case CONTRAST         :
142
      case SMOOTH_CONTRAST  : mSta1.set(mInter[0]/100.0f);
143
                              break;
144

  
145
      case CHROMA           :
146
      case SMOOTH_CHROMA    : mSta1.set(mInter[0]/100.0f);
147
                              mSta3.set(255.0f*mInter[1]/100,
148
                                        255.0f*mInter[2]/100,
149
                                        255.0f*mInter[3]/100);
150
                              break;
151
      }
152
    }
153

  
154
///////////////////////////////////////////////////////////////////////////////////////////////////
155
// requires knowledge about effect nature
156

  
157
  private void setDefaultInter()
158
    {
159
    switch(mName)
160
      {
161
      case DISTORT          :
162
      case DEFORM           : mInter[0] = 50;
163
                              mInter[1] = 50;
164
                              mInter[2] = 50;
165
                              break;
166
      case SINK             :
167
      case SWIRL            : mInter[0] = 50;
168
                              break;
169

  
170
      case ALPHA            :
171
      case SMOOTH_ALPHA     :
172
      case BRIGHTNESS       :
173
      case SMOOTH_BRIGHTNESS:
174
      case SATURATION       :
175
      case SMOOTH_SATURATION:
176
      case CONTRAST         :
177
      case SMOOTH_CONTRAST  : mInter[0] = 50;
178
                              break;
179

  
180
      case CHROMA           :
181
      case SMOOTH_CHROMA    : mInter[0] = 50;
182
                              mInter[1] =  0;
183
                              mInter[2] =  0;
184
                              mInter[3] =  0;
185
                              break;
186
      }
187
    }
188

  
189
///////////////////////////////////////////////////////////////////////////////////////////////////
190
// requires knowledge about effect nature
191

  
192
  private void setText()
193
    {
194
    String text = mName.name();
195

  
196
    if( mName.getType()== EffectTypes.FRAGMENT )
197
      {
198
      text+=(" "+((int)(mSta1.getX()*100))/100.0f);
199
      }
200

  
201
    if( mName==EffectNames.DISTORT || mName==EffectNames.DEFORM       ||
202
        mName==EffectNames.CHROMA  || mName==EffectNames.SMOOTH_CHROMA )
203
      {
204
      int f1 = (int)mSta3.getX();
205
      int f2 = (int)mSta3.getY();
206
      int f3 = (int)mSta3.getZ();
207
      text += " ("+f1+","+f2+","+f3+")";
208
      }
209
    else if(mName.getType() == EffectTypes.VERTEX)
210
      {
211
      float f1 = ((int)(mSta1.getX()*100))/100.0f;
212
      text += " ("+f1+")";
213
      }
214

  
215
    mText.setText(text);
216
    }
217

  
218
///////////////////////////////////////////////////////////////////////////////////////////////////
219

  
220
  private void fillCenterStatics()
221
    {
222
    Effects3DActivity act = mAct.get();
223

  
224
    float x = (mInterCenter[0]*0.012f - 0.1f)*act.getWidth();
225
    float y = (mInterCenter[1]*0.012f - 0.1f)*act.getHeight();
226
    mCenterSta.set(x,y);
227
    }
228

  
229
///////////////////////////////////////////////////////////////////////////////////////////////////
230

  
231
  private void setDefaultCenterInter()
232
    {
233
    mInterCenter[0] = 50;
234
    mInterCenter[1] = 50;
235
    }
236

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

  
239
  private void setCenterText()
240
    {
241
    int f0 = (int)mCenterSta.getX();
242
    int f1 = (int)mCenterSta.getY();
243

  
244
    mTextCenter.setText("center ("+f0+","+f1+")");
245
    }
246

  
247
///////////////////////////////////////////////////////////////////////////////////////////////////
248

  
249
  private void fillRegionStatics()
250
    {
251
    Effects3DActivity act = mAct.get();
252

  
253
    float factorX = act.getWidth() / 100.0f;
254
    float factorY = act.getHeight()/ 100.0f;
255

  
256
    int deduct = (mName.getType() == EffectTypes.VERTEX ? 50:0);
257

  
258
    float  x = (mInterRegion[0]-deduct)*factorX;
259
    float  y = (mInterRegion[1]-deduct)*factorY;
260
    float rx =  mInterRegion[2]        *factorX;
261
    float ry =  mInterRegion[3]        *factorY;
262

  
263
    mRegionSta.set(x,y,rx,ry);
264
    }
265

  
266
///////////////////////////////////////////////////////////////////////////////////////////////////
267

  
268
  private void setDefaultRegionInter()
269
    {
270
    mInterRegion[0] = 50;
271
    mInterRegion[1] = 50;
272
    mInterRegion[2] = 50;
273
    mInterRegion[3] = 50;
274
    }
275

  
276
///////////////////////////////////////////////////////////////////////////////////////////////////
277

  
278
  private void setRegionText()
279
    {
280
    int f0 = (int)mRegionSta.getX();
281
    int f1 = (int)mRegionSta.getY();
282
    int f2 = (int)mRegionSta.getZ();
283
    int f3 = (int)mRegionSta.getW();
284

  
285
    mTextRegion.setText("region ("+f0+","+f1+","+f2+","+f3+")");
286
    }
287

  
288
///////////////////////////////////////////////////////////////////////////////////////////////////
289

  
290
  public Effects3DEffect(EffectNames name, Effects3DActivity act)
291
    {
292
    mAct = new WeakReference<>(act);
293
    mName = name;
294

  
295
    mDimension = getDimension();
296

  
297
    switch(mDimension)
298
      {
299
      case 1 : mDyn1 = new Dynamic1D();
300
               mSta1 = new Static1D(0);
301
               mDyn1.add(mSta1);
302
               mDyn3 = null;
303
               break;
304
      case 3 : mDyn3 = new Dynamic3D();
305
               mSta3 = new Static3D(0,0,0);
306
               mDyn3.add(mSta3);
307
               mDyn1 = null;
308
               break;
309
      case 4 : mDyn3 = new Dynamic3D();
310
               mSta3 = new Static3D(0,0,0);
311
               mDyn3.add(mSta3);
312
               mDyn1 = new Dynamic1D();
313
               mSta1 = new Static1D(0);
314
               mDyn1.add(mSta1);
315
               break;
316
      default: throw new RuntimeException("unsupported effect");
317
      }
318

  
319
    mInter = new int[mDimension];
320
    mSeekID= new int[mDimension];
321

  
322
    mInterRegion = new int[4];
323
    mSeekRegionID= new int[4];
324
    mRegionDyn   = new Dynamic4D();
325
    mRegionSta   = new Static4D(0,0,0,0);
326
    mRegionDyn.add(mRegionSta);
327

  
328
    mInterCenter = new int[2];
329
    mSeekCenterID= new int[2];
330
    mCenterDyn   = new Dynamic2D();
331
    mCenterSta   = new Static2D(0,0);
332
    mCenterDyn.add(mCenterSta);
333
    }
334

  
335
///////////////////////////////////////////////////////////////////////////////////////////////////
336

  
337
  public View createView()
338
    {
339
    View effect;
340
    SeekBar[] seek = new SeekBar[mDimension];
341

  
342
    Effects3DActivity act = mAct.get();
343

  
344
    switch(mDimension)
345
      {
346
      case 1 : effect     = act.getLayoutInflater().inflate(R.layout.effect1d, null);
347
               mText      = (TextView)effect.findViewById(R.id.effect1dText);
348
               seek[0]    = (SeekBar)effect.findViewById(R.id.effect1dbar1);
349
               mSeekID[0] = seek[0].getId();
350
               break;
351
      case 2 : effect     = act.getLayoutInflater().inflate(R.layout.effect2d, null);
352
               mText      = (TextView)effect.findViewById(R.id.effect2dText);
353
               seek[0]    = (SeekBar)effect.findViewById(R.id.effect2dbar1);
354
               seek[1]    = (SeekBar)effect.findViewById(R.id.effect2dbar2);
355
               mSeekID[0] = seek[0].getId();
356
               mSeekID[1] = seek[1].getId();
357
               break;
358
      case 3 : effect     = act.getLayoutInflater().inflate(R.layout.effect3d, null);
359
               mText      = (TextView)effect.findViewById(R.id.effect3dText);
360
               seek[0]    = (SeekBar)effect.findViewById(R.id.effect3dbar1);
361
               seek[1]    = (SeekBar)effect.findViewById(R.id.effect3dbar2);
362
               seek[2]    = (SeekBar)effect.findViewById(R.id.effect3dbar3);
363
               mSeekID[0] = seek[0].getId();
364
               mSeekID[1] = seek[1].getId();
365
               mSeekID[2] = seek[2].getId();
366
               break;
367
      case 4 : effect     = act.getLayoutInflater().inflate(R.layout.effect4d, null);
368
               mText      = (TextView)effect.findViewById(R.id.effect4dText);
369
               seek[0]    = (SeekBar)effect.findViewById(R.id.effect4dbar1);
370
               seek[1]    = (SeekBar)effect.findViewById(R.id.effect4dbar2);
371
               seek[2]    = (SeekBar)effect.findViewById(R.id.effect4dbar3);
372
               seek[3]    = (SeekBar)effect.findViewById(R.id.effect4dbar4);
373
               mSeekID[0] = seek[0].getId();
374
               mSeekID[1] = seek[1].getId();
375
               mSeekID[2] = seek[2].getId();
376
               mSeekID[3] = seek[3].getId();
377
               break;
378
      default: android.util.Log.e("Effects3DEffect", "dimension "+mDimension+" not supported!");
379
               return null;
380
      }
381

  
382
    setDefaultInter();
383

  
384
    for(int i=0; i<mDimension; i++)
385
      {
386
      seek[i].setOnSeekBarChangeListener(this);
387
      seek[i].setProgress( mInter[i] );
388
      }
389

  
390
    Effects3DActivity.setSupportsRegion(supportsRegion());
391
    Effects3DActivity.setTypeVertex(mName.getType() == EffectTypes.VERTEX);
392

  
393
    return effect;
394
    }
395

  
396
///////////////////////////////////////////////////////////////////////////////////////////////////
397

  
398
  public View createRegion()
399
    {
400
    Effects3DActivity act = mAct.get();
401

  
402
    View region = act.getLayoutInflater().inflate(R.layout.effectregion, null);
403

  
404
    SeekBar[] seek = new SeekBar[4];
405

  
406
    seek[0] = (SeekBar)region.findViewById(R.id.effectRegionBarX );
407
    seek[1] = (SeekBar)region.findViewById(R.id.effectRegionBarY );
408
    seek[2] = (SeekBar)region.findViewById(R.id.effectRegionBarRX);
409
    seek[3] = (SeekBar)region.findViewById(R.id.effectRegionBarRY);
410

  
411
    mSeekRegionID[0] = seek[0].getId();
412
    mSeekRegionID[1] = seek[1].getId();
413
    mSeekRegionID[2] = seek[2].getId();
414
    mSeekRegionID[3] = seek[3].getId();
415

  
416
    mTextRegion = (TextView)region.findViewById(R.id.effectRegionText);
417

  
418
    setDefaultRegionInter();
419

  
420
    for(int i=0; i<4; i++)
421
      {
422
      seek[i].setOnSeekBarChangeListener(this);
423
      seek[i].setProgress( mInterRegion[i] );
424
      }
425

  
426
    act.setRegion(mRegionSta.getX(),mRegionSta.getY(),mRegionSta.getZ());
427

  
428
    return region;
429
    }
430

  
431
///////////////////////////////////////////////////////////////////////////////////////////////////
432

  
433
  public View createCenter()
434
    {
435
    Effects3DActivity act = mAct.get();
436

  
437
    View center = act.getLayoutInflater().inflate(R.layout.effectcenter, null);
438

  
439
    SeekBar[] seek = new SeekBar[2];
440

  
441
    seek[0] = (SeekBar)center.findViewById(R.id.effectCenterBarX );
442
    seek[1] = (SeekBar)center.findViewById(R.id.effectCenterBarY );
443

  
444
    mSeekCenterID[0] = seek[0].getId();
445
    mSeekCenterID[1] = seek[1].getId();
446

  
447
    mTextCenter = (TextView)center.findViewById(R.id.effectCenterText);
448

  
449
    setDefaultCenterInter();
450

  
451
    for(int i=0; i<2; i++)
452
      {
453
      seek[i].setOnSeekBarChangeListener(this);
454
      seek[i].setProgress( mInterCenter[i] );
455
      }
456

  
457
    act.setCenter(mCenterSta.getX(),mCenterSta.getY());
458

  
459
    return center;
460
    }
461

  
462
///////////////////////////////////////////////////////////////////////////////////////////////////
463

  
464
  public void onProgressChanged(SeekBar bar, int progress, boolean fromUser)
465
    {
466
    if ( mDimension>=1 && bar.getId()==mSeekID[0] )
467
      {
468
      mInter[0] = progress;
469
      fillStatics();
470
      setText();
471
      }
472
    if ( mDimension>=2 && bar.getId()==mSeekID[1] )
473
      {
474
      mInter[1] = progress;
475
      fillStatics();
476
      setText();
477
      }
478
    if ( mDimension>=3 && bar.getId()==mSeekID[2] )
479
      {
480
      mInter[2] = progress;
481
      fillStatics();
482
      setText();
483
      }
484
    if ( mDimension>=4 && bar.getId()==mSeekID[3] )
485
      {
486
      mInter[3] = progress;
487
      fillStatics();
488
      setText();
489
      }
490

  
491
    if( bar.getId() == mSeekRegionID[0] )
492
      {
493
      mInterRegion[0] = progress;
494
      fillRegionStatics();
495
      setRegionText();
496
      }
497
    if( bar.getId() == mSeekRegionID[1] )
498
      {
499
      mInterRegion[1] = progress;
500
      fillRegionStatics();
501
      setRegionText();
502
      }
503
    if( bar.getId() == mSeekRegionID[2] )
504
      {
505
      mInterRegion[2] = progress;
506
      fillRegionStatics();
507
      setRegionText();
508
      }
509
    if( bar.getId() == mSeekRegionID[3] )
510
      {
511
      mInterRegion[3] = progress;
512
      fillRegionStatics();
513
      setRegionText();
514
      }
515

  
516
    if( bar.getId() == mSeekCenterID[0] )
517
      {
518
      mInterCenter[0] = progress;
519
      fillCenterStatics();
520
      setCenterText();
521
      }
522
    if( bar.getId() == mSeekCenterID[1] )
523
      {
524
      mInterCenter[1] = progress;
525
      fillCenterStatics();
526
      setCenterText();
527
      }
528

  
529
    if( fromUser )
530
      {
531
      Effects3DActivity.setSupportsRegion(supportsRegion());
532
      Effects3DActivity.setTypeVertex(mName.getType() == EffectTypes.VERTEX);
533

  
534
      Effects3DActivity act = mAct.get();
535

  
536
      act.setCenter(mCenterSta.getX(),mCenterSta.getY());
537
      act.setRegion(mRegionSta.getX(),mRegionSta.getY(),mRegionSta.getZ());
538
      }
539
    }
540

  
541
///////////////////////////////////////////////////////////////////////////////////////////////////
542

  
543
  public void onStartTrackingTouch(SeekBar bar) { }
544

  
545
///////////////////////////////////////////////////////////////////////////////////////////////////
546

  
547
  public void onStopTrackingTouch(SeekBar bar)  { }
548
  }
src/main/java/org/distorted/examples/effects3d/Effects3DRenderer.java
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.effects3d;
21

  
22
import android.graphics.Bitmap;
23
import android.graphics.BitmapFactory;
24
import android.opengl.GLES20;
25
import android.opengl.GLSurfaceView;
26

  
27
import org.distorted.examples.R;
28
import org.distorted.library.Distorted;
29
import org.distorted.library.DistortedBitmap;
30
import org.distorted.library.DistortedObject;
31
import org.distorted.library.EffectTypes;
32
import org.distorted.library.type.Dynamic3D;
33
import org.distorted.library.type.DynamicQuat;
34
import org.distorted.library.type.Static3D;
35
import org.distorted.library.type.Static4D;
36

  
37
import java.io.IOException;
38
import java.io.InputStream;
39

  
40
import javax.microedition.khronos.egl.EGLConfig;
41
import javax.microedition.khronos.opengles.GL10;
42

  
43
///////////////////////////////////////////////////////////////////////////////////////////////////
44

  
45
class Effects3DRenderer implements GLSurfaceView.Renderer
46
{
47
    private GLSurfaceView mView;
48
    private DistortedObject mObject;
49
    private DistortedBitmap mBackground;
50
    private DistortedBitmap mCenter;
51
    private DistortedBitmap mRegion;
52
    private int mObjWidth, mObjHeight, mObjDepth;
53
    private DynamicQuat mQuatInt1, mQuatInt2;
54

  
55
    private Dynamic3D mCenterInter, mRegionInter;
56
    private Static3D mCenterPoint, mRegionPoint;
57
    private Dynamic3D mRegionScaleInter;
58
    private Static3D mRegionScalePoint;
59

  
60
    private float mFactorObj, mFactorReg;
61

  
62
    Static4D mQuat1, mQuat2;
63
    int mScreenMin;
64

  
65
///////////////////////////////////////////////////////////////////////////////////////////////////
66

  
67
    public Effects3DRenderer(GLSurfaceView v)
68
      {
69
      mView = v;
70

  
71
      mObject     = ((Effects3DActivity)v.getContext()).getObject();
72
      mBackground = new DistortedBitmap(100, 100, 1);
73
      mCenter     = new DistortedBitmap(100, 100, 1);
74
      mRegion     = new DistortedBitmap(100, 100, 1);
75

  
76
      mObjWidth = mObject.getWidth();
77
      mObjHeight= mObject.getHeight();
78
      mObjDepth = mObject.getDepth();
79

  
80
      mQuat1 = new Static4D(0,0,0,1);  // unity
81
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
82

  
83
      mQuatInt1 = new DynamicQuat(0,0.5f);
84
      mQuatInt2 = new DynamicQuat(0,0.5f);
85

  
86
      mQuatInt1.add(mQuat1);
87
      mQuatInt2.add(mQuat2);
88

  
89
      mCenterInter= new Dynamic3D();
90
      mCenterPoint= new Static3D(0,0,0);
91
      mCenterInter.add(mCenterPoint);
92

  
93
      mRegionInter= new Dynamic3D();
94
      mRegionPoint= new Static3D(0,0,0);
95
      mRegionInter.add(mRegionPoint);
96

  
97
      mRegionScaleInter = new Dynamic3D();
98
      mRegionScalePoint = new Static3D(0,0,0);
99
      mRegionScaleInter.add(mRegionScalePoint);
100
      }
101

  
102
///////////////////////////////////////////////////////////////////////////////////////////////////
103

  
104
    public void setCenter(float x, float y)
105
      {
106
      mCenterPoint.set(mFactorObj*x,mFactorObj*y,0);
107
      }
108

  
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110

  
111
    public void setRegion(float x, float y, float r)
112
      {
113
      mFactorReg = 2*mFactorObj*r/mRegion.getWidth();
114
      mRegionPoint.set(mFactorObj*x,mFactorObj*y,0);
115
      mRegionScalePoint.set(mFactorReg,mFactorReg,mFactorReg);
116
      }
117

  
118
///////////////////////////////////////////////////////////////////////////////////////////////////
119

  
120
    public void onDrawFrame(GL10 glUnused)
121
      {
122
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
123
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
124

  
125
      long time = System.currentTimeMillis();
126

  
127
      mBackground.draw(time);
128
      mObject.draw(time);
129

  
130
      if( Effects3DActivity.isTypeVertex() )
131
        {
132
        mCenter.draw(time);
133
        if( Effects3DActivity.supportsRegion() ) mRegion.draw(time);
134
        }
135
      }
136

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

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

  
143
      mObject.abortEffects(EffectTypes.MATRIX);
144
      mBackground.abortEffects(EffectTypes.MATRIX);
145
      mCenter.abortEffects(EffectTypes.MATRIX);
146
      mRegion.abortEffects(EffectTypes.MATRIX);
147

  
148
      float factorCen;
149
      int centerSize = mCenter.getWidth();
150
      int regionSize = mRegion.getWidth();
151

  
152
      if( width*mObjHeight > height*mObjWidth ) // screen is more 'horizontal' than the Object
153
        {
154
        mFactorObj = (0.80f*height)/mObjHeight;
155
        factorCen  = (0.08f*height)/centerSize;
156
        }
157
      else
158
        {
159
        mFactorObj = (0.80f*width)/mObjWidth;
160
        factorCen  = (0.08f*width)/centerSize;
161
        }
162

  
163
      Effects3DActivity act = (Effects3DActivity)mView.getContext();
164
      mCenterPoint.set(mFactorObj*act.getCenterX(),mFactorObj*act.getCenterY(),0);
165
      mRegionPoint.set(mFactorObj*act.getRegionX(),mFactorObj*act.getRegionY(),0);
166

  
167
      mFactorReg = 2*mFactorObj*act.getRegionR()/regionSize;
168
      mRegionScalePoint.set(mFactorReg,mFactorReg,mFactorReg);
169

  
170
      Static3D rotateObj = new Static3D(mObjWidth/2,mObjHeight/2, 0);
171

  
172
      mObject.move( new Static3D( (width-mFactorObj*mObjWidth)/2 , (height-mFactorObj*mObjHeight)/2 , 0) );
173
      mObject.scale(mFactorObj);
174
      mObject.quaternion(mQuatInt1, rotateObj);
175
      mObject.quaternion(mQuatInt2, rotateObj);
176

  
177
      Static3D rotateCen = new Static3D(width/2,height/2, 0);
178

  
179
      mCenter.quaternion(mQuatInt1, rotateCen);
180
      mCenter.quaternion(mQuatInt2, rotateCen);
181

  
182
      mCenter.move( new Static3D( (width -factorCen*centerSize-mFactorObj*mObjWidth )/2 ,
183
                                  (height-factorCen*centerSize-mFactorObj*mObjHeight)/2 , mFactorObj*mObjDepth/2+10) );
184
      mCenter.move(mCenterInter);
185
      mCenter.scale(factorCen);
186

  
187
      mRegion.quaternion(mQuatInt1, rotateCen);
188
      mRegion.quaternion(mQuatInt2, rotateCen);
189

  
190
      mRegion.move( new Static3D( (width -mFactorObj*mObjWidth )/2 ,
191
                                  (height-mFactorObj*mObjHeight)/2 , mFactorObj*mObjDepth/2+12) );
192
      mRegion.move(mCenterInter);
193
      mRegion.move(mRegionInter);
194
      mRegion.scale(mRegionScaleInter);
195
      mRegion.move( new Static3D( -regionSize/2 , -regionSize/2 , 0) );
196

  
197
      int backgroundSize = mBackground.getWidth();
198
      float factorBackX = ((float)width)/backgroundSize;
199
      float factorBackY = ((float)height)/backgroundSize;
200

  
201
      mBackground.move(new Static3D( -width/2, -height/2,-mFactorObj*(mObjWidth+mObjHeight)/2) );
202
      mBackground.scale(new Static3D(2*factorBackX, 2*factorBackY, 1.0f) );
203

  
204
      Distorted.onSurfaceChanged(width, height);
205
      }
206

  
207
///////////////////////////////////////////////////////////////////////////////////////////////////
208

  
209
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
210
      {
211
      Effects3DActivity act = (Effects3DActivity)mView.getContext();
212

  
213
      InputStream is1 = act.getResources().openRawResource(R.raw.water);
214
      InputStream is2 = act.getResources().openRawResource(R.raw.center);
215
      InputStream is3 = act.getResources().openRawResource(R.raw.region);
216

  
217
      Bitmap bitmap1,bitmap2,bitmap3;
218
        
219
      try 
220
        {
221
        bitmap1 = BitmapFactory.decodeStream(is1);
222
        bitmap2 = BitmapFactory.decodeStream(is2);
223
        bitmap3 = BitmapFactory.decodeStream(is3);
224
        }
225
      finally 
226
        {
227
        try 
228
          {
229
          is1.close();
230
          is2.close();
231
          is3.close();
232
          }
233
        catch(IOException e) { }
234
        }  
235
      
236
      mObject.setBitmap( act.getBitmap() );
237
      mBackground.setBitmap(bitmap1);
238
      mCenter.setBitmap(bitmap2);
239
      mRegion.setBitmap(bitmap3);
240

  
241
      try
242
        {
243
        Distorted.onSurfaceCreated(mView.getContext());
244
        }
245
      catch(Exception ex)
246
        {
247
        android.util.Log.e("Effects3D", ex.getMessage() );
248
        }
249
      }
250
}
src/main/java/org/distorted/examples/effects3d/Effects3DSurfaceView.java
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.effects3d;
21

  
22
import android.content.Context;
23
import android.opengl.GLSurfaceView;
24
import android.os.Build;
25
import android.util.AttributeSet;
26
import android.view.MotionEvent;
27

  
28
///////////////////////////////////////////////////////////////////////////////////////////////////
29

  
30
class Effects3DSurfaceView extends GLSurfaceView
31
  {
32
  private int mX, mY;
33
  private Effects3DRenderer mRenderer;
34

  
35
///////////////////////////////////////////////////////////////////////////////////////////////////
36
   
37
  public Effects3DSurfaceView(Context c, AttributeSet attrs)
38
    {
39
    super(c, attrs);
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff