1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2016 Leszek Koltunski //
|
3
|
// //
|
4
|
// This file is part of Distorted. //
|
5
|
// //
|
6
|
// Distorted is free software: you can redistribute it and/or modify //
|
7
|
// 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
|
// Distorted is distributed in the hope that it will be useful, //
|
12
|
// 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
|
// along with Distorted. If not, see <http://www.gnu.org/licenses/>. //
|
18
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
19
|
|
20
|
package org.distorted.examples.sink;
|
21
|
|
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
|
import org.distorted.examples.R;
|
29
|
|
30
|
import org.distorted.library.Distorted;
|
31
|
import org.distorted.library.DistortedBitmap;
|
32
|
import org.distorted.library.EffectTypes;
|
33
|
import org.distorted.library.type.Static2D;
|
34
|
import org.distorted.library.type.Static3D;
|
35
|
import org.distorted.library.type.Static4D;
|
36
|
|
37
|
import android.graphics.Bitmap;
|
38
|
import android.graphics.BitmapFactory;
|
39
|
import android.opengl.GLES20;
|
40
|
import android.opengl.GLSurfaceView;
|
41
|
|
42
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
43
|
|
44
|
class SinkRenderer implements GLSurfaceView.Renderer
|
45
|
{
|
46
|
private GLSurfaceView mView;
|
47
|
private DistortedBitmap sinkBmp;
|
48
|
private Static2D pLeft, pRight;
|
49
|
private Static4D Region;
|
50
|
private int bmpHeight, bmpWidth;
|
51
|
|
52
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
53
|
|
54
|
public SinkRenderer(GLSurfaceView v)
|
55
|
{
|
56
|
mView = v;
|
57
|
|
58
|
pLeft = new Static2D(214, 206);
|
59
|
pRight= new Static2D(390, 212);
|
60
|
Region= new Static4D(0,0,60,60);
|
61
|
}
|
62
|
|
63
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
64
|
|
65
|
public void onDrawFrame(GL10 glUnused)
|
66
|
{
|
67
|
GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
68
|
GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
|
69
|
|
70
|
sinkBmp.draw(System.currentTimeMillis());
|
71
|
}
|
72
|
|
73
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
74
|
|
75
|
public void onSurfaceChanged(GL10 glUnused, int width, int height)
|
76
|
{
|
77
|
sinkBmp.abortEffects(EffectTypes.MATRIX);
|
78
|
|
79
|
if( bmpHeight/bmpWidth > height/width )
|
80
|
{
|
81
|
int w = (height*bmpWidth)/bmpHeight;
|
82
|
float factor = (float)height/bmpHeight;
|
83
|
|
84
|
sinkBmp.move( new Static3D((width-w)/2,0,0) );
|
85
|
sinkBmp.scale( new Static3D(factor,factor,factor) );
|
86
|
}
|
87
|
else
|
88
|
{
|
89
|
int h = (width*bmpHeight)/bmpWidth;
|
90
|
float factor = (float)width/bmpWidth;
|
91
|
|
92
|
sinkBmp.move( new Static3D(0,(height-h)/2,0) );
|
93
|
sinkBmp.scale( new Static3D(factor,factor,factor) );
|
94
|
}
|
95
|
|
96
|
Distorted.onSurfaceChanged(width, height);
|
97
|
}
|
98
|
|
99
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
100
|
|
101
|
public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
|
102
|
{
|
103
|
InputStream is = mView.getContext().getResources().openRawResource(R.raw.dog);
|
104
|
Bitmap bitmap;
|
105
|
|
106
|
try
|
107
|
{
|
108
|
bitmap = BitmapFactory.decodeStream(is);
|
109
|
}
|
110
|
finally
|
111
|
{
|
112
|
try
|
113
|
{
|
114
|
is.close();
|
115
|
}
|
116
|
catch(IOException e) { }
|
117
|
}
|
118
|
|
119
|
bmpHeight = bitmap.getHeight();
|
120
|
bmpWidth = bitmap.getWidth();
|
121
|
|
122
|
sinkBmp = new DistortedBitmap(bitmap, 30);
|
123
|
sinkBmp.sink( 10.0f, Region, pLeft, 2000, 0.0f);
|
124
|
sinkBmp.sink(10.0f, Region, pRight,2000, 0.0f);
|
125
|
|
126
|
try
|
127
|
{
|
128
|
Distorted.onSurfaceCreated(mView.getContext());
|
129
|
}
|
130
|
catch(Exception ex)
|
131
|
{
|
132
|
android.util.Log.e("Sink", ex.getMessage() );
|
133
|
}
|
134
|
}
|
135
|
}
|