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.triblur;
|
21
|
|
22
|
import android.graphics.Bitmap;
|
23
|
import android.graphics.BitmapFactory;
|
24
|
import android.opengl.GLSurfaceView;
|
25
|
|
26
|
import org.distorted.examples.R;
|
27
|
import org.distorted.library.effect.EffectQuality;
|
28
|
import org.distorted.library.effect.FragmentEffectChroma;
|
29
|
import org.distorted.library.effect.MatrixEffectMove;
|
30
|
import org.distorted.library.effect.MatrixEffectQuaternion;
|
31
|
import org.distorted.library.effect.MatrixEffectScale;
|
32
|
import org.distorted.library.effect.PostprocessEffectBlur;
|
33
|
import org.distorted.library.main.Distorted;
|
34
|
import org.distorted.library.main.DistortedEffects;
|
35
|
import org.distorted.library.main.DistortedNode;
|
36
|
import org.distorted.library.main.DistortedScreen;
|
37
|
import org.distorted.library.main.DistortedTexture;
|
38
|
import org.distorted.library.main.MeshCubes;
|
39
|
import org.distorted.library.type.Static1D;
|
40
|
import org.distorted.library.type.Static3D;
|
41
|
import org.distorted.library.type.Static4D;
|
42
|
|
43
|
import java.io.IOException;
|
44
|
import java.io.InputStream;
|
45
|
|
46
|
import javax.microedition.khronos.egl.EGLConfig;
|
47
|
import javax.microedition.khronos.opengles.GL10;
|
48
|
|
49
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
50
|
|
51
|
class TriblurRenderer implements GLSurfaceView.Renderer
|
52
|
{
|
53
|
private static final int NUM = 6; // 6 ints (x,y,z,R,G,B) each describe 1 object below
|
54
|
|
55
|
private static final int[] OBJECTS =
|
56
|
{
|
57
|
-1, 0, 0, 255, 0, 0, // x,y,z, R,G,B
|
58
|
0, 0, 0, 255, 255, 0, //
|
59
|
+1, 0, 0, 0, 255, 0, //
|
60
|
};
|
61
|
|
62
|
private static final int NUM_OBJECTS = OBJECTS.length/NUM;
|
63
|
private static final int OBJ_SIZE = 100;
|
64
|
|
65
|
private GLSurfaceView mView;
|
66
|
private DistortedTexture mTex;
|
67
|
private DistortedNode[] mNode;
|
68
|
private Static3D[] mMoveVector;
|
69
|
private Static3D[] mChromaVector;
|
70
|
private Static1D[] mBlurVector;
|
71
|
private DistortedScreen mScreen;
|
72
|
private PostprocessEffectBlur[] mBlur;
|
73
|
private FragmentEffectChroma[] mChroma;
|
74
|
private int mDistance;
|
75
|
private boolean[] mBlurStatus;
|
76
|
private Static3D mMove1, mMove2, mScale1, mScale2, mCenter;
|
77
|
|
78
|
Static4D mQuat1, mQuat2;
|
79
|
int mScreenMin;
|
80
|
|
81
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
82
|
|
83
|
TriblurRenderer(GLSurfaceView v)
|
84
|
{
|
85
|
mView = v;
|
86
|
mDistance = (int)(OBJ_SIZE*0.6f);
|
87
|
|
88
|
MeshCubes mesh = new MeshCubes(1,1,1);
|
89
|
|
90
|
mTex = new DistortedTexture(OBJ_SIZE,OBJ_SIZE);
|
91
|
|
92
|
mQuat1 = new Static4D(0,0,0,1); // unity
|
93
|
mQuat2 = new Static4D(0,0,0,1); // quaternions
|
94
|
|
95
|
mScreen = new DistortedScreen();
|
96
|
mScreen.setDebug(DistortedScreen.DEBUG_FPS);
|
97
|
|
98
|
mBlurStatus = new boolean[NUM_OBJECTS];
|
99
|
mMoveVector = new Static3D[NUM_OBJECTS];
|
100
|
mChromaVector = new Static3D[NUM_OBJECTS];
|
101
|
mBlurVector = new Static1D[NUM_OBJECTS];
|
102
|
mNode = new DistortedNode[NUM_OBJECTS];
|
103
|
mBlur = new PostprocessEffectBlur[NUM_OBJECTS];
|
104
|
mChroma = new FragmentEffectChroma[NUM_OBJECTS];
|
105
|
|
106
|
DistortedEffects[] effects= new DistortedEffects[NUM_OBJECTS];
|
107
|
|
108
|
mMove1 = new Static3D(0,0,0);
|
109
|
mMove2 = new Static3D(0,0,0);
|
110
|
mScale1 = new Static3D(1,1,1);
|
111
|
mScale2 = new Static3D(1.5f,1.5f,1.5f);
|
112
|
mCenter = new Static3D(0,0,0);
|
113
|
|
114
|
MatrixEffectMove moveEffect1 = new MatrixEffectMove(mMove1);
|
115
|
MatrixEffectMove moveEffect2 = new MatrixEffectMove(mMove2);
|
116
|
MatrixEffectScale scaleEffect1 = new MatrixEffectScale(mScale1);
|
117
|
MatrixEffectScale scaleEffect2 = new MatrixEffectScale(mScale2);
|
118
|
MatrixEffectQuaternion quatEffect1 = new MatrixEffectQuaternion(mQuat1, mCenter);
|
119
|
MatrixEffectQuaternion quatEffect2 = new MatrixEffectQuaternion(mQuat2, mCenter);
|
120
|
|
121
|
for(int i=0; i<NUM_OBJECTS; i++)
|
122
|
{
|
123
|
mMoveVector[i] = new Static3D(0,0,0);
|
124
|
mChromaVector[i] = new Static3D(OBJECTS[NUM*i+3],OBJECTS[NUM*i+4],OBJECTS[NUM*i+5]);
|
125
|
mBlurVector[i] = new Static1D(10);
|
126
|
mBlur[i] = new PostprocessEffectBlur(mBlurVector[i]);
|
127
|
mChroma[i] = new FragmentEffectChroma( new Static1D(0.3f), mChromaVector[i]);
|
128
|
effects[i] = new DistortedEffects();
|
129
|
|
130
|
effects[i].apply(mBlur[i]);
|
131
|
effects[i].apply(mChroma[i]);
|
132
|
effects[i].apply( (i==0||i==NUM_OBJECTS-1) ? moveEffect1 : moveEffect2 );
|
133
|
effects[i].apply( (i==0||i==NUM_OBJECTS-1) ? scaleEffect1 : scaleEffect2 );
|
134
|
effects[i].apply(quatEffect1);
|
135
|
effects[i].apply(quatEffect2);
|
136
|
effects[i].apply(new MatrixEffectMove(mMoveVector[i]));
|
137
|
|
138
|
mBlurStatus[i] = true;
|
139
|
mNode[i] = new DistortedNode(mTex, effects[i], mesh );
|
140
|
mScreen.attach(mNode[i]);
|
141
|
}
|
142
|
|
143
|
setQuality(EffectQuality.HIGH);
|
144
|
}
|
145
|
|
146
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
147
|
|
148
|
void setQuality(EffectQuality quality)
|
149
|
{
|
150
|
for(int i=0; i<NUM_OBJECTS; i++)
|
151
|
{
|
152
|
mBlur[i].setQuality(quality);
|
153
|
}
|
154
|
}
|
155
|
|
156
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
157
|
|
158
|
void setBackground(float r, float g, float b, float a)
|
159
|
{
|
160
|
mScreen.glClearColor(r,g,b,a);
|
161
|
}
|
162
|
|
163
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
164
|
|
165
|
public void onDrawFrame(GL10 glUnused)
|
166
|
{
|
167
|
mScreen.render(System.currentTimeMillis());
|
168
|
}
|
169
|
|
170
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
171
|
|
172
|
public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
|
173
|
{
|
174
|
InputStream is = mView.getContext().getResources().openRawResource(R.raw.cat);
|
175
|
Bitmap bitmap;
|
176
|
|
177
|
try
|
178
|
{
|
179
|
bitmap = BitmapFactory.decodeStream(is);
|
180
|
}
|
181
|
finally
|
182
|
{
|
183
|
try
|
184
|
{
|
185
|
is.close();
|
186
|
}
|
187
|
catch(IOException e) { }
|
188
|
}
|
189
|
|
190
|
mTex.setTexture(bitmap);
|
191
|
|
192
|
PostprocessEffectBlur.enable();
|
193
|
FragmentEffectChroma.enable();
|
194
|
|
195
|
try
|
196
|
{
|
197
|
Distorted.onCreate(mView.getContext());
|
198
|
}
|
199
|
catch(Exception ex)
|
200
|
{
|
201
|
android.util.Log.e("Triblur", ex.getMessage() );
|
202
|
}
|
203
|
}
|
204
|
|
205
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
206
|
|
207
|
public void onSurfaceChanged(GL10 glUnused, int width, int height)
|
208
|
{
|
209
|
mScreenMin = width<height ? width:height;
|
210
|
|
211
|
float factor1 = 0.22f*mScreenMin/OBJ_SIZE;
|
212
|
float factor2 = 1.50f*factor1;
|
213
|
mScale1.set(factor1,factor1,factor1);
|
214
|
mScale2.set(factor2,factor2,factor2);
|
215
|
mCenter.set((float)OBJ_SIZE/2, (float)OBJ_SIZE/2, -(float)OBJ_SIZE/2 );
|
216
|
mMove1.set( (width -factor1*OBJ_SIZE)/2 ,(height-factor1*OBJ_SIZE)/2 ,0);
|
217
|
mMove2.set( (width -factor2*OBJ_SIZE)/2 ,(height-factor2*OBJ_SIZE)/2 ,0);
|
218
|
computeMoveVectors();
|
219
|
mScreen.resize(width, height);
|
220
|
}
|
221
|
|
222
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
223
|
|
224
|
private void computeMoveVectors()
|
225
|
{
|
226
|
float size= 0.026f*OBJ_SIZE*mDistance;
|
227
|
|
228
|
for(int i=0; i<NUM_OBJECTS; i++)
|
229
|
{
|
230
|
mMoveVector[i].set(size*OBJECTS[NUM*i], size*OBJECTS[NUM*i+1], size*OBJECTS[NUM*i+2]);
|
231
|
}
|
232
|
}
|
233
|
|
234
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
235
|
|
236
|
void setRange(int object, int range)
|
237
|
{
|
238
|
if( object>=0 && object<NUM_OBJECTS )
|
239
|
{
|
240
|
mBlurVector[object].set(range / 2);
|
241
|
}
|
242
|
}
|
243
|
|
244
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
245
|
|
246
|
boolean[] getChecked()
|
247
|
{
|
248
|
return mBlurStatus;
|
249
|
}
|
250
|
|
251
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
252
|
|
253
|
void setChecked(int object, boolean checked)
|
254
|
{
|
255
|
if( object>=0 && object<NUM_OBJECTS )
|
256
|
{
|
257
|
if( checked && !mBlurStatus[object] )
|
258
|
{
|
259
|
mBlurStatus[object] = true;
|
260
|
mNode[object].getEffects().apply(mBlur[object]);
|
261
|
}
|
262
|
if( !checked && mBlurStatus[object] )
|
263
|
{
|
264
|
mBlurStatus[object] = false;
|
265
|
mNode[object].getEffects().abortEffect(mBlur[object]);
|
266
|
}
|
267
|
}
|
268
|
else
|
269
|
{
|
270
|
android.util.Log.e("renderer", "Error, number: "+object+" checked: "+checked );
|
271
|
}
|
272
|
|
273
|
//android.util.Log.d("renderer", "setting box "+number+" BLUR state to "+checked);
|
274
|
}
|
275
|
}
|