1 |
43bda3db
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2 |
|
|
// Copyright 2016 Leszek Koltunski //
|
3 |
|
|
// //
|
4 |
e3900503
|
Leszek Koltunski
|
// This file is part of DistortedLibrary. //
|
5 |
43bda3db
|
Leszek Koltunski
|
// //
|
6 |
e3900503
|
Leszek Koltunski
|
// DistortedLibrary is free software: you can redistribute it and/or modify //
|
7 |
43bda3db
|
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 |
e3900503
|
Leszek Koltunski
|
// DistortedLibrary is distributed in the hope that it will be useful, //
|
12 |
43bda3db
|
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 |
e3900503
|
Leszek Koltunski
|
// along with DistortedLibrary. If not, see <http://www.gnu.org/licenses/>. //
|
18 |
43bda3db
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
19 |
|
|
|
20 |
|
|
package org.distorted.examples.blur;
|
21 |
|
|
|
22 |
|
|
import android.graphics.Bitmap;
|
23 |
|
|
import android.graphics.BitmapFactory;
|
24 |
|
|
import android.opengl.GLSurfaceView;
|
25 |
|
|
|
26 |
|
|
import org.distorted.examples.R;
|
27 |
fa9b6494
|
Leszek Koltunski
|
import org.distorted.library.effect.MatrixEffectMove;
|
28 |
|
|
import org.distorted.library.effect.MatrixEffectScale;
|
29 |
|
|
import org.distorted.library.effect.PostprocessEffectBlur;
|
30 |
e3900503
|
Leszek Koltunski
|
import org.distorted.library.main.DistortedLibrary;
|
31 |
01782e85
|
Leszek Koltunski
|
import org.distorted.library.main.DistortedEffects;
|
32 |
da57b7da
|
Leszek Koltunski
|
import org.distorted.library.main.DistortedFramebuffer;
|
33 |
01782e85
|
Leszek Koltunski
|
import org.distorted.library.main.DistortedNode;
|
34 |
|
|
import org.distorted.library.main.DistortedScreen;
|
35 |
|
|
import org.distorted.library.main.DistortedTexture;
|
36 |
107e4b72
|
Leszek Koltunski
|
import org.distorted.library.mesh.MeshFlat;
|
37 |
43bda3db
|
Leszek Koltunski
|
import org.distorted.library.type.Dynamic1D;
|
38 |
|
|
import org.distorted.library.type.Static1D;
|
39 |
|
|
import org.distorted.library.type.Static3D;
|
40 |
|
|
|
41 |
|
|
import java.io.IOException;
|
42 |
|
|
import java.io.InputStream;
|
43 |
|
|
|
44 |
|
|
import javax.microedition.khronos.egl.EGLConfig;
|
45 |
|
|
import javax.microedition.khronos.opengles.GL10;
|
46 |
|
|
|
47 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
48 |
|
|
|
49 |
|
|
class BlurRenderer implements GLSurfaceView.Renderer
|
50 |
|
|
{
|
51 |
da57b7da
|
Leszek Koltunski
|
private final static int SIZE = 500;
|
52 |
|
|
|
53 |
43bda3db
|
Leszek Koltunski
|
private GLSurfaceView mView;
|
54 |
59540ba2
|
Leszek Koltunski
|
private DistortedTexture mTexture;
|
55 |
da57b7da
|
Leszek Koltunski
|
private DistortedEffects mEffects, mBufferEffects;
|
56 |
d218d64e
|
leszek
|
private DistortedScreen mScreen;
|
57 |
da57b7da
|
Leszek Koltunski
|
private DistortedFramebuffer mBuffer;
|
58 |
43bda3db
|
Leszek Koltunski
|
private MeshFlat mMesh;
|
59 |
|
|
private Static1D mRadiusSta;
|
60 |
fa9b6494
|
Leszek Koltunski
|
private int mObjHeight, mObjWidth;
|
61 |
da57b7da
|
Leszek Koltunski
|
private Static3D mMove, mScale, mBufferMove, mBufferScale;
|
62 |
43bda3db
|
Leszek Koltunski
|
|
63 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
64 |
|
|
|
65 |
|
|
BlurRenderer(GLSurfaceView v)
|
66 |
|
|
{
|
67 |
66d31749
|
Leszek Koltunski
|
mView = v;
|
68 |
|
|
mMesh = new MeshFlat(1,1);
|
69 |
e4330c89
|
Leszek Koltunski
|
mScreen = new DistortedScreen();
|
70 |
a929d64d
|
Leszek Koltunski
|
mBuffer = new DistortedFramebuffer(SIZE,SIZE,1, DistortedFramebuffer.NO_DEPTH_NO_STENCIL);
|
71 |
43bda3db
|
Leszek Koltunski
|
|
72 |
|
|
mRadiusSta = new Static1D(5);
|
73 |
|
|
Dynamic1D radiusDyn = new Dynamic1D();
|
74 |
|
|
radiusDyn.add(mRadiusSta);
|
75 |
|
|
|
76 |
fa9b6494
|
Leszek Koltunski
|
mMove = new Static3D(0,0,0);
|
77 |
|
|
mScale= new Static3D(1,1,1);
|
78 |
da57b7da
|
Leszek Koltunski
|
mBufferMove = new Static3D(0,0,0);
|
79 |
|
|
mBufferScale= new Static3D(1,1,1);
|
80 |
|
|
|
81 |
|
|
mBufferEffects = new DistortedEffects();
|
82 |
|
|
mBufferEffects.apply(new MatrixEffectMove(mBufferMove));
|
83 |
|
|
mBufferEffects.apply(new MatrixEffectScale(mBufferScale));
|
84 |
fa9b6494
|
Leszek Koltunski
|
|
85 |
66d31749
|
Leszek Koltunski
|
mEffects = new DistortedEffects();
|
86 |
|
|
mEffects.apply( new PostprocessEffectBlur(radiusDyn) );
|
87 |
fa9b6494
|
Leszek Koltunski
|
mEffects.apply(new MatrixEffectMove(mMove));
|
88 |
|
|
mEffects.apply(new MatrixEffectScale(mScale));
|
89 |
43bda3db
|
Leszek Koltunski
|
}
|
90 |
|
|
|
91 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
92 |
|
|
|
93 |
a6da86cc
|
Leszek Koltunski
|
int setBlur(int blur)
|
94 |
|
|
{
|
95 |
3365725b
|
Leszek Koltunski
|
int radius = blur/2;
|
96 |
a6da86cc
|
Leszek Koltunski
|
mRadiusSta.set(radius);
|
97 |
|
|
return radius;
|
98 |
43bda3db
|
Leszek Koltunski
|
}
|
99 |
|
|
|
100 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
101 |
|
|
|
102 |
|
|
public void onDrawFrame(GL10 glUnused)
|
103 |
|
|
{
|
104 |
da57b7da
|
Leszek Koltunski
|
long time = System.currentTimeMillis();
|
105 |
|
|
|
106 |
|
|
mBuffer.render(time);
|
107 |
|
|
mScreen.render(time);
|
108 |
43bda3db
|
Leszek Koltunski
|
}
|
109 |
|
|
|
110 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
111 |
|
|
|
112 |
fa9b6494
|
Leszek Koltunski
|
public void onSurfaceChanged(GL10 glUnused, int width, int height)
|
113 |
|
|
{
|
114 |
da57b7da
|
Leszek Koltunski
|
float qw1 = (float)width /SIZE;
|
115 |
|
|
float qh1 = (float)height/SIZE;
|
116 |
|
|
float factor1 = 0.8f* (qw1<qh1 ? qw1:qh1);
|
117 |
|
|
int w1 = (int)(factor1*SIZE);
|
118 |
|
|
int h1 = (int)(factor1*SIZE);
|
119 |
|
|
|
120 |
|
|
mBufferMove.set((width-w1)/2 ,(height-h1)/2, 0);
|
121 |
|
|
mBufferScale.set( factor1,factor1,factor1 );
|
122 |
|
|
|
123 |
|
|
float qw2 = (float)SIZE/mObjWidth;
|
124 |
|
|
float qh2 = (float)SIZE/mObjHeight;
|
125 |
|
|
float factor2 = 0.9f* (qw2<qh2 ? qw2:qh2);
|
126 |
|
|
int w2 = (int)(factor2*mObjWidth);
|
127 |
|
|
int h2 = (int)(factor2*mObjHeight);
|
128 |
|
|
|
129 |
|
|
mMove.set((SIZE-w2)/2 ,(SIZE-h2)/2, 0);
|
130 |
|
|
mScale.set( factor2,factor2,factor2 );
|
131 |
|
|
|
132 |
463f1f9c
|
leszek
|
mScreen.resize(width, height);
|
133 |
fa9b6494
|
Leszek Koltunski
|
}
|
134 |
43bda3db
|
Leszek Koltunski
|
|
135 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
136 |
|
|
|
137 |
fa9b6494
|
Leszek Koltunski
|
public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
|
138 |
|
|
{
|
139 |
|
|
InputStream is = mView.getContext().getResources().openRawResource(R.raw.dog);
|
140 |
|
|
Bitmap bitmap;
|
141 |
43bda3db
|
Leszek Koltunski
|
|
142 |
fa9b6494
|
Leszek Koltunski
|
try
|
143 |
|
|
{
|
144 |
|
|
bitmap = BitmapFactory.decodeStream(is);
|
145 |
|
|
}
|
146 |
|
|
finally
|
147 |
|
|
{
|
148 |
|
|
try
|
149 |
|
|
{
|
150 |
|
|
is.close();
|
151 |
|
|
}
|
152 |
|
|
catch(IOException e) { }
|
153 |
|
|
}
|
154 |
43bda3db
|
Leszek Koltunski
|
|
155 |
fa9b6494
|
Leszek Koltunski
|
mObjHeight = bitmap.getHeight();
|
156 |
|
|
mObjWidth = bitmap.getWidth();
|
157 |
|
|
|
158 |
|
|
if( mTexture==null ) mTexture = new DistortedTexture(mObjWidth,mObjHeight);
|
159 |
|
|
mTexture.setTexture(bitmap);
|
160 |
|
|
|
161 |
|
|
mScreen.detachAll();
|
162 |
da57b7da
|
Leszek Koltunski
|
mScreen.attach(mBuffer, mBufferEffects, mMesh);
|
163 |
|
|
mBuffer.attach(new DistortedNode(mTexture,mEffects,mMesh));
|
164 |
fa9b6494
|
Leszek Koltunski
|
|
165 |
885b9cca
|
leszek
|
PostprocessEffectBlur.enable();
|
166 |
fa9b6494
|
Leszek Koltunski
|
|
167 |
|
|
try
|
168 |
|
|
{
|
169 |
e3900503
|
Leszek Koltunski
|
DistortedLibrary.onCreate(mView.getContext());
|
170 |
fa9b6494
|
Leszek Koltunski
|
}
|
171 |
|
|
catch(Exception ex)
|
172 |
|
|
{
|
173 |
|
|
android.util.Log.e("Blur", ex.getMessage() );
|
174 |
|
|
}
|
175 |
|
|
}
|
176 |
43bda3db
|
Leszek Koltunski
|
}
|