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
|
mEffects= new DistortedEffects(1);
|
84
|
|
85
|
int maxsize = numCols > numRows ? (numCols>numSlic ? numCols:numSlic) : (numRows>numSlic ? numRows:numSlic) ;
|
86
|
|
87
|
createBitmap(maxsize, bitmapID);
|
88
|
|
89
|
switch(objectType)
|
90
|
{
|
91
|
case 0: if( bitmapID!=-1 )
|
92
|
{
|
93
|
mMesh = new MeshCubes(numCols, str, numSlic);
|
94
|
}
|
95
|
else
|
96
|
{
|
97
|
Static4D mapFB = new Static4D(0.0f,0.0f, (float)numCols/maxsize, (float)numRows/maxsize);
|
98
|
Static4D mapLR = new Static4D(0.0f,0.0f, (float)numSlic/maxsize, (float)numRows/maxsize);
|
99
|
Static4D mapTB = new Static4D(0.0f,0.0f, (float)numCols/maxsize, (float)numSlic/maxsize);
|
100
|
|
101
|
mMesh = new MeshCubes(numCols, str, numSlic, mapFB, mapFB, mapLR, mapLR, mapTB, mapTB);
|
102
|
}
|
103
|
mEffects.setStretch(numCols,numRows,numSlic);
|
104
|
break;
|
105
|
case 1: mMesh = new MeshRectangles(numCols,numRows);
|
106
|
mEffects.setStretch(numCols,numRows,0);
|
107
|
break;
|
108
|
case 2: mMesh = new MeshSphere(numRows);
|
109
|
mEffects.setStretch(numRows,numRows,numRows);
|
110
|
break;
|
111
|
case 3: mMesh = new MeshQuad();
|
112
|
break;
|
113
|
}
|
114
|
|
115
|
mMesh.setShowNormals(mShowNormal);
|
116
|
mTexture= new DistortedTexture();
|
117
|
|
118
|
final View view = getLayoutInflater().inflate(R.layout.genericlayout, null);
|
119
|
|
120
|
setContentView(view);
|
121
|
|
122
|
mViewPager = new GenericViewPager(this,R.id.generic_viewpager);
|
123
|
TabLayout tabLayout = findViewById(R.id.generic_sliding_tabs);
|
124
|
tabLayout.setupWithViewPager(mViewPager.getPager());
|
125
|
}
|
126
|
|
127
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
128
|
|
129
|
private void createBitmap(int size, int bitmapID)
|
130
|
{
|
131
|
if( bitmapID!=-1)
|
132
|
{
|
133
|
InputStream is = getResources().openRawResource(bitmapID);
|
134
|
|
135
|
try
|
136
|
{
|
137
|
mBitmap = BitmapFactory.decodeStream(is);
|
138
|
}
|
139
|
finally
|
140
|
{
|
141
|
try
|
142
|
{
|
143
|
is.close();
|
144
|
}
|
145
|
catch(IOException e) { }
|
146
|
}
|
147
|
}
|
148
|
else
|
149
|
{
|
150
|
final int T = 64;
|
151
|
final int S = T*size;
|
152
|
|
153
|
Paint paint = new Paint();
|
154
|
mBitmap = Bitmap.createBitmap(S,S, Bitmap.Config.ARGB_8888);
|
155
|
Canvas canvas = new Canvas(mBitmap);
|
156
|
|
157
|
paint.setAntiAlias(true);
|
158
|
paint.setTextAlign(Paint.Align.CENTER);
|
159
|
paint.setColor(0xff008800);
|
160
|
paint.setStyle(Paint.Style.FILL);
|
161
|
canvas.drawRect(0, 0, S, S, paint);
|
162
|
paint.setColor(0xffffffff);
|
163
|
|
164
|
for(int i=0; i<=size ; i++ )
|
165
|
{
|
166
|
canvas.drawRect( T*i-1, 0, T*i+1, S, paint);
|
167
|
canvas.drawRect( 0, T*i-1, S, T*i+1, paint);
|
168
|
}
|
169
|
}
|
170
|
}
|
171
|
|
172
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
173
|
|
174
|
public DistortedTexture getTexture()
|
175
|
{
|
176
|
return mTexture;
|
177
|
}
|
178
|
|
179
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
180
|
|
181
|
public MeshBase getMesh()
|
182
|
{
|
183
|
return mMesh;
|
184
|
}
|
185
|
|
186
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
187
|
|
188
|
public Bitmap getBitmap()
|
189
|
{
|
190
|
return mBitmap;
|
191
|
}
|
192
|
|
193
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
194
|
|
195
|
public DistortedEffects getEffects()
|
196
|
{
|
197
|
return mEffects;
|
198
|
}
|
199
|
|
200
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
201
|
|
202
|
public GenericViewPager getPager()
|
203
|
{
|
204
|
return mViewPager;
|
205
|
}
|
206
|
|
207
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
208
|
|
209
|
public int getWidth()
|
210
|
{
|
211
|
return mEffects==null ? 0: mEffects.getStartchX();
|
212
|
}
|
213
|
|
214
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
215
|
|
216
|
public int getHeight()
|
217
|
{
|
218
|
return mEffects==null ? 0: mEffects.getStartchY();
|
219
|
}
|
220
|
|
221
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
222
|
|
223
|
public int getDepth()
|
224
|
{
|
225
|
return mEffects==null ? 0: mEffects.getStartchZ();
|
226
|
}
|
227
|
|
228
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
229
|
|
230
|
public int getScreenWidth()
|
231
|
{
|
232
|
GenericRenderer r = ((GenericSurfaceView)findViewById(R.id.genericSurfaceView)).getRenderer();
|
233
|
return r.getWidth();
|
234
|
}
|
235
|
|
236
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
237
|
|
238
|
void setCenter(Static3D center)
|
239
|
{
|
240
|
GenericSurfaceView view = findViewById(R.id.genericSurfaceView);
|
241
|
view.getRenderer().setCenter( center.get0(), center.get1(), center.get2() );
|
242
|
}
|
243
|
|
244
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
245
|
|
246
|
void setRegion(Static4D region)
|
247
|
{
|
248
|
GenericSurfaceView view = findViewById(R.id.genericSurfaceView);
|
249
|
view.getRenderer().setRegion( region.get0(), region.get1(), region.get2(), region.get3() );
|
250
|
}
|
251
|
|
252
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
253
|
|
254
|
public void showRegionAndCenter(View view)
|
255
|
{
|
256
|
CheckBox center = findViewById(R.id.genericCheckBoxCenter);
|
257
|
mShowCenter = center.isChecked();
|
258
|
CheckBox region = findViewById(R.id.genericCheckBoxRegion);
|
259
|
mShowRegion = region.isChecked();
|
260
|
|
261
|
GenericSurfaceView sv = findViewById(R.id.genericSurfaceView);
|
262
|
|
263
|
mViewPager.showRegionAndCenter(mShowRegion, mShowCenter, sv);
|
264
|
}
|
265
|
|
266
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
267
|
|
268
|
public void showNormal(View view)
|
269
|
{
|
270
|
CheckBox box = (CheckBox)view;
|
271
|
mShowNormal = box.isChecked();
|
272
|
|
273
|
if ( mMesh!=null )
|
274
|
{
|
275
|
mMesh.setShowNormals(mShowNormal);
|
276
|
}
|
277
|
}
|
278
|
|
279
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
280
|
|
281
|
public void triggerOIT(View view)
|
282
|
{
|
283
|
CheckBox box = (CheckBox)view;
|
284
|
mUseOIT = box.isChecked();
|
285
|
|
286
|
GenericSurfaceView sv = findViewById(R.id.genericSurfaceView);
|
287
|
sv.getRenderer().useOIT(mUseOIT);
|
288
|
}
|
289
|
|
290
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
291
|
// Overrides
|
292
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
293
|
|
294
|
@Override
|
295
|
protected void onPause()
|
296
|
{
|
297
|
GLSurfaceView mView = findViewById(R.id.genericSurfaceView);
|
298
|
if( mView!=null ) mView.onPause();
|
299
|
|
300
|
DistortedLibrary.onPause();
|
301
|
super.onPause();
|
302
|
}
|
303
|
|
304
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
305
|
|
306
|
@Override
|
307
|
protected void onResume()
|
308
|
{
|
309
|
super.onResume();
|
310
|
GLSurfaceView mView = findViewById(R.id.genericSurfaceView);
|
311
|
if( mView!=null ) mView.onResume();
|
312
|
}
|
313
|
|
314
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
315
|
|
316
|
@Override
|
317
|
protected void onDestroy()
|
318
|
{
|
319
|
DistortedLibrary.onDestroy();
|
320
|
super.onDestroy();
|
321
|
}
|
322
|
}
|