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.flatblur2;
|
21
|
|
22
|
import android.app.ActivityManager;
|
23
|
import android.content.Context;
|
24
|
import android.content.pm.ConfigurationInfo;
|
25
|
import android.content.res.Resources;
|
26
|
import android.graphics.Bitmap;
|
27
|
import android.graphics.BitmapFactory;
|
28
|
import android.opengl.GLSurfaceView;
|
29
|
|
30
|
import org.distorted.examples.R;
|
31
|
import org.distorted.library.effect.EffectQuality;
|
32
|
import org.distorted.library.effect.EffectType;
|
33
|
import org.distorted.library.effect.MatrixEffectMove;
|
34
|
import org.distorted.library.effect.MatrixEffectRotate;
|
35
|
import org.distorted.library.effect.MatrixEffectScale;
|
36
|
import org.distorted.library.effect.PostprocessEffectGlow;
|
37
|
import org.distorted.library.main.DistortedEffects;
|
38
|
import org.distorted.library.main.DistortedLibrary;
|
39
|
import org.distorted.library.main.DistortedNode;
|
40
|
import org.distorted.library.main.DistortedScreen;
|
41
|
import org.distorted.library.main.DistortedTexture;
|
42
|
import org.distorted.library.mesh.MeshCubes;
|
43
|
import org.distorted.library.mesh.MeshQuad;
|
44
|
|
45
|
import org.distorted.library.message.EffectListener;
|
46
|
import org.distorted.library.type.Dynamic2D;
|
47
|
import org.distorted.library.type.Dynamic4D;
|
48
|
import org.distorted.library.type.Static1D;
|
49
|
import org.distorted.library.type.Static2D;
|
50
|
import org.distorted.library.type.Static3D;
|
51
|
import org.distorted.library.type.Static4D;
|
52
|
|
53
|
import java.io.IOException;
|
54
|
import java.io.InputStream;
|
55
|
|
56
|
import javax.microedition.khronos.egl.EGLConfig;
|
57
|
import javax.microedition.khronos.opengles.GL10;
|
58
|
|
59
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
60
|
|
61
|
class FlatBlur2Renderer implements GLSurfaceView.Renderer, DistortedLibrary.LibraryUser, ListenerOverlay, EffectListener
|
62
|
{
|
63
|
private final GLSurfaceView mView;
|
64
|
private final Resources mResources;
|
65
|
private final DistortedScreen mScreen;
|
66
|
private Static3D mScale1, mScale2, mMove;
|
67
|
private DistortedNode mNode;
|
68
|
private float mHeight;
|
69
|
|
70
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
71
|
|
72
|
FlatBlur2Renderer(GLSurfaceView v)
|
73
|
{
|
74
|
mView = v;
|
75
|
mResources = v.getResources();
|
76
|
mScreen = new DistortedScreen();
|
77
|
}
|
78
|
|
79
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
80
|
|
81
|
public void onDrawFrame(GL10 glUnused)
|
82
|
{
|
83
|
mScreen.render(System.currentTimeMillis());
|
84
|
}
|
85
|
|
86
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
87
|
|
88
|
public void onSurfaceChanged(GL10 glUnused, int width, int height)
|
89
|
{
|
90
|
mHeight = height;
|
91
|
float s = width*0.05f;
|
92
|
mScale1.set(width,width,0);
|
93
|
mScale2.set(s,s,s);
|
94
|
mScreen.resize(width,height);
|
95
|
}
|
96
|
|
97
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
98
|
|
99
|
public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
|
100
|
{
|
101
|
DistortedTexture surface1 = new DistortedTexture();
|
102
|
surface1.setColorARGB(0xffff0000);
|
103
|
MeshQuad mesh1 = new MeshQuad();
|
104
|
DistortedEffects effects1 = new DistortedEffects();
|
105
|
|
106
|
mScale1 = new Static3D(1,1,1);
|
107
|
MatrixEffectScale scale1 = new MatrixEffectScale(mScale1);
|
108
|
effects1.apply(scale1);
|
109
|
mNode = new DistortedNode(surface1,effects1,mesh1);
|
110
|
mScreen.attach(mNode);
|
111
|
|
112
|
DistortedTexture surface2 = new DistortedTexture();
|
113
|
surface2.setColorARGB(0xff0000ff);
|
114
|
MeshCubes mesh2 = new MeshCubes(2,2,2);
|
115
|
DistortedEffects effects2 = new DistortedEffects();
|
116
|
|
117
|
mScale2 = new Static3D(1,1,1);
|
118
|
MatrixEffectScale scale2 = new MatrixEffectScale(mScale2);
|
119
|
effects2.apply(scale2);
|
120
|
Static1D angle = new Static1D(30);
|
121
|
float SQ3 = (float)Math.sqrt(3);
|
122
|
Static3D axis = new Static3D(SQ3/3,SQ3/3,SQ3/3);
|
123
|
Static3D center = new Static3D(0,0,0);
|
124
|
MatrixEffectRotate rot = new MatrixEffectRotate(angle,axis,center);
|
125
|
effects2.apply(rot);
|
126
|
DistortedNode node2 = new DistortedNode(surface2,effects2,mesh2);
|
127
|
mNode.attach(node2);
|
128
|
|
129
|
InputStream is = mView.getContext().getResources().openRawResource(R.raw.leaf);
|
130
|
Bitmap bitmap;
|
131
|
|
132
|
try
|
133
|
{
|
134
|
bitmap = BitmapFactory.decodeStream(is);
|
135
|
}
|
136
|
finally
|
137
|
{
|
138
|
try
|
139
|
{
|
140
|
is.close();
|
141
|
}
|
142
|
catch(IOException ignored) { }
|
143
|
}
|
144
|
|
145
|
DistortedTexture tex = new DistortedTexture();
|
146
|
tex.setTexture(bitmap);
|
147
|
DistortedEffects effects = new DistortedEffects();
|
148
|
MeshQuad mesh = new MeshQuad();
|
149
|
DistortedNode leaf = new DistortedNode(tex,effects,mesh);
|
150
|
mScreen.attach(leaf);
|
151
|
|
152
|
MatrixEffectScale sc = new MatrixEffectScale(new Static3D(50,50,50));
|
153
|
effects.apply(sc);
|
154
|
mMove = new Static3D(0,0,0);
|
155
|
MatrixEffectMove mv = new MatrixEffectMove(mMove);
|
156
|
effects.apply((mv));
|
157
|
|
158
|
OverlayStars.enableEffects();
|
159
|
DistortedLibrary.onSurfaceCreated(this);
|
160
|
}
|
161
|
|
162
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
163
|
|
164
|
public void button1()
|
165
|
{
|
166
|
mMove.set(0,mHeight*0.5f,0);
|
167
|
}
|
168
|
|
169
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
170
|
|
171
|
public void button2()
|
172
|
{
|
173
|
PostprocessEffectGlow glow = constructGlow();
|
174
|
DistortedEffects effects = mNode.getEffects();
|
175
|
effects.apply(glow);
|
176
|
}
|
177
|
|
178
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
179
|
|
180
|
private PostprocessEffectGlow constructGlow()
|
181
|
{
|
182
|
final int DUR_GLO = 3000;
|
183
|
Dynamic2D haloRadius = new Dynamic2D(DUR_GLO,0.5f);
|
184
|
Static2D point20 = new Static2D( 0, 0);
|
185
|
Static2D point21 = new Static2D(15,50);
|
186
|
Dynamic4D color = new Dynamic4D(DUR_GLO, 0.5f);
|
187
|
Static4D point40 = new Static4D(1,1,1,0.0f);
|
188
|
Static4D point41 = new Static4D(1,1,1,0.8f);
|
189
|
|
190
|
haloRadius.add(point20);
|
191
|
haloRadius.add(point21);
|
192
|
haloRadius.add(point20);
|
193
|
color.add(point40);
|
194
|
color.add(point41);
|
195
|
color.add(point40);
|
196
|
|
197
|
PostprocessEffectGlow glow = new PostprocessEffectGlow(haloRadius,color);
|
198
|
glow.setQuality(EffectQuality.MEDIUM);
|
199
|
glow.notifyWhenFinished(this);
|
200
|
return glow;
|
201
|
}
|
202
|
|
203
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
204
|
|
205
|
public void overlayFinished(long id)
|
206
|
{
|
207
|
|
208
|
}
|
209
|
|
210
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
211
|
|
212
|
public void effectFinished(long id)
|
213
|
{
|
214
|
DistortedEffects effects = mNode.getEffects();
|
215
|
effects.abortByType(EffectType.POSTPROCESS);
|
216
|
}
|
217
|
|
218
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
219
|
|
220
|
public void distortedException(Exception ex)
|
221
|
{
|
222
|
android.util.Log.e("FlatBlur2", ex.getMessage() );
|
223
|
}
|
224
|
|
225
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
226
|
|
227
|
public InputStream localFile(int fileID)
|
228
|
{
|
229
|
return mResources.openRawResource(fileID);
|
230
|
}
|
231
|
|
232
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
233
|
|
234
|
public void logMessage(String message)
|
235
|
{
|
236
|
android.util.Log.e("FlatBlur2", message );
|
237
|
}
|
238
|
}
|