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 |
|
|
import org.distorted.library.mesh.MeshCubes;
|
27 |
f3ca895f
|
Leszek Koltunski
|
import org.distorted.library.mesh.MeshRectangles;
|
28 |
64558e4e
|
Leszek Koltunski
|
import org.distorted.library.mesh.MeshQuad;
|
29 |
|
|
import org.distorted.library.mesh.MeshSphere;
|
30 |
4db4e39b
|
Leszek Koltunski
|
import org.distorted.library.type.Static4D;
|
31 |
64558e4e
|
Leszek Koltunski
|
|
32 |
77a500b3
|
Leszek Koltunski
|
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 |
64558e4e
|
Leszek Koltunski
|
import android.view.View;
|
40 |
|
|
import android.widget.AdapterView;
|
41 |
|
|
import android.widget.ArrayAdapter;
|
42 |
77a500b3
|
Leszek Koltunski
|
import android.widget.SeekBar;
|
43 |
64558e4e
|
Leszek Koltunski
|
import android.widget.Spinner;
|
44 |
77a500b3
|
Leszek Koltunski
|
import android.widget.TextView;
|
45 |
|
|
|
46 |
|
|
import java.io.IOException;
|
47 |
|
|
import java.io.InputStream;
|
48 |
|
|
|
49 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
50 |
|
|
|
51 |
64558e4e
|
Leszek Koltunski
|
public class InflateActivity2 extends Activity implements SeekBar.OnSeekBarChangeListener, AdapterView.OnItemSelectedListener
|
52 |
77a500b3
|
Leszek Koltunski
|
{
|
53 |
|
|
private Bitmap mBitmap;
|
54 |
64558e4e
|
Leszek Koltunski
|
private TextView mTextLevel;
|
55 |
|
|
private DistortedTexture mTexture;
|
56 |
|
|
private MeshBase mMesh;
|
57 |
77a500b3
|
Leszek Koltunski
|
|
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 |
e3900503
|
Leszek Koltunski
|
DistortedLibrary.onPause();
|
67 |
77a500b3
|
Leszek Koltunski
|
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 |
e3900503
|
Leszek Koltunski
|
DistortedLibrary.onDestroy();
|
87 |
77a500b3
|
Leszek Koltunski
|
super.onDestroy();
|
88 |
|
|
}
|
89 |
64558e4e
|
Leszek Koltunski
|
|
90 |
77a500b3
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
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 |
4db4e39b
|
Leszek Koltunski
|
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 |
77a500b3
|
Leszek Koltunski
|
|
110 |
24624a1a
|
Leszek Koltunski
|
switch(objectType)
|
111 |
|
|
{
|
112 |
4db4e39b
|
Leszek Koltunski
|
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 |
f3ca895f
|
Leszek Koltunski
|
case 1: mMesh = new MeshRectangles(numCols,numRows);
|
126 |
4db4e39b
|
Leszek Koltunski
|
break;
|
127 |
|
|
case 2: mMesh = new MeshSphere(numRows);
|
128 |
|
|
break;
|
129 |
|
|
case 3: mMesh = new MeshQuad();
|
130 |
|
|
break;
|
131 |
24624a1a
|
Leszek Koltunski
|
}
|
132 |
77a500b3
|
Leszek Koltunski
|
|
133 |
4db4e39b
|
Leszek Koltunski
|
mTexture = new DistortedTexture(numCols,numRows);
|
134 |
77a500b3
|
Leszek Koltunski
|
|
135 |
|
|
setContentView(R.layout.inflatelayout);
|
136 |
|
|
|
137 |
64558e4e
|
Leszek Koltunski
|
Spinner renderSpinner = findViewById(R.id.inflateSpinnerMode);
|
138 |
|
|
renderSpinner.setOnItemSelectedListener(this);
|
139 |
77a500b3
|
Leszek Koltunski
|
|
140 |
64558e4e
|
Leszek Koltunski
|
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 |
77a500b3
|
Leszek Koltunski
|
|
148 |
64558e4e
|
Leszek Koltunski
|
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 |
77a500b3
|
Leszek Koltunski
|
|
157 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
158 |
|
|
|
159 |
4db4e39b
|
Leszek Koltunski
|
private void createBitmap(int size, int bitmapID)
|
160 |
77a500b3
|
Leszek Koltunski
|
{
|
161 |
4db4e39b
|
Leszek Koltunski
|
if( bitmapID!=-1)
|
162 |
77a500b3
|
Leszek Koltunski
|
{
|
163 |
4db4e39b
|
Leszek Koltunski
|
InputStream is = getResources().openRawResource(bitmapID);
|
164 |
77a500b3
|
Leszek Koltunski
|
|
165 |
4db4e39b
|
Leszek Koltunski
|
try
|
166 |
|
|
{
|
167 |
|
|
mBitmap = BitmapFactory.decodeStream(is);
|
168 |
|
|
}
|
169 |
|
|
finally
|
170 |
|
|
{
|
171 |
77a500b3
|
Leszek Koltunski
|
try
|
172 |
|
|
{
|
173 |
4db4e39b
|
Leszek Koltunski
|
is.close();
|
174 |
77a500b3
|
Leszek Koltunski
|
}
|
175 |
4db4e39b
|
Leszek Koltunski
|
catch(IOException e) { }
|
176 |
77a500b3
|
Leszek Koltunski
|
}
|
177 |
4db4e39b
|
Leszek Koltunski
|
}
|
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 |
77a500b3
|
Leszek Koltunski
|
{
|
196 |
4db4e39b
|
Leszek Koltunski
|
canvas.drawRect( T*i-1, 0, T*i+1, S, paint);
|
197 |
|
|
canvas.drawRect( 0, T*i-1, S, T*i+1, paint);
|
198 |
77a500b3
|
Leszek Koltunski
|
}
|
199 |
|
|
}
|
200 |
4db4e39b
|
Leszek Koltunski
|
}
|
201 |
77a500b3
|
Leszek Koltunski
|
|
202 |
4db4e39b
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
203 |
|
|
|
204 |
|
|
public Bitmap getBitmap()
|
205 |
|
|
{
|
206 |
77a500b3
|
Leszek Koltunski
|
return mBitmap;
|
207 |
|
|
}
|
208 |
|
|
|
209 |
64558e4e
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
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 |
77a500b3
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
230 |
|
|
|
231 |
|
|
public void onProgressChanged(SeekBar bar, int progress, boolean fromUser)
|
232 |
|
|
{
|
233 |
|
|
switch (bar.getId())
|
234 |
|
|
{
|
235 |
64558e4e
|
Leszek Koltunski
|
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 |
77a500b3
|
Leszek Koltunski
|
}
|
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 |
|
|
}
|