| 1 |
03a2fd30
|
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.mirror;
|
| 21 |
|
|
|
| 22 |
|
|
import android.graphics.Bitmap;
|
| 23 |
|
|
import android.graphics.BitmapFactory;
|
| 24 |
41a81a14
|
Leszek Koltunski
|
import android.opengl.GLES30;
|
| 25 |
03a2fd30
|
Leszek Koltunski
|
import android.opengl.GLSurfaceView;
|
| 26 |
|
|
|
| 27 |
|
|
import org.distorted.examples.R;
|
| 28 |
|
|
import org.distorted.library.Distorted;
|
| 29 |
|
|
import org.distorted.library.DistortedEffects;
|
| 30 |
|
|
import org.distorted.library.DistortedFramebuffer;
|
| 31 |
d218d64e
|
leszek
|
import org.distorted.library.DistortedScreen;
|
| 32 |
03a2fd30
|
Leszek Koltunski
|
import org.distorted.library.DistortedTexture;
|
| 33 |
b01acdaf
|
Leszek Koltunski
|
import org.distorted.library.MeshFlat;
|
| 34 |
03a2fd30
|
Leszek Koltunski
|
import org.distorted.library.type.Dynamic3D;
|
| 35 |
88048c61
|
Leszek Koltunski
|
import org.distorted.library.type.Static1D;
|
| 36 |
03a2fd30
|
Leszek Koltunski
|
import org.distorted.library.type.Static3D;
|
| 37 |
|
|
|
| 38 |
|
|
import java.io.IOException;
|
| 39 |
|
|
import java.io.InputStream;
|
| 40 |
|
|
|
| 41 |
|
|
import javax.microedition.khronos.egl.EGLConfig;
|
| 42 |
|
|
import javax.microedition.khronos.opengles.GL10;
|
| 43 |
|
|
|
| 44 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
| 45 |
|
|
|
| 46 |
|
|
class MirrorRenderer implements GLSurfaceView.Renderer
|
| 47 |
|
|
{
|
| 48 |
88048c61
|
Leszek Koltunski
|
private static final float MIRROR_SCALE =0.70f; // each next mirror will be 70% of the size or the previous
|
| 49 |
|
|
private static final float HEAD_SCALE =0.30f; // Head's height will be 30% of the height of the mirror
|
| 50 |
|
|
private static final float MIRROR_BRIGHTNESS=0.70f; // Each next mirror 30% darker
|
| 51 |
|
|
private static final float MIRROR_MARGIN =0.11f; // The frame of the mirror takes up 11% of its width
|
| 52 |
|
|
private static final float MIRROR_MOVE =0.12f; // Each next mirror is moved to the right by 12% of
|
| 53 |
|
|
// the length of the previous one
|
| 54 |
03a2fd30
|
Leszek Koltunski
|
|
| 55 |
|
|
private GLSurfaceView mView;
|
| 56 |
2c2616f1
|
Leszek Koltunski
|
private DistortedEffects mEffectsMirror, mEffectsHead, mEffectsNull;
|
| 57 |
7f9d5a91
|
Leszek Koltunski
|
private DistortedEffects mEffectsOffscreen1, mEffectsOffscreen2;
|
| 58 |
2c2616f1
|
Leszek Koltunski
|
private DistortedTexture mTextureMirror, mTextureHead;
|
| 59 |
d218d64e
|
leszek
|
private DistortedFramebuffer mOffScreen1, mOffScreen2;
|
| 60 |
|
|
private DistortedScreen mScreen;
|
| 61 |
b01acdaf
|
Leszek Koltunski
|
private MeshFlat mQuad;
|
| 62 |
2c2616f1
|
Leszek Koltunski
|
private Static3D mHeadPosition;
|
| 63 |
|
|
private Dynamic3D mHeadDyn;
|
| 64 |
7f9d5a91
|
Leszek Koltunski
|
private int mX;
|
| 65 |
03a2fd30
|
Leszek Koltunski
|
|
| 66 |
2c2616f1
|
Leszek Koltunski
|
private int mMirrorW, mMirrorH, mHeadW, mHeadH;
|
| 67 |
03a2fd30
|
Leszek Koltunski
|
private int mScreenW, mScreenH;
|
| 68 |
|
|
|
| 69 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
| 70 |
|
|
|
| 71 |
|
|
MirrorRenderer(GLSurfaceView view)
|
| 72 |
|
|
{
|
| 73 |
|
|
mView = view;
|
| 74 |
b01acdaf
|
Leszek Koltunski
|
mQuad = new MeshFlat(1,1);
|
| 75 |
d218d64e
|
leszek
|
mScreen = new DistortedScreen();
|
| 76 |
03a2fd30
|
Leszek Koltunski
|
|
| 77 |
7f9d5a91
|
Leszek Koltunski
|
mEffectsMirror = new DistortedEffects();
|
| 78 |
2c2616f1
|
Leszek Koltunski
|
mEffectsHead = new DistortedEffects();
|
| 79 |
7f9d5a91
|
Leszek Koltunski
|
mEffectsOffscreen1= new DistortedEffects();
|
| 80 |
|
|
mEffectsOffscreen2= new DistortedEffects();
|
| 81 |
|
|
mEffectsNull = new DistortedEffects();
|
| 82 |
|
|
|
| 83 |
76c9dd1d
|
Leszek Koltunski
|
mX = MirrorActivity.INIT_POSITION;
|
| 84 |
03a2fd30
|
Leszek Koltunski
|
|
| 85 |
2c2616f1
|
Leszek Koltunski
|
mHeadPosition = new Static3D(0,0,0);
|
| 86 |
|
|
mHeadDyn = new Dynamic3D();
|
| 87 |
|
|
mHeadDyn.add(mHeadPosition);
|
| 88 |
03a2fd30
|
Leszek Koltunski
|
}
|
| 89 |
|
|
|
| 90 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
| 91 |
|
|
|
| 92 |
|
|
void setPosition(int pos)
|
| 93 |
|
|
{
|
| 94 |
7f9d5a91
|
Leszek Koltunski
|
mX = pos;
|
| 95 |
|
|
|
| 96 |
2c2616f1
|
Leszek Koltunski
|
float headW = (HEAD_SCALE *mScreenH* mHeadW) / (mScreenW* mHeadH);
|
| 97 |
7f9d5a91
|
Leszek Koltunski
|
|
| 98 |
2c2616f1
|
Leszek Koltunski
|
mHeadPosition.set1(mX*(1.0f-2*MIRROR_MARGIN-headW)*mScreenW / 100.0f + MIRROR_MARGIN*mScreenW);
|
| 99 |
03a2fd30
|
Leszek Koltunski
|
}
|
| 100 |
|
|
|
| 101 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
| 102 |
|
|
|
| 103 |
|
|
public void onResume()
|
| 104 |
|
|
{
|
| 105 |
|
|
mScreenW = 0;
|
| 106 |
|
|
mScreenH = 0;
|
| 107 |
|
|
}
|
| 108 |
|
|
|
| 109 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
| 110 |
|
|
|
| 111 |
|
|
public void onDrawFrame(GL10 glUnused)
|
| 112 |
|
|
{
|
| 113 |
41a81a14
|
Leszek Koltunski
|
GLES30.glClear( GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_COLOR_BUFFER_BIT);
|
| 114 |
03a2fd30
|
Leszek Koltunski
|
|
| 115 |
|
|
long time = System.currentTimeMillis();
|
| 116 |
|
|
|
| 117 |
7f9d5a91
|
Leszek Koltunski
|
mOffScreen1.renderTo( mTextureMirror, mQuad, mEffectsMirror , time );
|
| 118 |
|
|
mOffScreen1.renderTo( mOffScreen2 , mQuad, mEffectsOffscreen2, time );
|
| 119 |
2c2616f1
|
Leszek Koltunski
|
mOffScreen1.renderTo( mTextureHead , mQuad, mEffectsHead , time );
|
| 120 |
7f9d5a91
|
Leszek Koltunski
|
mOffScreen2.renderTo( mOffScreen1 , mQuad, mEffectsOffscreen1, time );
|
| 121 |
|
|
mScreen.renderTo ( mOffScreen1 , mQuad, mEffectsNull , time );
|
| 122 |
03a2fd30
|
Leszek Koltunski
|
}
|
| 123 |
|
|
|
| 124 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
| 125 |
|
|
|
| 126 |
|
|
public void onSurfaceChanged(GL10 glUnused, int width, int height)
|
| 127 |
|
|
{
|
| 128 |
|
|
if( mScreenW!=width || mScreenH!=height)
|
| 129 |
|
|
{
|
| 130 |
|
|
mScreenW = width;
|
| 131 |
|
|
mScreenH = height;
|
| 132 |
|
|
|
| 133 |
|
|
mOffScreen1 = new DistortedFramebuffer(mScreenW,mScreenH);
|
| 134 |
7f9d5a91
|
Leszek Koltunski
|
mOffScreen2 = new DistortedFramebuffer( (int)(MIRROR_SCALE*mScreenW), (int)(MIRROR_SCALE*mScreenH) );
|
| 135 |
03a2fd30
|
Leszek Koltunski
|
|
| 136 |
2c2616f1
|
Leszek Koltunski
|
mEffectsHead.abortAllEffects();
|
| 137 |
03a2fd30
|
Leszek Koltunski
|
mEffectsMirror.abortAllEffects();
|
| 138 |
7f9d5a91
|
Leszek Koltunski
|
mEffectsOffscreen1.abortAllEffects();
|
| 139 |
|
|
mEffectsOffscreen2.abortAllEffects();
|
| 140 |
03a2fd30
|
Leszek Koltunski
|
|
| 141 |
|
|
mEffectsMirror.scale( new Static3D( (float)mScreenW/mMirrorW, (float)mScreenH/mMirrorH, 1.0f) );
|
| 142 |
7f9d5a91
|
Leszek Koltunski
|
mEffectsOffscreen1.scale(MIRROR_SCALE);
|
| 143 |
88048c61
|
Leszek Koltunski
|
mEffectsOffscreen1.brightness(new Static1D(MIRROR_BRIGHTNESS));
|
| 144 |
7f9d5a91
|
Leszek Koltunski
|
mEffectsOffscreen2.move( new Static3D( MIRROR_MOVE*mScreenW, MIRROR_MOVE*mScreenH*mMirrorW/mMirrorH, 0) );
|
| 145 |
|
|
|
| 146 |
2c2616f1
|
Leszek Koltunski
|
mEffectsHead.move(mHeadDyn);
|
| 147 |
|
|
float headScale = HEAD_SCALE *mScreenH/ mHeadH;
|
| 148 |
|
|
mEffectsHead.scale(headScale);
|
| 149 |
|
|
mHeadPosition.set2( mScreenH*(1.0f-MIRROR_MARGIN*mMirrorW/mMirrorH) - headScale* mHeadH);
|
| 150 |
7f9d5a91
|
Leszek Koltunski
|
setPosition(mX);
|
| 151 |
03a2fd30
|
Leszek Koltunski
|
|
| 152 |
|
|
mScreen.resize(mScreenW,mScreenH);
|
| 153 |
|
|
}
|
| 154 |
|
|
}
|
| 155 |
|
|
|
| 156 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
| 157 |
|
|
|
| 158 |
|
|
public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
|
| 159 |
|
|
{
|
| 160 |
41a81a14
|
Leszek Koltunski
|
GLES30.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
| 161 |
03a2fd30
|
Leszek Koltunski
|
|
| 162 |
|
|
InputStream isM = mView.getContext().getResources().openRawResource(R.raw.mirror);
|
| 163 |
2c2616f1
|
Leszek Koltunski
|
InputStream isH = mView.getContext().getResources().openRawResource(R.raw.messi);
|
| 164 |
03a2fd30
|
Leszek Koltunski
|
|
| 165 |
2c2616f1
|
Leszek Koltunski
|
Bitmap bitmapM, bitmapH;
|
| 166 |
03a2fd30
|
Leszek Koltunski
|
|
| 167 |
|
|
try
|
| 168 |
|
|
{
|
| 169 |
|
|
bitmapM = BitmapFactory.decodeStream(isM);
|
| 170 |
2c2616f1
|
Leszek Koltunski
|
bitmapH = BitmapFactory.decodeStream(isH);
|
| 171 |
03a2fd30
|
Leszek Koltunski
|
}
|
| 172 |
|
|
finally
|
| 173 |
|
|
{
|
| 174 |
|
|
try
|
| 175 |
|
|
{
|
| 176 |
|
|
isM.close();
|
| 177 |
2c2616f1
|
Leszek Koltunski
|
isH.close();
|
| 178 |
03a2fd30
|
Leszek Koltunski
|
}
|
| 179 |
|
|
catch(IOException e) { }
|
| 180 |
|
|
}
|
| 181 |
|
|
|
| 182 |
|
|
mMirrorW = bitmapM.getWidth();
|
| 183 |
|
|
mMirrorH = bitmapM.getHeight();
|
| 184 |
2c2616f1
|
Leszek Koltunski
|
mHeadW = bitmapH.getWidth();
|
| 185 |
|
|
mHeadH = bitmapH.getHeight();
|
| 186 |
03a2fd30
|
Leszek Koltunski
|
|
| 187 |
|
|
mTextureMirror = new DistortedTexture(mMirrorW,mMirrorH);
|
| 188 |
2c2616f1
|
Leszek Koltunski
|
mTextureHead = new DistortedTexture(mHeadW, mHeadH);
|
| 189 |
03a2fd30
|
Leszek Koltunski
|
|
| 190 |
|
|
mTextureMirror.setTexture(bitmapM);
|
| 191 |
2c2616f1
|
Leszek Koltunski
|
mTextureHead.setTexture(bitmapH);
|
| 192 |
03a2fd30
|
Leszek Koltunski
|
|
| 193 |
|
|
try
|
| 194 |
|
|
{
|
| 195 |
76f9798b
|
Leszek Koltunski
|
Distorted.onCreate(mView.getContext());
|
| 196 |
03a2fd30
|
Leszek Koltunski
|
}
|
| 197 |
|
|
catch(Exception ex)
|
| 198 |
|
|
{
|
| 199 |
|
|
android.util.Log.e("Mirror", ex.getMessage() );
|
| 200 |
|
|
}
|
| 201 |
|
|
}
|
| 202 |
|
|
}
|