Project

General

Profile

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

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

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
    mTab = new Effects3DTab[NUM_TABS];
84

    
85
    Bundle b = getIntent().getExtras();
86

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

    
94
    DistortedEffects.setMax(EffectType.VERTEX ,20);    // those have to be called before
95
    DistortedEffects.setMax(EffectType.FRAGMENT,3);    // any DistortedEffect get created!
96

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

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

    
104
    final View view = getLayoutInflater().inflate(R.layout.effects3dlayout, null);
105

    
106
    setContentView(view);
107

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

    
115
    resetData();
116
    }
117

    
118
///////////////////////////////////////////////////////////////////////////////////////////////////
119

    
120
  public DistortedTexture getTexture()
121
    {
122
    return mTexture;
123
    }
124

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

    
127
  public MeshObject getMesh()
128
    {
129
    return mMesh;
130
    }
131

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

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

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

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

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

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

    
176
    return mBitmap;
177
    }
178

    
179
///////////////////////////////////////////////////////////////////////////////////////////////////
180

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

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

    
191
///////////////////////////////////////////////////////////////////////////////////////////////////
192

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

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

    
203
///////////////////////////////////////////////////////////////////////////////////////////////////
204

    
205
  public float getCenterX()
206
    {
207
    return mCenterX;
208
    }
209

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

    
212
  public float getCenterY()
213
    {
214
    return mCenterY;
215
    }
216

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

    
219
  public float getRegionX()
220
    {
221
    return mRegionX;
222
    }
223

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

    
226
  public float getRegionY()
227
    {
228
    return mRegionY;
229
    }
230

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

    
233
  public float getRegionR()
234
    {
235
    return mRegionR;
236
    }
237

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

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

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

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

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

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

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

    
261
  public int getScreenWidth()
262
    {
263
    return 0;
264
    }
265

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

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

    
276
    Effects3DSurfaceView view = findViewById(R.id.effects3dSurfaceView);
277
    Effects3DRenderer renderer= view.getRenderer();
278

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

    
285
///////////////////////////////////////////////////////////////////////////////////////////////////
286
// 'second screen' methods
287
///////////////////////////////////////////////////////////////////////////////////////////////////
288

    
289
  public DistortedEffects getEffects()
290
    {
291
    return mEffects;
292
    }
293

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

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

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

    
303
///////////////////////////////////////////////////////////////////////////////////////////////////
304

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

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

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

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

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

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

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

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

    
330
  boolean getShowCenter()
331
    {
332
    return mShowCenter;
333
    }
334

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

    
337
  boolean getShowRegion()
338
    {
339
    return mShowRegion;
340
    }
341

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

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

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

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

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

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

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

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

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

    
377
///////////////////////////////////////////////////////////////////////////////////////////////////
378

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

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

    
388
///////////////////////////////////////////////////////////////////////////////////////////////////
389
// Overrides
390
///////////////////////////////////////////////////////////////////////////////////////////////////
391

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

    
398
    Distorted.onPause();
399
    super.onPause();
400
    }
401

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

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

    
421
  }
(2-2/7)