1 |
efc280af
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2 |
|
|
// Copyright 2016 Leszek Koltunski //
|
3 |
|
|
// //
|
4 |
71c8884f
|
Leszek Koltunski
|
// This file is part of Distorted. //
|
5 |
efc280af
|
Leszek Koltunski
|
// //
|
6 |
71c8884f
|
Leszek Koltunski
|
// Distorted is free software: you can redistribute it and/or modify //
|
7 |
efc280af
|
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 |
efc280af
|
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 |
efc280af
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
19 |
|
|
|
20 |
|
|
package org.distorted.examples.postprocesstree;
|
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.FragmentEffectChroma;
|
28 |
|
|
import org.distorted.library.effect.MatrixEffectMove;
|
29 |
|
|
import org.distorted.library.effect.MatrixEffectRotate;
|
30 |
|
|
import org.distorted.library.effect.MatrixEffectScale;
|
31 |
|
|
import org.distorted.library.effect.PostprocessEffectBlur;
|
32 |
e3900503
|
Leszek Koltunski
|
import org.distorted.library.main.DistortedLibrary;
|
33 |
efc280af
|
Leszek Koltunski
|
import org.distorted.library.main.DistortedEffects;
|
34 |
|
|
import org.distorted.library.main.DistortedNode;
|
35 |
|
|
import org.distorted.library.main.DistortedScreen;
|
36 |
|
|
import org.distorted.library.main.DistortedTexture;
|
37 |
698ad0a8
|
Leszek Koltunski
|
import org.distorted.library.mesh.MeshQuad;
|
38 |
efc280af
|
Leszek Koltunski
|
import org.distorted.library.type.Dynamic1D;
|
39 |
678c391d
|
Leszek Koltunski
|
import org.distorted.library.type.Dynamic2D;
|
40 |
efc280af
|
Leszek Koltunski
|
import org.distorted.library.type.Static1D;
|
41 |
678c391d
|
Leszek Koltunski
|
import org.distorted.library.type.Static2D;
|
42 |
efc280af
|
Leszek Koltunski
|
import org.distorted.library.type.Static3D;
|
43 |
|
|
|
44 |
|
|
import java.io.IOException;
|
45 |
|
|
import java.io.InputStream;
|
46 |
|
|
|
47 |
|
|
import javax.microedition.khronos.egl.EGLConfig;
|
48 |
|
|
import javax.microedition.khronos.opengles.GL10;
|
49 |
|
|
|
50 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
51 |
|
|
|
52 |
061449ed
|
Leszek Koltunski
|
class PostprocessTreeRenderer implements GLSurfaceView.Renderer, DistortedLibrary.ExceptionListener
|
53 |
efc280af
|
Leszek Koltunski
|
{
|
54 |
|
|
private static final int LEAF_SIZE = 100;
|
55 |
|
|
private static final int NUM_LEAVES = 8;
|
56 |
|
|
|
57 |
|
|
private GLSurfaceView mView;
|
58 |
|
|
private DistortedTexture mLeaf;
|
59 |
|
|
private DistortedScreen mScreen;
|
60 |
16b22aab
|
Leszek Koltunski
|
private Static3D mScale;
|
61 |
678c391d
|
Leszek Koltunski
|
private Static2D mHaloRadius;
|
62 |
efc280af
|
Leszek Koltunski
|
|
63 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
64 |
|
|
|
65 |
|
|
PostprocessTreeRenderer(GLSurfaceView v)
|
66 |
|
|
{
|
67 |
|
|
mView = v;
|
68 |
|
|
|
69 |
0a098d35
|
Leszek Koltunski
|
final int OUTER = 9;
|
70 |
|
|
final int INNER = 4;
|
71 |
efc280af
|
Leszek Koltunski
|
|
72 |
678c391d
|
Leszek Koltunski
|
mHaloRadius = new Static2D(30,5);
|
73 |
|
|
Dynamic2D haloRadiusDyn = new Dynamic2D();
|
74 |
|
|
haloRadiusDyn.add(mHaloRadius);
|
75 |
|
|
PostprocessEffectBlur blurEffect = new PostprocessEffectBlur(haloRadiusDyn);
|
76 |
efc280af
|
Leszek Koltunski
|
|
77 |
687263cc
|
Leszek Koltunski
|
mLeaf = new DistortedTexture();
|
78 |
0a098d35
|
Leszek Koltunski
|
mScale= new Static3D(1,1,1);
|
79 |
5a683295
|
Leszek Koltunski
|
MeshQuad mesh = new MeshQuad();
|
80 |
efc280af
|
Leszek Koltunski
|
|
81 |
698ad0a8
|
Leszek Koltunski
|
DistortedEffects rootEffects = new DistortedEffects();
|
82 |
|
|
DistortedEffects innerEffects = new DistortedEffects();
|
83 |
efc280af
|
Leszek Koltunski
|
DistortedEffects[] innerLeafEffects= new DistortedEffects[NUM_LEAVES];
|
84 |
|
|
DistortedEffects[] outerLeafEffects= new DistortedEffects[NUM_LEAVES];
|
85 |
|
|
|
86 |
5a683295
|
Leszek Koltunski
|
DistortedNode root = new DistortedNode(new DistortedTexture(), rootEffects, mesh);
|
87 |
|
|
root.resizeFBO(OUTER*LEAF_SIZE,OUTER*LEAF_SIZE);
|
88 |
efc280af
|
Leszek Koltunski
|
|
89 |
0a098d35
|
Leszek Koltunski
|
rootEffects.apply(new MatrixEffectScale(mScale));
|
90 |
fba88037
|
Leszek Koltunski
|
rootEffects.apply(blurEffect);
|
91 |
0a098d35
|
Leszek Koltunski
|
|
92 |
|
|
Dynamic1D rotate = new Dynamic1D(5000,0.0f);
|
93 |
|
|
rotate.setMode(Dynamic1D.MODE_JUMP);
|
94 |
|
|
rotate.add(new Static1D( 0));
|
95 |
|
|
rotate.add(new Static1D(360));
|
96 |
efc280af
|
Leszek Koltunski
|
|
97 |
16b22aab
|
Leszek Koltunski
|
Static3D center = new Static3D(0,0,0);
|
98 |
efc280af
|
Leszek Koltunski
|
Static3D axis = new Static3D(0,0,1);
|
99 |
678c391d
|
Leszek Koltunski
|
Static3D innerMoveVector = new Static3D( (1-INNER)*LEAF_SIZE*0.5f, 0, 0);
|
100 |
|
|
Static3D outerMoveVector = new Static3D( (4-OUTER)*LEAF_SIZE*0.5f, 0, 0);
|
101 |
efc280af
|
Leszek Koltunski
|
|
102 |
|
|
for(int j=0; j<NUM_LEAVES; j++)
|
103 |
|
|
{
|
104 |
698ad0a8
|
Leszek Koltunski
|
outerLeafEffects[j] = new DistortedEffects();
|
105 |
5a683295
|
Leszek Koltunski
|
outerLeafEffects[j].apply( new MatrixEffectScale(LEAF_SIZE) );
|
106 |
|
|
outerLeafEffects[j].apply( new MatrixEffectMove(outerMoveVector));
|
107 |
0ba5de00
|
Leszek Koltunski
|
outerLeafEffects[j].apply( new MatrixEffectRotate(new Static1D(j*(360/NUM_LEAVES)), axis, center) );
|
108 |
efc280af
|
Leszek Koltunski
|
|
109 |
5a683295
|
Leszek Koltunski
|
root.attach(mLeaf, outerLeafEffects[j], mesh);
|
110 |
efc280af
|
Leszek Koltunski
|
}
|
111 |
|
|
|
112 |
5a683295
|
Leszek Koltunski
|
innerEffects.apply( new MatrixEffectScale(INNER*LEAF_SIZE) );
|
113 |
16b22aab
|
Leszek Koltunski
|
innerEffects.apply( new MatrixEffectRotate(rotate, axis, center) );
|
114 |
efc280af
|
Leszek Koltunski
|
innerEffects.apply( new FragmentEffectChroma(new Static1D(0.5f), new Static3D(1,0,0) ) );
|
115 |
d8f9a204
|
Leszek Koltunski
|
innerEffects.apply(blurEffect);
|
116 |
efc280af
|
Leszek Koltunski
|
|
117 |
5a683295
|
Leszek Koltunski
|
DistortedNode innerNode = new DistortedNode( new DistortedTexture(), innerEffects, mesh);
|
118 |
|
|
innerNode.resizeFBO(INNER*LEAF_SIZE,INNER*LEAF_SIZE);
|
119 |
efc280af
|
Leszek Koltunski
|
root.attach(innerNode);
|
120 |
|
|
|
121 |
|
|
for(int j=0; j<NUM_LEAVES; j++)
|
122 |
|
|
{
|
123 |
698ad0a8
|
Leszek Koltunski
|
innerLeafEffects[j] = new DistortedEffects();
|
124 |
5a683295
|
Leszek Koltunski
|
innerLeafEffects[j].apply( new MatrixEffectScale(LEAF_SIZE) );
|
125 |
efc280af
|
Leszek Koltunski
|
innerLeafEffects[j].apply(new MatrixEffectMove(innerMoveVector));
|
126 |
0ba5de00
|
Leszek Koltunski
|
innerLeafEffects[j].apply( new MatrixEffectRotate(new Static1D(j*(360/NUM_LEAVES)), axis, center) );
|
127 |
efc280af
|
Leszek Koltunski
|
|
128 |
5a683295
|
Leszek Koltunski
|
innerNode.attach( mLeaf, innerLeafEffects[j], mesh );
|
129 |
efc280af
|
Leszek Koltunski
|
}
|
130 |
|
|
|
131 |
|
|
mScreen = new DistortedScreen();
|
132 |
|
|
mScreen.attach(root);
|
133 |
|
|
}
|
134 |
|
|
|
135 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
136 |
|
|
|
137 |
|
|
int setBlur(int blur)
|
138 |
|
|
{
|
139 |
|
|
int radius = blur/2;
|
140 |
678c391d
|
Leszek Koltunski
|
mHaloRadius.set1(radius);
|
141 |
efc280af
|
Leszek Koltunski
|
return radius;
|
142 |
|
|
}
|
143 |
|
|
|
144 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
145 |
|
|
|
146 |
|
|
public void onDrawFrame(GL10 glUnused)
|
147 |
|
|
{
|
148 |
|
|
mScreen.render(System.currentTimeMillis());
|
149 |
|
|
}
|
150 |
|
|
|
151 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
152 |
|
|
|
153 |
|
|
public void onSurfaceChanged(GL10 glUnused, int width, int height)
|
154 |
|
|
{
|
155 |
5a683295
|
Leszek Koltunski
|
float horiRatio = (float)width ;
|
156 |
|
|
float vertRatio = (float)height;
|
157 |
678c391d
|
Leszek Koltunski
|
float factor = Math.min(horiRatio, vertRatio);
|
158 |
efc280af
|
Leszek Koltunski
|
|
159 |
0a098d35
|
Leszek Koltunski
|
mScale.set(factor,factor,factor);
|
160 |
efc280af
|
Leszek Koltunski
|
mScreen.resize(width, height);
|
161 |
|
|
}
|
162 |
|
|
|
163 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
164 |
|
|
|
165 |
|
|
public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
|
166 |
|
|
{
|
167 |
|
|
InputStream is = mView.getContext().getResources().openRawResource(R.raw.leaf);
|
168 |
|
|
Bitmap leaf;
|
169 |
|
|
|
170 |
|
|
try
|
171 |
|
|
{
|
172 |
|
|
leaf = BitmapFactory.decodeStream(is);
|
173 |
|
|
}
|
174 |
|
|
finally
|
175 |
|
|
{
|
176 |
|
|
try
|
177 |
|
|
{
|
178 |
|
|
is.close();
|
179 |
|
|
}
|
180 |
|
|
catch(IOException e) { }
|
181 |
|
|
}
|
182 |
|
|
|
183 |
|
|
mLeaf.setTexture(leaf);
|
184 |
|
|
|
185 |
|
|
FragmentEffectChroma.enable();
|
186 |
|
|
PostprocessEffectBlur.enable();
|
187 |
|
|
|
188 |
061449ed
|
Leszek Koltunski
|
DistortedLibrary.onCreate(mView.getContext(), this);
|
189 |
|
|
}
|
190 |
|
|
|
191 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
192 |
|
|
|
193 |
|
|
public void distortedException(Exception ex)
|
194 |
|
|
{
|
195 |
|
|
android.util.Log.e("PostprocessTree", ex.getMessage() );
|
196 |
efc280af
|
Leszek Koltunski
|
}
|
197 |
|
|
}
|