Project

General

Profile

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

examples / src / main / java / org / distorted / examples / effects3d / Effects3DActivity2.java @ 06c636a5

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.main.MeshCubes;
40
import org.distorted.library.main.MeshFlat;
41
import org.distorted.library.main.MeshObject;
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 = 3;
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 MeshObject 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 static boolean mShowCenter = true;
70
  private static boolean mShowRegion = true;
71
  private static boolean mShowNormal = true;
72
  private static boolean mUseOIT     = false;
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.Theme_AppCompat_NoActionBar);
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
    DistortedEffects.setMax(EffectType.VERTEX ,20);    // those have to be called before
97
    DistortedEffects.setMax(EffectType.FRAGMENT,3);    // any DistortedEffect get created!
98

    
99
    if( mObjectType==1 ) mMesh = new MeshFlat(mNumCols,mNumRows);
100
    else                 mMesh = new MeshCubes(mNumCols, mString, mNumSlic);
101

    
102
    mMesh.setShowNormals(mShowNormal);
103
    mTexture= new DistortedTexture(mNumCols,mNumRows);
104
    mEffects= new DistortedEffects();
105

    
106
    final View view = getLayoutInflater().inflate(R.layout.effects3dlayout, null);
107

    
108
    setContentView(view);
109

    
110
    mViewPager = findViewById(R.id.effects3d_viewpager);
111
    mViewPager.setOffscreenPageLimit( (NUM_TABS+1)/2 );
112
    mPager = new Effects3DTabViewPager(this, getSupportFragmentManager() );
113
    mViewPager.setAdapter(mPager);
114
    TabLayout tabLayout = findViewById(R.id.effects3d_sliding_tabs);
115
    tabLayout.setupWithViewPager(mViewPager);
116

    
117
    resetData();
118
    }
119

    
120
///////////////////////////////////////////////////////////////////////////////////////////////////
121

    
122
  public DistortedTexture getTexture()
123
    {
124
    return mTexture;
125
    }
126

    
127
///////////////////////////////////////////////////////////////////////////////////////////////////
128

    
129
  public MeshObject getMesh()
130
    {
131
    return mMesh;
132
    }
133

    
134
///////////////////////////////////////////////////////////////////////////////////////////////////
135

    
136
  public Bitmap getBitmap()
137
    {
138
    if( mBitmap==null )
139
      {
140
      if( mBitmapID!=-1)
141
        {
142
        InputStream is = getResources().openRawResource(mBitmapID);
143

    
144
        try
145
          {
146
          mBitmap = BitmapFactory.decodeStream(is);
147
          }
148
        finally
149
          {
150
          try
151
            {
152
            is.close();
153
            }
154
          catch(IOException e) { }
155
          }
156
        }
157
      else
158
        {
159
        final int W = 64*mNumCols;
160
        final int H = 64*mNumRows;
161

    
162
        Paint paint = new Paint();
163
        mBitmap = Bitmap.createBitmap(W,H, Bitmap.Config.ARGB_8888);
164
        Canvas canvas = new Canvas(mBitmap);
165

    
166
        paint.setAntiAlias(true);
167
        paint.setTextAlign(Paint.Align.CENTER);
168
        paint.setColor(0xff008800);
169
        paint.setStyle(Paint.Style.FILL);
170
        canvas.drawRect(0, 0, W, H, paint);
171
        paint.setColor(0xffffffff);
172

    
173
        for(int i=0; i<=mNumCols ; i++ ) canvas.drawRect(W*i/mNumCols-1, 0, W*i/mNumCols + 1, H, paint);
174
        for(int i=0; i<=mNumRows ; i++ ) canvas.drawRect( 0, H*i/mNumRows-1, W,  H*i/mNumRows+1, paint);
175
        }
176
      }
177

    
178
    return mBitmap;
179
    }
180

    
181
///////////////////////////////////////////////////////////////////////////////////////////////////
182

    
183
  public void setRegion(float x, float y, float r)
184
    {
185
    mRegionX = x;
186
    mRegionY =-y;
187
    mRegionR = r;
188

    
189
    Effects3DSurfaceView view = findViewById(R.id.effects3dSurfaceView);
190
    view.getRenderer().setRegion(mRegionX, mRegionY, mRegionR);
191
    }
192

    
193
///////////////////////////////////////////////////////////////////////////////////////////////////
194

    
195
  public void setCenter(float x, float y, float z)
196
    {
197
    mCenterX = x;
198
    mCenterY = y;
199
    mCenterZ = z;
200

    
201
    Effects3DSurfaceView view = findViewById(R.id.effects3dSurfaceView);
202
    view.getRenderer().setCenter( mCenterX, mCenterY, mCenterZ );
203
    }
204

    
205
///////////////////////////////////////////////////////////////////////////////////////////////////
206

    
207
  public float getCenterX()
208
    {
209
    return mCenterX;
210
    }
211

    
212
///////////////////////////////////////////////////////////////////////////////////////////////////
213

    
214
  public float getCenterY()
215
    {
216
    return mCenterY;
217
    }
218

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

    
221
  public float getRegionX()
222
    {
223
    return mRegionX;
224
    }
225

    
226
///////////////////////////////////////////////////////////////////////////////////////////////////
227

    
228
  public float getRegionY()
229
    {
230
    return mRegionY;
231
    }
232

    
233
///////////////////////////////////////////////////////////////////////////////////////////////////
234

    
235
  public float getRegionR()
236
    {
237
    return mRegionR;
238
    }
239

    
240
///////////////////////////////////////////////////////////////////////////////////////////////////
241

    
242
  public int getWidth()
243
    {
244
    return mTexture==null ? 0: mTexture.getWidth();
245
    }
246

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

    
249
  public int getHeight()
250
    {
251
    return mTexture==null ? 0: mTexture.getHeight();
252
    }
253

    
254
///////////////////////////////////////////////////////////////////////////////////////////////////
255

    
256
  public int getDepth()
257
    {
258
    return mTexture==null ? 0: mTexture.getDepth(mMesh);
259
    }
260

    
261
///////////////////////////////////////////////////////////////////////////////////////////////////
262

    
263
  public int getScreenWidth()
264
    {
265
    return 0;
266
    }
267

    
268
///////////////////////////////////////////////////////////////////////////////////////////////////
269

    
270
  void resetData()
271
    {
272
    mCenterX = 0.5f*getWidth();
273
    mCenterY = 0.5f*getHeight();
274
    mRegionX = 0;
275
    mRegionY = 0;
276
    mRegionR = getWidth()/2;
277

    
278
    Effects3DSurfaceView view = findViewById(R.id.effects3dSurfaceView);
279
    Effects3DRenderer renderer= view.getRenderer();
280

    
281
    renderer.setCenter( mCenterX, mCenterY, mCenterZ );
282
    renderer.setRegion( mRegionX, mRegionY, mRegionR );
283
    renderer.mQuat1.set(0,0,0,1);
284
    renderer.mQuat2.set(0,0,0,1);
285
    }
286

    
287
///////////////////////////////////////////////////////////////////////////////////////////////////
288
// 'second screen' methods
289
///////////////////////////////////////////////////////////////////////////////////////////////////
290

    
291
  public DistortedEffects getEffects()
292
    {
293
    return mEffects;
294
    }
295

    
296
///////////////////////////////////////////////////////////////////////////////////////////////////
297

    
298
  public void newEffect(View v)
299
    {
300
    int pos = mViewPager.getCurrentItem();
301

    
302
    if( pos>=0 && pos<NUM_TABS ) mTab[pos].newEffect();
303
    }
304

    
305
///////////////////////////////////////////////////////////////////////////////////////////////////
306

    
307
  public void removeAll(View v)
308
    {
309
    int pos = mViewPager.getCurrentItem();
310

    
311
    if( pos>=0 && pos<NUM_TABS ) mTab[pos].removeAll();
312
    }
313

    
314
///////////////////////////////////////////////////////////////////////////////////////////////////
315

    
316
  public void remove(View v)
317
    {
318
    int pos = mViewPager.getCurrentItem();
319

    
320
    if( pos>=0 && pos<NUM_TABS ) mTab[pos].remove(v);
321
    }
322

    
323
///////////////////////////////////////////////////////////////////////////////////////////////////
324

    
325
  void setTab(int pos, Effects3DTab tab)
326
    {
327
    if( pos>=0 && pos<NUM_TABS ) mTab[pos] = tab;
328
    }
329

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

    
332
  boolean getShowCenter()
333
    {
334
    return mShowCenter;
335
    }
336

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

    
339
  boolean getShowRegion()
340
    {
341
    return mShowRegion;
342
    }
343

    
344
///////////////////////////////////////////////////////////////////////////////////////////////////
345

    
346
  public void showCenter(View view)
347
    {
348
    CheckBox box = (CheckBox)view;
349
    mShowCenter = box.isChecked();
350

    
351
    Effects3DSurfaceView sv = findViewById(R.id.effects3dSurfaceView);
352
    sv.getRenderer().showRegionAndCenter(mShowRegion,mShowCenter);
353
    }
354

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

    
357
  public void showRegion(View view)
358
    {
359
    CheckBox box = (CheckBox)view;
360
    mShowRegion = box.isChecked();
361

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

    
366
///////////////////////////////////////////////////////////////////////////////////////////////////
367

    
368
  public void showNormal(View view)
369
    {
370
    CheckBox box = (CheckBox)view;
371
    mShowNormal = box.isChecked();
372

    
373
    if ( mMesh!=null )
374
      {
375
      mMesh.setShowNormals(mShowNormal);
376
      }
377
    }
378

    
379
///////////////////////////////////////////////////////////////////////////////////////////////////
380

    
381
  public void triggerOIT(View view)
382
    {
383
    CheckBox box = (CheckBox)view;
384
    mUseOIT = box.isChecked();
385

    
386
    Effects3DSurfaceView sv = findViewById(R.id.effects3dSurfaceView);
387
    sv.getRenderer().useOIT(mUseOIT);
388
    }
389

    
390
///////////////////////////////////////////////////////////////////////////////////////////////////
391
// Overrides
392
///////////////////////////////////////////////////////////////////////////////////////////////////
393

    
394
  @Override
395
  protected void onPause()
396
    {
397
    GLSurfaceView mView = findViewById(R.id.effects3dSurfaceView);
398
    if( mView!=null ) mView.onPause();
399

    
400
    Distorted.onPause();
401
    super.onPause();
402
    }
403

    
404
///////////////////////////////////////////////////////////////////////////////////////////////////
405
    
406
  @Override
407
  protected void onResume()
408
    {
409
    super.onResume();
410
    GLSurfaceView mView = findViewById(R.id.effects3dSurfaceView);
411
    if( mView!=null ) mView.onResume();
412
    }
413

    
414
///////////////////////////////////////////////////////////////////////////////////////////////////
415
    
416
  @Override
417
  protected void onDestroy()
418
    {
419
    Distorted.onDestroy();
420
    super.onDestroy();
421
    }
422

    
423
  }
(2-2/7)