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