Project

General

Profile

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

examples / src / main / java / org / distorted / examples / generic / Effects3DActivity2.java @ e52bd0c8

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.generic;
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.main.Distorted;
36
import org.distorted.library.main.DistortedEffects;
37
import org.distorted.library.main.DistortedTexture;
38
import org.distorted.library.mesh.MeshCubes;
39
import org.distorted.library.mesh.MeshFlat;
40
import org.distorted.library.mesh.MeshBase;
41
import org.distorted.library.mesh.MeshQuad;
42
import org.distorted.library.mesh.MeshSphere;
43
import org.distorted.library.type.Static4D;
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 DistortedTexture mTexture;
55
  private MeshBase mMesh;
56
  private Bitmap mBitmap;
57

    
58
  private float mCenterX, mCenterY, mCenterZ;
59
  private float mRegionX, mRegionY, mRegionZ, mRegionR;
60
  private DistortedEffects mEffects;
61
  private ViewPager mViewPager;
62

    
63
  private boolean mShowCenter;
64
  private boolean mShowRegion;
65
  private boolean mShowNormal;
66
  private boolean mUseOIT    ;
67

    
68
  private Effects3DTab[] mTab;
69

    
70
///////////////////////////////////////////////////////////////////////////////////////////////////
71

    
72
  @Override
73
  protected void onCreate(Bundle savedState)
74
    {
75
    super.onCreate(savedState);
76

    
77
    setTheme(R.style.CustomActivityThemeNoActionBar);
78

    
79
    mTab = new Effects3DTab[NUM_TABS];
80

    
81
    Bundle b = getIntent().getExtras();
82

    
83
    int objectType = b.getInt("type");
84
    int numCols    = b.getInt("cols");
85
    int numRows    = b.getInt("rows");
86
    int numSlic    = b.getInt("slices");
87
    int bitmapID   = b.getInt("bitmap");
88
    String str     = b.getString("string");
89

    
90
    mShowCenter = false;
91
    mShowRegion = false;
92
    mShowNormal = false;
93
    mUseOIT     = false;
94

    
95
    int maxsize = numCols > numRows ? (numCols>numSlic ? numCols:numSlic) : (numRows>numSlic ? numRows:numSlic) ;
96

    
97
    createBitmap(maxsize, bitmapID);
98

    
99
    switch(objectType)
100
      {
101
      case 0: if( bitmapID!=-1 )
102
                {
103
                mMesh = new MeshCubes(numCols, str, numSlic);
104
                }
105
              else
106
                {
107
                Static4D mapFB = new Static4D(0.0f,0.0f, (float)numCols/maxsize, (float)numRows/maxsize);
108
                Static4D mapLR = new Static4D(0.0f,0.0f, (float)numSlic/maxsize, (float)numRows/maxsize);
109
                Static4D mapTB = new Static4D(0.0f,0.0f, (float)numCols/maxsize, (float)numSlic/maxsize);
110

    
111
                mMesh = new MeshCubes(numCols, str, numSlic, mapFB, mapFB, mapLR, mapLR, mapTB, mapTB);
112
                }
113
              break;
114
      case 1: mMesh = new MeshFlat(numCols,numRows);
115
              break;
116
      case 2: mMesh = new MeshSphere(numRows);
117
              break;
118
      case 3: mMesh = new MeshQuad();
119
              break;
120
      }
121

    
122
    mMesh.setShowNormals(mShowNormal);
123
    mTexture= new DistortedTexture(numCols,numRows);
124
    mEffects= new DistortedEffects();
125

    
126
    final View view = getLayoutInflater().inflate(R.layout.effects3dlayout, null);
127

    
128
    setContentView(view);
129

    
130
    mViewPager = findViewById(R.id.effects3d_viewpager);
131
    mViewPager.setOffscreenPageLimit( NUM_TABS-1 );
132
    Effects3DTabViewPager pager = new Effects3DTabViewPager(this, getSupportFragmentManager() );
133
    mViewPager.setAdapter(pager);
134
    TabLayout tabLayout = findViewById(R.id.effects3d_sliding_tabs);
135
    tabLayout.setupWithViewPager(mViewPager);
136

    
137
    resetData();
138
    }
139

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

    
142
  private void createBitmap(int size, int bitmapID)
143
    {
144
    if( bitmapID!=-1)
145
      {
146
      InputStream is = getResources().openRawResource(bitmapID);
147

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

    
166
      Paint paint = new Paint();
167
      mBitmap = Bitmap.createBitmap(S,S, Bitmap.Config.ARGB_8888);
168
      Canvas canvas = new Canvas(mBitmap);
169

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

    
177
      for(int i=0; i<=size ; i++ )
178
        {
179
        canvas.drawRect( T*i-1, 0, T*i+1, S, paint);
180
        canvas.drawRect( 0, T*i-1, S, T*i+1, paint);
181
        }
182
      }
183
    }
184

    
185
///////////////////////////////////////////////////////////////////////////////////////////////////
186

    
187
  public DistortedTexture getTexture()
188
    {
189
    return mTexture;
190
    }
191

    
192
///////////////////////////////////////////////////////////////////////////////////////////////////
193

    
194
  public MeshBase getMesh()
195
    {
196
    return mMesh;
197
    }
198

    
199
///////////////////////////////////////////////////////////////////////////////////////////////////
200

    
201
  public Bitmap getBitmap()
202
    {
203
    return mBitmap;
204
    }
205

    
206
///////////////////////////////////////////////////////////////////////////////////////////////////
207

    
208
  public void setRegion(float x, float y, float z, float r)
209
    {
210
    mRegionX = x;
211
    mRegionY =-y;
212
    mRegionZ = z;
213
    mRegionR = r;
214

    
215
    Effects3DSurfaceView view = findViewById(R.id.effects3dSurfaceView);
216
    view.getRenderer().setRegion(mRegionX, mRegionY, mRegionZ, mRegionR);
217
    }
218

    
219
///////////////////////////////////////////////////////////////////////////////////////////////////
220

    
221
  public void setCenter(float x, float y, float z)
222
    {
223
    mCenterX = x;
224
    mCenterY = y;
225
    mCenterZ = z;
226

    
227
    Effects3DSurfaceView view = findViewById(R.id.effects3dSurfaceView);
228
    view.getRenderer().setCenter( mCenterX, mCenterY, mCenterZ );
229
    }
230

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

    
233
  public float getCenterX()
234
    {
235
    return mCenterX;
236
    }
237

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

    
240
  public float getCenterY()
241
    {
242
    return mCenterY;
243
    }
244

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

    
247
  public float getRegionX()
248
    {
249
    return mRegionX;
250
    }
251

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

    
254
  public float getRegionY()
255
    {
256
    return mRegionY;
257
    }
258

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

    
261
  public float getRegionZ()
262
    {
263
    return mRegionZ;
264
    }
265

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

    
268
  public float getRegionR()
269
    {
270
    return mRegionR;
271
    }
272

    
273
///////////////////////////////////////////////////////////////////////////////////////////////////
274

    
275
  public int getWidth()
276
    {
277
    return mTexture==null ? 0: mTexture.getWidth();
278
    }
279

    
280
///////////////////////////////////////////////////////////////////////////////////////////////////
281

    
282
  public int getHeight()
283
    {
284
    return mTexture==null ? 0: mTexture.getHeight();
285
    }
286

    
287
///////////////////////////////////////////////////////////////////////////////////////////////////
288

    
289
  public int getDepth()
290
    {
291
    return mTexture==null ? 0: mTexture.getDepth(mMesh);
292
    }
293

    
294
///////////////////////////////////////////////////////////////////////////////////////////////////
295

    
296
  public int getScreenWidth()
297
    {
298
    Effects3DRenderer r = ((Effects3DSurfaceView)findViewById(R.id.effects3dSurfaceView)).getRenderer();
299
    return r.getWidth();
300
    }
301

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

    
304
  void resetData()
305
    {
306
    mCenterX = 0.5f*getWidth();
307
    mCenterY = 0.5f*getHeight();
308
    mRegionX = 0;
309
    mRegionY = 0;
310
    mRegionZ = 0;
311
    mRegionR = getWidth()/2;
312

    
313
    Effects3DSurfaceView view = findViewById(R.id.effects3dSurfaceView);
314
    Effects3DRenderer renderer= view.getRenderer();
315

    
316
    renderer.setCenter( mCenterX, mCenterY, mCenterZ );
317
    renderer.setRegion( mRegionX, mRegionY, mRegionZ, mRegionR );
318
    }
319

    
320
///////////////////////////////////////////////////////////////////////////////////////////////////
321

    
322
  void resetMatrixEffects()
323
    {
324
    Effects3DSurfaceView view = findViewById(R.id.effects3dSurfaceView);
325
    Effects3DRenderer renderer= view.getRenderer();
326

    
327
    renderer.resetMatrixEffects();
328
    }
329

    
330
///////////////////////////////////////////////////////////////////////////////////////////////////
331

    
332
  public DistortedEffects getEffects()
333
    {
334
    return mEffects;
335
    }
336

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

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

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

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

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

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

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

    
357
  public void remove(View v)
358
    {
359
    int pos = mViewPager.getCurrentItem();
360

    
361
    if( pos>=0 && pos<NUM_TABS ) mTab[pos].remove(v);
362
    }
363

    
364
///////////////////////////////////////////////////////////////////////////////////////////////////
365

    
366
  void setTab(int pos, Effects3DTab tab)
367
    {
368
    if( pos>=0 && pos<NUM_TABS ) mTab[pos] = tab;
369
    }
370

    
371
///////////////////////////////////////////////////////////////////////////////////////////////////
372

    
373
  boolean getShowCenter()
374
    {
375
    return mShowCenter;
376
    }
377

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

    
380
  boolean getShowRegion()
381
    {
382
    return mShowRegion;
383
    }
384

    
385
///////////////////////////////////////////////////////////////////////////////////////////////////
386

    
387
  public void showCenter(View view)
388
    {
389
    CheckBox box = (CheckBox)view;
390
    mShowCenter = box.isChecked();
391

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

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

    
398
  public void showRegion(View view)
399
    {
400
    CheckBox box = (CheckBox)view;
401
    mShowRegion = box.isChecked();
402

    
403
    Effects3DSurfaceView sv = findViewById(R.id.effects3dSurfaceView);
404
    sv.getRenderer().showRegionAndCenter(mShowRegion,mShowCenter);
405
    }
406

    
407
///////////////////////////////////////////////////////////////////////////////////////////////////
408

    
409
  public void showNormal(View view)
410
    {
411
    CheckBox box = (CheckBox)view;
412
    mShowNormal = box.isChecked();
413

    
414
    if ( mMesh!=null )
415
      {
416
      mMesh.setShowNormals(mShowNormal);
417
      }
418
    }
419

    
420
///////////////////////////////////////////////////////////////////////////////////////////////////
421

    
422
  public void triggerOIT(View view)
423
    {
424
    CheckBox box = (CheckBox)view;
425
    mUseOIT = box.isChecked();
426

    
427
    Effects3DSurfaceView sv = findViewById(R.id.effects3dSurfaceView);
428
    sv.getRenderer().useOIT(mUseOIT);
429
    }
430

    
431
///////////////////////////////////////////////////////////////////////////////////////////////////
432
// Overrides
433
///////////////////////////////////////////////////////////////////////////////////////////////////
434

    
435
  @Override
436
  protected void onPause()
437
    {
438
    GLSurfaceView mView = findViewById(R.id.effects3dSurfaceView);
439
    if( mView!=null ) mView.onPause();
440

    
441
    Distorted.onPause();
442
    super.onPause();
443
    }
444

    
445
///////////////////////////////////////////////////////////////////////////////////////////////////
446
    
447
  @Override
448
  protected void onResume()
449
    {
450
    super.onResume();
451
    GLSurfaceView mView = findViewById(R.id.effects3dSurfaceView);
452
    if( mView!=null ) mView.onResume();
453
    }
454

    
455
///////////////////////////////////////////////////////////////////////////////////////////////////
456
    
457
  @Override
458
  protected void onDestroy()
459
    {
460
    Distorted.onDestroy();
461
    super.onDestroy();
462
    }
463

    
464
  }
(2-2/7)