Project

General

Profile

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

examples / src / main / java / org / distorted / examples / effects3d / Effects3DActivity2.java @ 57d7fdba

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

    
43
import java.io.IOException;
44
import java.io.InputStream;
45

    
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47

    
48
public class Effects3DActivity2 extends AppCompatActivity
49
  {
50
  public static final int NUM_TABS = 4;
51

    
52
  private int mNumCols;
53
  private int mNumRows;
54
  private int mNumSlic;
55
  private int mObjectType;
56
  private int mBitmapID;
57
  private String mString;
58

    
59
  private DistortedTexture mTexture;
60
  private MeshBase mMesh;
61
  private Bitmap mBitmap;
62

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

    
69
  private boolean mShowCenter;
70
  private boolean mShowRegion;
71
  private boolean mShowNormal;
72
  private boolean mUseOIT    ;
73

    
74
  private Effects3DTab[] mTab;
75

    
76
///////////////////////////////////////////////////////////////////////////////////////////////////
77

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

    
83
    setTheme(R.style.CustomActivityThemeNoActionBar);
84

    
85
    mTab = new Effects3DTab[NUM_TABS];
86

    
87
    Bundle b = getIntent().getExtras();
88

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

    
96
    mShowCenter = true;
97
    mShowRegion = true;
98
    mShowNormal = true;
99
    mUseOIT     = false;
100

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

    
104
    if( mObjectType==1 ) mMesh = new MeshFlat(mNumCols,mNumRows);
105
    else                 mMesh = new MeshCubes(mNumCols, mString, mNumSlic);
106

    
107
    mMesh.setShowNormals(mShowNormal);
108
    mTexture= new DistortedTexture(mNumCols,mNumRows);
109
    mEffects= new DistortedEffects();
110

    
111
    final View view = getLayoutInflater().inflate(R.layout.effects3dlayout, null);
112

    
113
    setContentView(view);
114

    
115
    mViewPager = findViewById(R.id.effects3d_viewpager);
116
    mViewPager.setOffscreenPageLimit( (NUM_TABS+1)/2 );
117
    mPager = new Effects3DTabViewPager(this, getSupportFragmentManager() );
118
    mViewPager.setAdapter(mPager);
119
    TabLayout tabLayout = findViewById(R.id.effects3d_sliding_tabs);
120
    tabLayout.setupWithViewPager(mViewPager);
121

    
122
    resetData();
123
    }
124

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

    
127
  public DistortedTexture getTexture()
128
    {
129
    return mTexture;
130
    }
131

    
132
///////////////////////////////////////////////////////////////////////////////////////////////////
133

    
134
  public MeshBase getMesh()
135
    {
136
    return mMesh;
137
    }
138

    
139
///////////////////////////////////////////////////////////////////////////////////////////////////
140

    
141
  public Bitmap getBitmap()
142
    {
143
    if( mBitmap==null )
144
      {
145
      if( mBitmapID!=-1)
146
        {
147
        InputStream is = getResources().openRawResource(mBitmapID);
148

    
149
        try
150
          {
151
          mBitmap = BitmapFactory.decodeStream(is);
152
          }
153
        finally
154
          {
155
          try
156
            {
157
            is.close();
158
            }
159
          catch(IOException e) { }
160
          }
161
        }
162
      else
163
        {
164
        final int W = 64*mNumCols;
165
        final int H = 64*mNumRows;
166

    
167
        Paint paint = new Paint();
168
        mBitmap = Bitmap.createBitmap(W,H, Bitmap.Config.ARGB_8888);
169
        Canvas canvas = new Canvas(mBitmap);
170

    
171
        paint.setAntiAlias(true);
172
        paint.setTextAlign(Paint.Align.CENTER);
173
        paint.setColor(0xff008800);
174
        paint.setStyle(Paint.Style.FILL);
175
        canvas.drawRect(0, 0, W, H, paint);
176
        paint.setColor(0xffffffff);
177

    
178
        for(int i=0; i<=mNumCols ; i++ ) canvas.drawRect(W*i/mNumCols-1, 0, W*i/mNumCols + 1, H, paint);
179
        for(int i=0; i<=mNumRows ; i++ ) canvas.drawRect( 0, H*i/mNumRows-1, W,  H*i/mNumRows+1, paint);
180
        }
181
      }
182

    
183
    return mBitmap;
184
    }
185

    
186
///////////////////////////////////////////////////////////////////////////////////////////////////
187

    
188
  public void setRegion(float x, float y, float r)
189
    {
190
    mRegionX = x;
191
    mRegionY =-y;
192
    mRegionR = r;
193

    
194
    Effects3DSurfaceView view = findViewById(R.id.effects3dSurfaceView);
195
    view.getRenderer().setRegion(mRegionX, mRegionY, mRegionR);
196
    }
197

    
198
///////////////////////////////////////////////////////////////////////////////////////////////////
199

    
200
  public void setCenter(float x, float y, float z)
201
    {
202
    mCenterX = x;
203
    mCenterY = y;
204
    mCenterZ = z;
205

    
206
    Effects3DSurfaceView view = findViewById(R.id.effects3dSurfaceView);
207
    view.getRenderer().setCenter( mCenterX, mCenterY, mCenterZ );
208
    }
209

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

    
212
  public float getCenterX()
213
    {
214
    return mCenterX;
215
    }
216

    
217
///////////////////////////////////////////////////////////////////////////////////////////////////
218

    
219
  public float getCenterY()
220
    {
221
    return mCenterY;
222
    }
223

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

    
226
  public float getRegionX()
227
    {
228
    return mRegionX;
229
    }
230

    
231
///////////////////////////////////////////////////////////////////////////////////////////////////
232

    
233
  public float getRegionY()
234
    {
235
    return mRegionY;
236
    }
237

    
238
///////////////////////////////////////////////////////////////////////////////////////////////////
239

    
240
  public float getRegionR()
241
    {
242
    return mRegionR;
243
    }
244

    
245
///////////////////////////////////////////////////////////////////////////////////////////////////
246

    
247
  public int getWidth()
248
    {
249
    return mTexture==null ? 0: mTexture.getWidth();
250
    }
251

    
252
///////////////////////////////////////////////////////////////////////////////////////////////////
253

    
254
  public int getHeight()
255
    {
256
    return mTexture==null ? 0: mTexture.getHeight();
257
    }
258

    
259
///////////////////////////////////////////////////////////////////////////////////////////////////
260

    
261
  public int getDepth()
262
    {
263
    return mTexture==null ? 0: mTexture.getDepth(mMesh);
264
    }
265

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

    
268
  public int getScreenWidth()
269
    {
270
    Effects3DRenderer r = ((Effects3DSurfaceView)findViewById(R.id.effects3dSurfaceView)).getRenderer();
271
    return r.getWidth();
272
    }
273

    
274
///////////////////////////////////////////////////////////////////////////////////////////////////
275

    
276
  void resetData()
277
    {
278
    mCenterX = 0.5f*getWidth();
279
    mCenterY = 0.5f*getHeight();
280
    mRegionX = 0;
281
    mRegionY = 0;
282
    mRegionR = getWidth()/2;
283

    
284
    Effects3DSurfaceView view = findViewById(R.id.effects3dSurfaceView);
285
    Effects3DRenderer renderer= view.getRenderer();
286

    
287
    renderer.setCenter( mCenterX, mCenterY, mCenterZ );
288
    renderer.setRegion( mRegionX, mRegionY, mRegionR );
289
    }
290

    
291
///////////////////////////////////////////////////////////////////////////////////////////////////
292

    
293
  void resetMatrixEffects()
294
    {
295
    Effects3DSurfaceView view = findViewById(R.id.effects3dSurfaceView);
296
    Effects3DRenderer renderer= view.getRenderer();
297

    
298
    renderer.resetMatrixEffects();
299
    }
300

    
301
///////////////////////////////////////////////////////////////////////////////////////////////////
302

    
303
  public DistortedEffects getEffects()
304
    {
305
    return mEffects;
306
    }
307

    
308
///////////////////////////////////////////////////////////////////////////////////////////////////
309

    
310
  public void newEffect(View v)
311
    {
312
    int pos = mViewPager.getCurrentItem();
313

    
314
    if( pos>=0 && pos<NUM_TABS ) mTab[pos].newEffect();
315
    }
316

    
317
///////////////////////////////////////////////////////////////////////////////////////////////////
318

    
319
  public void removeAll(View v)
320
    {
321
    int pos = mViewPager.getCurrentItem();
322

    
323
    if( pos>=0 && pos<NUM_TABS ) mTab[pos].removeAll();
324
    }
325

    
326
///////////////////////////////////////////////////////////////////////////////////////////////////
327

    
328
  public void remove(View v)
329
    {
330
    int pos = mViewPager.getCurrentItem();
331

    
332
    if( pos>=0 && pos<NUM_TABS ) mTab[pos].remove(v);
333
    }
334

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

    
337
  void setTab(int pos, Effects3DTab tab)
338
    {
339
    if( pos>=0 && pos<NUM_TABS ) mTab[pos] = tab;
340
    }
341

    
342
///////////////////////////////////////////////////////////////////////////////////////////////////
343

    
344
  boolean getShowCenter()
345
    {
346
    return mShowCenter;
347
    }
348

    
349
///////////////////////////////////////////////////////////////////////////////////////////////////
350

    
351
  boolean getShowRegion()
352
    {
353
    return mShowRegion;
354
    }
355

    
356
///////////////////////////////////////////////////////////////////////////////////////////////////
357

    
358
  public void showCenter(View view)
359
    {
360
    CheckBox box = (CheckBox)view;
361
    mShowCenter = box.isChecked();
362

    
363
    Effects3DSurfaceView sv = findViewById(R.id.effects3dSurfaceView);
364
    sv.getRenderer().showRegionAndCenter(mShowRegion,mShowCenter);
365
    }
366

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

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

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

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

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

    
385
    if ( mMesh!=null )
386
      {
387
      mMesh.setShowNormals(mShowNormal);
388
      }
389
    }
390

    
391
///////////////////////////////////////////////////////////////////////////////////////////////////
392

    
393
  public void triggerOIT(View view)
394
    {
395
    CheckBox box = (CheckBox)view;
396
    mUseOIT = box.isChecked();
397

    
398
    Effects3DSurfaceView sv = findViewById(R.id.effects3dSurfaceView);
399
    sv.getRenderer().useOIT(mUseOIT);
400
    }
401

    
402
///////////////////////////////////////////////////////////////////////////////////////////////////
403
// Overrides
404
///////////////////////////////////////////////////////////////////////////////////////////////////
405

    
406
  @Override
407
  protected void onPause()
408
    {
409
    GLSurfaceView mView = findViewById(R.id.effects3dSurfaceView);
410
    if( mView!=null ) mView.onPause();
411

    
412
    Distorted.onPause();
413
    super.onPause();
414
    }
415

    
416
///////////////////////////////////////////////////////////////////////////////////////////////////
417
    
418
  @Override
419
  protected void onResume()
420
    {
421
    super.onResume();
422
    GLSurfaceView mView = findViewById(R.id.effects3dSurfaceView);
423
    if( mView!=null ) mView.onResume();
424
    }
425

    
426
///////////////////////////////////////////////////////////////////////////////////////////////////
427
    
428
  @Override
429
  protected void onDestroy()
430
    {
431
    Distorted.onDestroy();
432
    super.onDestroy();
433
    }
434

    
435
  }
(2-2/7)