1 |
77a500b3
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2 |
|
|
// Copyright 2016 Leszek Koltunski //
|
3 |
|
|
// //
|
4 |
71c8884f
|
Leszek Koltunski
|
// This file is part of Distorted. //
|
5 |
77a500b3
|
Leszek Koltunski
|
// //
|
6 |
71c8884f
|
Leszek Koltunski
|
// Distorted is free software: you can redistribute it and/or modify //
|
7 |
77a500b3
|
Leszek Koltunski
|
// 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 |
71c8884f
|
Leszek Koltunski
|
// Distorted is distributed in the hope that it will be useful, //
|
12 |
77a500b3
|
Leszek Koltunski
|
// 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 |
71c8884f
|
Leszek Koltunski
|
// along with Distorted. If not, see <http://www.gnu.org/licenses/>. //
|
18 |
77a500b3
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
19 |
|
|
|
20 |
|
|
package org.distorted.examples.inflate;
|
21 |
|
|
|
22 |
e3900503
|
Leszek Koltunski
|
import org.distorted.library.main.DistortedLibrary;
|
23 |
64558e4e
|
Leszek Koltunski
|
import org.distorted.examples.R;
|
24 |
|
|
import org.distorted.library.main.DistortedTexture;
|
25 |
|
|
import org.distorted.library.mesh.MeshBase;
|
26 |
|
|
|
27 |
77a500b3
|
Leszek Koltunski
|
import android.app.Activity;
|
28 |
|
|
import android.graphics.Bitmap;
|
29 |
|
|
import android.graphics.BitmapFactory;
|
30 |
|
|
import android.graphics.Canvas;
|
31 |
|
|
import android.graphics.Paint;
|
32 |
|
|
import android.opengl.GLSurfaceView;
|
33 |
|
|
import android.os.Bundle;
|
34 |
64558e4e
|
Leszek Koltunski
|
import android.view.View;
|
35 |
|
|
import android.widget.AdapterView;
|
36 |
|
|
import android.widget.ArrayAdapter;
|
37 |
77a500b3
|
Leszek Koltunski
|
import android.widget.SeekBar;
|
38 |
64558e4e
|
Leszek Koltunski
|
import android.widget.Spinner;
|
39 |
77a500b3
|
Leszek Koltunski
|
import android.widget.TextView;
|
40 |
|
|
|
41 |
|
|
import java.io.IOException;
|
42 |
|
|
import java.io.InputStream;
|
43 |
|
|
|
44 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
45 |
|
|
|
46 |
64558e4e
|
Leszek Koltunski
|
public class InflateActivity2 extends Activity implements SeekBar.OnSeekBarChangeListener, AdapterView.OnItemSelectedListener
|
47 |
77a500b3
|
Leszek Koltunski
|
{
|
48 |
|
|
private Bitmap mBitmap;
|
49 |
64558e4e
|
Leszek Koltunski
|
private TextView mTextLevel;
|
50 |
|
|
private DistortedTexture mTexture;
|
51 |
|
|
private MeshBase mMesh;
|
52 |
e3c87517
|
Leszek Koltunski
|
private int mNumRows, mNumCols, mNumSlic;
|
53 |
77a500b3
|
Leszek Koltunski
|
|
54 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
55 |
|
|
|
56 |
|
|
@Override
|
57 |
|
|
protected void onPause()
|
58 |
|
|
{
|
59 |
|
|
super.onPause();
|
60 |
c0f27889
|
Leszek Koltunski
|
GLSurfaceView view = findViewById(R.id.inflateSurfaceView);
|
61 |
|
|
view.onPause();
|
62 |
|
|
DistortedLibrary.onPause();
|
63 |
77a500b3
|
Leszek Koltunski
|
}
|
64 |
|
|
|
65 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
66 |
|
|
|
67 |
|
|
@Override
|
68 |
|
|
protected void onResume()
|
69 |
|
|
{
|
70 |
|
|
super.onResume();
|
71 |
c0f27889
|
Leszek Koltunski
|
GLSurfaceView view = findViewById(R.id.inflateSurfaceView);
|
72 |
|
|
view.onResume();
|
73 |
77a500b3
|
Leszek Koltunski
|
}
|
74 |
|
|
|
75 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
76 |
|
|
|
77 |
|
|
@Override
|
78 |
|
|
protected void onDestroy()
|
79 |
|
|
{
|
80 |
e3900503
|
Leszek Koltunski
|
DistortedLibrary.onDestroy();
|
81 |
77a500b3
|
Leszek Koltunski
|
super.onDestroy();
|
82 |
|
|
}
|
83 |
64558e4e
|
Leszek Koltunski
|
|
84 |
77a500b3
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
85 |
|
|
|
86 |
|
|
@Override
|
87 |
|
|
protected void onCreate(Bundle savedState)
|
88 |
|
|
{
|
89 |
|
|
super.onCreate(savedState);
|
90 |
|
|
|
91 |
|
|
Bundle b = getIntent().getExtras();
|
92 |
|
|
|
93 |
|
|
String str = b.getString("string");
|
94 |
|
|
int objectType = b.getInt("type");
|
95 |
4db4e39b
|
Leszek Koltunski
|
int bitmapID = b.getInt("bitmap");
|
96 |
e3c87517
|
Leszek Koltunski
|
mNumCols = b.getInt("cols");
|
97 |
|
|
mNumRows = b.getInt("rows");
|
98 |
|
|
mNumSlic = b.getInt("slices");
|
99 |
4db4e39b
|
Leszek Koltunski
|
|
100 |
e3c87517
|
Leszek Koltunski
|
int maxsize = mNumCols > mNumRows ? (Math.max(mNumCols, mNumSlic)) : (Math.max(mNumRows, mNumSlic));
|
101 |
4db4e39b
|
Leszek Koltunski
|
createBitmap(maxsize,bitmapID);
|
102 |
e3c87517
|
Leszek Koltunski
|
mMesh = InflateMeshList.createMesh(objectType, mNumCols, mNumRows, mNumSlic, bitmapID, str);
|
103 |
|
|
|
104 |
687263cc
|
Leszek Koltunski
|
mTexture = new DistortedTexture();
|
105 |
77a500b3
|
Leszek Koltunski
|
|
106 |
|
|
setContentView(R.layout.inflatelayout);
|
107 |
|
|
|
108 |
83e0ca5e
|
Leszek Koltunski
|
Spinner renderSpinner = findViewById(R.id.inflateSpinnerMode);
|
109 |
64558e4e
|
Leszek Koltunski
|
renderSpinner.setOnItemSelectedListener(this);
|
110 |
77a500b3
|
Leszek Koltunski
|
|
111 |
64558e4e
|
Leszek Koltunski
|
String[] objectBitmap = new String[] { "Render: Normal", "Render: OIT" };
|
112 |
|
|
|
113 |
|
|
ArrayAdapter<String> adapterBitmap = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, objectBitmap);
|
114 |
|
|
adapterBitmap.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
|
115 |
|
|
renderSpinner.setAdapter(adapterBitmap);
|
116 |
|
|
|
117 |
|
|
mTextLevel = findViewById(R.id.inflateInflateText);
|
118 |
77a500b3
|
Leszek Koltunski
|
|
119 |
64558e4e
|
Leszek Koltunski
|
SeekBar transparencyBar = findViewById(R.id.inflateTransparency);
|
120 |
|
|
transparencyBar.setOnSeekBarChangeListener(this);
|
121 |
|
|
transparencyBar.setProgress(50);
|
122 |
|
|
|
123 |
|
|
SeekBar inflateBar = findViewById(R.id.inflateInflateLevel);
|
124 |
|
|
inflateBar.setOnSeekBarChangeListener(this);
|
125 |
|
|
inflateBar.setProgress(50);
|
126 |
|
|
}
|
127 |
77a500b3
|
Leszek Koltunski
|
|
128 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
129 |
|
|
|
130 |
4db4e39b
|
Leszek Koltunski
|
private void createBitmap(int size, int bitmapID)
|
131 |
77a500b3
|
Leszek Koltunski
|
{
|
132 |
4db4e39b
|
Leszek Koltunski
|
if( bitmapID!=-1)
|
133 |
77a500b3
|
Leszek Koltunski
|
{
|
134 |
e3c87517
|
Leszek Koltunski
|
try (InputStream is = getResources().openRawResource(bitmapID))
|
135 |
4db4e39b
|
Leszek Koltunski
|
{
|
136 |
|
|
mBitmap = BitmapFactory.decodeStream(is);
|
137 |
|
|
}
|
138 |
e3c87517
|
Leszek Koltunski
|
catch( IOException ex ) { android.util.Log.e("act", "failed to open resource "+bitmapID); }
|
139 |
4db4e39b
|
Leszek Koltunski
|
}
|
140 |
|
|
else
|
141 |
|
|
{
|
142 |
|
|
final int T = 64;
|
143 |
|
|
final int S = T*size;
|
144 |
|
|
|
145 |
|
|
Paint paint = new Paint();
|
146 |
|
|
mBitmap = Bitmap.createBitmap(S,S, Bitmap.Config.ARGB_8888);
|
147 |
|
|
Canvas canvas = new Canvas(mBitmap);
|
148 |
|
|
|
149 |
|
|
paint.setAntiAlias(true);
|
150 |
|
|
paint.setTextAlign(Paint.Align.CENTER);
|
151 |
|
|
paint.setColor(0xff008800);
|
152 |
|
|
paint.setStyle(Paint.Style.FILL);
|
153 |
|
|
canvas.drawRect(0, 0, S, S, paint);
|
154 |
|
|
paint.setColor(0xffffffff);
|
155 |
|
|
|
156 |
|
|
for(int i=0; i<=size ; i++ )
|
157 |
77a500b3
|
Leszek Koltunski
|
{
|
158 |
4db4e39b
|
Leszek Koltunski
|
canvas.drawRect( T*i-1, 0, T*i+1, S, paint);
|
159 |
|
|
canvas.drawRect( 0, T*i-1, S, T*i+1, paint);
|
160 |
77a500b3
|
Leszek Koltunski
|
}
|
161 |
|
|
}
|
162 |
4db4e39b
|
Leszek Koltunski
|
}
|
163 |
77a500b3
|
Leszek Koltunski
|
|
164 |
e3c87517
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
165 |
|
|
|
166 |
|
|
int getRows()
|
167 |
|
|
{
|
168 |
|
|
return mNumRows;
|
169 |
|
|
}
|
170 |
|
|
|
171 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
172 |
|
|
|
173 |
|
|
int getCols()
|
174 |
|
|
{
|
175 |
|
|
return mNumCols;
|
176 |
|
|
}
|
177 |
|
|
|
178 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
179 |
|
|
|
180 |
|
|
int getSlic()
|
181 |
|
|
{
|
182 |
|
|
return mNumSlic;
|
183 |
|
|
}
|
184 |
|
|
|
185 |
4db4e39b
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
186 |
|
|
|
187 |
|
|
public Bitmap getBitmap()
|
188 |
|
|
{
|
189 |
77a500b3
|
Leszek Koltunski
|
return mBitmap;
|
190 |
|
|
}
|
191 |
|
|
|
192 |
64558e4e
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
193 |
|
|
|
194 |
|
|
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
|
195 |
|
|
{
|
196 |
|
|
InflateSurfaceView v = this.findViewById(R.id.inflateSurfaceView);
|
197 |
|
|
InflateRenderer renderer = v.getRenderer();
|
198 |
|
|
|
199 |
e3c87517
|
Leszek Koltunski
|
if( parent.getId()==R.id.inflateSpinnerMode)
|
200 |
64558e4e
|
Leszek Koltunski
|
{
|
201 |
e3c87517
|
Leszek Koltunski
|
renderer.setRenderModeToOIT(pos==1);
|
202 |
64558e4e
|
Leszek Koltunski
|
}
|
203 |
|
|
}
|
204 |
|
|
|
205 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
206 |
|
|
|
207 |
|
|
public void onNothingSelected(AdapterView<?> parent)
|
208 |
|
|
{
|
209 |
|
|
}
|
210 |
|
|
|
211 |
77a500b3
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
212 |
|
|
|
213 |
|
|
public void onProgressChanged(SeekBar bar, int progress, boolean fromUser)
|
214 |
|
|
{
|
215 |
|
|
switch (bar.getId())
|
216 |
|
|
{
|
217 |
64558e4e
|
Leszek Koltunski
|
case R.id.inflateTransparency: InflateSurfaceView v1 = this.findViewById(R.id.inflateSurfaceView);
|
218 |
|
|
v1.getRenderer().setTransparency(progress);
|
219 |
|
|
break;
|
220 |
|
|
case R.id.inflateInflateLevel: InflateSurfaceView v2 = this.findViewById(R.id.inflateSurfaceView);
|
221 |
|
|
float level = v2.getRenderer().setLevel(progress);
|
222 |
|
|
mTextLevel.setText(getString(R.string.inflate_placeholder, level));
|
223 |
|
|
break;
|
224 |
77a500b3
|
Leszek Koltunski
|
}
|
225 |
|
|
}
|
226 |
|
|
|
227 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
228 |
|
|
|
229 |
|
|
public void onStartTrackingTouch(SeekBar bar) { }
|
230 |
|
|
|
231 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
232 |
|
|
|
233 |
|
|
public void onStopTrackingTouch(SeekBar bar) { }
|
234 |
|
|
|
235 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
236 |
|
|
|
237 |
|
|
public DistortedTexture getTexture()
|
238 |
|
|
{
|
239 |
|
|
return mTexture;
|
240 |
|
|
}
|
241 |
|
|
|
242 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
243 |
|
|
|
244 |
|
|
public MeshBase getMesh()
|
245 |
|
|
{
|
246 |
|
|
return mMesh;
|
247 |
|
|
}
|
248 |
|
|
}
|