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.differentbitmaps;
|
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 |
8dfa45c4
|
leszek
|
import org.distorted.library.effect.MatrixEffectMove;
|
31 |
|
|
import org.distorted.library.effect.MatrixEffectScale;
|
32 |
|
|
import org.distorted.library.effect.VertexEffectDistort;
|
33 |
5c3b92c4
|
Leszek Koltunski
|
import org.distorted.library.effect.VertexEffectScale;
|
34 |
8dfa45c4
|
leszek
|
import org.distorted.library.effect.VertexEffectSink;
|
35 |
e3900503
|
Leszek Koltunski
|
import org.distorted.library.main.DistortedLibrary;
|
36 |
01782e85
|
Leszek Koltunski
|
import org.distorted.library.main.DistortedScreen;
|
37 |
|
|
import org.distorted.library.main.DistortedTexture;
|
38 |
|
|
import org.distorted.library.main.DistortedEffects;
|
39 |
f3ca895f
|
Leszek Koltunski
|
import org.distorted.library.mesh.MeshRectangles;
|
40 |
b5cc7760
|
Leszek Koltunski
|
import org.distorted.library.type.Dynamic3D;
|
41 |
|
|
import org.distorted.library.type.Static1D;
|
42 |
7589635e
|
Leszek Koltunski
|
import org.distorted.library.type.Static3D;
|
43 |
|
|
import org.distorted.library.type.Static4D;
|
44 |
427ab7bf
|
Leszek Koltunski
|
|
45 |
|
|
import android.graphics.Bitmap;
|
46 |
|
|
import android.graphics.BitmapFactory;
|
47 |
|
|
import android.opengl.GLSurfaceView;
|
48 |
|
|
|
49 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
50 |
|
|
|
51 |
061449ed
|
Leszek Koltunski
|
class DifferentBitmapsRenderer implements GLSurfaceView.Renderer, DistortedLibrary.ExceptionListener
|
52 |
427ab7bf
|
Leszek Koltunski
|
{
|
53 |
16b22aab
|
Leszek Koltunski
|
private static final int[] bitmap = { R.raw.dog, R.raw.face, R.raw.cat };
|
54 |
|
|
private static final int NUM = bitmap.length;
|
55 |
427ab7bf
|
Leszek Koltunski
|
|
56 |
|
|
private GLSurfaceView mView;
|
57 |
d04a4886
|
Leszek Koltunski
|
private DistortedEffects[] mEffects;
|
58 |
b0ebdf5e
|
Leszek Koltunski
|
private DistortedTexture[] mTexture;
|
59 |
f3ca895f
|
Leszek Koltunski
|
private MeshRectangles mMesh;
|
60 |
d218d64e
|
leszek
|
private DistortedScreen mScreen;
|
61 |
5c3b92c4
|
Leszek Koltunski
|
private Static3D mScaleMatrix, mScaleVertex;
|
62 |
8dfa45c4
|
leszek
|
private Static3D[] mMove;
|
63 |
|
|
|
64 |
427ab7bf
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
65 |
|
|
|
66 |
b4237a76
|
Leszek Koltunski
|
DifferentBitmapsRenderer(GLSurfaceView v)
|
67 |
427ab7bf
|
Leszek Koltunski
|
{
|
68 |
|
|
mView = v;
|
69 |
|
|
|
70 |
f6d884d5
|
Leszek Koltunski
|
Dynamic3D dDistort = new Dynamic3D(3000,0.0f);
|
71 |
b4237a76
|
Leszek Koltunski
|
dDistort.add(new Static3D( 25,0,0));
|
72 |
|
|
dDistort.add(new Static3D(-25,0,0));
|
73 |
513b2e9c
|
Leszek Koltunski
|
Static3D point = new Static3D(0,-60,0);
|
74 |
|
|
Static4D region= new Static4D(0,0,0,300);
|
75 |
f6d884d5
|
Leszek Koltunski
|
|
76 |
5c3b92c4
|
Leszek Koltunski
|
mScaleMatrix = new Static3D(1,1,1);
|
77 |
|
|
mScaleVertex = new Static3D(1,1,1);
|
78 |
|
|
|
79 |
d04a4886
|
Leszek Koltunski
|
mEffects = new DistortedEffects[NUM];
|
80 |
698ad0a8
|
Leszek Koltunski
|
mEffects[0] = new DistortedEffects();
|
81 |
f6d884d5
|
Leszek Koltunski
|
for(int i=1; i<NUM; i++)
|
82 |
e3900503
|
Leszek Koltunski
|
mEffects[i] = new DistortedEffects(mEffects[0], DistortedLibrary.CLONE_VERTEX| DistortedLibrary.CLONE_FRAGMENT);
|
83 |
f6d884d5
|
Leszek Koltunski
|
|
84 |
|
|
// Add the effects only to the first queue - all VERTEX and FRAGMENT effects are shared!
|
85 |
|
|
// (Matrix effect cannot be shared as we have to display each Texture in a different location)
|
86 |
5c3b92c4
|
Leszek Koltunski
|
VertexEffectScale scale = new VertexEffectScale(mScaleVertex);
|
87 |
|
|
VertexEffectSink sink = new VertexEffectSink(new Static1D(8), point, new Static4D(0,0,0,80));
|
88 |
513b2e9c
|
Leszek Koltunski
|
VertexEffectDistort distort = new VertexEffectDistort(dDistort,point,region);
|
89 |
5c3b92c4
|
Leszek Koltunski
|
mEffects[0].apply(scale); // scale the whole thing (so far by 1, but mScaleVertex will be reset once we know the size of the bitmap)
|
90 |
8dfa45c4
|
leszek
|
mEffects[0].apply(sink); // enlarge the nose
|
91 |
|
|
mEffects[0].apply(distort); // keep moving the whole bitmap left and right.
|
92 |
|
|
|
93 |
|
|
// Now the Matrix effects
|
94 |
5c3b92c4
|
Leszek Koltunski
|
|
95 |
|
|
MatrixEffectScale scaleEffect = new MatrixEffectScale(mScaleMatrix);
|
96 |
8dfa45c4
|
leszek
|
mMove = new Static3D[NUM];
|
97 |
|
|
MatrixEffectMove[] moveEffect = new MatrixEffectMove[NUM];
|
98 |
|
|
|
99 |
|
|
for(int i=0; i<NUM; i++)
|
100 |
|
|
{
|
101 |
|
|
mMove[i] = new Static3D(0,0,0);
|
102 |
|
|
moveEffect[i] = new MatrixEffectMove(mMove[i]);
|
103 |
|
|
mEffects[i].apply(scaleEffect);
|
104 |
dae661e9
|
Leszek Koltunski
|
mEffects[i].apply(moveEffect[i]);
|
105 |
8dfa45c4
|
leszek
|
}
|
106 |
392e16fd
|
Leszek Koltunski
|
|
107 |
e4330c89
|
Leszek Koltunski
|
mScreen = new DistortedScreen();
|
108 |
855ba24e
|
leszek
|
mScreen.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
|
109 |
427ab7bf
|
Leszek Koltunski
|
}
|
110 |
|
|
|
111 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
112 |
|
|
|
113 |
e4330c89
|
Leszek Koltunski
|
public void onDrawFrame(GL10 glUnused)
|
114 |
427ab7bf
|
Leszek Koltunski
|
{
|
115 |
fe59d375
|
Leszek Koltunski
|
mScreen.render( System.currentTimeMillis() );
|
116 |
427ab7bf
|
Leszek Koltunski
|
}
|
117 |
|
|
|
118 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
119 |
|
|
|
120 |
e4330c89
|
Leszek Koltunski
|
public void onSurfaceChanged(GL10 glUnused, int width, int height)
|
121 |
|
|
{
|
122 |
5c3b92c4
|
Leszek Koltunski
|
float horiRatio = (float)width / (NUM*mScaleVertex.get0());
|
123 |
|
|
float vertRatio = (float)height/ ( mScaleVertex.get1());
|
124 |
|
|
float factor = Math.min(horiRatio, vertRatio);
|
125 |
e4330c89
|
Leszek Koltunski
|
|
126 |
5c3b92c4
|
Leszek Koltunski
|
mScaleMatrix.set( factor,factor,factor );
|
127 |
8dfa45c4
|
leszek
|
|
128 |
5c3b92c4
|
Leszek Koltunski
|
for(int i=NUM-1; i>=0; i--) mMove[i].set((i-(NUM-1)/2.0f)*width/NUM, 0, 0);
|
129 |
8dfa45c4
|
leszek
|
|
130 |
e4330c89
|
Leszek Koltunski
|
mScreen.resize(width, height);
|
131 |
|
|
}
|
132 |
427ab7bf
|
Leszek Koltunski
|
|
133 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
134 |
|
|
|
135 |
e4330c89
|
Leszek Koltunski
|
public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
|
136 |
|
|
{
|
137 |
16b22aab
|
Leszek Koltunski
|
Bitmap[] bmp = new Bitmap[NUM];
|
138 |
|
|
|
139 |
|
|
for(int i=0; i<NUM; i++)
|
140 |
|
|
{
|
141 |
|
|
bmp[i] = readBitmap(bitmap[i]);
|
142 |
|
|
}
|
143 |
|
|
|
144 |
|
|
int bmpHeight = bmp[0].getHeight();
|
145 |
|
|
int bmpWidth = bmp[0].getWidth();
|
146 |
b0ebdf5e
|
Leszek Koltunski
|
|
147 |
5c3b92c4
|
Leszek Koltunski
|
mScaleVertex.set(bmpWidth,bmpHeight,0);
|
148 |
|
|
|
149 |
e4330c89
|
Leszek Koltunski
|
if( mTexture==null )
|
150 |
|
|
{
|
151 |
|
|
mTexture = new DistortedTexture[NUM];
|
152 |
b0ebdf5e
|
Leszek Koltunski
|
|
153 |
e4330c89
|
Leszek Koltunski
|
for(int i=0; i<NUM; i++)
|
154 |
687263cc
|
Leszek Koltunski
|
mTexture[i] = new DistortedTexture();
|
155 |
e4330c89
|
Leszek Koltunski
|
}
|
156 |
b0ebdf5e
|
Leszek Koltunski
|
|
157 |
16b22aab
|
Leszek Koltunski
|
for(int i=0; i<NUM; i++)
|
158 |
|
|
{
|
159 |
|
|
mTexture[i].setTexture(bmp[i]);
|
160 |
|
|
}
|
161 |
fe59d375
|
Leszek Koltunski
|
|
162 |
5c3b92c4
|
Leszek Koltunski
|
if( mMesh==null ) mMesh = new MeshRectangles(30,30*bmpHeight/bmpWidth);
|
163 |
e8b6aa95
|
Leszek Koltunski
|
|
164 |
e4330c89
|
Leszek Koltunski
|
mScreen.detachAll();
|
165 |
|
|
for(int i=NUM-1; i>=0; i--) mScreen.attach(mTexture[i], mEffects[i], mMesh);
|
166 |
e8b6aa95
|
Leszek Koltunski
|
|
167 |
5c3b92c4
|
Leszek Koltunski
|
VertexEffectScale.enable();
|
168 |
885b9cca
|
leszek
|
VertexEffectSink.enable();
|
169 |
|
|
VertexEffectDistort.enable();
|
170 |
6637d0f2
|
Leszek Koltunski
|
|
171 |
061449ed
|
Leszek Koltunski
|
DistortedLibrary.onCreate(mView.getContext(),this);
|
172 |
|
|
}
|
173 |
|
|
|
174 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
175 |
|
|
|
176 |
|
|
public void distortedException(Exception ex)
|
177 |
|
|
{
|
178 |
|
|
android.util.Log.e("DifferentBitmaps", ex.getMessage() );
|
179 |
e4330c89
|
Leszek Koltunski
|
}
|
180 |
16b22aab
|
Leszek Koltunski
|
|
181 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
182 |
|
|
|
183 |
|
|
private Bitmap readBitmap(int id)
|
184 |
|
|
{
|
185 |
|
|
InputStream is = mView.getContext().getResources().openRawResource(id);
|
186 |
|
|
Bitmap bitmap;
|
187 |
|
|
|
188 |
|
|
try
|
189 |
|
|
{
|
190 |
|
|
bitmap = BitmapFactory.decodeStream(is);
|
191 |
|
|
}
|
192 |
|
|
finally
|
193 |
|
|
{
|
194 |
|
|
try
|
195 |
|
|
{
|
196 |
|
|
is.close();
|
197 |
|
|
}
|
198 |
|
|
catch(IOException e) { }
|
199 |
|
|
}
|
200 |
|
|
|
201 |
|
|
return bitmap;
|
202 |
|
|
}
|
203 |
|
|
|
204 |
427ab7bf
|
Leszek Koltunski
|
}
|