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