Revision f6d884d5
Added by Leszek Koltunski over 7 years ago
src/main/java/org/distorted/examples/deform/DeformRenderer.java | ||
---|---|---|
23 | 23 |
import javax.microedition.khronos.opengles.GL10; |
24 | 24 |
|
25 | 25 |
import org.distorted.library.Distorted; |
26 |
import org.distorted.library.DistortedObject; |
|
26 |
import org.distorted.library.DistortedTexture; |
|
27 |
import org.distorted.library.DistortedEffectQueues; |
|
27 | 28 |
import org.distorted.library.GridObject; |
28 | 29 |
import org.distorted.library.GridFlat; |
29 | 30 |
|
... | ... | |
43 | 44 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
44 | 45 |
|
45 | 46 |
class DeformRenderer implements GLSurfaceView.Renderer |
46 |
{ |
|
47 |
{
|
|
47 | 48 |
private static final int NUM_VECTORS = 8; |
48 | 49 |
private static final int NUM_LINES = 10; |
49 | 50 |
private static final int NUM_FRAMES = 10; |
50 | 51 |
|
51 | 52 |
private GLSurfaceView mView; |
52 |
private DistortedObject fps, stretch; |
|
53 |
private DistortedTexture fpsTexture, stretchTexture; |
|
54 |
private DistortedEffectQueues fpsQueues, stretchQueues; |
|
53 | 55 |
private GridObject fpsGrid, stretchGrid; |
54 | 56 |
private Static3D touchPoint; |
55 | 57 |
|
... | ... | |
83 | 85 |
DeformRenderer(GLSurfaceView view) |
84 | 86 |
{ |
85 | 87 |
mView = view; |
86 |
|
|
88 |
|
|
89 |
mPaint = new Paint(); |
|
90 |
mPaint.setAntiAlias(true); |
|
91 |
mPaint.setTextAlign(Paint.Align.CENTER); |
|
92 |
|
|
93 |
fpsQueues = new DistortedEffectQueues(); |
|
94 |
stretchQueues = new DistortedEffectQueues(); |
|
95 |
|
|
87 | 96 |
mRegion = new Static4D(0,0,0,0); |
88 | 97 |
|
89 | 98 |
durations = new long[NUM_FRAMES+1]; |
90 | 99 |
currDuration = 0; |
91 | 100 |
|
92 |
for(int i=0; i<NUM_FRAMES+1; i++) |
|
93 |
{ |
|
94 |
durations[i]=0; |
|
95 |
} |
|
101 |
for(int i=0; i<NUM_FRAMES+1; i++) durations[i]=0; |
|
96 | 102 |
|
97 | 103 |
// DISTORT |
98 | 104 |
mReleasedDistortDynamic = new Dynamic3D(NUM_VECTORS*500, 0.5f); |
... | ... | |
159 | 165 |
|
160 | 166 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
161 | 167 |
|
162 |
public void onPause()
|
|
168 |
public void onPause() |
|
163 | 169 |
{ |
164 | 170 |
bitmapCreated = false; |
165 | 171 |
} |
166 | 172 |
|
167 | 173 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
168 | 174 |
|
169 |
public void onDrawFrame(GL10 glUnused)
|
|
170 |
{
|
|
171 |
GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
|
|
175 |
public void onDrawFrame(GL10 glUnused)
|
|
176 |
{ |
|
177 |
GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT); |
|
172 | 178 |
|
173 |
long time = System.currentTimeMillis();
|
|
179 |
long time = System.currentTimeMillis(); |
|
174 | 180 |
|
175 |
stretch.draw(time,stretchGrid);
|
|
181 |
stretchQueues.draw(time,stretchTexture,stretchGrid);
|
|
176 | 182 |
|
177 |
mPaint.setColor(0xffffffff);
|
|
178 |
fpsCanvas.drawRect(0, 0, fpsW, fpsH, mPaint);
|
|
179 |
mPaint.setColor(0xff000000);
|
|
180 |
fpsCanvas.drawText(fpsString, fpsW/2, 5*fpsH/6, mPaint);
|
|
183 |
mPaint.setColor(0xffffffff); |
|
184 |
fpsCanvas.drawRect(0, 0, fpsW, fpsH, mPaint); |
|
185 |
mPaint.setColor(0xff000000); |
|
186 |
fpsCanvas.drawText(fpsString, fpsW/2, 5*fpsH/6, mPaint); |
|
181 | 187 |
|
182 |
fps.setTexture(fpsBitmap);
|
|
183 |
fps.draw(time,fpsGrid);
|
|
188 |
fpsTexture.setTexture(fpsBitmap);
|
|
189 |
fpsQueues.draw(time,fpsTexture,fpsGrid);
|
|
184 | 190 |
|
185 |
computeFPS(time);
|
|
186 |
}
|
|
191 |
computeFPS(time); |
|
192 |
} |
|
187 | 193 |
|
188 | 194 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
189 | 195 |
|
190 |
public void onSurfaceChanged(GL10 glUnused, int width, int height)
|
|
191 |
{
|
|
192 |
scrHeight = height;
|
|
193 |
scrWidth = width;
|
|
196 |
public void onSurfaceChanged(GL10 glUnused, int width, int height)
|
|
197 |
{
|
|
198 |
scrHeight = height; |
|
199 |
scrWidth = width; |
|
194 | 200 |
|
195 |
setRegionRadius(50);
|
|
201 |
setRegionRadius(50); |
|
196 | 202 |
|
197 |
Distorted.onSurfaceChanged(width, height);
|
|
203 |
Distorted.onSurfaceChanged(width, height); |
|
198 | 204 |
|
199 |
if( !bitmapCreated )
|
|
200 |
{
|
|
201 |
createBitmap(scrWidth/2,scrHeight/2);
|
|
202 |
stretch.move( new Static3D(scrWidth/4,scrHeight/4,0) );
|
|
203 |
fps.move( new Static3D(5,5,0) );
|
|
204 |
bitmapCreated=true;
|
|
205 |
}
|
|
206 |
else
|
|
207 |
{
|
|
208 |
stretch.abortEffects(EffectTypes.VERTEX);
|
|
209 |
stretch.abortEffects(EffectTypes.FRAGMENT);
|
|
210 |
stretch.abortEffects(EffectNames.SHEAR);
|
|
211 |
}
|
|
212 |
}
|
|
205 |
if( !bitmapCreated ) |
|
206 |
{ |
|
207 |
createBitmap(scrWidth/2,scrHeight/2); |
|
208 |
stretchQueues.move( new Static3D(scrWidth/4,scrHeight/4,0) );
|
|
209 |
fpsQueues.move( new Static3D(5,5,0) );
|
|
210 |
bitmapCreated=true; |
|
211 |
} |
|
212 |
else |
|
213 |
{ |
|
214 |
stretchQueues.abortEffects(EffectTypes.VERTEX);
|
|
215 |
stretchQueues.abortEffects(EffectTypes.FRAGMENT);
|
|
216 |
stretchQueues.abortEffects(EffectNames.SHEAR);
|
|
217 |
} |
|
218 |
} |
|
213 | 219 |
|
214 | 220 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
215 | 221 |
|
216 |
public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
|
|
217 |
{
|
|
218 |
GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
|
219 |
|
|
220 |
try
|
|
221 |
{
|
|
222 |
Distorted.onSurfaceCreated(mView.getContext());
|
|
223 |
}
|
|
224 |
catch(Exception ex)
|
|
225 |
{
|
|
226 |
android.util.Log.e("DeformRenderer", ex.toString() );
|
|
227 |
}
|
|
228 |
}
|
|
222 |
public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
|
|
223 |
{ |
|
224 |
GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f); |
|
225 |
|
|
226 |
try |
|
227 |
{ |
|
228 |
Distorted.onSurfaceCreated(mView.getContext()); |
|
229 |
} |
|
230 |
catch(Exception ex) |
|
231 |
{ |
|
232 |
android.util.Log.e("DeformRenderer", ex.toString() ); |
|
233 |
} |
|
234 |
} |
|
229 | 235 |
|
230 | 236 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
231 | 237 |
|
232 |
private void createBitmap(int w, int h)
|
|
233 |
{
|
|
234 |
Canvas stretchCanvas;
|
|
238 |
private void createBitmap(int w, int h) |
|
239 |
{
|
|
240 |
Canvas stretchCanvas;
|
|
235 | 241 |
|
236 |
mPaint = new Paint(); |
|
237 |
stretch = new DistortedObject(w,h,1); |
|
238 |
stretchGrid = new GridFlat(50,50*h/w); |
|
239 |
Bitmap stretchBitmap = Bitmap.createBitmap(w,h, Bitmap.Config.ARGB_8888); |
|
240 |
stretchCanvas = new Canvas(stretchBitmap); |
|
242 |
stretchTexture = new DistortedTexture(w,h,0); |
|
243 |
stretchGrid = new GridFlat(50,50*h/w); |
|
244 |
Bitmap stretchBitmap = Bitmap.createBitmap(w,h, Bitmap.Config.ARGB_8888); |
|
245 |
stretchCanvas = new Canvas(stretchBitmap); |
|
241 | 246 |
|
242 |
fpsW = scrWidth/5; |
|
243 |
fpsH = fpsW/2; |
|
244 |
|
|
245 |
mPaint.setAntiAlias(true); |
|
246 |
mPaint.setTextAlign(Paint.Align.CENTER); |
|
247 |
mPaint.setTextSize(2*fpsH/3); |
|
248 |
mPaint.setColor(0xff008800); |
|
249 |
mPaint.setStyle(Style.FILL); |
|
250 |
stretchCanvas.drawRect(0, 0, w, h, mPaint); |
|
251 |
mPaint.setColor(0xffffffff); |
|
247 |
fpsW = scrWidth/5; |
|
248 |
fpsH = fpsW/2; |
|
249 |
|
|
250 |
mPaint.setTextSize(2*fpsH/3); |
|
251 |
mPaint.setColor(0xff008800); |
|
252 |
mPaint.setStyle(Style.FILL); |
|
253 |
stretchCanvas.drawRect(0, 0, w, h, mPaint); |
|
254 |
mPaint.setColor(0xffffffff); |
|
252 | 255 |
|
253 |
for(int i=0; i<=NUM_LINES ; i++ )
|
|
254 |
{
|
|
255 |
stretchCanvas.drawRect(w*i/NUM_LINES - 1, 0, w*i/NUM_LINES + 1, h , mPaint);
|
|
256 |
stretchCanvas.drawRect( 0, h *i/NUM_LINES -1, w , h*i/NUM_LINES + 1, mPaint);
|
|
257 |
}
|
|
256 |
for(int i=0; i<=NUM_LINES ; i++ ) |
|
257 |
{ |
|
258 |
stretchCanvas.drawRect(w*i/NUM_LINES - 1, 0, w*i/NUM_LINES + 1, h , mPaint); |
|
259 |
stretchCanvas.drawRect( 0, h *i/NUM_LINES -1, w , h*i/NUM_LINES + 1, mPaint); |
|
260 |
} |
|
258 | 261 |
|
259 |
touchPoint= new Static3D(0,0,0);
|
|
262 |
touchPoint= new Static3D(0,0,0); |
|
260 | 263 |
|
261 |
fps = new DistortedObject( fpsW, fpsH, 1);
|
|
262 |
fpsGrid = new GridFlat(1,1);
|
|
264 |
fpsTexture = new DistortedTexture( fpsW, fpsH, 0);
|
|
265 |
fpsGrid = new GridFlat(1,1); |
|
263 | 266 |
|
264 |
fpsBitmap = Bitmap.createBitmap(fpsW,fpsH, Bitmap.Config.ARGB_8888);
|
|
265 |
fpsCanvas = new Canvas(fpsBitmap);
|
|
267 |
fpsBitmap = Bitmap.createBitmap(fpsW,fpsH, Bitmap.Config.ARGB_8888); |
|
268 |
fpsCanvas = new Canvas(fpsBitmap); |
|
266 | 269 |
|
267 |
stretch.setTexture(stretchBitmap);
|
|
268 |
fps.setTexture(fpsBitmap);
|
|
269 |
}
|
|
270 |
stretchTexture.setTexture(stretchBitmap);
|
|
271 |
fpsTexture.setTexture(fpsBitmap);
|
|
272 |
} |
|
270 | 273 |
|
271 | 274 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
272 | 275 |
|
273 |
void down(int x, int y)
|
|
274 |
{
|
|
275 |
int xt = x-scrWidth/4;
|
|
276 |
int yt = y-scrHeight/4;
|
|
276 |
void down(int x, int y) |
|
277 |
{ |
|
278 |
int xt = x-scrWidth/4; |
|
279 |
int yt = y-scrHeight/4; |
|
277 | 280 |
|
278 |
if( xt<0 ) xt=0;
|
|
279 |
if( xt>scrWidth/2 ) xt=scrWidth/2;
|
|
280 |
if( yt<0 ) yt=0;
|
|
281 |
if( yt>scrHeight/2 ) yt=scrHeight/2;
|
|
281 |
if( xt<0 ) xt=0; |
|
282 |
if( xt>scrWidth/2 ) xt=scrWidth/2; |
|
283 |
if( yt<0 ) yt=0; |
|
284 |
if( yt>scrHeight/2 ) yt=scrHeight/2; |
|
282 | 285 |
|
283 |
touchPoint.set(xt,yt,0);
|
|
284 |
|
|
285 |
switch(mMode)
|
|
286 |
{
|
|
287 |
case DISTORT: vDistort[0].set(0,0,0);
|
|
288 |
mLastEffect = stretch.distort( mMovingDistortDynamic, touchPoint, mRegion);
|
|
289 |
break;
|
|
290 |
case DEFORM : vDeform[0].set(0,0,0);
|
|
291 |
mLastEffect = stretch.deform( mMovingDeformDynamic, touchPoint, mRegion);
|
|
292 |
break;
|
|
293 |
case SHEAR : vShear[0].set(0,0,0);
|
|
294 |
mLastEffect = stretch.shear(mMovingShearDynamic, touchPoint);
|
|
295 |
break;
|
|
296 |
}
|
|
297 |
}
|
|
286 |
touchPoint.set(xt,yt,0); |
|
287 |
|
|
288 |
switch(mMode) |
|
289 |
{ |
|
290 |
case DISTORT: vDistort[0].set(0,0,0); |
|
291 |
mLastEffect = stretchQueues.distort( mMovingDistortDynamic, touchPoint, mRegion);
|
|
292 |
break; |
|
293 |
case DEFORM : vDeform[0].set(0,0,0); |
|
294 |
mLastEffect = stretchQueues.deform( mMovingDeformDynamic, touchPoint, mRegion);
|
|
295 |
break; |
|
296 |
case SHEAR : vShear[0].set(0,0,0); |
|
297 |
mLastEffect = stretchQueues.shear(mMovingShearDynamic, touchPoint);
|
|
298 |
break; |
|
299 |
}
|
|
300 |
} |
|
298 | 301 |
|
299 | 302 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
300 | 303 |
|
301 |
void move(int x, int y)
|
|
302 |
{
|
|
303 |
switch(mMode)
|
|
304 |
{
|
|
305 |
case DISTORT: vDistort[0].set(x,y);
|
|
306 |
break;
|
|
307 |
case DEFORM: vDeform[0].set(x,y);
|
|
308 |
break;
|
|
309 |
case SHEAR: vShear[0].set( (float)x/(scrWidth/2), (float)y/(scrHeight/2));
|
|
310 |
break;
|
|
311 |
}
|
|
312 |
}
|
|
304 |
void move(int x, int y) |
|
305 |
{ |
|
306 |
switch(mMode) |
|
307 |
{ |
|
308 |
case DISTORT: vDistort[0].set(x,y); |
|
309 |
break; |
|
310 |
case DEFORM: vDeform[0].set(x,y); |
|
311 |
break; |
|
312 |
case SHEAR: vShear[0].set( (float)x/(scrWidth/2), (float)y/(scrHeight/2)); |
|
313 |
break; |
|
314 |
} |
|
315 |
} |
|
313 | 316 |
|
314 | 317 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
315 | 318 |
|
316 |
void up()
|
|
317 |
{
|
|
318 |
stretch.abortEffect(mLastEffect);
|
|
319 |
|
|
320 |
float damp = -0.65f;
|
|
321 |
|
|
322 |
switch(mMode)
|
|
323 |
{
|
|
324 |
case DISTORT: for(int i=1; i<NUM_VECTORS-1; i++)
|
|
325 |
{
|
|
326 |
vDistort[i].set( vDistort[i-1].getX()*damp, vDistort[i-1].getY()*damp );
|
|
327 |
}
|
|
328 |
vDistort[NUM_VECTORS-1].set(0,0);
|
|
329 |
stretch.distort( mReleasedDistortDynamic, touchPoint, mRegion);
|
|
330 |
break;
|
|
331 |
case DEFORM : for(int i=1; i<NUM_VECTORS-1; i++)
|
|
332 |
{
|
|
333 |
vDeform[i].set( vDeform[i-1].getX()*damp, vDeform[i-1].getY()*damp );
|
|
334 |
}
|
|
335 |
vDeform[NUM_VECTORS-1].set(0,0);
|
|
336 |
stretch.deform( mReleasedDeformDynamic, touchPoint, mRegion);
|
|
337 |
break;
|
|
338 |
case SHEAR : for(int i=1; i<NUM_VECTORS-1; i++)
|
|
339 |
{
|
|
340 |
vShear[i].set( vShear[i-1].getX()*damp, vShear[i-1].getY()*damp );
|
|
341 |
}
|
|
342 |
vShear[NUM_VECTORS-1].set(0,0);
|
|
343 |
stretch.shear(mReleasedShearDynamic, touchPoint);
|
|
344 |
break;
|
|
345 |
}
|
|
346 |
}
|
|
319 |
void up() |
|
320 |
{ |
|
321 |
stretchQueues.abortEffect(mLastEffect);
|
|
322 |
|
|
323 |
float damp = -0.65f; |
|
324 |
|
|
325 |
switch(mMode) |
|
326 |
{ |
|
327 |
case DISTORT: for(int i=1; i<NUM_VECTORS-1; i++) |
|
328 |
{ |
|
329 |
vDistort[i].set( vDistort[i-1].getX()*damp, vDistort[i-1].getY()*damp ); |
|
330 |
} |
|
331 |
vDistort[NUM_VECTORS-1].set(0,0); |
|
332 |
stretchQueues.distort( mReleasedDistortDynamic, touchPoint, mRegion);
|
|
333 |
break; |
|
334 |
case DEFORM : for(int i=1; i<NUM_VECTORS-1; i++) |
|
335 |
{ |
|
336 |
vDeform[i].set( vDeform[i-1].getX()*damp, vDeform[i-1].getY()*damp ); |
|
337 |
} |
|
338 |
vDeform[NUM_VECTORS-1].set(0,0); |
|
339 |
stretchQueues.deform( mReleasedDeformDynamic, touchPoint, mRegion);
|
|
340 |
break; |
|
341 |
case SHEAR : for(int i=1; i<NUM_VECTORS-1; i++) |
|
342 |
{ |
|
343 |
vShear[i].set( vShear[i-1].getX()*damp, vShear[i-1].getY()*damp ); |
|
344 |
} |
|
345 |
vShear[NUM_VECTORS-1].set(0,0); |
|
346 |
stretchQueues.shear(mReleasedShearDynamic, touchPoint);
|
|
347 |
break; |
|
348 |
}
|
|
349 |
} |
|
347 | 350 |
|
348 | 351 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
349 | 352 |
|
350 |
private void computeFPS(long currentTime)
|
|
351 |
{
|
|
352 |
if( lastTime!=0 )
|
|
353 |
{
|
|
354 |
currDuration++;
|
|
355 |
if( currDuration>=NUM_FRAMES ) currDuration = 0;
|
|
356 |
durations[NUM_FRAMES] += ((currentTime-lastTime)-durations[currDuration]);
|
|
357 |
durations[currDuration] = currentTime-lastTime;
|
|
358 |
|
|
359 |
fpsString = "" + ((int)(10000.0f*NUM_FRAMES/durations[NUM_FRAMES]))/10.0f;
|
|
360 |
}
|
|
353 |
private void computeFPS(long currentTime) |
|
354 |
{ |
|
355 |
if( lastTime!=0 ) |
|
356 |
{ |
|
357 |
currDuration++; |
|
358 |
if( currDuration>=NUM_FRAMES ) currDuration = 0; |
|
359 |
durations[NUM_FRAMES] += ((currentTime-lastTime)-durations[currDuration]); |
|
360 |
durations[currDuration] = currentTime-lastTime; |
|
361 |
|
|
362 |
fpsString = "" + ((int)(10000.0f*NUM_FRAMES/durations[NUM_FRAMES]))/10.0f; |
|
363 |
} |
|
361 | 364 |
|
362 |
lastTime = currentTime; |
|
363 |
} |
|
364 |
} |
|
365 |
lastTime = currentTime; |
|
366 |
} |
|
367 |
} |
Also available in: Unified diff
Complete the split DistortedObject -> (DistortedTexture,DistortedEffectQueue)