Project

General

Profile

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

examples / src / main / java / org / distorted / examples / effects3d / Effects3DActivity2.java @ 348dbeea

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.app.Activity;
23
import android.graphics.Bitmap;
24
import android.graphics.BitmapFactory;
25
import android.graphics.Canvas;
26
import android.graphics.Paint;
27
import android.opengl.GLSurfaceView;
28
import android.os.Bundle;
29
import android.support.design.widget.TabLayout;
30
import android.support.v4.view.ViewPager;
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 Activity
49
  {
50
  private int mNumCols;
51
  private int mNumRows;
52
  private int mNumSlic;
53
  private int mObjectType;
54
  private int mBitmapID;
55
  private String mString;
56

    
57
  private DistortedTexture mTexture;
58
  private MeshObject mMesh;
59
  private Bitmap mBitmap;
60

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

    
67
  private static boolean mShowCenter = true;
68
  private static boolean mShowRegion = true;
69
  private static boolean mShowNormal = true;
70
  private static boolean mUseOIT     = false;
71

    
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73

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

    
79
    Bundle b = getIntent().getExtras();
80

    
81
    mObjectType = b.getInt("type");
82
    mNumCols    = b.getInt("cols");
83
    mNumRows    = b.getInt("rows");
84
    mNumSlic    = b.getInt("slices");
85
    mBitmapID   = b.getInt("bitmap");
86
    mString     = b.getString("string");
87

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

    
91
    if( mObjectType==1 ) mMesh = new MeshFlat(mNumCols,mNumRows);
92
    else                 mMesh = new MeshCubes(mNumCols, mString, mNumSlic);
93

    
94
    mMesh.setShowNormals(mShowNormal);
95
    mTexture= new DistortedTexture(mNumCols,mNumRows);
96
    mEffects= new DistortedEffects();
97

    
98
    final View view = getLayoutInflater().inflate(R.layout.effects3dlayout, null);
99

    
100
    setContentView(view);
101

    
102
    mViewPager = findViewById(R.id.effects3d_viewpager);
103
    mViewPager.setOffscreenPageLimit(2);
104
    mPager = new Effects3DTabViewPager(this, getFragmentManager() );
105
    mViewPager.setAdapter(mPager);
106
    TabLayout tabLayout = findViewById(R.id.effects3d_sliding_tabs);
107
    tabLayout.setupWithViewPager(mViewPager);
108

    
109
    resetData();
110
    }
111

    
112
///////////////////////////////////////////////////////////////////////////////////////////////////
113

    
114
  public DistortedTexture getTexture()
115
    {
116
    return mTexture;
117
    }
118

    
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120

    
121
  public MeshObject getMesh()
122
    {
123
    return mMesh;
124
    }
125

    
126
///////////////////////////////////////////////////////////////////////////////////////////////////
127

    
128
  public Bitmap getBitmap()
129
    {
130
    if( mBitmap==null )
131
      {
132
      if( mBitmapID!=-1)
133
        {
134
        InputStream is = getResources().openRawResource(mBitmapID);
135

    
136
        try
137
          {
138
          mBitmap = BitmapFactory.decodeStream(is);
139
          }
140
        finally
141
          {
142
          try
143
            {
144
            is.close();
145
            }
146
          catch(IOException e) { }
147
          }
148
        }
149
      else
150
        {
151
        final int W = 64*mNumCols;
152
        final int H = 64*mNumRows;
153

    
154
        Paint paint = new Paint();
155
        mBitmap = Bitmap.createBitmap(W,H, Bitmap.Config.ARGB_8888);
156
        Canvas canvas = new Canvas(mBitmap);
157

    
158
        paint.setAntiAlias(true);
159
        paint.setTextAlign(Paint.Align.CENTER);
160
        paint.setColor(0xff008800);
161
        paint.setStyle(Paint.Style.FILL);
162
        canvas.drawRect(0, 0, W, H, paint);
163
        paint.setColor(0xffffffff);
164

    
165
        for(int i=0; i<=mNumCols ; i++ ) canvas.drawRect(W*i/mNumCols-1, 0, W*i/mNumCols + 1, H, paint);
166
        for(int i=0; i<=mNumRows ; i++ ) canvas.drawRect( 0, H*i/mNumRows-1, W,  H*i/mNumRows+1, paint);
167
        }
168
      }
169

    
170
    return mBitmap;
171
    }
172

    
173
///////////////////////////////////////////////////////////////////////////////////////////////////
174

    
175
  public void setRegion(float x, float y, float r)
176
    {
177
    mRegionX = x;
178
    mRegionY =-y;
179
    mRegionR = r;
180

    
181
    Effects3DSurfaceView view = findViewById(R.id.effects3dSurfaceView);
182
    view.getRenderer().setRegion(mRegionX, mRegionY, mRegionR);
183
    }
184

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

    
187
  public void setCenter(float x, float y, float z)
188
    {
189
    mCenterX = x;
190
    mCenterY = y;
191
    mCenterZ = z;
192

    
193
    Effects3DSurfaceView view = findViewById(R.id.effects3dSurfaceView);
194
    view.getRenderer().setCenter( mCenterX, mCenterY, mCenterZ );
195
    }
196

    
197
///////////////////////////////////////////////////////////////////////////////////////////////////
198

    
199
  public float getCenterX()
200
    {
201
    return mCenterX;
202
    }
203

    
204
///////////////////////////////////////////////////////////////////////////////////////////////////
205

    
206
  public float getCenterY()
207
    {
208
    return mCenterY;
209
    }
210

    
211
///////////////////////////////////////////////////////////////////////////////////////////////////
212

    
213
  public float getCenterZ()
214
    {
215
    return mCenterZ;
216
    }
217

    
218
///////////////////////////////////////////////////////////////////////////////////////////////////
219

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

    
225
///////////////////////////////////////////////////////////////////////////////////////////////////
226

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

    
232
///////////////////////////////////////////////////////////////////////////////////////////////////
233

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

    
239
///////////////////////////////////////////////////////////////////////////////////////////////////
240

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

    
246
///////////////////////////////////////////////////////////////////////////////////////////////////
247

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

    
253
///////////////////////////////////////////////////////////////////////////////////////////////////
254

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

    
260
///////////////////////////////////////////////////////////////////////////////////////////////////
261

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

    
267
///////////////////////////////////////////////////////////////////////////////////////////////////
268

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

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

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

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

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

    
295
///////////////////////////////////////////////////////////////////////////////////////////////////
296

    
297
  public void newEffect(View v)
298
    {
299
    mPager.newEffect(v,mViewPager.getCurrentItem());
300
    }
301

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

    
304
  public void removeAll(View v)
305
    {
306
    mPager.removeAll(v,mViewPager.getCurrentItem());
307
    }
308

    
309
///////////////////////////////////////////////////////////////////////////////////////////////////
310

    
311
  public void remove(View v)
312
    {
313
    mPager.remove(v,mViewPager.getCurrentItem());
314
    }
315

    
316
///////////////////////////////////////////////////////////////////////////////////////////////////
317

    
318
  boolean getShowCenter()
319
    {
320
    return mShowCenter;
321
    }
322

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

    
325
  boolean getShowRegion()
326
    {
327
    return mShowRegion;
328
    }
329

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

    
332
  public void showCenter(View view)
333
    {
334
    CheckBox box = (CheckBox)view;
335
    mShowCenter = box.isChecked();
336

    
337
    Effects3DSurfaceView sv = findViewById(R.id.effects3dSurfaceView);
338
    sv.getRenderer().showRegionAndCenter(mShowRegion,mShowCenter);
339
    }
340

    
341
///////////////////////////////////////////////////////////////////////////////////////////////////
342

    
343
  public void showRegion(View view)
344
    {
345
    CheckBox box = (CheckBox)view;
346
    mShowRegion = box.isChecked();
347

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

    
352
///////////////////////////////////////////////////////////////////////////////////////////////////
353

    
354
  public void showNormal(View view)
355
    {
356
    CheckBox box = (CheckBox)view;
357
    mShowNormal = box.isChecked();
358

    
359
    if ( mMesh!=null )
360
      {
361
      mMesh.setShowNormals(mShowNormal);
362
      }
363
    }
364

    
365
///////////////////////////////////////////////////////////////////////////////////////////////////
366

    
367
  public void triggerOIT(View view)
368
    {
369
    CheckBox box = (CheckBox)view;
370
    mUseOIT = box.isChecked();
371

    
372
    Effects3DSurfaceView sv = findViewById(R.id.effects3dSurfaceView);
373
    sv.getRenderer().useOIT(mUseOIT);
374
    }
375

    
376
///////////////////////////////////////////////////////////////////////////////////////////////////
377
// Overrides
378
///////////////////////////////////////////////////////////////////////////////////////////////////
379

    
380
  @Override
381
  protected void onPause()
382
    {
383
    GLSurfaceView mView = findViewById(R.id.effects3dSurfaceView);
384
    if( mView!=null ) mView.onPause();
385

    
386
    Distorted.onPause();
387
    super.onPause();
388
    }
389

    
390
///////////////////////////////////////////////////////////////////////////////////////////////////
391
    
392
  @Override
393
  protected void onResume()
394
    {
395
    super.onResume();
396
    GLSurfaceView mView = findViewById(R.id.effects3dSurfaceView);
397
    if( mView!=null ) mView.onResume();
398
    }
399

    
400
///////////////////////////////////////////////////////////////////////////////////////////////////
401
    
402
  @Override
403
  protected void onDestroy()
404
    {
405
    Distorted.onDestroy();
406
    super.onDestroy();
407
    }
408

    
409
  }
(2-2/7)