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.monalisa;
|
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 |
c6526577
|
Leszek Koltunski
|
import org.distorted.library.effect.MatrixEffectScale;
|
30 |
|
|
import org.distorted.library.effect.VertexEffectDistort;
|
31 |
e3900503
|
Leszek Koltunski
|
import org.distorted.library.main.DistortedLibrary;
|
32 |
01782e85
|
Leszek Koltunski
|
import org.distorted.library.main.DistortedEffects;
|
33 |
|
|
import org.distorted.library.main.DistortedScreen;
|
34 |
|
|
import org.distorted.library.main.DistortedTexture;
|
35 |
f3ca895f
|
Leszek Koltunski
|
import org.distorted.library.mesh.MeshRectangles;
|
36 |
7bf107f7
|
Leszek Koltunski
|
import org.distorted.library.type.Dynamic3D;
|
37 |
7589635e
|
Leszek Koltunski
|
import org.distorted.library.type.Static3D;
|
38 |
|
|
import org.distorted.library.type.Static4D;
|
39 |
427ab7bf
|
Leszek Koltunski
|
|
40 |
|
|
import android.graphics.Bitmap;
|
41 |
|
|
import android.graphics.BitmapFactory;
|
42 |
|
|
import android.opengl.GLSurfaceView;
|
43 |
|
|
|
44 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
45 |
|
|
|
46 |
|
|
class MonaLisaRenderer implements GLSurfaceView.Renderer
|
47 |
|
|
{
|
48 |
|
|
private GLSurfaceView mView;
|
49 |
d04a4886
|
Leszek Koltunski
|
private DistortedEffects mEffects;
|
50 |
b0ebdf5e
|
Leszek Koltunski
|
private DistortedTexture mTexture;
|
51 |
f3ca895f
|
Leszek Koltunski
|
private MeshRectangles mMesh;
|
52 |
d218d64e
|
leszek
|
private DistortedScreen mScreen;
|
53 |
d381aa80
|
Leszek Koltunski
|
private Static3D mScale;
|
54 |
7bf107f7
|
Leszek Koltunski
|
|
55 |
427ab7bf
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
56 |
|
|
|
57 |
f6d884d5
|
Leszek Koltunski
|
MonaLisaRenderer(GLSurfaceView v)
|
58 |
427ab7bf
|
Leszek Koltunski
|
{
|
59 |
|
|
mView = v;
|
60 |
8ff32d4d
|
Leszek Koltunski
|
|
61 |
849e0034
|
Leszek Koltunski
|
// MonaLisa bitmap is 320x366, this is thus (90,108) pixels from the lower-left corner
|
62 |
|
|
Static3D centerLeft = new Static3D( 90-320/2, 108-366/2,0);
|
63 |
|
|
// (176,111) from the lower left
|
64 |
|
|
Static3D centerRight = new Static3D(176-320/2, 111-366/2,0);
|
65 |
8ff32d4d
|
Leszek Koltunski
|
|
66 |
a4d59c0b
|
Leszek Koltunski
|
// two regions defining the areas affected by the Distort effect
|
67 |
|
|
Static4D rLeft = new Static4D(-10, 10, 0, 25);
|
68 |
|
|
Static4D rRight = new Static4D( 10, 5, 0, 25);
|
69 |
|
|
|
70 |
|
|
// two dynamics for interpolating through vectors of distortion. Interpolate
|
71 |
|
|
// every 1000 miliseconds, indefinately ('0.0f').
|
72 |
f6d884d5
|
Leszek Koltunski
|
Dynamic3D dLeft = new Dynamic3D(1000,0.0f);
|
73 |
|
|
Dynamic3D dRight= new Dynamic3D(1000,0.0f);
|
74 |
a4d59c0b
|
Leszek Koltunski
|
|
75 |
|
|
// two vectors of distortion the left tip of the mouth gets distorted with -
|
76 |
|
|
// interpolated from (0,0,0) - i.e. no distortion - to (-20,20,0), i.e.
|
77 |
|
|
// slightly to the top left.
|
78 |
|
|
dLeft.add ( new Static3D( 0, 0, 0) );
|
79 |
|
|
dLeft.add ( new Static3D(-20, 20, 0) );
|
80 |
|
|
|
81 |
|
|
// likewise two vectors the right tip is distorted with.
|
82 |
|
|
dRight.add( new Static3D( 0, 0, 0) );
|
83 |
|
|
dRight.add( new Static3D( 20, 10, 0) );
|
84 |
f6d884d5
|
Leszek Koltunski
|
|
85 |
698ad0a8
|
Leszek Koltunski
|
mEffects = new DistortedEffects();
|
86 |
849e0034
|
Leszek Koltunski
|
mEffects.apply( new VertexEffectDistort(dLeft , centerLeft , rLeft ) );
|
87 |
|
|
mEffects.apply( new VertexEffectDistort(dRight, centerRight, rRight) );
|
88 |
c6526577
|
Leszek Koltunski
|
|
89 |
|
|
mScale= new Static3D(1,1,1);
|
90 |
|
|
mEffects.apply(new MatrixEffectScale(mScale));
|
91 |
392e16fd
|
Leszek Koltunski
|
|
92 |
e4330c89
|
Leszek Koltunski
|
mScreen = new DistortedScreen();
|
93 |
427ab7bf
|
Leszek Koltunski
|
}
|
94 |
|
|
|
95 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
96 |
|
|
|
97 |
|
|
public void onDrawFrame(GL10 glUnused)
|
98 |
|
|
{
|
99 |
fe59d375
|
Leszek Koltunski
|
mScreen.render( System.currentTimeMillis() );
|
100 |
427ab7bf
|
Leszek Koltunski
|
}
|
101 |
|
|
|
102 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
103 |
|
|
|
104 |
|
|
public void onSurfaceChanged(GL10 glUnused, int width, int height)
|
105 |
d381aa80
|
Leszek Koltunski
|
{
|
106 |
26c4e181
|
Leszek Koltunski
|
float horiRatio = (float)width / mMesh.getStretchX();
|
107 |
|
|
float vertRatio = (float)height/ mMesh.getStretchY();
|
108 |
d381aa80
|
Leszek Koltunski
|
float factor = horiRatio > vertRatio ? vertRatio : horiRatio;
|
109 |
7589635e
|
Leszek Koltunski
|
|
110 |
d381aa80
|
Leszek Koltunski
|
mScale.set( factor,factor,factor );
|
111 |
392e16fd
|
Leszek Koltunski
|
mScreen.resize(width, height);
|
112 |
427ab7bf
|
Leszek Koltunski
|
}
|
113 |
|
|
|
114 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
115 |
|
|
|
116 |
|
|
public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
|
117 |
|
|
{
|
118 |
|
|
InputStream is = mView.getContext().getResources().openRawResource(R.raw.monalisa);
|
119 |
|
|
Bitmap bitmap;
|
120 |
|
|
|
121 |
|
|
try
|
122 |
|
|
{
|
123 |
|
|
bitmap = BitmapFactory.decodeStream(is);
|
124 |
|
|
}
|
125 |
|
|
finally
|
126 |
|
|
{
|
127 |
|
|
try
|
128 |
|
|
{
|
129 |
|
|
is.close();
|
130 |
|
|
}
|
131 |
|
|
catch(IOException e) { }
|
132 |
|
|
}
|
133 |
3c28857f
|
Leszek Koltunski
|
|
134 |
d381aa80
|
Leszek Koltunski
|
int bmpHeight = bitmap.getHeight();
|
135 |
|
|
int bmpWidth = bitmap.getWidth();
|
136 |
8ff32d4d
|
Leszek Koltunski
|
|
137 |
698ad0a8
|
Leszek Koltunski
|
// We could have gotten here after the activity went to the background
|
138 |
|
|
// for a brief amount of time; in this case mTexture is already created.
|
139 |
|
|
// Do not leak memory by creating it the second time around.
|
140 |
|
|
if( mTexture==null ) mTexture = new DistortedTexture();
|
141 |
|
|
|
142 |
|
|
// likewise the Mesh
|
143 |
687263cc
|
Leszek Koltunski
|
// This will make the Mesh stretched by bmpWidth x bmpHeight even before any effects
|
144 |
|
|
// are applied to it (the Mesh - MeshRectangles - is flat, so the third parameter does not
|
145 |
|
|
// not matter). bmpWight x bmpHeight is the size of the Bitmap, thus this means that we can
|
146 |
|
|
// conveniently work with Effects thinking in Bitmap's native size in pixels. The origin is
|
147 |
|
|
// in Bitmap's lower-left corner, thus e.g. a rotation which is meant to rotate the bitmap
|
148 |
|
|
// around its center has to be centered at (bmpWidth/2, bmpHeight/2, 0).
|
149 |
|
|
// Without this call, the default size of the Mesh is 1x1x0 ( or 1x1x1 in case of not-flat
|
150 |
|
|
// Meshes) so we would need to be rotating around (0.5,0.5,0.0).
|
151 |
698ad0a8
|
Leszek Koltunski
|
if( mMesh==null )
|
152 |
|
|
{
|
153 |
|
|
mMesh = new MeshRectangles(9,9*bmpHeight/bmpWidth);
|
154 |
|
|
mMesh.setStretch(bmpWidth,bmpHeight,0);
|
155 |
|
|
}
|
156 |
5f2a53b6
|
leszek
|
|
157 |
b0ebdf5e
|
Leszek Koltunski
|
// even if mTexture wasn't null, we still need to call setTexture() on it
|
158 |
|
|
// because every time activity goes to background, its OpenGL resources
|
159 |
|
|
// - including Textures - get deleted. We always need to call setTexture()
|
160 |
|
|
// to recreate the internal OpenGL textures.
|
161 |
|
|
mTexture.setTexture(bitmap);
|
162 |
fe59d375
|
Leszek Koltunski
|
|
163 |
|
|
mScreen.detachAll();
|
164 |
5f2a53b6
|
leszek
|
mScreen.attach(mTexture,mEffects,mMesh);
|
165 |
7bf107f7
|
Leszek Koltunski
|
|
166 |
a4d59c0b
|
Leszek Koltunski
|
// All effects are by default disabled!
|
167 |
885b9cca
|
leszek
|
VertexEffectDistort.enable();
|
168 |
6637d0f2
|
Leszek Koltunski
|
|
169 |
427ab7bf
|
Leszek Koltunski
|
try
|
170 |
|
|
{
|
171 |
e3900503
|
Leszek Koltunski
|
DistortedLibrary.onCreate(mView.getContext());
|
172 |
427ab7bf
|
Leszek Koltunski
|
}
|
173 |
|
|
catch(Exception ex)
|
174 |
|
|
{
|
175 |
|
|
android.util.Log.e("MonaLisa", ex.getMessage() );
|
176 |
|
|
}
|
177 |
|
|
}
|
178 |
|
|
}
|