Project

General

Profile

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

examples / src / main / java / org / distorted / examples / effects3d / Effects3DActivity2.java @ c2c45d0a

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
  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
    setTheme(R.style.Theme_AppCompat_NoActionBar);
80

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

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

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

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

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

    
100
    final View view = getLayoutInflater().inflate(R.layout.effects3dlayout, null);
101

    
102
    setContentView(view);
103

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

    
111
    resetData();
112
    }
113

    
114
///////////////////////////////////////////////////////////////////////////////////////////////////
115

    
116
  public DistortedTexture getTexture()
117
    {
118
    return mTexture;
119
    }
120

    
121
///////////////////////////////////////////////////////////////////////////////////////////////////
122

    
123
  public MeshObject getMesh()
124
    {
125
    return mMesh;
126
    }
127

    
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129

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

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

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

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

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

    
172
    return mBitmap;
173
    }
174

    
175
///////////////////////////////////////////////////////////////////////////////////////////////////
176

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

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

    
187
///////////////////////////////////////////////////////////////////////////////////////////////////
188

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

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

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

    
201
  public float getCenterX()
202
    {
203
    return mCenterX;
204
    }
205

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

    
208
  public float getCenterY()
209
    {
210
    return mCenterY;
211
    }
212

    
213
///////////////////////////////////////////////////////////////////////////////////////////////////
214

    
215
  public float getRegionX()
216
    {
217
    return mRegionX;
218
    }
219

    
220
///////////////////////////////////////////////////////////////////////////////////////////////////
221

    
222
  public float getRegionY()
223
    {
224
    return mRegionY;
225
    }
226

    
227
///////////////////////////////////////////////////////////////////////////////////////////////////
228

    
229
  public float getRegionR()
230
    {
231
    return mRegionR;
232
    }
233

    
234
///////////////////////////////////////////////////////////////////////////////////////////////////
235

    
236
  public int getWidth()
237
    {
238
    return mTexture==null ? 0: mTexture.getWidth();
239
    }
240

    
241
///////////////////////////////////////////////////////////////////////////////////////////////////
242

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

    
248
///////////////////////////////////////////////////////////////////////////////////////////////////
249

    
250
  public int getDepth()
251
    {
252
    return mTexture==null ? 0: mTexture.getDepth(mMesh);
253
    }
254

    
255
///////////////////////////////////////////////////////////////////////////////////////////////////
256

    
257
  public int getScreenWidth()
258
    {
259
    return 0;
260
    }
261

    
262
///////////////////////////////////////////////////////////////////////////////////////////////////
263

    
264
  void resetData()
265
    {
266
    mCenterX = 0.5f*getWidth();
267
    mCenterY = 0.5f*getHeight();
268
    mRegionX = 0;
269
    mRegionY = 0;
270
    mRegionR = getWidth()/2;
271

    
272
    Effects3DSurfaceView view = findViewById(R.id.effects3dSurfaceView);
273
    Effects3DRenderer renderer= view.getRenderer();
274

    
275
    renderer.setCenter( mCenterX, mCenterY, mCenterZ );
276
    renderer.setRegion( mRegionX, mRegionY, mRegionR );
277
    renderer.mQuat1.set(0,0,0,1);
278
    renderer.mQuat2.set(0,0,0,1);
279
    }
280

    
281
///////////////////////////////////////////////////////////////////////////////////////////////////
282
// 'second screen' methods
283
///////////////////////////////////////////////////////////////////////////////////////////////////
284

    
285
  public DistortedEffects getEffects()
286
    {
287
    return mEffects;
288
    }
289

    
290
///////////////////////////////////////////////////////////////////////////////////////////////////
291

    
292
  public void newEffect(View v)
293
    {
294
    mPager.newEffect(v,mViewPager.getCurrentItem());
295
    }
296

    
297
///////////////////////////////////////////////////////////////////////////////////////////////////
298

    
299
  public void removeAll(View v)
300
    {
301
    mPager.removeAll(v,mViewPager.getCurrentItem());
302
    }
303

    
304
///////////////////////////////////////////////////////////////////////////////////////////////////
305

    
306
  public void remove(View v)
307
    {
308
    mPager.remove(v,mViewPager.getCurrentItem());
309
    }
310

    
311
///////////////////////////////////////////////////////////////////////////////////////////////////
312

    
313
  boolean getShowCenter()
314
    {
315
    return mShowCenter;
316
    }
317

    
318
///////////////////////////////////////////////////////////////////////////////////////////////////
319

    
320
  boolean getShowRegion()
321
    {
322
    return mShowRegion;
323
    }
324

    
325
///////////////////////////////////////////////////////////////////////////////////////////////////
326

    
327
  public void showCenter(View view)
328
    {
329
    CheckBox box = (CheckBox)view;
330
    mShowCenter = box.isChecked();
331

    
332
    Effects3DSurfaceView sv = findViewById(R.id.effects3dSurfaceView);
333
    sv.getRenderer().showRegionAndCenter(mShowRegion,mShowCenter);
334
    }
335

    
336
///////////////////////////////////////////////////////////////////////////////////////////////////
337

    
338
  public void showRegion(View view)
339
    {
340
    CheckBox box = (CheckBox)view;
341
    mShowRegion = box.isChecked();
342

    
343
    Effects3DSurfaceView sv = findViewById(R.id.effects3dSurfaceView);
344
    sv.getRenderer().showRegionAndCenter(mShowRegion,mShowCenter);
345
    }
346

    
347
///////////////////////////////////////////////////////////////////////////////////////////////////
348

    
349
  public void showNormal(View view)
350
    {
351
    CheckBox box = (CheckBox)view;
352
    mShowNormal = box.isChecked();
353

    
354
    if ( mMesh!=null )
355
      {
356
      mMesh.setShowNormals(mShowNormal);
357
      }
358
    }
359

    
360
///////////////////////////////////////////////////////////////////////////////////////////////////
361

    
362
  public void triggerOIT(View view)
363
    {
364
    CheckBox box = (CheckBox)view;
365
    mUseOIT = box.isChecked();
366

    
367
    Effects3DSurfaceView sv = findViewById(R.id.effects3dSurfaceView);
368
    sv.getRenderer().useOIT(mUseOIT);
369
    }
370

    
371
///////////////////////////////////////////////////////////////////////////////////////////////////
372
// Overrides
373
///////////////////////////////////////////////////////////////////////////////////////////////////
374

    
375
  @Override
376
  protected void onPause()
377
    {
378
    GLSurfaceView mView = findViewById(R.id.effects3dSurfaceView);
379
    if( mView!=null ) mView.onPause();
380

    
381
    Distorted.onPause();
382
    super.onPause();
383
    }
384

    
385
///////////////////////////////////////////////////////////////////////////////////////////////////
386
    
387
  @Override
388
  protected void onResume()
389
    {
390
    super.onResume();
391
    GLSurfaceView mView = findViewById(R.id.effects3dSurfaceView);
392
    if( mView!=null ) mView.onResume();
393
    }
394

    
395
///////////////////////////////////////////////////////////////////////////////////////////////////
396
    
397
  @Override
398
  protected void onDestroy()
399
    {
400
    Distorted.onDestroy();
401
    super.onDestroy();
402
    }
403

    
404
  }
(2-2/7)