| 1 |
08602667
|
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 |
11298b49
|
Leszek Koltunski
|
package org.distorted.examples.movingglow;
|
| 21 |
08602667
|
Leszek Koltunski
|
|
| 22 |
|
|
import android.graphics.Bitmap;
|
| 23 |
|
|
import android.graphics.BitmapFactory;
|
| 24 |
|
|
import android.opengl.GLSurfaceView;
|
| 25 |
|
|
|
| 26 |
|
|
import org.distorted.examples.R;
|
| 27 |
2890c5df
|
Leszek Koltunski
|
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.PostprocessEffectGlow;
|
| 32 |
01782e85
|
Leszek Koltunski
|
import org.distorted.library.main.Distorted;
|
| 33 |
|
|
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 |
|
|
import org.distorted.library.main.MeshFlat;
|
| 38 |
|
|
import org.distorted.library.main.MeshObject;
|
| 39 |
3a91bfe1
|
Leszek Koltunski
|
import org.distorted.library.message.EffectListener;
|
| 40 |
|
|
import org.distorted.library.message.EffectMessage;
|
| 41 |
08602667
|
Leszek Koltunski
|
import org.distorted.library.type.Dynamic1D;
|
| 42 |
3a91bfe1
|
Leszek Koltunski
|
import org.distorted.library.type.Dynamic4D;
|
| 43 |
08602667
|
Leszek Koltunski
|
import org.distorted.library.type.Static1D;
|
| 44 |
|
|
import org.distorted.library.type.Static3D;
|
| 45 |
3a91bfe1
|
Leszek Koltunski
|
import org.distorted.library.type.Static4D;
|
| 46 |
08602667
|
Leszek Koltunski
|
|
| 47 |
|
|
import java.io.IOException;
|
| 48 |
|
|
import java.io.InputStream;
|
| 49 |
|
|
|
| 50 |
|
|
import javax.microedition.khronos.egl.EGLConfig;
|
| 51 |
|
|
import javax.microedition.khronos.opengles.GL10;
|
| 52 |
|
|
|
| 53 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
| 54 |
|
|
|
| 55 |
11298b49
|
Leszek Koltunski
|
class MovingGlowRenderer implements GLSurfaceView.Renderer,EffectListener
|
| 56 |
08602667
|
Leszek Koltunski
|
{
|
| 57 |
|
|
private static final int[] colors = new int[] {0,0,1, 0,0,0, 1,0,0, 1,1,0, 0,1,0, 1,1,1}; // blue, black, red, yellow, green, white
|
| 58 |
|
|
private static final int LEAF_SIZE = 100;
|
| 59 |
|
|
private static final int NUM_LEAVES= colors.length/3;
|
| 60 |
|
|
|
| 61 |
|
|
private GLSurfaceView mView;
|
| 62 |
|
|
private DistortedTexture mLeaf;
|
| 63 |
|
|
private DistortedScreen mScreen;
|
| 64 |
ed89e05e
|
Leszek Koltunski
|
private DistortedEffects[] mLeafEffects = new DistortedEffects[NUM_LEAVES];
|
| 65 |
2890c5df
|
Leszek Koltunski
|
private PostprocessEffectGlow[] mGlow = new PostprocessEffectGlow[NUM_LEAVES];
|
| 66 |
08602667
|
Leszek Koltunski
|
private int mRootW, mRootH;
|
| 67 |
3a91bfe1
|
Leszek Koltunski
|
private int mGlowing;
|
| 68 |
2890c5df
|
Leszek Koltunski
|
private Static3D mMove, mScale;
|
| 69 |
08602667
|
Leszek Koltunski
|
|
| 70 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
| 71 |
|
|
|
| 72 |
11298b49
|
Leszek Koltunski
|
MovingGlowRenderer(GLSurfaceView v)
|
| 73 |
08602667
|
Leszek Koltunski
|
{
|
| 74 |
|
|
mView = v;
|
| 75 |
|
|
|
| 76 |
1a3f8cc2
|
Leszek Koltunski
|
mRootW = 4*LEAF_SIZE;
|
| 77 |
|
|
mRootH = 4*LEAF_SIZE;
|
| 78 |
08602667
|
Leszek Koltunski
|
|
| 79 |
|
|
mLeaf = new DistortedTexture(LEAF_SIZE,LEAF_SIZE);
|
| 80 |
|
|
DistortedTexture surface = new DistortedTexture(mRootW,mRootH);
|
| 81 |
|
|
MeshObject mesh = new MeshFlat(1,1);
|
| 82 |
|
|
|
| 83 |
2890c5df
|
Leszek Koltunski
|
DistortedNode root = new DistortedNode(surface, new DistortedEffects(), mesh);
|
| 84 |
08602667
|
Leszek Koltunski
|
|
| 85 |
1a3f8cc2
|
Leszek Koltunski
|
Static3D moveVector = new Static3D(0.25f*LEAF_SIZE, (mRootH-LEAF_SIZE)/2, 0);
|
| 86 |
08602667
|
Leszek Koltunski
|
Static1D chromaLevel= new Static1D(0.5f);
|
| 87 |
1a3f8cc2
|
Leszek Koltunski
|
Static3D center = new Static3D(mRootW/2, mRootH/2, 0);
|
| 88 |
08602667
|
Leszek Koltunski
|
Static3D axis = new Static3D(0,0,1);
|
| 89 |
|
|
|
| 90 |
2890c5df
|
Leszek Koltunski
|
MatrixEffectMove leafMove = new MatrixEffectMove(moveVector);
|
| 91 |
|
|
|
| 92 |
08602667
|
Leszek Koltunski
|
for(int j=0; j<NUM_LEAVES; j++)
|
| 93 |
|
|
{
|
| 94 |
ed89e05e
|
Leszek Koltunski
|
mLeafEffects[j] = new DistortedEffects();
|
| 95 |
|
|
mLeafEffects[j].registerForMessages(this);
|
| 96 |
|
|
mLeafEffects[j].apply( new MatrixEffectRotate(new Static1D(j*(360/NUM_LEAVES)), axis, center) );
|
| 97 |
|
|
mLeafEffects[j].apply(leafMove);
|
| 98 |
|
|
mLeafEffects[j].apply( new FragmentEffectChroma(chromaLevel, new Static3D(colors[3*j],colors[3*j+1], colors[3*j+2])) );
|
| 99 |
|
|
DistortedNode node = new DistortedNode( mLeaf, mLeafEffects[j], mesh);
|
| 100 |
2890c5df
|
Leszek Koltunski
|
root.attach(node);
|
| 101 |
08602667
|
Leszek Koltunski
|
}
|
| 102 |
|
|
|
| 103 |
2890c5df
|
Leszek Koltunski
|
mMove = new Static3D(0,0,0);
|
| 104 |
|
|
mScale= new Static3D(1,1,1);
|
| 105 |
|
|
|
| 106 |
|
|
Dynamic1D rot = new Dynamic1D(5000,0.0f);
|
| 107 |
|
|
rot.setMode(Dynamic1D.MODE_JUMP);
|
| 108 |
|
|
rot.add(new Static1D( 0));
|
| 109 |
|
|
rot.add(new Static1D(360));
|
| 110 |
|
|
|
| 111 |
|
|
DistortedEffects effects = root.getEffects();
|
| 112 |
|
|
effects.apply(new MatrixEffectMove(mMove));
|
| 113 |
|
|
effects.apply(new MatrixEffectScale(mScale));
|
| 114 |
|
|
effects.apply( new MatrixEffectRotate(rot, axis, center) );
|
| 115 |
|
|
|
| 116 |
1a94a1ab
|
leszek
|
Dynamic1D radiusDyn = new Dynamic1D(3000,1.0f);
|
| 117 |
ed89e05e
|
Leszek Koltunski
|
radiusDyn.add(new Static1D( 0));
|
| 118 |
|
|
radiusDyn.add(new Static1D(50));
|
| 119 |
2890c5df
|
Leszek Koltunski
|
|
| 120 |
|
|
for(int leaf=0; leaf<NUM_LEAVES; leaf++)
|
| 121 |
|
|
{
|
| 122 |
1a3f8cc2
|
Leszek Koltunski
|
Dynamic4D color= new Dynamic4D(3000,0.5f);
|
| 123 |
|
|
Static4D P1 = new Static4D(colors[3*leaf],colors[3*leaf+1], colors[3*leaf+2], 1);
|
| 124 |
|
|
Static4D P2 = new Static4D(colors[3*leaf],colors[3*leaf+1], colors[3*leaf+2], 1);
|
| 125 |
|
|
color.add(P1);
|
| 126 |
|
|
color.add(P2);
|
| 127 |
2890c5df
|
Leszek Koltunski
|
|
| 128 |
ed89e05e
|
Leszek Koltunski
|
mGlow[leaf] = new PostprocessEffectGlow(radiusDyn,color);
|
| 129 |
2890c5df
|
Leszek Koltunski
|
}
|
| 130 |
ebb06a48
|
leszek
|
|
| 131 |
|
|
makeGlow(0);
|
| 132 |
|
|
|
| 133 |
e4330c89
|
Leszek Koltunski
|
mScreen = new DistortedScreen();
|
| 134 |
ebb06a48
|
leszek
|
mScreen.attach(root);
|
| 135 |
08602667
|
Leszek Koltunski
|
}
|
| 136 |
|
|
|
| 137 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
| 138 |
3a91bfe1
|
Leszek Koltunski
|
|
| 139 |
|
|
private void makeGlow(int leaf)
|
| 140 |
|
|
{
|
| 141 |
|
|
//android.util.Log.e("glow", "glowing: "+leaf);
|
| 142 |
|
|
|
| 143 |
|
|
mGlowing = leaf;
|
| 144 |
ed89e05e
|
Leszek Koltunski
|
mLeafEffects[leaf].apply(mGlow[leaf]);
|
| 145 |
3a91bfe1
|
Leszek Koltunski
|
}
|
| 146 |
08602667
|
Leszek Koltunski
|
|
| 147 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
| 148 |
3a91bfe1
|
Leszek Koltunski
|
// Glow finished. Make the next Leaf glow.
|
| 149 |
|
|
|
| 150 |
2890c5df
|
Leszek Koltunski
|
public void effectMessage(final EffectMessage em, final long effectID, final long objectID)
|
| 151 |
3a91bfe1
|
Leszek Koltunski
|
{
|
| 152 |
|
|
switch(em)
|
| 153 |
|
|
{
|
| 154 |
|
|
case EFFECT_FINISHED: //android.util.Log.e("glow", "effectMessage FINISHED");
|
| 155 |
|
|
int glowing = mGlowing+1;
|
| 156 |
|
|
if( glowing>=NUM_LEAVES ) glowing = 0;
|
| 157 |
|
|
makeGlow(glowing);
|
| 158 |
|
|
break;
|
| 159 |
|
|
default: //android.util.Log.e("glow", "effectMessage REMOVED");
|
| 160 |
|
|
break;
|
| 161 |
|
|
}
|
| 162 |
|
|
}
|
| 163 |
08602667
|
Leszek Koltunski
|
|
| 164 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
| 165 |
|
|
|
| 166 |
3a91bfe1
|
Leszek Koltunski
|
public void onDrawFrame(GL10 glUnused)
|
| 167 |
|
|
{
|
| 168 |
|
|
mScreen.render(System.currentTimeMillis());
|
| 169 |
|
|
}
|
| 170 |
|
|
|
| 171 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
| 172 |
|
|
|
| 173 |
|
|
public void onSurfaceChanged(GL10 glUnused, int width, int height)
|
| 174 |
|
|
{
|
| 175 |
|
|
float qw = (float)width /mRootW;
|
| 176 |
|
|
float qh = (float)height/mRootH;
|
| 177 |
1a3f8cc2
|
Leszek Koltunski
|
float factor = 0.9f* (qw<qh ? qw:qh);
|
| 178 |
3a91bfe1
|
Leszek Koltunski
|
int w = (int)(factor*mRootW);
|
| 179 |
|
|
int h = (int)(factor*mRootH);
|
| 180 |
|
|
|
| 181 |
2890c5df
|
Leszek Koltunski
|
mMove.set((width-w)/2 ,(height-h)/2, 0);
|
| 182 |
|
|
mScale.set( factor,factor,factor );
|
| 183 |
3a91bfe1
|
Leszek Koltunski
|
mScreen.resize(width, height);
|
| 184 |
|
|
}
|
| 185 |
08602667
|
Leszek Koltunski
|
|
| 186 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
| 187 |
|
|
|
| 188 |
3a91bfe1
|
Leszek Koltunski
|
public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
|
| 189 |
|
|
{
|
| 190 |
|
|
InputStream is = mView.getContext().getResources().openRawResource(R.raw.leaf);
|
| 191 |
|
|
Bitmap leaf;
|
| 192 |
|
|
|
| 193 |
|
|
try
|
| 194 |
|
|
{
|
| 195 |
|
|
leaf = BitmapFactory.decodeStream(is);
|
| 196 |
|
|
}
|
| 197 |
|
|
finally
|
| 198 |
|
|
{
|
| 199 |
|
|
try
|
| 200 |
|
|
{
|
| 201 |
|
|
is.close();
|
| 202 |
|
|
}
|
| 203 |
|
|
catch(IOException e) { }
|
| 204 |
|
|
}
|
| 205 |
|
|
|
| 206 |
|
|
mLeaf.setTexture(leaf);
|
| 207 |
|
|
|
| 208 |
885b9cca
|
leszek
|
FragmentEffectChroma.enable();
|
| 209 |
|
|
PostprocessEffectGlow.enable();
|
| 210 |
3a91bfe1
|
Leszek Koltunski
|
|
| 211 |
|
|
try
|
| 212 |
|
|
{
|
| 213 |
|
|
Distorted.onCreate(mView.getContext());
|
| 214 |
|
|
}
|
| 215 |
|
|
catch(Exception ex)
|
| 216 |
|
|
{
|
| 217 |
11298b49
|
Leszek Koltunski
|
android.util.Log.e("MovingGlow", ex.getMessage() );
|
| 218 |
3a91bfe1
|
Leszek Koltunski
|
}
|
| 219 |
|
|
}
|
| 220 |
08602667
|
Leszek Koltunski
|
}
|