Revision e052248f
Added by Leszek Koltunski almost 5 years ago
src/main/java/org/distorted/examples/listener/ListenerRenderer.java | ||
---|---|---|
46 | 46 |
import android.opengl.GLSurfaceView; |
47 | 47 |
|
48 | 48 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
49 |
// Show how to use the setMax, notifyWhenFinished / effectFinished APIs |
|
49 | 50 |
|
50 | 51 |
class ListenerRenderer implements GLSurfaceView.Renderer,EffectListener |
51 | 52 |
{ |
... | ... | |
56 | 57 |
private DistortedScreen mScreen; |
57 | 58 |
private DistortedTexture mTexture; |
58 | 59 |
private MeshRectangles mMesh; |
59 |
private int bmpHeight, bmpWidth; |
|
60 | 60 |
private Random mRnd; |
61 | 61 |
private Static3D mScale; |
62 |
private float mBmpRatio; |
|
62 | 63 |
|
63 | 64 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
64 | 65 |
|
... | ... | |
66 | 67 |
{ |
67 | 68 |
mView = v; |
68 | 69 |
|
69 |
mScreen = new DistortedScreen(); |
|
70 |
mRnd = new Random(0); |
|
71 |
mScale= new Static3D(1,1,1); |
|
70 |
mTexture = new DistortedTexture(); |
|
71 |
mScreen = new DistortedScreen(); |
|
72 |
mRnd = new Random(0); |
|
73 |
mScale = new Static3D(1,1,1); |
|
74 |
mEffects = new DistortedEffects(); |
|
75 |
mEffects.apply(new MatrixEffectScale(mScale)); |
|
76 |
|
|
77 |
for(int i=0; i<NUM_CONCURRENT_BUBBLES; i++) |
|
78 |
{ |
|
79 |
VertexEffectDistort newEffect = createNewBubble(); |
|
80 |
|
|
81 |
if( !mEffects.apply(newEffect) ) |
|
82 |
{ |
|
83 |
android.util.Log.e("listener", "failed to add bubble - this should never happen!"); |
|
84 |
} |
|
85 |
} |
|
72 | 86 |
} |
73 | 87 |
|
74 | 88 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
75 | 89 |
|
76 |
private boolean addNewBubble()
|
|
90 |
private VertexEffectDistort createNewBubble()
|
|
77 | 91 |
{ |
78 |
int radius = (int)(( 0.10f + 0.70f*mRnd.nextFloat())*bmpWidth); // pop up a bubble of size (radius,height)
|
|
79 |
int height = (int)((-0.10f + 0.20f*mRnd.nextFloat())*bmpWidth); //
|
|
80 |
int pointx = (int)(0.8f * bmpWidth * (mRnd.nextFloat()-0.5f)); // at a random place on the bitmap (but not near the edge)
|
|
81 |
int pointy = (int)(0.8f * bmpHeight* (mRnd.nextFloat()-0.5f)); //
|
|
82 |
int duration = 1000 + mRnd.nextInt(3000); // for anytime from 1 to 4 seconds
|
|
92 |
float radius = 0.10f + 0.70f*mRnd.nextFloat() ; // pop up a bubble of size (radius,height)
|
|
93 |
float height = -0.10f + 0.20f*mRnd.nextFloat() ; //
|
|
94 |
float pointx = 0.80f * (mRnd.nextFloat()-0.5f); // at a random place on the bitmap (but not near the edge)
|
|
95 |
float pointy = 0.80f * (mRnd.nextFloat()-0.5f); //
|
|
96 |
int duration = 1000 + mRnd.nextInt(3000); // for anytime from 1 to 4 seconds
|
|
83 | 97 |
|
98 |
// '1.0f' i.e. from (0,0,0) to (0,0,height) and back to (0,0,0) - full circle. |
|
84 | 99 |
Dynamic3D dDistort = new Dynamic3D(duration,1.0f); |
85 | 100 |
dDistort.add(new Static3D(0,0, 0)); |
86 | 101 |
dDistort.add(new Static3D(0,0,height)); |
87 | 102 |
|
88 |
VertexEffectDistort distort = new VertexEffectDistort(dDistort, new Static3D(pointx,pointy,0), new Static4D(0,0,0,radius)); |
|
103 |
Static3D center = new Static3D(pointx,pointy,0); |
|
104 |
Static4D region = new Static4D(0,0,0,radius); |
|
105 |
VertexEffectDistort distort = new VertexEffectDistort(dDistort,center,region); |
|
106 |
|
|
107 |
// We want to know when this effect finishes so that we can add a new one. |
|
89 | 108 |
distort.notifyWhenFinished(this); |
90 | 109 |
|
91 |
return mEffects.apply(distort);
|
|
110 |
return distort;
|
|
92 | 111 |
} |
93 | 112 |
|
94 | 113 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
114 |
// An effect just finished, i.e. its Dynamic 'dDistort' reached its end point. |
|
115 |
// The ID of the finished effect is 'effectID' but here we know it must be one of the Distorts |
|
116 |
// and we don't care which one it is - just remove the finished effect from the Effect queues and |
|
117 |
// add a new one right back in. |
|
95 | 118 |
|
96 | 119 |
public void effectFinished(final long effectID) |
97 | 120 |
{ |
98 | 121 |
mEffects.abortById(effectID); |
99 | 122 |
|
100 |
if( !addNewBubble() ) |
|
123 |
VertexEffectDistort newEffect = createNewBubble(); |
|
124 |
|
|
125 |
if( !mEffects.apply(newEffect) ) |
|
101 | 126 |
{ |
102 | 127 |
android.util.Log.e("Listener", "failed to add new bubble - this should never happen!"); |
103 | 128 |
} |
... | ... | |
105 | 130 |
|
106 | 131 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
107 | 132 |
|
108 |
public void onDrawFrame(GL10 glUnused)
|
|
133 |
public void onDrawFrame(GL10 glUnused)
|
|
109 | 134 |
{ |
110 | 135 |
mScreen.render( System.currentTimeMillis() ); |
111 | 136 |
} |
... | ... | |
114 | 139 |
|
115 | 140 |
public void onSurfaceChanged(GL10 glUnused, int width, int height) |
116 | 141 |
{ |
117 |
float horiRatio = (float)width / mMesh.getStretchX(); |
|
118 |
float vertRatio = (float)height/ mMesh.getStretchY(); |
|
119 |
float factor = horiRatio > vertRatio ? vertRatio : horiRatio; |
|
142 |
float min= width>height ? height : width; |
|
120 | 143 |
|
121 |
mScale.set( factor,factor,factor );
|
|
144 |
mScale.set( min, min*mBmpRatio, min );
|
|
122 | 145 |
mScreen.resize(width, height); |
123 | 146 |
} |
124 | 147 |
|
... | ... | |
142 | 165 |
catch(IOException e) { } |
143 | 166 |
} |
144 | 167 |
|
145 |
bmpHeight = bitmap.getHeight(); |
|
146 |
bmpWidth = bitmap.getWidth(); |
|
147 |
|
|
148 |
if( mTexture==null ) mTexture = new DistortedTexture(); |
|
168 |
mBmpRatio = (float)bitmap.getHeight()/bitmap.getWidth(); |
|
149 | 169 |
mTexture.setTexture(bitmap); |
150 | 170 |
|
151 |
if( mMesh==null ) |
|
152 |
{ |
|
153 |
mMesh = new MeshRectangles(50,50*bmpHeight/bmpWidth); |
|
154 |
mMesh.setStretch(bmpWidth,bmpHeight,0); |
|
155 |
} |
|
156 |
|
|
157 |
if( mEffects==null ) |
|
158 |
{ |
|
159 |
mEffects = new DistortedEffects(); |
|
160 |
mEffects.apply(new MatrixEffectScale(mScale)); |
|
161 |
} |
|
171 |
if( mMesh==null ) mMesh = new MeshRectangles(50, (int)(50*mBmpRatio) ); |
|
162 | 172 |
|
163 | 173 |
mScreen.detachAll(); |
164 | 174 |
mScreen.attach(mTexture,mEffects,mMesh); |
165 | 175 |
|
166 |
for(int i = 0; i< NUM_CONCURRENT_BUBBLES; i++) addNewBubble(); |
|
167 |
|
|
168 |
// one more than we have bubbles at any given time because it can sometimes |
|
169 |
// happen that the old bubble is not yet removed when we add a new one |
|
176 |
// Normally we can only hold no more than 5 (see EffectType.reset()) Vertex |
|
177 |
// effects at any given time. Here we want 12, so we need to increase the default. |
|
178 |
// We need to call this before we call onCreate; best done here. |
|
179 |
// After onCreate we can also call this but only to decrease this value! |
|
180 |
// |
|
181 |
// One more than we have bubbles at any given time because it can sometimes |
|
182 |
// happen that the old bubble is not yet removed when we add a new one. |
|
170 | 183 |
DistortedLibrary.setMax(EffectType.VERTEX,NUM_CONCURRENT_BUBBLES+1); |
171 | 184 |
VertexEffectDistort.enable(); |
172 | 185 |
|
Also available in: Unified diff
Port Listener to the new setStretch-less API.