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.monalisa;
|
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
|
import org.distorted.library.effect.MatrixEffectScale;
|
30
|
import org.distorted.library.effect.VertexEffectDistort;
|
31
|
import org.distorted.library.main.DistortedLibrary;
|
32
|
import org.distorted.library.main.DistortedEffects;
|
33
|
import org.distorted.library.main.DistortedScreen;
|
34
|
import org.distorted.library.main.DistortedTexture;
|
35
|
import org.distorted.library.mesh.MeshRectangles;
|
36
|
import org.distorted.library.type.Dynamic3D;
|
37
|
import org.distorted.library.type.Static3D;
|
38
|
import org.distorted.library.type.Static4D;
|
39
|
|
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
|
private DistortedEffects mEffects;
|
50
|
private DistortedTexture mTexture;
|
51
|
private MeshRectangles mMesh;
|
52
|
private DistortedScreen mScreen;
|
53
|
private Static3D mScale;
|
54
|
private float mBmpRatio;
|
55
|
|
56
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
57
|
|
58
|
MonaLisaRenderer(GLSurfaceView v)
|
59
|
{
|
60
|
mView = v;
|
61
|
|
62
|
// Centers of the Effects.
|
63
|
// center = (0,0,0) would be the midpoint of the Bitmap.
|
64
|
// MonaLisa bitmap is 320x366, this is thus (90,108) pixels from the lower-left corner
|
65
|
Static3D centerLeft = new Static3D( ( 90-320*0.5f)/320.0f, (108-366*0.5f)/366.0f, 0);
|
66
|
// (176,111) from the lower left
|
67
|
Static3D centerRight = new Static3D( (176-320*0.5f)/320.0f, (111-366*0.5f)/366.0f, 0);
|
68
|
|
69
|
// two Regions defining the areas affected by the Distort effect
|
70
|
// a Region is like a mask that specifies which area around the Center is affected by
|
71
|
// the Distort. Here (-10,10,0) pixels from the left center, with radius 25 pixels
|
72
|
Static4D regionLeft = new Static4D( -10/320.0f, 10/366.0f, 0, 25/320.0f);
|
73
|
// and likewise (10,5,0) pixels from the right Center, radius 25 pixels.
|
74
|
Static4D regionRight = new Static4D( 10/320.0f, 5/366.0f, 0, 25/320.0f);
|
75
|
|
76
|
// two dynamics for interpolating through vectors of distortion. Interpolate
|
77
|
// every 1000 milliseconds, indefinitely ('0.0f').
|
78
|
Dynamic3D degLeft = new Dynamic3D(1000,0.0f);
|
79
|
Dynamic3D degRight= new Dynamic3D(1000,0.0f);
|
80
|
|
81
|
// two vectors of distortion the left tip of the mouth gets distorted with -
|
82
|
// interpolated from (0,0,0) - i.e. no distortion - to (-20 pixels, 20 pixels, 0),
|
83
|
// i.e. slightly to the top left.
|
84
|
degLeft.add ( new Static3D( 0, 0, 0) );
|
85
|
degLeft.add ( new Static3D(-20/320.0f, 20/366.0f, 0) );
|
86
|
|
87
|
// likewise two vectors the right tip is distorted with.
|
88
|
degRight.add( new Static3D( 0, 0, 0) );
|
89
|
degRight.add( new Static3D( 20/320.0f, 10/366.0f, 0) );
|
90
|
|
91
|
// Equip MonaLisa with the Effects we want to draw her with - i.e. two Distorts of the mouth
|
92
|
mEffects = new DistortedEffects();
|
93
|
mEffects.apply( new VertexEffectDistort( degLeft , centerLeft , regionLeft ) );
|
94
|
mEffects.apply( new VertexEffectDistort( degRight, centerRight, regionRight) );
|
95
|
|
96
|
// ... and a Scale - so far by (1,1,1), i.e. no scale. The mScale point will be computed
|
97
|
// later, in onSurfaceChanged, when we actually know the size of the screen and thus how
|
98
|
// much to scale the initial Mesh (which is 1x1x0 in size)
|
99
|
mScale= new Static3D(1,1,1);
|
100
|
mEffects.apply(new MatrixEffectScale(mScale));
|
101
|
|
102
|
mTexture = new DistortedTexture();
|
103
|
mScreen = new DistortedScreen();
|
104
|
}
|
105
|
|
106
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
107
|
|
108
|
public void onDrawFrame(GL10 glUnused)
|
109
|
{
|
110
|
mScreen.render( System.currentTimeMillis() );
|
111
|
}
|
112
|
|
113
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
114
|
|
115
|
public void onSurfaceChanged(GL10 glUnused, int width, int height)
|
116
|
{
|
117
|
if( width<height ) mScale.set( width, width*mBmpRatio, 1 );
|
118
|
else mScale.set( height/mBmpRatio, height, 1 );
|
119
|
|
120
|
mScreen.resize(width, height);
|
121
|
}
|
122
|
|
123
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
124
|
|
125
|
public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
|
126
|
{
|
127
|
InputStream is = mView.getContext().getResources().openRawResource(R.raw.monalisa);
|
128
|
Bitmap bitmap;
|
129
|
|
130
|
try
|
131
|
{
|
132
|
bitmap = BitmapFactory.decodeStream(is);
|
133
|
}
|
134
|
finally
|
135
|
{
|
136
|
try
|
137
|
{
|
138
|
is.close();
|
139
|
}
|
140
|
catch(IOException e) { }
|
141
|
}
|
142
|
|
143
|
mBmpRatio = (float)bitmap.getHeight()/bitmap.getWidth();
|
144
|
|
145
|
// Create an underlying Mesh of 9x10 vertices - this is for the Distort
|
146
|
// effect to have vertices to distort. We multiply by mBmpRatio here so
|
147
|
// that the Mesh's cells are as close to squares as possible (when they
|
148
|
// finally be rendered with a Scale - see mScale)
|
149
|
if( mMesh==null ) mMesh = new MeshRectangles(9, (int)(9*mBmpRatio) );
|
150
|
|
151
|
// Every time activity goes to background, its OpenGL resources - including
|
152
|
// Textures - get deleted. We always need to call setTexture() here to
|
153
|
// recreate the internal OpenGL textures.
|
154
|
mTexture.setTexture(bitmap);
|
155
|
|
156
|
// Build the Scene Graph - attach all Objects we want to be rendered to the Screen.
|
157
|
mScreen.detachAll();
|
158
|
mScreen.attach(mTexture,mEffects,mMesh);
|
159
|
|
160
|
// All effects are by default disabled! Enable all we need (and better only those)
|
161
|
// before the call to DistortedLibrary.onCreate(). Best done here.
|
162
|
VertexEffectDistort.enable();
|
163
|
|
164
|
try
|
165
|
{
|
166
|
DistortedLibrary.onCreate(mView.getContext());
|
167
|
}
|
168
|
catch(Exception ex)
|
169
|
{
|
170
|
android.util.Log.e("MonaLisa", ex.getMessage() );
|
171
|
}
|
172
|
}
|
173
|
}
|