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.sink;
|
21 |
427ab7bf
|
Leszek Koltunski
|
|
22 |
|
|
import java.io.IOException;
|
23 |
|
|
import java.io.InputStream;
|
24 |
|
|
|
25 |
|
|
import javax.microedition.khronos.egl.EGLConfig;
|
26 |
|
|
import javax.microedition.khronos.opengles.GL10;
|
27 |
|
|
|
28 |
5068fa06
|
Leszek Koltunski
|
import org.distorted.examples.R;
|
29 |
427ab7bf
|
Leszek Koltunski
|
|
30 |
c9ff05c1
|
leszek
|
import org.distorted.library.effect.MatrixEffectScale;
|
31 |
|
|
import org.distorted.library.effect.VertexEffectSink;
|
32 |
e3900503
|
Leszek Koltunski
|
import org.distorted.library.main.DistortedLibrary;
|
33 |
01782e85
|
Leszek Koltunski
|
import org.distorted.library.main.DistortedEffects;
|
34 |
|
|
import org.distorted.library.main.DistortedScreen;
|
35 |
f3ca895f
|
Leszek Koltunski
|
import org.distorted.library.mesh.MeshRectangles;
|
36 |
01782e85
|
Leszek Koltunski
|
import org.distorted.library.main.DistortedTexture;
|
37 |
b5cc7760
|
Leszek Koltunski
|
import org.distorted.library.type.Dynamic1D;
|
38 |
|
|
import org.distorted.library.type.Static1D;
|
39 |
7589635e
|
Leszek Koltunski
|
import org.distorted.library.type.Static3D;
|
40 |
513b2e9c
|
Leszek Koltunski
|
import org.distorted.library.type.Static4D;
|
41 |
427ab7bf
|
Leszek Koltunski
|
|
42 |
|
|
import android.graphics.Bitmap;
|
43 |
|
|
import android.graphics.BitmapFactory;
|
44 |
|
|
import android.opengl.GLSurfaceView;
|
45 |
|
|
|
46 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
47 |
|
|
|
48 |
061449ed
|
Leszek Koltunski
|
class SinkRenderer implements GLSurfaceView.Renderer, DistortedLibrary.ExceptionListener
|
49 |
bc0a685b
|
Leszek Koltunski
|
{
|
50 |
|
|
private GLSurfaceView mView;
|
51 |
d04a4886
|
Leszek Koltunski
|
private DistortedEffects mEffects;
|
52 |
d218d64e
|
leszek
|
private DistortedScreen mScreen;
|
53 |
b0ebdf5e
|
Leszek Koltunski
|
private DistortedTexture mTexture;
|
54 |
f3ca895f
|
Leszek Koltunski
|
private MeshRectangles mMesh;
|
55 |
d381aa80
|
Leszek Koltunski
|
private Static3D mScale;
|
56 |
f209a803
|
Leszek Koltunski
|
private float mBmpRatio;
|
57 |
c9ff05c1
|
leszek
|
|
58 |
427ab7bf
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
59 |
|
|
|
60 |
e7a4ef16
|
Leszek Koltunski
|
SinkRenderer(GLSurfaceView v)
|
61 |
bc0a685b
|
Leszek Koltunski
|
{
|
62 |
4c42bc15
|
Leszek Koltunski
|
mView = v;
|
63 |
|
|
mTexture = new DistortedTexture();
|
64 |
|
|
mEffects = new DistortedEffects();
|
65 |
|
|
mScreen = new DistortedScreen();
|
66 |
f6d884d5
|
Leszek Koltunski
|
|
67 |
f209a803
|
Leszek Koltunski
|
Dynamic1D sinkStrength = new Dynamic1D(2000,0.0f);
|
68 |
513b2e9c
|
Leszek Koltunski
|
sinkStrength.add(new Static1D(2.0f));
|
69 |
f209a803
|
Leszek Koltunski
|
sinkStrength.add(new Static1D(0.2f));
|
70 |
f6d884d5
|
Leszek Koltunski
|
|
71 |
f209a803
|
Leszek Koltunski
|
// strength [changing in time from 1.0 to 0.2 and back],
|
72 |
|
|
// center [(0,0,0), i.e. center of the bitmap],
|
73 |
513b2e9c
|
Leszek Koltunski
|
// region [ (0,0,0) with radius 0.5 - apply to the center circle of the bitmap ]
|
74 |
|
|
VertexEffectSink sinkEffect = new VertexEffectSink(sinkStrength, new Static3D(0,0,0), new Static4D(0,0,0,0.5f) );
|
75 |
c9ff05c1
|
leszek
|
mEffects.apply(sinkEffect);
|
76 |
|
|
|
77 |
|
|
mScale = new Static3D(1,1,1);
|
78 |
|
|
mEffects.apply(new MatrixEffectScale(mScale));
|
79 |
bc0a685b
|
Leszek Koltunski
|
}
|
80 |
427ab7bf
|
Leszek Koltunski
|
|
81 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
82 |
|
|
|
83 |
bc0a685b
|
Leszek Koltunski
|
public void onDrawFrame(GL10 glUnused)
|
84 |
|
|
{
|
85 |
fe59d375
|
Leszek Koltunski
|
mScreen.render( System.currentTimeMillis() );
|
86 |
bc0a685b
|
Leszek Koltunski
|
}
|
87 |
427ab7bf
|
Leszek Koltunski
|
|
88 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
89 |
|
|
|
90 |
bc0a685b
|
Leszek Koltunski
|
public void onSurfaceChanged(GL10 glUnused, int width, int height)
|
91 |
d381aa80
|
Leszek Koltunski
|
{
|
92 |
c7a31368
|
Leszek Koltunski
|
if( width<height ) mScale.set( width, width*mBmpRatio, 1 );
|
93 |
|
|
else mScale.set( height/mBmpRatio, height, 1 );
|
94 |
d381aa80
|
Leszek Koltunski
|
|
95 |
392e16fd
|
Leszek Koltunski
|
mScreen.resize(width, height);
|
96 |
bc0a685b
|
Leszek Koltunski
|
}
|
97 |
427ab7bf
|
Leszek Koltunski
|
|
98 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
99 |
|
|
|
100 |
bc0a685b
|
Leszek Koltunski
|
public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
|
101 |
|
|
{
|
102 |
13155c68
|
Leszek Koltunski
|
InputStream is = mView.getContext().getResources().openRawResource(R.raw.cat);
|
103 |
bc0a685b
|
Leszek Koltunski
|
Bitmap bitmap;
|
104 |
427ab7bf
|
Leszek Koltunski
|
|
105 |
bc0a685b
|
Leszek Koltunski
|
try
|
106 |
|
|
{
|
107 |
|
|
bitmap = BitmapFactory.decodeStream(is);
|
108 |
|
|
}
|
109 |
|
|
finally
|
110 |
|
|
{
|
111 |
427ab7bf
|
Leszek Koltunski
|
try
|
112 |
|
|
{
|
113 |
bc0a685b
|
Leszek Koltunski
|
is.close();
|
114 |
427ab7bf
|
Leszek Koltunski
|
}
|
115 |
bc0a685b
|
Leszek Koltunski
|
catch(IOException e) { }
|
116 |
|
|
}
|
117 |
427ab7bf
|
Leszek Koltunski
|
|
118 |
f209a803
|
Leszek Koltunski
|
mBmpRatio = (float)bitmap.getHeight()/bitmap.getWidth();
|
119 |
b5cc7760
|
Leszek Koltunski
|
|
120 |
b0ebdf5e
|
Leszek Koltunski
|
mTexture.setTexture(bitmap);
|
121 |
26c4e181
|
Leszek Koltunski
|
|
122 |
f209a803
|
Leszek Koltunski
|
if( mMesh==null ) mMesh = new MeshRectangles(30,(int)(30*mBmpRatio));
|
123 |
698ad0a8
|
Leszek Koltunski
|
|
124 |
fe59d375
|
Leszek Koltunski
|
mScreen.detachAll();
|
125 |
5f2a53b6
|
leszek
|
mScreen.attach(mTexture,mEffects,mMesh);
|
126 |
b5cc7760
|
Leszek Koltunski
|
|
127 |
885b9cca
|
leszek
|
VertexEffectSink.enable();
|
128 |
061449ed
|
Leszek Koltunski
|
DistortedLibrary.onCreate(mView.getContext(), this);
|
129 |
|
|
}
|
130 |
6637d0f2
|
Leszek Koltunski
|
|
131 |
061449ed
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
132 |
|
|
|
133 |
|
|
public void distortedException(Exception ex)
|
134 |
|
|
{
|
135 |
|
|
android.util.Log.e("Sink", ex.getMessage() );
|
136 |
bc0a685b
|
Leszek Koltunski
|
}
|
137 |
|
|
}
|