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.inflate;
|
21
|
|
22
|
import org.distorted.library.main.DistortedLibrary;
|
23
|
import org.distorted.examples.R;
|
24
|
import org.distorted.library.main.DistortedTexture;
|
25
|
import org.distorted.library.mesh.MeshBase;
|
26
|
import org.distorted.library.mesh.MeshCubes;
|
27
|
import org.distorted.library.mesh.MeshRectangles;
|
28
|
import org.distorted.library.mesh.MeshQuad;
|
29
|
import org.distorted.library.mesh.MeshSphere;
|
30
|
import org.distorted.library.type.Static4D;
|
31
|
|
32
|
import android.app.Activity;
|
33
|
import android.graphics.Bitmap;
|
34
|
import android.graphics.BitmapFactory;
|
35
|
import android.graphics.Canvas;
|
36
|
import android.graphics.Paint;
|
37
|
import android.opengl.GLSurfaceView;
|
38
|
import android.os.Bundle;
|
39
|
import android.view.View;
|
40
|
import android.widget.AdapterView;
|
41
|
import android.widget.ArrayAdapter;
|
42
|
import android.widget.SeekBar;
|
43
|
import android.widget.Spinner;
|
44
|
import android.widget.TextView;
|
45
|
|
46
|
import java.io.IOException;
|
47
|
import java.io.InputStream;
|
48
|
|
49
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
50
|
|
51
|
public class InflateActivity2 extends Activity implements SeekBar.OnSeekBarChangeListener, AdapterView.OnItemSelectedListener
|
52
|
{
|
53
|
private Bitmap mBitmap;
|
54
|
private TextView mTextLevel;
|
55
|
private DistortedTexture mTexture;
|
56
|
private MeshBase mMesh;
|
57
|
private int mNumCols, mNumRows, mNumSlic;
|
58
|
|
59
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
60
|
|
61
|
@Override
|
62
|
protected void onPause()
|
63
|
{
|
64
|
GLSurfaceView mView = this.findViewById(R.id.inflateSurfaceView);
|
65
|
if( mView!=null ) mView.onPause();
|
66
|
|
67
|
DistortedLibrary.onPause();
|
68
|
super.onPause();
|
69
|
}
|
70
|
|
71
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
72
|
|
73
|
@Override
|
74
|
protected void onResume()
|
75
|
{
|
76
|
super.onResume();
|
77
|
|
78
|
GLSurfaceView mView = this.findViewById(R.id.inflateSurfaceView);
|
79
|
if( mView!=null ) mView.onResume();
|
80
|
}
|
81
|
|
82
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
83
|
|
84
|
@Override
|
85
|
protected void onDestroy()
|
86
|
{
|
87
|
DistortedLibrary.onDestroy();
|
88
|
super.onDestroy();
|
89
|
}
|
90
|
|
91
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
92
|
|
93
|
@Override
|
94
|
protected void onCreate(Bundle savedState)
|
95
|
{
|
96
|
super.onCreate(savedState);
|
97
|
|
98
|
Bundle b = getIntent().getExtras();
|
99
|
|
100
|
String str = b.getString("string");
|
101
|
int objectType = b.getInt("type");
|
102
|
int bitmapID = b.getInt("bitmap");
|
103
|
mNumCols = b.getInt("cols");
|
104
|
mNumRows = b.getInt("rows");
|
105
|
mNumSlic = b.getInt("slices");
|
106
|
|
107
|
int maxsize = mNumCols > mNumRows ? (mNumCols>mNumSlic ? mNumCols:mNumSlic) : (mNumRows>mNumSlic ? mNumRows:mNumSlic) ;
|
108
|
|
109
|
createBitmap(maxsize,bitmapID);
|
110
|
|
111
|
switch(objectType)
|
112
|
{
|
113
|
case 0: if( bitmapID!=-1 )
|
114
|
{
|
115
|
mMesh = new MeshCubes(mNumCols, str, mNumSlic);
|
116
|
}
|
117
|
else
|
118
|
{
|
119
|
Static4D mapFB = new Static4D(0.0f,0.0f, (float)mNumCols/maxsize, (float)mNumRows/maxsize);
|
120
|
Static4D mapLR = new Static4D(0.0f,0.0f, (float)mNumSlic/maxsize, (float)mNumRows/maxsize);
|
121
|
Static4D mapTB = new Static4D(0.0f,0.0f, (float)mNumCols/maxsize, (float)mNumSlic/maxsize);
|
122
|
|
123
|
mMesh = new MeshCubes(mNumCols, str, mNumSlic, mapFB, mapFB, mapLR, mapLR, mapTB, mapTB);
|
124
|
}
|
125
|
mMesh.setStretch(mNumCols,mNumRows,mNumSlic);
|
126
|
break;
|
127
|
case 1: mMesh = new MeshRectangles(mNumCols,mNumRows);
|
128
|
mMesh.setStretch(mNumCols,mNumRows,0);
|
129
|
break;
|
130
|
case 2: mMesh = new MeshSphere(mNumRows);
|
131
|
mMesh.setStretch(mNumRows,mNumRows,mNumRows);
|
132
|
break;
|
133
|
case 3: mMesh = new MeshQuad();
|
134
|
mMesh.setStretch(1,1,0);
|
135
|
break;
|
136
|
}
|
137
|
|
138
|
mTexture = new DistortedTexture();
|
139
|
|
140
|
setContentView(R.layout.inflatelayout);
|
141
|
|
142
|
Spinner renderSpinner = findViewById(R.id.inflateSpinnerMode);
|
143
|
renderSpinner.setOnItemSelectedListener(this);
|
144
|
|
145
|
String[] objectBitmap = new String[] { "Render: Normal", "Render: OIT" };
|
146
|
|
147
|
ArrayAdapter<String> adapterBitmap = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, objectBitmap);
|
148
|
adapterBitmap.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
|
149
|
renderSpinner.setAdapter(adapterBitmap);
|
150
|
|
151
|
mTextLevel = findViewById(R.id.inflateInflateText);
|
152
|
|
153
|
SeekBar transparencyBar = findViewById(R.id.inflateTransparency);
|
154
|
transparencyBar.setOnSeekBarChangeListener(this);
|
155
|
transparencyBar.setProgress(50);
|
156
|
|
157
|
SeekBar inflateBar = findViewById(R.id.inflateInflateLevel);
|
158
|
inflateBar.setOnSeekBarChangeListener(this);
|
159
|
inflateBar.setProgress(50);
|
160
|
}
|
161
|
|
162
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
163
|
|
164
|
private void createBitmap(int size, int bitmapID)
|
165
|
{
|
166
|
if( bitmapID!=-1)
|
167
|
{
|
168
|
InputStream is = getResources().openRawResource(bitmapID);
|
169
|
|
170
|
try
|
171
|
{
|
172
|
mBitmap = BitmapFactory.decodeStream(is);
|
173
|
}
|
174
|
finally
|
175
|
{
|
176
|
try
|
177
|
{
|
178
|
is.close();
|
179
|
}
|
180
|
catch(IOException e) { }
|
181
|
}
|
182
|
}
|
183
|
else
|
184
|
{
|
185
|
final int T = 64;
|
186
|
final int S = T*size;
|
187
|
|
188
|
Paint paint = new Paint();
|
189
|
mBitmap = Bitmap.createBitmap(S,S, Bitmap.Config.ARGB_8888);
|
190
|
Canvas canvas = new Canvas(mBitmap);
|
191
|
|
192
|
paint.setAntiAlias(true);
|
193
|
paint.setTextAlign(Paint.Align.CENTER);
|
194
|
paint.setColor(0xff008800);
|
195
|
paint.setStyle(Paint.Style.FILL);
|
196
|
canvas.drawRect(0, 0, S, S, paint);
|
197
|
paint.setColor(0xffffffff);
|
198
|
|
199
|
for(int i=0; i<=size ; i++ )
|
200
|
{
|
201
|
canvas.drawRect( T*i-1, 0, T*i+1, S, paint);
|
202
|
canvas.drawRect( 0, T*i-1, S, T*i+1, paint);
|
203
|
}
|
204
|
}
|
205
|
}
|
206
|
|
207
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
208
|
|
209
|
public Bitmap getBitmap()
|
210
|
{
|
211
|
return mBitmap;
|
212
|
}
|
213
|
|
214
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
215
|
|
216
|
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
|
217
|
{
|
218
|
InflateSurfaceView v = this.findViewById(R.id.inflateSurfaceView);
|
219
|
InflateRenderer renderer = v.getRenderer();
|
220
|
|
221
|
switch(parent.getId())
|
222
|
{
|
223
|
case R.id.inflateSpinnerMode: renderer.setRenderModeToOIT(pos==1);
|
224
|
break;
|
225
|
}
|
226
|
}
|
227
|
|
228
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
229
|
|
230
|
public void onNothingSelected(AdapterView<?> parent)
|
231
|
{
|
232
|
}
|
233
|
|
234
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
235
|
|
236
|
public void onProgressChanged(SeekBar bar, int progress, boolean fromUser)
|
237
|
{
|
238
|
switch (bar.getId())
|
239
|
{
|
240
|
case R.id.inflateTransparency: InflateSurfaceView v1 = this.findViewById(R.id.inflateSurfaceView);
|
241
|
v1.getRenderer().setTransparency(progress);
|
242
|
break;
|
243
|
case R.id.inflateInflateLevel: InflateSurfaceView v2 = this.findViewById(R.id.inflateSurfaceView);
|
244
|
float level = v2.getRenderer().setLevel(progress);
|
245
|
mTextLevel.setText(getString(R.string.inflate_placeholder, level));
|
246
|
break;
|
247
|
}
|
248
|
}
|
249
|
|
250
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
251
|
|
252
|
public void onStartTrackingTouch(SeekBar bar) { }
|
253
|
|
254
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
255
|
|
256
|
public void onStopTrackingTouch(SeekBar bar) { }
|
257
|
|
258
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
259
|
|
260
|
public DistortedTexture getTexture()
|
261
|
{
|
262
|
return mTexture;
|
263
|
}
|
264
|
|
265
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
266
|
|
267
|
public MeshBase getMesh()
|
268
|
{
|
269
|
return mMesh;
|
270
|
}
|
271
|
|
272
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
273
|
|
274
|
public int getNumCols()
|
275
|
{
|
276
|
return mNumCols;
|
277
|
}
|
278
|
|
279
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
280
|
|
281
|
public int getNumRows()
|
282
|
{
|
283
|
return mNumRows;
|
284
|
}
|
285
|
|
286
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
287
|
|
288
|
public int getNumSlic()
|
289
|
{
|
290
|
return mNumSlic;
|
291
|
}
|
292
|
}
|