Project

General

Profile

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

examples / src / main / java / org / distorted / examples / generic / GenericActivity2.java @ f3ca895f

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.v7.app.AppCompatActivity;
30
import android.view.View;
31
import android.widget.CheckBox;
32

    
33
import org.distorted.examples.R;
34
import org.distorted.library.main.DistortedLibrary;
35
import org.distorted.library.main.DistortedEffects;
36
import org.distorted.library.main.DistortedTexture;
37
import org.distorted.library.mesh.MeshCubes;
38
import org.distorted.library.mesh.MeshRectangles;
39
import org.distorted.library.mesh.MeshBase;
40
import org.distorted.library.mesh.MeshQuad;
41
import org.distorted.library.mesh.MeshSphere;
42
import org.distorted.library.type.Static3D;
43
import org.distorted.library.type.Static4D;
44

    
45
import java.io.IOException;
46
import java.io.InputStream;
47

    
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49

    
50
public class GenericActivity2 extends AppCompatActivity
51
  {
52
  private DistortedTexture mTexture;
53
  private MeshBase mMesh;
54
  private Bitmap mBitmap;
55
  private DistortedEffects mEffects;
56
  private GenericViewPager mViewPager;
57

    
58
  private boolean mShowCenter, mShowRegion, mShowNormal, mUseOIT;
59

    
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61

    
62
  @Override
63
  protected void onCreate(Bundle savedState)
64
    {
65
    super.onCreate(savedState);
66

    
67
    setTheme(R.style.CustomActivityThemeNoActionBar);
68

    
69
    Bundle b = getIntent().getExtras();
70

    
71
    int objectType = b.getInt("type");
72
    int numCols    = b.getInt("cols");
73
    int numRows    = b.getInt("rows");
74
    int numSlic    = b.getInt("slices");
75
    int bitmapID   = b.getInt("bitmap");
76
    String str     = b.getString("string");
77

    
78
    mShowCenter = false;
79
    mShowRegion = false;
80
    mShowNormal = false;
81
    mUseOIT     = false;
82

    
83
    int maxsize = numCols > numRows ? (numCols>numSlic ? numCols:numSlic) : (numRows>numSlic ? numRows:numSlic) ;
84

    
85
    createBitmap(maxsize, bitmapID);
86

    
87
    switch(objectType)
88
      {
89
      case 0: if( bitmapID!=-1 )
90
                {
91
                mMesh = new MeshCubes(numCols, str, numSlic);
92
                }
93
              else
94
                {
95
                Static4D mapFB = new Static4D(0.0f,0.0f, (float)numCols/maxsize, (float)numRows/maxsize);
96
                Static4D mapLR = new Static4D(0.0f,0.0f, (float)numSlic/maxsize, (float)numRows/maxsize);
97
                Static4D mapTB = new Static4D(0.0f,0.0f, (float)numCols/maxsize, (float)numSlic/maxsize);
98

    
99
                mMesh = new MeshCubes(numCols, str, numSlic, mapFB, mapFB, mapLR, mapLR, mapTB, mapTB);
100
                }
101
              break;
102
      case 1: mMesh = new MeshRectangles(numCols,numRows);
103
              break;
104
      case 2: mMesh = new MeshSphere(numRows);
105
              break;
106
      case 3: mMesh = new MeshQuad();
107
              break;
108
      }
109

    
110
    mMesh.setShowNormals(mShowNormal);
111
    mTexture= new DistortedTexture(numCols,numRows);
112
    mEffects= new DistortedEffects();
113

    
114
    final View view = getLayoutInflater().inflate(R.layout.genericlayout, null);
115

    
116
    setContentView(view);
117

    
118
    mViewPager = new GenericViewPager(this,R.id.generic_viewpager);
119
    TabLayout tabLayout = findViewById(R.id.generic_sliding_tabs);
120
    tabLayout.setupWithViewPager(mViewPager.getPager());
121
    }
122

    
123
///////////////////////////////////////////////////////////////////////////////////////////////////
124

    
125
  private void createBitmap(int size, int bitmapID)
126
    {
127
    if( bitmapID!=-1)
128
      {
129
      InputStream is = getResources().openRawResource(bitmapID);
130

    
131
      try
132
        {
133
        mBitmap = BitmapFactory.decodeStream(is);
134
        }
135
      finally
136
        {
137
        try
138
          {
139
          is.close();
140
          }
141
        catch(IOException e) { }
142
        }
143
      }
144
    else
145
      {
146
      final int T = 64;
147
      final int S = T*size;
148

    
149
      Paint paint = new Paint();
150
      mBitmap = Bitmap.createBitmap(S,S, Bitmap.Config.ARGB_8888);
151
      Canvas canvas = new Canvas(mBitmap);
152

    
153
      paint.setAntiAlias(true);
154
      paint.setTextAlign(Paint.Align.CENTER);
155
      paint.setColor(0xff008800);
156
      paint.setStyle(Paint.Style.FILL);
157
      canvas.drawRect(0, 0, S, S, paint);
158
      paint.setColor(0xffffffff);
159

    
160
      for(int i=0; i<=size ; i++ )
161
        {
162
        canvas.drawRect( T*i-1, 0, T*i+1, S, paint);
163
        canvas.drawRect( 0, T*i-1, S, T*i+1, paint);
164
        }
165
      }
166
    }
167

    
168
///////////////////////////////////////////////////////////////////////////////////////////////////
169

    
170
  public DistortedTexture getTexture()
171
    {
172
    return mTexture;
173
    }
174

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

    
177
  public MeshBase getMesh()
178
    {
179
    return mMesh;
180
    }
181

    
182
///////////////////////////////////////////////////////////////////////////////////////////////////
183

    
184
  public Bitmap getBitmap()
185
    {
186
    return mBitmap;
187
    }
188

    
189
///////////////////////////////////////////////////////////////////////////////////////////////////
190

    
191
  public DistortedEffects getEffects()
192
    {
193
    return mEffects;
194
    }
195

    
196
///////////////////////////////////////////////////////////////////////////////////////////////////
197

    
198
  public GenericViewPager getPager()
199
    {
200
    return mViewPager;
201
    }
202

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

    
205
  public int getWidth()
206
    {
207
    return mTexture==null ? 0: mTexture.getWidth();
208
    }
209

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

    
212
  public int getHeight()
213
    {
214
    return mTexture==null ? 0: mTexture.getHeight();
215
    }
216

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

    
219
  public int getDepth()
220
    {
221
    return mTexture==null ? 0: mTexture.getDepth(mMesh);
222
    }
223

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

    
226
  public int getScreenWidth()
227
    {
228
    GenericRenderer r = ((GenericSurfaceView)findViewById(R.id.genericSurfaceView)).getRenderer();
229
    return r.getWidth();
230
    }
231

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

    
234
  void setCenter(Static3D center)
235
    {
236
    GenericSurfaceView view = findViewById(R.id.genericSurfaceView);
237
    view.getRenderer().setCenter( center.get0(), center.get1(), center.get2() );
238
    }
239

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

    
242
  void setRegion(Static4D region)
243
    {
244
    GenericSurfaceView view = findViewById(R.id.genericSurfaceView);
245
    view.getRenderer().setRegion( region.get0(), region.get1(), region.get2(), region.get3() );
246
    }
247

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

    
250
  public void showRegionAndCenter(View view)
251
    {
252
    CheckBox center = findViewById(R.id.genericCheckBoxCenter);
253
    mShowCenter = center.isChecked();
254
    CheckBox region = findViewById(R.id.genericCheckBoxRegion);
255
    mShowRegion = region.isChecked();
256

    
257
    GenericSurfaceView sv = findViewById(R.id.genericSurfaceView);
258

    
259
    mViewPager.showRegionAndCenter(mShowRegion, mShowCenter, sv);
260
    }
261

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

    
264
  public void showNormal(View view)
265
    {
266
    CheckBox box = (CheckBox)view;
267
    mShowNormal = box.isChecked();
268

    
269
    if ( mMesh!=null )
270
      {
271
      mMesh.setShowNormals(mShowNormal);
272
      }
273
    }
274

    
275
///////////////////////////////////////////////////////////////////////////////////////////////////
276

    
277
  public void triggerOIT(View view)
278
    {
279
    CheckBox box = (CheckBox)view;
280
    mUseOIT = box.isChecked();
281

    
282
    GenericSurfaceView sv = findViewById(R.id.genericSurfaceView);
283
    sv.getRenderer().useOIT(mUseOIT);
284
    }
285

    
286
///////////////////////////////////////////////////////////////////////////////////////////////////
287
// Overrides
288
///////////////////////////////////////////////////////////////////////////////////////////////////
289

    
290
  @Override
291
  protected void onPause()
292
    {
293
    GLSurfaceView mView = findViewById(R.id.genericSurfaceView);
294
    if( mView!=null ) mView.onPause();
295

    
296
    DistortedLibrary.onPause();
297
    super.onPause();
298
    }
299

    
300
///////////////////////////////////////////////////////////////////////////////////////////////////
301
    
302
  @Override
303
  protected void onResume()
304
    {
305
    super.onResume();
306
    GLSurfaceView mView = findViewById(R.id.genericSurfaceView);
307
    if( mView!=null ) mView.onResume();
308
    }
309

    
310
///////////////////////////////////////////////////////////////////////////////////////////////////
311
    
312
  @Override
313
  protected void onDestroy()
314
    {
315
    DistortedLibrary.onDestroy();
316
    super.onDestroy();
317
    }
318
  }
(2-2/7)