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