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 com.google.android.material.tabs.TabLayout;
|
29
|
import androidx.appcompat.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.MeshBase;
|
38
|
import org.distorted.library.type.Static4D;
|
39
|
|
40
|
import java.io.IOException;
|
41
|
import java.io.InputStream;
|
42
|
|
43
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
44
|
|
45
|
public class GenericActivity2 extends AppCompatActivity
|
46
|
{
|
47
|
private DistortedTexture mTexture;
|
48
|
private MeshBase mMesh;
|
49
|
private Bitmap mBitmap;
|
50
|
private DistortedEffects mEffects;
|
51
|
private GenericViewPager mViewPager;
|
52
|
|
53
|
private boolean mShowCenter, mShowRegion, mShowNormal, mUseOIT;
|
54
|
private float mScaleX, mScaleY, mScaleZ;
|
55
|
|
56
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
57
|
|
58
|
@Override
|
59
|
protected void onCreate(Bundle savedState)
|
60
|
{
|
61
|
super.onCreate(savedState);
|
62
|
|
63
|
setTheme(R.style.CustomActivityThemeNoActionBar);
|
64
|
|
65
|
Bundle b = getIntent().getExtras();
|
66
|
|
67
|
int objectType = b.getInt("type");
|
68
|
int numCols = b.getInt("cols");
|
69
|
int numRows = b.getInt("rows");
|
70
|
int numSlic = b.getInt("slices");
|
71
|
int bitmapID = b.getInt("bitmap");
|
72
|
String str = b.getString("string");
|
73
|
|
74
|
mShowCenter = false;
|
75
|
mShowRegion = false;
|
76
|
mShowNormal = false;
|
77
|
mUseOIT = false;
|
78
|
|
79
|
mScaleX = numCols;
|
80
|
mScaleY = numRows;
|
81
|
mScaleZ = numSlic;
|
82
|
|
83
|
int maxsize = numCols > numRows ? (Math.max(numCols, numSlic)) : (Math.max(numRows, numSlic));
|
84
|
createBitmap(maxsize, bitmapID);
|
85
|
mMesh = GenericMeshList.createMesh(objectType, numCols, numRows, numSlic, bitmapID, str);
|
86
|
|
87
|
mMesh.setShowNormals(mShowNormal);
|
88
|
mTexture= new DistortedTexture();
|
89
|
mEffects= new DistortedEffects();
|
90
|
|
91
|
final View view = getLayoutInflater().inflate(R.layout.genericlayout, null);
|
92
|
|
93
|
setContentView(view);
|
94
|
|
95
|
mViewPager = new GenericViewPager(this,R.id.generic_viewpager);
|
96
|
TabLayout tabLayout = findViewById(R.id.generic_sliding_tabs);
|
97
|
tabLayout.setupWithViewPager(mViewPager.getPager());
|
98
|
}
|
99
|
|
100
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
101
|
|
102
|
private void createBitmap(int size, int bitmapID)
|
103
|
{
|
104
|
if( bitmapID!=-1)
|
105
|
{
|
106
|
InputStream is = getResources().openRawResource(bitmapID);
|
107
|
|
108
|
try
|
109
|
{
|
110
|
mBitmap = BitmapFactory.decodeStream(is);
|
111
|
}
|
112
|
finally
|
113
|
{
|
114
|
try
|
115
|
{
|
116
|
is.close();
|
117
|
}
|
118
|
catch(IOException ignored) { }
|
119
|
}
|
120
|
}
|
121
|
else
|
122
|
{
|
123
|
final int T = 64;
|
124
|
final int S = T*size;
|
125
|
|
126
|
Paint paint = new Paint();
|
127
|
mBitmap = Bitmap.createBitmap(S,S, Bitmap.Config.ARGB_8888);
|
128
|
Canvas canvas = new Canvas(mBitmap);
|
129
|
|
130
|
paint.setAntiAlias(true);
|
131
|
paint.setTextAlign(Paint.Align.CENTER);
|
132
|
paint.setColor(0xff008800);
|
133
|
paint.setStyle(Paint.Style.FILL);
|
134
|
canvas.drawRect(0, 0, S, S, paint);
|
135
|
paint.setColor(0xffffffff);
|
136
|
|
137
|
for(int i=0; i<=size ; i++ )
|
138
|
{
|
139
|
canvas.drawRect( T*i-1, 0, T*i+1, S, paint);
|
140
|
canvas.drawRect( 0, T*i-1, S, T*i+1, paint);
|
141
|
}
|
142
|
}
|
143
|
}
|
144
|
|
145
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
146
|
|
147
|
public DistortedTexture getTexture()
|
148
|
{
|
149
|
return mTexture;
|
150
|
}
|
151
|
|
152
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
153
|
|
154
|
public MeshBase getMesh()
|
155
|
{
|
156
|
return mMesh;
|
157
|
}
|
158
|
|
159
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
160
|
|
161
|
public Bitmap getBitmap()
|
162
|
{
|
163
|
return mBitmap;
|
164
|
}
|
165
|
|
166
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
167
|
|
168
|
public DistortedEffects getEffects()
|
169
|
{
|
170
|
return mEffects;
|
171
|
}
|
172
|
|
173
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
174
|
|
175
|
public GenericViewPager getPager()
|
176
|
{
|
177
|
return mViewPager;
|
178
|
}
|
179
|
|
180
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
181
|
|
182
|
public float getWidth()
|
183
|
{
|
184
|
return mScaleX;
|
185
|
}
|
186
|
|
187
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
188
|
|
189
|
public float getHeight()
|
190
|
{
|
191
|
return mScaleY;
|
192
|
}
|
193
|
|
194
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
195
|
|
196
|
public float getDepth()
|
197
|
{
|
198
|
return mScaleZ;
|
199
|
}
|
200
|
|
201
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
202
|
|
203
|
public int getScreenWidth()
|
204
|
{
|
205
|
GenericSurfaceView view = findViewById(R.id.genericSurfaceView);
|
206
|
GenericRenderer renderer = view.getRenderer();
|
207
|
return renderer.getWidth();
|
208
|
}
|
209
|
|
210
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
211
|
|
212
|
public float getScaleFactor()
|
213
|
{
|
214
|
GenericSurfaceView view = findViewById(R.id.genericSurfaceView);
|
215
|
GenericRenderer renderer = view.getRenderer();
|
216
|
return renderer.getScaleFactor();
|
217
|
}
|
218
|
|
219
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
220
|
|
221
|
void setCenter(float x, float y, float z)
|
222
|
{
|
223
|
GenericSurfaceView view = findViewById(R.id.genericSurfaceView);
|
224
|
GenericRenderer renderer = view.getRenderer();
|
225
|
renderer.setCenter(x,y,z);
|
226
|
}
|
227
|
|
228
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
229
|
|
230
|
void setRegion(Static4D region)
|
231
|
{
|
232
|
GenericSurfaceView view = findViewById(R.id.genericSurfaceView);
|
233
|
GenericRenderer renderer = view.getRenderer();
|
234
|
renderer.setRegion( region.get0(), region.get1(), region.get2(), region.get3() );
|
235
|
}
|
236
|
|
237
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
238
|
|
239
|
public void showRegionAndCenter(View view)
|
240
|
{
|
241
|
CheckBox center = findViewById(R.id.genericCheckBoxCenter);
|
242
|
mShowCenter = center.isChecked();
|
243
|
CheckBox region = findViewById(R.id.genericCheckBoxRegion);
|
244
|
mShowRegion = region.isChecked();
|
245
|
|
246
|
GenericSurfaceView sv = findViewById(R.id.genericSurfaceView);
|
247
|
|
248
|
mViewPager.showRegionAndCenter(mShowRegion, mShowCenter, sv);
|
249
|
}
|
250
|
|
251
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
252
|
|
253
|
public void showNormal(View view)
|
254
|
{
|
255
|
CheckBox box = (CheckBox)view;
|
256
|
mShowNormal = box.isChecked();
|
257
|
|
258
|
if ( mMesh!=null )
|
259
|
{
|
260
|
mMesh.setShowNormals(mShowNormal);
|
261
|
}
|
262
|
}
|
263
|
|
264
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
265
|
|
266
|
public void triggerOIT(View view)
|
267
|
{
|
268
|
CheckBox box = (CheckBox)view;
|
269
|
mUseOIT = box.isChecked();
|
270
|
|
271
|
GenericSurfaceView sv = findViewById(R.id.genericSurfaceView);
|
272
|
sv.getRenderer().useOIT(mUseOIT);
|
273
|
}
|
274
|
|
275
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
276
|
// Overrides
|
277
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
278
|
|
279
|
@Override
|
280
|
protected void onPause()
|
281
|
{
|
282
|
super.onPause();
|
283
|
GLSurfaceView view = findViewById(R.id.genericSurfaceView);
|
284
|
view.onPause();
|
285
|
DistortedLibrary.onPause();
|
286
|
}
|
287
|
|
288
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
289
|
|
290
|
@Override
|
291
|
protected void onResume()
|
292
|
{
|
293
|
super.onResume();
|
294
|
GLSurfaceView view = findViewById(R.id.genericSurfaceView);
|
295
|
view.onResume();
|
296
|
}
|
297
|
|
298
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
299
|
|
300
|
@Override
|
301
|
protected void onDestroy()
|
302
|
{
|
303
|
DistortedLibrary.onDestroy();
|
304
|
super.onDestroy();
|
305
|
}
|
306
|
}
|