Revision 3a91bfe1
Added by Leszek Koltunski over 8 years ago
| src/main/java/org/distorted/examples/glow/GlowRenderer.java | ||
|---|---|---|
| 26 | 26 |
import org.distorted.examples.R; |
| 27 | 27 |
import org.distorted.library.Distorted; |
| 28 | 28 |
import org.distorted.library.DistortedEffects; |
| 29 |
import org.distorted.library.DistortedEffectsPostprocess; |
|
| 29 | 30 |
import org.distorted.library.DistortedNode; |
| 30 | 31 |
import org.distorted.library.DistortedScreen; |
| 31 | 32 |
import org.distorted.library.DistortedTexture; |
| ... | ... | |
| 33 | 34 |
import org.distorted.library.EffectTypes; |
| 34 | 35 |
import org.distorted.library.MeshFlat; |
| 35 | 36 |
import org.distorted.library.MeshObject; |
| 37 |
import org.distorted.library.message.EffectListener; |
|
| 38 |
import org.distorted.library.message.EffectMessage; |
|
| 36 | 39 |
import org.distorted.library.type.Dynamic1D; |
| 40 |
import org.distorted.library.type.Dynamic4D; |
|
| 37 | 41 |
import org.distorted.library.type.Static1D; |
| 38 | 42 |
import org.distorted.library.type.Static3D; |
| 43 |
import org.distorted.library.type.Static4D; |
|
| 39 | 44 |
|
| 40 | 45 |
import java.io.IOException; |
| 41 | 46 |
import java.io.InputStream; |
| ... | ... | |
| 45 | 50 |
|
| 46 | 51 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 47 | 52 |
|
| 48 |
class GlowRenderer implements GLSurfaceView.Renderer |
|
| 53 |
class GlowRenderer implements GLSurfaceView.Renderer,EffectListener
|
|
| 49 | 54 |
{
|
| 50 | 55 |
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
|
| 51 | 56 |
private static final int LEAF_SIZE = 100; |
| ... | ... | |
| 55 | 60 |
private DistortedNode mRoot; |
| 56 | 61 |
private DistortedTexture mLeaf; |
| 57 | 62 |
private DistortedScreen mScreen; |
| 63 |
private DistortedEffectsPostprocess[] mLeafGlow = new DistortedEffectsPostprocess[NUM_LEAVES]; |
|
| 58 | 64 |
private int mRootW, mRootH; |
| 65 |
private int mGlowing; |
|
| 59 | 66 |
|
| 60 | 67 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 61 | 68 |
|
| ... | ... | |
| 80 | 87 |
|
| 81 | 88 |
for(int j=0; j<NUM_LEAVES; j++) |
| 82 | 89 |
{
|
| 90 |
mLeafGlow[j] = new DistortedEffectsPostprocess(); |
|
| 91 |
mLeafGlow[j].registerForMessages(this); |
|
| 92 |
|
|
| 83 | 93 |
leafEffects[j] = new DistortedEffects(); |
| 84 | 94 |
leafEffects[j].rotate( new Static1D(j*(360/NUM_LEAVES)), axis, center ); |
| 85 | 95 |
leafEffects[j].move(moveVector); |
| 86 | 96 |
leafEffects[j].chroma( chromaLevel, new Static3D(colors[3*j],colors[3*j+1], colors[3*j+2]) ); |
| 87 |
|
|
| 88 |
mRoot.attach( mLeaf, leafEffects[j], mesh); |
|
| 97 |
DistortedNode node = new DistortedNode( mLeaf, leafEffects[j], mesh); |
|
| 98 |
node.setPostprocessEffects(mLeafGlow[j]); |
|
| 99 |
mRoot.attach(node); |
|
| 89 | 100 |
} |
| 90 | 101 |
|
| 102 |
makeGlow(0); |
|
| 103 |
|
|
| 91 | 104 |
mScreen = new DistortedScreen(mView); |
| 92 | 105 |
mScreen.attach(mRoot); |
| 93 | 106 |
} |
| 94 | 107 |
|
| 95 | 108 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 96 |
|
|
| 97 |
public void onDrawFrame(GL10 glUnused) |
|
| 98 |
{
|
|
| 99 |
mScreen.render(System.currentTimeMillis()); |
|
| 100 |
} |
|
| 109 |
|
|
| 110 |
private void makeGlow(int leaf) |
|
| 111 |
{
|
|
| 112 |
//android.util.Log.e("glow", "glowing: "+leaf);
|
|
| 113 |
|
|
| 114 |
mGlowing = leaf; |
|
| 115 |
|
|
| 116 |
Dynamic1D radius = new Dynamic1D(5000,1.0f); |
|
| 117 |
Static1D startR = new Static1D( 0); |
|
| 118 |
Static1D endR = new Static1D(50); |
|
| 119 |
radius.add(startR); |
|
| 120 |
radius.add(endR); |
|
| 121 |
|
|
| 122 |
Dynamic4D color = new Dynamic4D(5000,1.0f); |
|
| 123 |
Static4D startC = new Static4D(colors[3*leaf],colors[3*leaf+1], colors[3*leaf+2], 0); |
|
| 124 |
Static4D endC = new Static4D(colors[3*leaf],colors[3*leaf+1], colors[3*leaf+2], 1); |
|
| 125 |
color.add(startC); |
|
| 126 |
color.add(endC); |
|
| 127 |
|
|
| 128 |
mLeafGlow[leaf].glow( radius, color ); |
|
| 129 |
} |
|
| 101 | 130 |
|
| 102 | 131 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 103 |
|
|
| 104 |
public void onSurfaceChanged(GL10 glUnused, int width, int height) |
|
| 105 |
{
|
|
| 106 |
float qw = (float)width /mRootW; |
|
| 107 |
float qh = (float)height/mRootH; |
|
| 108 |
float factor = 0.6f* (qw<qh ? qw:qh); |
|
| 109 |
int w = (int)(factor*mRootW); |
|
| 110 |
int h = (int)(factor*mRootH); |
|
| 111 |
|
|
| 112 |
Dynamic1D rot = new Dynamic1D(5000,0.0f); |
|
| 113 |
rot.setMode(Dynamic1D.MODE_JUMP); |
|
| 114 |
rot.add(new Static1D( 0)); |
|
| 115 |
rot.add(new Static1D(360)); |
|
| 116 |
|
|
| 117 |
Static3D center = new Static3D(3*LEAF_SIZE/2, 3*LEAF_SIZE/2, 0); |
|
| 118 |
Static3D axis = new Static3D(0,0,1); |
|
| 119 |
|
|
| 120 |
DistortedEffects effects = mRoot.getEffects(); |
|
| 121 |
effects.abortEffects(EffectTypes.MATRIX); |
|
| 122 |
effects.move( new Static3D((width-w)/2 ,(height-h)/2, 0) ); |
|
| 123 |
effects.scale( factor ); |
|
| 124 |
effects.rotate( rot, axis, center ); |
|
| 125 |
|
|
| 126 |
mScreen.resize(width, height); |
|
| 127 |
} |
|
| 132 |
// Glow finished. Make the next Leaf glow. |
|
| 133 |
|
|
| 134 |
public void effectMessage(final EffectMessage em, final long effectID, final EffectNames effectName, final long objectID) |
|
| 135 |
{
|
|
| 136 |
switch(em) |
|
| 137 |
{
|
|
| 138 |
case EFFECT_FINISHED: //android.util.Log.e("glow", "effectMessage FINISHED");
|
|
| 139 |
int glowing = mGlowing+1; |
|
| 140 |
if( glowing>=NUM_LEAVES ) glowing = 0; |
|
| 141 |
makeGlow(glowing); |
|
| 142 |
break; |
|
| 143 |
default: //android.util.Log.e("glow", "effectMessage REMOVED");
|
|
| 144 |
break; |
|
| 145 |
} |
|
| 146 |
} |
|
| 128 | 147 |
|
| 129 | 148 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 130 |
|
|
| 131 |
public void onSurfaceCreated(GL10 glUnused, EGLConfig config) |
|
| 132 |
{
|
|
| 133 |
InputStream is = mView.getContext().getResources().openRawResource(R.raw.leaf); |
|
| 134 |
Bitmap leaf; |
|
| 135 |
|
|
| 136 |
try |
|
| 137 |
{
|
|
| 138 |
leaf = BitmapFactory.decodeStream(is); |
|
| 139 |
} |
|
| 140 |
finally |
|
| 141 |
{
|
|
| 142 |
try |
|
| 143 |
{
|
|
| 144 |
is.close(); |
|
| 145 |
} |
|
| 146 |
catch(IOException e) { }
|
|
| 147 |
} |
|
| 148 |
|
|
| 149 |
mLeaf.setTexture(leaf); |
|
| 150 | 149 |
|
| 151 |
DistortedEffects.enableEffect(EffectNames.CHROMA); |
|
| 152 |
DistortedEffects.enableEffect(EffectNames.GLOW); |
|
| 150 |
public void onDrawFrame(GL10 glUnused) |
|
| 151 |
{
|
|
| 152 |
mScreen.render(System.currentTimeMillis()); |
|
| 153 |
} |
|
| 154 |
|
|
| 155 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 156 |
|
|
| 157 |
public void onSurfaceChanged(GL10 glUnused, int width, int height) |
|
| 158 |
{
|
|
| 159 |
float qw = (float)width /mRootW; |
|
| 160 |
float qh = (float)height/mRootH; |
|
| 161 |
float factor = 0.6f* (qw<qh ? qw:qh); |
|
| 162 |
int w = (int)(factor*mRootW); |
|
| 163 |
int h = (int)(factor*mRootH); |
|
| 164 |
|
|
| 165 |
Dynamic1D rot = new Dynamic1D(5000,0.0f); |
|
| 166 |
rot.setMode(Dynamic1D.MODE_JUMP); |
|
| 167 |
rot.add(new Static1D( 0)); |
|
| 168 |
rot.add(new Static1D(360)); |
|
| 169 |
|
|
| 170 |
Static3D center = new Static3D(3*LEAF_SIZE/2, 3*LEAF_SIZE/2, 0); |
|
| 171 |
Static3D axis = new Static3D(0,0,1); |
|
| 172 |
|
|
| 173 |
DistortedEffects effects = mRoot.getEffects(); |
|
| 174 |
effects.abortEffects(EffectTypes.MATRIX); |
|
| 175 |
effects.move( new Static3D((width-w)/2 ,(height-h)/2, 0) ); |
|
| 176 |
effects.scale( factor ); |
|
| 177 |
effects.rotate( rot, axis, center ); |
|
| 178 |
|
|
| 179 |
mScreen.resize(width, height); |
|
| 180 |
} |
|
| 153 | 181 |
|
| 154 |
try |
|
| 155 |
{
|
|
| 156 |
Distorted.onCreate(mView.getContext()); |
|
| 157 |
} |
|
| 158 |
catch(Exception ex) |
|
| 159 |
{
|
|
| 160 |
android.util.Log.e("Glow", ex.getMessage() );
|
|
| 161 |
} |
|
| 162 |
} |
|
| 163 |
|
|
| 164 | 182 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 165 | 183 |
|
| 184 |
public void onSurfaceCreated(GL10 glUnused, EGLConfig config) |
|
| 185 |
{
|
|
| 186 |
InputStream is = mView.getContext().getResources().openRawResource(R.raw.leaf); |
|
| 187 |
Bitmap leaf; |
|
| 188 |
|
|
| 189 |
try |
|
| 190 |
{
|
|
| 191 |
leaf = BitmapFactory.decodeStream(is); |
|
| 192 |
} |
|
| 193 |
finally |
|
| 194 |
{
|
|
| 195 |
try |
|
| 196 |
{
|
|
| 197 |
is.close(); |
|
| 198 |
} |
|
| 199 |
catch(IOException e) { }
|
|
| 200 |
} |
|
| 201 |
|
|
| 202 |
mLeaf.setTexture(leaf); |
|
| 203 |
|
|
| 204 |
DistortedEffects.enableEffect(EffectNames.CHROMA); |
|
| 205 |
DistortedEffects.enableEffect(EffectNames.GLOW); |
|
| 206 |
|
|
| 207 |
try |
|
| 208 |
{
|
|
| 209 |
Distorted.onCreate(mView.getContext()); |
|
| 210 |
} |
|
| 211 |
catch(Exception ex) |
|
| 212 |
{
|
|
| 213 |
android.util.Log.e("Glow", ex.getMessage() );
|
|
| 214 |
} |
|
| 215 |
} |
|
| 166 | 216 |
} |
| src/main/java/org/distorted/examples/listener/ListenerRenderer.java | ||
|---|---|---|
| 109 | 109 |
|
| 110 | 110 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 111 | 111 |
|
| 112 |
public void onSurfaceChanged(GL10 glUnused, int width, int height)
|
|
| 113 |
{
|
|
| 114 |
mEffects.abortEffects(EffectTypes.MATRIX);
|
|
| 112 |
public void onSurfaceChanged(GL10 glUnused, int width, int height)
|
|
| 113 |
{
|
|
| 114 |
mEffects.abortEffects(EffectTypes.MATRIX); |
|
| 115 | 115 |
|
| 116 |
if( (float)bmpHeight/bmpWidth > (float)height/width )
|
|
| 117 |
{
|
|
| 118 |
int w = (height*bmpWidth)/bmpHeight;
|
|
| 119 |
float factor = (float)height/bmpHeight;
|
|
| 120 |
|
|
| 121 |
mEffects.move( new Static3D((width-w)/2,0,0) );
|
|
| 122 |
mEffects.scale(factor);
|
|
| 123 |
}
|
|
| 124 |
else
|
|
| 125 |
{
|
|
| 126 |
int h = (width*bmpHeight)/bmpWidth;
|
|
| 127 |
float factor = (float)width/bmpWidth;
|
|
| 128 |
|
|
| 129 |
mEffects.move( new Static3D(0,(height-h)/2,0) );
|
|
| 130 |
mEffects.scale(factor);
|
|
| 131 |
}
|
|
| 116 |
if( (float)bmpHeight/bmpWidth > (float)height/width ) |
|
| 117 |
{
|
|
| 118 |
int w = (height*bmpWidth)/bmpHeight; |
|
| 119 |
float factor = (float)height/bmpHeight; |
|
| 120 |
|
|
| 121 |
mEffects.move( new Static3D((width-w)/2,0,0) ); |
|
| 122 |
mEffects.scale(factor); |
|
| 123 |
} |
|
| 124 |
else |
|
| 125 |
{
|
|
| 126 |
int h = (width*bmpHeight)/bmpWidth; |
|
| 127 |
float factor = (float)width/bmpWidth; |
|
| 128 |
|
|
| 129 |
mEffects.move( new Static3D(0,(height-h)/2,0) ); |
|
| 130 |
mEffects.scale(factor); |
|
| 131 |
} |
|
| 132 | 132 |
|
| 133 |
mScreen.resize(width, height);
|
|
| 134 |
}
|
|
| 133 |
mScreen.resize(width, height); |
|
| 134 |
} |
|
| 135 | 135 |
|
| 136 | 136 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 137 | 137 |
|
| 138 |
public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
|
|
| 139 |
{
|
|
| 140 |
InputStream is = mView.getContext().getResources().openRawResource(R.raw.water);
|
|
| 141 |
Bitmap bitmap;
|
|
| 138 |
public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
|
|
| 139 |
{
|
|
| 140 |
InputStream is = mView.getContext().getResources().openRawResource(R.raw.water); |
|
| 141 |
Bitmap bitmap; |
|
| 142 | 142 |
|
| 143 |
try
|
|
| 144 |
{
|
|
| 145 |
bitmap = BitmapFactory.decodeStream(is);
|
|
| 146 |
}
|
|
| 147 |
finally
|
|
| 148 |
{
|
|
| 149 |
try
|
|
| 150 |
{
|
|
| 151 |
is.close();
|
|
| 152 |
}
|
|
| 153 |
catch(IOException e) { }
|
|
| 154 |
}
|
|
| 155 |
|
|
| 156 |
bmpHeight = bitmap.getHeight();
|
|
| 157 |
bmpWidth = bitmap.getWidth();
|
|
| 158 |
|
|
| 159 |
if( mTexture==null ) mTexture = new DistortedTexture(bmpWidth,bmpHeight);
|
|
| 160 |
mTexture.setTexture(bitmap);
|
|
| 161 |
|
|
| 162 |
if( mMesh==null ) mMesh = new MeshFlat(50,50*bmpHeight/bmpWidth);
|
|
| 163 |
|
|
| 164 |
mScreen.detachAll();
|
|
| 165 |
mScreen.attach(mTexture,mEffects,mMesh);
|
|
| 166 |
|
|
| 167 |
for(int i=0; i<NUM_BUBBLES; i++) randomizeNewBubble();
|
|
| 168 |
|
|
| 169 |
DistortedEffects.enableEffect(EffectNames.DISTORT);
|
|
| 170 |
|
|
| 171 |
try
|
|
| 172 |
{
|
|
| 173 |
Distorted.onCreate(mView.getContext());
|
|
| 174 |
}
|
|
| 175 |
catch(Exception ex)
|
|
| 176 |
{
|
|
| 177 |
android.util.Log.e("Renderer", ex.getMessage() );
|
|
| 178 |
}
|
|
| 179 |
}
|
|
| 143 |
try
|
|
| 144 |
{
|
|
| 145 |
bitmap = BitmapFactory.decodeStream(is); |
|
| 146 |
}
|
|
| 147 |
finally
|
|
| 148 |
{
|
|
| 149 |
try
|
|
| 150 |
{
|
|
| 151 |
is.close(); |
|
| 152 |
}
|
|
| 153 |
catch(IOException e) { }
|
|
| 154 |
}
|
|
| 155 |
|
|
| 156 |
bmpHeight = bitmap.getHeight(); |
|
| 157 |
bmpWidth = bitmap.getWidth(); |
|
| 158 |
|
|
| 159 |
if( mTexture==null ) mTexture = new DistortedTexture(bmpWidth,bmpHeight); |
|
| 160 |
mTexture.setTexture(bitmap); |
|
| 161 |
|
|
| 162 |
if( mMesh==null ) mMesh = new MeshFlat(50,50*bmpHeight/bmpWidth); |
|
| 163 |
|
|
| 164 |
mScreen.detachAll(); |
|
| 165 |
mScreen.attach(mTexture,mEffects,mMesh); |
|
| 166 |
|
|
| 167 |
for(int i=0; i<NUM_BUBBLES; i++) randomizeNewBubble(); |
|
| 168 |
|
|
| 169 |
DistortedEffects.enableEffect(EffectNames.DISTORT); |
|
| 170 |
|
|
| 171 |
try |
|
| 172 |
{
|
|
| 173 |
Distorted.onCreate(mView.getContext()); |
|
| 174 |
} |
|
| 175 |
catch(Exception ex) |
|
| 176 |
{
|
|
| 177 |
android.util.Log.e("Renderer", ex.getMessage() );
|
|
| 178 |
} |
|
| 179 |
} |
|
| 180 | 180 |
} |
Also available in: Unified diff
Progress with GLOW.
Serious bug sorting Surface's children into postprocessing Buckets detected.