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