1 |
bc0a685b
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2 |
|
|
// Copyright 2016 Leszek Koltunski //
|
3 |
|
|
// //
|
4 |
71c8884f
|
Leszek Koltunski
|
// This file is part of Distorted. //
|
5 |
bc0a685b
|
Leszek Koltunski
|
// //
|
6 |
71c8884f
|
Leszek Koltunski
|
// Distorted is free software: you can redistribute it and/or modify //
|
7 |
bc0a685b
|
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 |
bc0a685b
|
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 |
bc0a685b
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
19 |
427ab7bf
|
Leszek Koltunski
|
|
20 |
5068fa06
|
Leszek Koltunski
|
package org.distorted.examples.listener;
|
21 |
427ab7bf
|
Leszek Koltunski
|
|
22 |
|
|
import java.io.IOException;
|
23 |
|
|
import java.io.InputStream;
|
24 |
|
|
import java.util.Random;
|
25 |
|
|
|
26 |
|
|
import javax.microedition.khronos.egl.EGLConfig;
|
27 |
|
|
import javax.microedition.khronos.opengles.GL10;
|
28 |
|
|
|
29 |
5068fa06
|
Leszek Koltunski
|
import org.distorted.examples.R;
|
30 |
427ab7bf
|
Leszek Koltunski
|
|
31 |
d76638f1
|
leszek
|
import org.distorted.library.effect.EffectType;
|
32 |
20e888ea
|
Leszek Koltunski
|
import org.distorted.library.effect.MatrixEffectScale;
|
33 |
|
|
import org.distorted.library.effect.VertexEffectDistort;
|
34 |
e3900503
|
Leszek Koltunski
|
import org.distorted.library.main.DistortedLibrary;
|
35 |
01782e85
|
Leszek Koltunski
|
import org.distorted.library.main.DistortedScreen;
|
36 |
f3ca895f
|
Leszek Koltunski
|
import org.distorted.library.mesh.MeshRectangles;
|
37 |
01782e85
|
Leszek Koltunski
|
import org.distorted.library.main.DistortedTexture;
|
38 |
|
|
import org.distorted.library.main.DistortedEffects;
|
39 |
7bf107f7
|
Leszek Koltunski
|
import org.distorted.library.type.Dynamic3D;
|
40 |
7589635e
|
Leszek Koltunski
|
import org.distorted.library.type.Static3D;
|
41 |
|
|
import org.distorted.library.type.Static4D;
|
42 |
13efa930
|
Leszek Koltunski
|
import org.distorted.library.message.EffectListener;
|
43 |
427ab7bf
|
Leszek Koltunski
|
|
44 |
|
|
import android.graphics.Bitmap;
|
45 |
|
|
import android.graphics.BitmapFactory;
|
46 |
|
|
import android.opengl.GLSurfaceView;
|
47 |
|
|
|
48 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
49 |
|
|
|
50 |
|
|
class ListenerRenderer implements GLSurfaceView.Renderer,EffectListener
|
51 |
|
|
{
|
52 |
d381aa80
|
Leszek Koltunski
|
private final int NUM_CONCURRENT_BUBBLES = 12;
|
53 |
e7a4ef16
|
Leszek Koltunski
|
|
54 |
427ab7bf
|
Leszek Koltunski
|
private GLSurfaceView mView;
|
55 |
d04a4886
|
Leszek Koltunski
|
private DistortedEffects mEffects;
|
56 |
d218d64e
|
leszek
|
private DistortedScreen mScreen;
|
57 |
b0ebdf5e
|
Leszek Koltunski
|
private DistortedTexture mTexture;
|
58 |
f3ca895f
|
Leszek Koltunski
|
private MeshRectangles mMesh;
|
59 |
427ab7bf
|
Leszek Koltunski
|
private int bmpHeight, bmpWidth;
|
60 |
|
|
private Random mRnd;
|
61 |
d381aa80
|
Leszek Koltunski
|
private Static3D mScale;
|
62 |
e7a4ef16
|
Leszek Koltunski
|
|
63 |
427ab7bf
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
64 |
|
|
|
65 |
e7a4ef16
|
Leszek Koltunski
|
ListenerRenderer(GLSurfaceView v)
|
66 |
427ab7bf
|
Leszek Koltunski
|
{
|
67 |
|
|
mView = v;
|
68 |
687263cc
|
Leszek Koltunski
|
|
69 |
e4330c89
|
Leszek Koltunski
|
mScreen = new DistortedScreen();
|
70 |
f6d884d5
|
Leszek Koltunski
|
mRnd = new Random(0);
|
71 |
20e888ea
|
Leszek Koltunski
|
mScale= new Static3D(1,1,1);
|
72 |
427ab7bf
|
Leszek Koltunski
|
}
|
73 |
|
|
|
74 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
75 |
|
|
|
76 |
a4d59c0b
|
Leszek Koltunski
|
private boolean addNewBubble()
|
77 |
427ab7bf
|
Leszek Koltunski
|
{
|
78 |
849e0034
|
Leszek Koltunski
|
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
|
83 |
7bf107f7
|
Leszek Koltunski
|
|
84 |
f988589e
|
Leszek Koltunski
|
Dynamic3D dDistort = new Dynamic3D(duration,1.0f);
|
85 |
7bf107f7
|
Leszek Koltunski
|
dDistort.add(new Static3D(0,0, 0));
|
86 |
|
|
dDistort.add(new Static3D(0,0,height));
|
87 |
|
|
|
88 |
8d5a8e06
|
Leszek Koltunski
|
VertexEffectDistort distort = new VertexEffectDistort(dDistort, new Static3D(pointx,pointy,0), new Static4D(0,0,0,radius));
|
89 |
|
|
distort.notifyWhenFinished(this);
|
90 |
|
|
|
91 |
|
|
return mEffects.apply(distort);
|
92 |
427ab7bf
|
Leszek Koltunski
|
}
|
93 |
|
|
|
94 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
95 |
|
|
|
96 |
8d5a8e06
|
Leszek Koltunski
|
public void effectFinished(final long effectID)
|
97 |
427ab7bf
|
Leszek Koltunski
|
{
|
98 |
8d5a8e06
|
Leszek Koltunski
|
mEffects.abortById(effectID);
|
99 |
|
|
|
100 |
|
|
if( !addNewBubble() )
|
101 |
|
|
{
|
102 |
|
|
android.util.Log.e("Listener", "failed to add new bubble - this should never happen!");
|
103 |
|
|
}
|
104 |
427ab7bf
|
Leszek Koltunski
|
}
|
105 |
|
|
|
106 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
107 |
|
|
|
108 |
|
|
public void onDrawFrame(GL10 glUnused)
|
109 |
|
|
{
|
110 |
fe59d375
|
Leszek Koltunski
|
mScreen.render( System.currentTimeMillis() );
|
111 |
427ab7bf
|
Leszek Koltunski
|
}
|
112 |
|
|
|
113 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
114 |
|
|
|
115 |
3a91bfe1
|
Leszek Koltunski
|
public void onSurfaceChanged(GL10 glUnused, int width, int height)
|
116 |
|
|
{
|
117 |
d381aa80
|
Leszek Koltunski
|
float horiRatio = (float)width / mTexture.getWidth();
|
118 |
|
|
float vertRatio = (float)height/ mTexture.getHeight();
|
119 |
|
|
float factor = horiRatio > vertRatio ? vertRatio : horiRatio;
|
120 |
3a91bfe1
|
Leszek Koltunski
|
|
121 |
d381aa80
|
Leszek Koltunski
|
mScale.set( factor,factor,factor );
|
122 |
3a91bfe1
|
Leszek Koltunski
|
mScreen.resize(width, height);
|
123 |
|
|
}
|
124 |
427ab7bf
|
Leszek Koltunski
|
|
125 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
126 |
|
|
|
127 |
3a91bfe1
|
Leszek Koltunski
|
public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
|
128 |
|
|
{
|
129 |
|
|
InputStream is = mView.getContext().getResources().openRawResource(R.raw.water);
|
130 |
|
|
Bitmap bitmap;
|
131 |
427ab7bf
|
Leszek Koltunski
|
|
132 |
3a91bfe1
|
Leszek Koltunski
|
try
|
133 |
|
|
{
|
134 |
|
|
bitmap = BitmapFactory.decodeStream(is);
|
135 |
|
|
}
|
136 |
|
|
finally
|
137 |
|
|
{
|
138 |
|
|
try
|
139 |
|
|
{
|
140 |
|
|
is.close();
|
141 |
|
|
}
|
142 |
|
|
catch(IOException e) { }
|
143 |
|
|
}
|
144 |
|
|
|
145 |
|
|
bmpHeight = bitmap.getHeight();
|
146 |
|
|
bmpWidth = bitmap.getWidth();
|
147 |
|
|
|
148 |
687263cc
|
Leszek Koltunski
|
if( mTexture==null ) mTexture = new DistortedTexture();
|
149 |
3a91bfe1
|
Leszek Koltunski
|
mTexture.setTexture(bitmap);
|
150 |
|
|
|
151 |
698ad0a8
|
Leszek Koltunski
|
if( mMesh==null )
|
152 |
|
|
{
|
153 |
|
|
mMesh = new MeshRectangles(50,50*bmpHeight/bmpWidth);
|
154 |
|
|
mMesh.setStretch(bmpWidth,bmpHeight,0);
|
155 |
|
|
}
|
156 |
3a91bfe1
|
Leszek Koltunski
|
|
157 |
687263cc
|
Leszek Koltunski
|
if( mEffects==null )
|
158 |
|
|
{
|
159 |
698ad0a8
|
Leszek Koltunski
|
mEffects = new DistortedEffects();
|
160 |
687263cc
|
Leszek Koltunski
|
mEffects.apply(new MatrixEffectScale(mScale));
|
161 |
|
|
}
|
162 |
|
|
|
163 |
3a91bfe1
|
Leszek Koltunski
|
mScreen.detachAll();
|
164 |
|
|
mScreen.attach(mTexture,mEffects,mMesh);
|
165 |
|
|
|
166 |
d381aa80
|
Leszek Koltunski
|
for(int i = 0; i< NUM_CONCURRENT_BUBBLES; i++) addNewBubble();
|
167 |
3a91bfe1
|
Leszek Koltunski
|
|
168 |
676c14da
|
Leszek Koltunski
|
// 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
|
170 |
386fd702
|
Leszek Koltunski
|
DistortedLibrary.setMax(EffectType.VERTEX,NUM_CONCURRENT_BUBBLES+1);
|
171 |
885b9cca
|
leszek
|
VertexEffectDistort.enable();
|
172 |
3a91bfe1
|
Leszek Koltunski
|
|
173 |
|
|
try
|
174 |
|
|
{
|
175 |
e3900503
|
Leszek Koltunski
|
DistortedLibrary.onCreate(mView.getContext());
|
176 |
3a91bfe1
|
Leszek Koltunski
|
}
|
177 |
|
|
catch(Exception ex)
|
178 |
|
|
{
|
179 |
9e7b6dbd
|
Leszek Koltunski
|
android.util.Log.e("Listener", ex.getMessage() );
|
180 |
3a91bfe1
|
Leszek Koltunski
|
}
|
181 |
|
|
}
|
182 |
427ab7bf
|
Leszek Koltunski
|
}
|