Project

General

Profile

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

examples / src / main / java / org / distorted / examples / effects3d / Effects3DActivity2.java @ 9e7b6dbd

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.graphics.Canvas;
25
import android.graphics.Paint;
26
import android.opengl.GLSurfaceView;
27
import android.os.Bundle;
28
import android.support.design.widget.TabLayout;
29
import android.support.v4.view.ViewPager;
30
import android.support.v7.app.AppCompatActivity;
31
import android.view.View;
32
import android.widget.CheckBox;
33

    
34
import org.distorted.examples.R;
35
import org.distorted.library.effect.EffectType;
36
import org.distorted.library.main.Distorted;
37
import org.distorted.library.main.DistortedEffects;
38
import org.distorted.library.main.DistortedTexture;
39
import org.distorted.library.mesh.MeshCubes;
40
import org.distorted.library.mesh.MeshFlat;
41
import org.distorted.library.mesh.MeshBase;
42
import org.distorted.library.mesh.MeshQuad;
43
import org.distorted.library.mesh.MeshSphere;
44

    
45
import java.io.IOException;
46
import java.io.InputStream;
47

    
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49

    
50
public class Effects3DActivity2 extends AppCompatActivity
51
  {
52
  public static final int NUM_TABS = 4;
53

    
54
  private int mNumCols;
55
  private int mNumRows;
56
  private int mNumSlic;
57
  private int mObjectType;
58
  private int mBitmapID;
59
  private String mString;
60

    
61
  private DistortedTexture mTexture;
62
  private MeshBase mMesh;
63
  private Bitmap mBitmap;
64

    
65
  private float mCenterX, mCenterY, mCenterZ;
66
  private float mRegionX, mRegionY, mRegionZ, mRegionR;
67
  private DistortedEffects mEffects;
68
  private ViewPager mViewPager;
69
  private Effects3DTabViewPager mPager;
70

    
71
  private boolean mShowCenter;
72
  private boolean mShowRegion;
73
  private boolean mShowNormal;
74
  private boolean mUseOIT    ;
75

    
76
  private Effects3DTab[] mTab;
77

    
78
///////////////////////////////////////////////////////////////////////////////////////////////////
79

    
80
  @Override
81
  protected void onCreate(Bundle savedState)
82
    {
83
    super.onCreate(savedState);
84

    
85
    setTheme(R.style.CustomActivityThemeNoActionBar);
86

    
87
    mTab = new Effects3DTab[NUM_TABS];
88

    
89
    Bundle b = getIntent().getExtras();
90

    
91
    mObjectType = b.getInt("type");
92
    mNumCols    = b.getInt("cols");
93
    mNumRows    = b.getInt("rows");
94
    mNumSlic    = b.getInt("slices");
95
    mBitmapID   = b.getInt("bitmap");
96
    mString     = b.getString("string");
97

    
98
    mShowCenter = false;
99
    mShowRegion = false;
100
    mShowNormal = false;
101
    mUseOIT     = false;
102

    
103
    DistortedEffects.setMax(EffectType.VERTEX  ,10);    // those have to be called before
104
    DistortedEffects.setMax(EffectType.FRAGMENT,10);    // any DistortedEffect get created!
105

    
106
    switch(mObjectType)
107
      {
108
      case 0: mMesh = new MeshCubes(mNumCols, mString, mNumSlic);
109
              break;
110
      case 1: mMesh = new MeshFlat(mNumCols,mNumRows);
111
              break;
112
      case 2: mMesh = new MeshSphere(mNumRows);
113
              break;
114
      case 3: mMesh = new MeshQuad();
115
              break;
116
      }
117

    
118
    mMesh.setShowNormals(mShowNormal);
119
    mTexture= new DistortedTexture(mNumCols,mNumRows);
120
    mEffects= new DistortedEffects();
121

    
122
    final View view = getLayoutInflater().inflate(R.layout.effects3dlayout, null);
123

    
124
    setContentView(view);
125

    
126
    mViewPager = findViewById(R.id.effects3d_viewpager);
127
    mViewPager.setOffscreenPageLimit( NUM_TABS-1 );
128
    mPager = new Effects3DTabViewPager(this, getSupportFragmentManager() );
129
    mViewPager.setAdapter(mPager);
130
    TabLayout tabLayout = findViewById(R.id.effects3d_sliding_tabs);
131
    tabLayout.setupWithViewPager(mViewPager);
132

    
133
    resetData();
134
    }
135

    
136
///////////////////////////////////////////////////////////////////////////////////////////////////
137

    
138
  public DistortedTexture getTexture()
139
    {
140
    return mTexture;
141
    }
142

    
143
///////////////////////////////////////////////////////////////////////////////////////////////////
144

    
145
  public MeshBase getMesh()
146
    {
147
    return mMesh;
148
    }
149

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

    
152
  public Bitmap getBitmap()
153
    {
154
    if( mBitmap==null )
155
      {
156
      if( mBitmapID!=-1)
157
        {
158
        InputStream is = getResources().openRawResource(mBitmapID);
159

    
160
        try
161
          {
162
          mBitmap = BitmapFactory.decodeStream(is);
163
          }
164
        finally
165
          {
166
          try
167
            {
168
            is.close();
169
            }
170
          catch(IOException e) { }
171
          }
172
        }
173
      else
174
        {
175
        final int W = 64*mNumCols;
176
        final int H = 64*mNumRows;
177

    
178
        Paint paint = new Paint();
179
        mBitmap = Bitmap.createBitmap(W,H, Bitmap.Config.ARGB_8888);
180
        Canvas canvas = new Canvas(mBitmap);
181

    
182
        paint.setAntiAlias(true);
183
        paint.setTextAlign(Paint.Align.CENTER);
184
        paint.setColor(0xff008800);
185
        paint.setStyle(Paint.Style.FILL);
186
        canvas.drawRect(0, 0, W, H, paint);
187
        paint.setColor(0xffffffff);
188

    
189
        for(int i=0; i<=mNumCols ; i++ ) canvas.drawRect(W*i/mNumCols-1, 0, W*i/mNumCols + 1, H, paint);
190
        for(int i=0; i<=mNumRows ; i++ ) canvas.drawRect( 0, H*i/mNumRows-1, W,  H*i/mNumRows+1, paint);
191
        }
192
      }
193

    
194
    return mBitmap;
195
    }
196

    
197
///////////////////////////////////////////////////////////////////////////////////////////////////
198

    
199
  public void setRegion(float x, float y, float z, float r)
200
    {
201
    mRegionX = x;
202
    mRegionY =-y;
203
    mRegionZ = z;
204
    mRegionR = r;
205

    
206
    Effects3DSurfaceView view = findViewById(R.id.effects3dSurfaceView);
207
    view.getRenderer().setRegion(mRegionX, mRegionY, mRegionZ, mRegionR);
208
    }
209

    
210
///////////////////////////////////////////////////////////////////////////////////////////////////
211

    
212
  public void setCenter(float x, float y, float z)
213
    {
214
    mCenterX = x;
215
    mCenterY = y;
216
    mCenterZ = z;
217

    
218
    Effects3DSurfaceView view = findViewById(R.id.effects3dSurfaceView);
219
    view.getRenderer().setCenter( mCenterX, mCenterY, mCenterZ );
220
    }
221

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

    
224
  public float getCenterX()
225
    {
226
    return mCenterX;
227
    }
228

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

    
231
  public float getCenterY()
232
    {
233
    return mCenterY;
234
    }
235

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

    
238
  public float getRegionX()
239
    {
240
    return mRegionX;
241
    }
242

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

    
245
  public float getRegionY()
246
    {
247
    return mRegionY;
248
    }
249

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

    
252
  public float getRegionZ()
253
    {
254
    return mRegionZ;
255
    }
256

    
257
///////////////////////////////////////////////////////////////////////////////////////////////////
258

    
259
  public float getRegionR()
260
    {
261
    return mRegionR;
262
    }
263

    
264
///////////////////////////////////////////////////////////////////////////////////////////////////
265

    
266
  public int getWidth()
267
    {
268
    return mTexture==null ? 0: mTexture.getWidth();
269
    }
270

    
271
///////////////////////////////////////////////////////////////////////////////////////////////////
272

    
273
  public int getHeight()
274
    {
275
    return mTexture==null ? 0: mTexture.getHeight();
276
    }
277

    
278
///////////////////////////////////////////////////////////////////////////////////////////////////
279

    
280
  public int getDepth()
281
    {
282
    return mTexture==null ? 0: mTexture.getDepth(mMesh);
283
    }
284

    
285
///////////////////////////////////////////////////////////////////////////////////////////////////
286

    
287
  public int getScreenWidth()
288
    {
289
    Effects3DRenderer r = ((Effects3DSurfaceView)findViewById(R.id.effects3dSurfaceView)).getRenderer();
290
    return r.getWidth();
291
    }
292

    
293
///////////////////////////////////////////////////////////////////////////////////////////////////
294

    
295
  void resetData()
296
    {
297
    mCenterX = 0.5f*getWidth();
298
    mCenterY = 0.5f*getHeight();
299
    mRegionX = 0;
300
    mRegionY = 0;
301
    mRegionZ = 0;
302
    mRegionR = getWidth()/2;
303

    
304
    Effects3DSurfaceView view = findViewById(R.id.effects3dSurfaceView);
305
    Effects3DRenderer renderer= view.getRenderer();
306

    
307
    renderer.setCenter( mCenterX, mCenterY, mCenterZ );
308
    renderer.setRegion( mRegionX, mRegionY, mRegionZ, mRegionR );
309
    }
310

    
311
///////////////////////////////////////////////////////////////////////////////////////////////////
312

    
313
  void resetMatrixEffects()
314
    {
315
    Effects3DSurfaceView view = findViewById(R.id.effects3dSurfaceView);
316
    Effects3DRenderer renderer= view.getRenderer();
317

    
318
    renderer.resetMatrixEffects();
319
    }
320

    
321
///////////////////////////////////////////////////////////////////////////////////////////////////
322

    
323
  public DistortedEffects getEffects()
324
    {
325
    return mEffects;
326
    }
327

    
328
///////////////////////////////////////////////////////////////////////////////////////////////////
329

    
330
  public void newEffect(View v)
331
    {
332
    int pos = mViewPager.getCurrentItem();
333

    
334
    if( pos>=0 && pos<NUM_TABS ) mTab[pos].newEffect();
335
    }
336

    
337
///////////////////////////////////////////////////////////////////////////////////////////////////
338

    
339
  public void removeAll(View v)
340
    {
341
    int pos = mViewPager.getCurrentItem();
342

    
343
    if( pos>=0 && pos<NUM_TABS ) mTab[pos].removeAll();
344
    }
345

    
346
///////////////////////////////////////////////////////////////////////////////////////////////////
347

    
348
  public void remove(View v)
349
    {
350
    int pos = mViewPager.getCurrentItem();
351

    
352
    if( pos>=0 && pos<NUM_TABS ) mTab[pos].remove(v);
353
    }
354

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

    
357
  void setTab(int pos, Effects3DTab tab)
358
    {
359
    if( pos>=0 && pos<NUM_TABS ) mTab[pos] = tab;
360
    }
361

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

    
364
  boolean getShowCenter()
365
    {
366
    return mShowCenter;
367
    }
368

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

    
371
  boolean getShowRegion()
372
    {
373
    return mShowRegion;
374
    }
375

    
376
///////////////////////////////////////////////////////////////////////////////////////////////////
377

    
378
  public void showCenter(View view)
379
    {
380
    CheckBox box = (CheckBox)view;
381
    mShowCenter = box.isChecked();
382

    
383
    Effects3DSurfaceView sv = findViewById(R.id.effects3dSurfaceView);
384
    sv.getRenderer().showRegionAndCenter(mShowRegion,mShowCenter);
385
    }
386

    
387
///////////////////////////////////////////////////////////////////////////////////////////////////
388

    
389
  public void showRegion(View view)
390
    {
391
    CheckBox box = (CheckBox)view;
392
    mShowRegion = box.isChecked();
393

    
394
    Effects3DSurfaceView sv = findViewById(R.id.effects3dSurfaceView);
395
    sv.getRenderer().showRegionAndCenter(mShowRegion,mShowCenter);
396
    }
397

    
398
///////////////////////////////////////////////////////////////////////////////////////////////////
399

    
400
  public void showNormal(View view)
401
    {
402
    CheckBox box = (CheckBox)view;
403
    mShowNormal = box.isChecked();
404

    
405
    if ( mMesh!=null )
406
      {
407
      mMesh.setShowNormals(mShowNormal);
408
      }
409
    }
410

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

    
413
  public void triggerOIT(View view)
414
    {
415
    CheckBox box = (CheckBox)view;
416
    mUseOIT = box.isChecked();
417

    
418
    Effects3DSurfaceView sv = findViewById(R.id.effects3dSurfaceView);
419
    sv.getRenderer().useOIT(mUseOIT);
420
    }
421

    
422
///////////////////////////////////////////////////////////////////////////////////////////////////
423
// Overrides
424
///////////////////////////////////////////////////////////////////////////////////////////////////
425

    
426
  @Override
427
  protected void onPause()
428
    {
429
    GLSurfaceView mView = findViewById(R.id.effects3dSurfaceView);
430
    if( mView!=null ) mView.onPause();
431

    
432
    Distorted.onPause();
433
    super.onPause();
434
    }
435

    
436
///////////////////////////////////////////////////////////////////////////////////////////////////
437
    
438
  @Override
439
  protected void onResume()
440
    {
441
    super.onResume();
442
    GLSurfaceView mView = findViewById(R.id.effects3dSurfaceView);
443
    if( mView!=null ) mView.onResume();
444
    }
445

    
446
///////////////////////////////////////////////////////////////////////////////////////////////////
447
    
448
  @Override
449
  protected void onDestroy()
450
    {
451
    Distorted.onDestroy();
452
    super.onDestroy();
453
    }
454

    
455
  }
(2-2/7)