1
|
|
2
|
package org.distorted.examples.monalisa;
|
3
|
|
4
|
import java.io.IOException;
|
5
|
import java.io.InputStream;
|
6
|
|
7
|
import javax.microedition.khronos.egl.EGLConfig;
|
8
|
import javax.microedition.khronos.opengles.GL10;
|
9
|
|
10
|
import org.distorted.examples.R;
|
11
|
import org.distorted.library.Distorted;
|
12
|
import org.distorted.library.DistortedBitmap;
|
13
|
import org.distorted.library.Float2D;
|
14
|
import org.distorted.library.Float3D;
|
15
|
import org.distorted.library.Float4D;
|
16
|
|
17
|
import android.graphics.Bitmap;
|
18
|
import android.graphics.BitmapFactory;
|
19
|
import android.opengl.GLES20;
|
20
|
import android.opengl.GLSurfaceView;
|
21
|
|
22
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
23
|
|
24
|
class MonaLisaRenderer implements GLSurfaceView.Renderer
|
25
|
{
|
26
|
private GLSurfaceView mView;
|
27
|
private DistortedBitmap monaLisa;
|
28
|
private Float2D pLeft, pRight;
|
29
|
private Float4D rLeft, rRight;
|
30
|
private Float3D vLeft, vRight;
|
31
|
private int bmpHeight, bmpWidth;
|
32
|
|
33
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
34
|
|
35
|
public MonaLisaRenderer(GLSurfaceView v)
|
36
|
{
|
37
|
mView = v;
|
38
|
|
39
|
pLeft = new Float2D( 90, 258);
|
40
|
pRight= new Float2D(176, 255);
|
41
|
|
42
|
rLeft = new Float4D(-10,-10,25,25);
|
43
|
rRight= new Float4D( 10, -5,25,25);
|
44
|
|
45
|
vLeft = new Float3D(-20,-20,0);
|
46
|
vRight= new Float3D( 20,-10,0);
|
47
|
}
|
48
|
|
49
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
50
|
|
51
|
public void onDrawFrame(GL10 glUnused)
|
52
|
{
|
53
|
GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
54
|
GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
|
55
|
|
56
|
monaLisa.draw(System.currentTimeMillis());
|
57
|
}
|
58
|
|
59
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
60
|
|
61
|
public void onSurfaceChanged(GL10 glUnused, int width, int height)
|
62
|
{
|
63
|
monaLisa.abortAllEffects(Distorted.TYPE_PRE);
|
64
|
|
65
|
if( bmpHeight/bmpWidth > height/width )
|
66
|
{
|
67
|
int w = (height*bmpWidth)/bmpHeight;
|
68
|
monaLisa.move((width-w)/2 ,0, 0);
|
69
|
monaLisa.scale((float)height/bmpHeight);
|
70
|
}
|
71
|
else
|
72
|
{
|
73
|
int h = (width*bmpHeight)/bmpWidth;
|
74
|
monaLisa.move(0 ,(height-h)/2, 0);
|
75
|
monaLisa.scale((float)width/bmpWidth);
|
76
|
}
|
77
|
|
78
|
Distorted.onSurfaceChanged(width, height);
|
79
|
}
|
80
|
|
81
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
82
|
|
83
|
public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
|
84
|
{
|
85
|
InputStream is = mView.getContext().getResources().openRawResource(R.raw.monalisa);
|
86
|
Bitmap bitmap;
|
87
|
|
88
|
try
|
89
|
{
|
90
|
bitmap = BitmapFactory.decodeStream(is);
|
91
|
}
|
92
|
finally
|
93
|
{
|
94
|
try
|
95
|
{
|
96
|
is.close();
|
97
|
}
|
98
|
catch(IOException e) { }
|
99
|
}
|
100
|
|
101
|
bmpHeight = bitmap.getHeight();
|
102
|
bmpWidth = bitmap.getWidth();
|
103
|
|
104
|
monaLisa = new DistortedBitmap(bitmap, 10);
|
105
|
monaLisa.distort( vLeft, rLeft , pLeft, 1000, 0);
|
106
|
monaLisa.distort(vRight, rRight, pRight,1000, 0);
|
107
|
|
108
|
try
|
109
|
{
|
110
|
Distorted.onSurfaceCreated(mView.getContext());
|
111
|
}
|
112
|
catch(Exception ex)
|
113
|
{
|
114
|
android.util.Log.e("MonaLisa", ex.getMessage() );
|
115
|
}
|
116
|
}
|
117
|
}
|