Project

General

Profile

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

examples / src / main / java / org / distorted / examples / effects3d / Effects3DActivity2.java @ 24624a1a

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, 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 r)
200
    {
201
    mRegionX = x;
202
    mRegionY =-y;
203
    mRegionR = r;
204

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

    
209
///////////////////////////////////////////////////////////////////////////////////////////////////
210

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

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

    
221
///////////////////////////////////////////////////////////////////////////////////////////////////
222

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

    
228
///////////////////////////////////////////////////////////////////////////////////////////////////
229

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

    
235
///////////////////////////////////////////////////////////////////////////////////////////////////
236

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

    
242
///////////////////////////////////////////////////////////////////////////////////////////////////
243

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

    
249
///////////////////////////////////////////////////////////////////////////////////////////////////
250

    
251
  public float getRegionR()
252
    {
253
    return mRegionR;
254
    }
255

    
256
///////////////////////////////////////////////////////////////////////////////////////////////////
257

    
258
  public int getWidth()
259
    {
260
    return mTexture==null ? 0: mTexture.getWidth();
261
    }
262

    
263
///////////////////////////////////////////////////////////////////////////////////////////////////
264

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

    
270
///////////////////////////////////////////////////////////////////////////////////////////////////
271

    
272
  public int getDepth()
273
    {
274
    return mTexture==null ? 0: mTexture.getDepth(mMesh);
275
    }
276

    
277
///////////////////////////////////////////////////////////////////////////////////////////////////
278

    
279
  public int getScreenWidth()
280
    {
281
    Effects3DRenderer r = ((Effects3DSurfaceView)findViewById(R.id.effects3dSurfaceView)).getRenderer();
282
    return r.getWidth();
283
    }
284

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

    
287
  void resetData()
288
    {
289
    mCenterX = 0.5f*getWidth();
290
    mCenterY = 0.5f*getHeight();
291
    mRegionX = 0;
292
    mRegionY = 0;
293
    mRegionR = getWidth()/2;
294

    
295
    Effects3DSurfaceView view = findViewById(R.id.effects3dSurfaceView);
296
    Effects3DRenderer renderer= view.getRenderer();
297

    
298
    renderer.setCenter( mCenterX, mCenterY, mCenterZ );
299
    renderer.setRegion( mRegionX, mRegionY, mRegionR );
300
    }
301

    
302
///////////////////////////////////////////////////////////////////////////////////////////////////
303

    
304
  void resetMatrixEffects()
305
    {
306
    Effects3DSurfaceView view = findViewById(R.id.effects3dSurfaceView);
307
    Effects3DRenderer renderer= view.getRenderer();
308

    
309
    renderer.resetMatrixEffects();
310
    }
311

    
312
///////////////////////////////////////////////////////////////////////////////////////////////////
313

    
314
  public DistortedEffects getEffects()
315
    {
316
    return mEffects;
317
    }
318

    
319
///////////////////////////////////////////////////////////////////////////////////////////////////
320

    
321
  public void newEffect(View v)
322
    {
323
    int pos = mViewPager.getCurrentItem();
324

    
325
    if( pos>=0 && pos<NUM_TABS ) mTab[pos].newEffect();
326
    }
327

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

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

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

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

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

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

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

    
348
  void setTab(int pos, Effects3DTab tab)
349
    {
350
    if( pos>=0 && pos<NUM_TABS ) mTab[pos] = tab;
351
    }
352

    
353
///////////////////////////////////////////////////////////////////////////////////////////////////
354

    
355
  boolean getShowCenter()
356
    {
357
    return mShowCenter;
358
    }
359

    
360
///////////////////////////////////////////////////////////////////////////////////////////////////
361

    
362
  boolean getShowRegion()
363
    {
364
    return mShowRegion;
365
    }
366

    
367
///////////////////////////////////////////////////////////////////////////////////////////////////
368

    
369
  public void showCenter(View view)
370
    {
371
    CheckBox box = (CheckBox)view;
372
    mShowCenter = box.isChecked();
373

    
374
    Effects3DSurfaceView sv = findViewById(R.id.effects3dSurfaceView);
375
    sv.getRenderer().showRegionAndCenter(mShowRegion,mShowCenter);
376
    }
377

    
378
///////////////////////////////////////////////////////////////////////////////////////////////////
379

    
380
  public void showRegion(View view)
381
    {
382
    CheckBox box = (CheckBox)view;
383
    mShowRegion = box.isChecked();
384

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

    
389
///////////////////////////////////////////////////////////////////////////////////////////////////
390

    
391
  public void showNormal(View view)
392
    {
393
    CheckBox box = (CheckBox)view;
394
    mShowNormal = box.isChecked();
395

    
396
    if ( mMesh!=null )
397
      {
398
      mMesh.setShowNormals(mShowNormal);
399
      }
400
    }
401

    
402
///////////////////////////////////////////////////////////////////////////////////////////////////
403

    
404
  public void triggerOIT(View view)
405
    {
406
    CheckBox box = (CheckBox)view;
407
    mUseOIT = box.isChecked();
408

    
409
    Effects3DSurfaceView sv = findViewById(R.id.effects3dSurfaceView);
410
    sv.getRenderer().useOIT(mUseOIT);
411
    }
412

    
413
///////////////////////////////////////////////////////////////////////////////////////////////////
414
// Overrides
415
///////////////////////////////////////////////////////////////////////////////////////////////////
416

    
417
  @Override
418
  protected void onPause()
419
    {
420
    GLSurfaceView mView = findViewById(R.id.effects3dSurfaceView);
421
    if( mView!=null ) mView.onPause();
422

    
423
    Distorted.onPause();
424
    super.onPause();
425
    }
426

    
427
///////////////////////////////////////////////////////////////////////////////////////////////////
428
    
429
  @Override
430
  protected void onResume()
431
    {
432
    super.onResume();
433
    GLSurfaceView mView = findViewById(R.id.effects3dSurfaceView);
434
    if( mView!=null ) mView.onResume();
435
    }
436

    
437
///////////////////////////////////////////////////////////////////////////////////////////////////
438
    
439
  @Override
440
  protected void onDestroy()
441
    {
442
    Distorted.onDestroy();
443
    super.onDestroy();
444
    }
445

    
446
  }
(2-2/7)