1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
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
|
import org.distorted.library.effect.FragmentEffectBrightness;
|
28
|
import org.distorted.library.effect.MatrixEffectMove;
|
29
|
import org.distorted.library.effect.MatrixEffectScale;
|
30
|
import org.distorted.library.effect.VertexEffectScale;
|
31
|
import org.distorted.library.main.DistortedLibrary;
|
32
|
import org.distorted.library.main.DistortedEffects;
|
33
|
import org.distorted.library.main.DistortedFramebuffer;
|
34
|
import org.distorted.library.main.DistortedScreen;
|
35
|
import org.distorted.library.main.DistortedTexture;
|
36
|
import org.distorted.library.mesh.MeshQuad;
|
37
|
import org.distorted.library.type.Static1D;
|
38
|
import org.distorted.library.type.Static3D;
|
39
|
|
40
|
import java.io.IOException;
|
41
|
import java.io.InputStream;
|
42
|
|
43
|
import javax.microedition.khronos.egl.EGLConfig;
|
44
|
import javax.microedition.khronos.opengles.GL10;
|
45
|
|
46
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
47
|
|
48
|
class MirrorRenderer implements GLSurfaceView.Renderer, DistortedLibrary.ExceptionListener
|
49
|
{
|
50
|
private static final float MIRROR_SCALE =0.70f; // each next mirror will be 70% of the size or the previous
|
51
|
private static final float HEAD_SCALE =0.30f; // Head's height will be 30% of the height of the mirror
|
52
|
private static final float MIRROR_BRIGHTNESS=0.70f; // Each next mirror 30% darker
|
53
|
private static final float MIRROR_MARGIN =0.11f; // The frame of the mirror takes up 11% of its width
|
54
|
private static final float MIRROR_MOVE_H =0.12f; // Each next mirror is moved to the left by 12% of
|
55
|
// the length of the previous one
|
56
|
private static final float MIRROR_MOVE_V =0.22f; // Move the mirror up
|
57
|
|
58
|
private GLSurfaceView mView;
|
59
|
private DistortedEffects mEffectsMirror, mEffectsHead, mEffectsScreen;
|
60
|
private DistortedEffects mEffectsOffscreen1, mEffectsOffscreen2;
|
61
|
private DistortedTexture mTextureMirror, mTextureHead;
|
62
|
private DistortedFramebuffer mOffScreen1, mOffScreen2;
|
63
|
private DistortedScreen mScreen;
|
64
|
private MeshQuad mQuad1, mQuad2, mQuadMirror, mQuadHead;
|
65
|
private Static3D mHeadPosition, mScaleMirror, mMoveOffscreen2, mScaleHead;
|
66
|
private Static3D mScaleOffscreen1, mScaleOffscreen2;
|
67
|
|
68
|
private int mX;
|
69
|
private int mMirrorW, mMirrorH, mHeadW, mHeadH;
|
70
|
private int mScreenW, mScreenH;
|
71
|
|
72
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
73
|
|
74
|
MirrorRenderer(GLSurfaceView view)
|
75
|
{
|
76
|
mView = view;
|
77
|
mScreen = new DistortedScreen();
|
78
|
|
79
|
mQuad1 = new MeshQuad();
|
80
|
mQuad2 = new MeshQuad();
|
81
|
mQuadMirror= new MeshQuad();
|
82
|
mQuadHead = new MeshQuad();
|
83
|
|
84
|
mEffectsMirror = new DistortedEffects();
|
85
|
mEffectsHead = new DistortedEffects();
|
86
|
mEffectsOffscreen1= new DistortedEffects();
|
87
|
mEffectsOffscreen2= new DistortedEffects();
|
88
|
mEffectsScreen = new DistortedEffects();
|
89
|
|
90
|
mX = MirrorActivity.INIT_POSITION;
|
91
|
|
92
|
mHeadPosition = new Static3D(0,0,0);
|
93
|
mScaleMirror = new Static3D(1,1,1);
|
94
|
mMoveOffscreen2 = new Static3D(0,0,0);
|
95
|
mScaleHead = new Static3D(1,1,1);
|
96
|
mScaleOffscreen1 = new Static3D(1,1,1);
|
97
|
mScaleOffscreen2 = new Static3D(1,1,1);
|
98
|
|
99
|
mEffectsMirror.apply( new VertexEffectScale(mScaleMirror));
|
100
|
mEffectsScreen.apply( new VertexEffectScale(mScaleOffscreen1) );
|
101
|
mEffectsOffscreen1.apply( new VertexEffectScale(mScaleOffscreen1) );
|
102
|
mEffectsOffscreen1.apply( new MatrixEffectScale( new Static3D(MIRROR_SCALE,MIRROR_SCALE,MIRROR_SCALE)));
|
103
|
mEffectsOffscreen2.apply( new VertexEffectScale(mScaleOffscreen2) );
|
104
|
mEffectsOffscreen2.apply( new MatrixEffectMove(mMoveOffscreen2) );
|
105
|
mEffectsHead.apply( new VertexEffectScale(mScaleHead) );
|
106
|
mEffectsHead.apply( new MatrixEffectMove(mHeadPosition) );
|
107
|
mEffectsOffscreen1.apply(new FragmentEffectBrightness(new Static1D(MIRROR_BRIGHTNESS)));
|
108
|
}
|
109
|
|
110
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
111
|
|
112
|
void setPosition(int pos)
|
113
|
{
|
114
|
mX = pos;
|
115
|
|
116
|
float headWdivScreenW = mScreenW >0 ? (float)mHeadW/mScreenW : 0.0f;
|
117
|
float left = headWdivScreenW -0.5f + MIRROR_MARGIN;
|
118
|
float right= -left;
|
119
|
|
120
|
mHeadPosition.set0( ((right-left)*(mX/100.0f) + left) * mScreenW );
|
121
|
}
|
122
|
|
123
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
124
|
|
125
|
public void onResume()
|
126
|
{
|
127
|
mScreenW = 0;
|
128
|
mScreenH = 0;
|
129
|
}
|
130
|
|
131
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
132
|
|
133
|
public void onDrawFrame(GL10 glUnused)
|
134
|
{
|
135
|
long time = System.currentTimeMillis();
|
136
|
|
137
|
mOffScreen1.render(time);
|
138
|
mOffScreen2.render(time);
|
139
|
mScreen.render(time);
|
140
|
}
|
141
|
|
142
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
143
|
|
144
|
public void onSurfaceChanged(GL10 glUnused, int width, int height)
|
145
|
{
|
146
|
if( mScreenW!=width || mScreenH!=height)
|
147
|
{
|
148
|
mScreenW = width;
|
149
|
mScreenH = height;
|
150
|
|
151
|
if( mOffScreen1!=null ) mOffScreen1.markForDeletion();
|
152
|
if( mOffScreen2!=null ) mOffScreen2.markForDeletion();
|
153
|
|
154
|
int offscreen1W = mScreenW;
|
155
|
int offscreen1H = mScreenH;
|
156
|
int offscreen2W = (int)(MIRROR_SCALE*mScreenW);
|
157
|
int offscreen2H = (int)(MIRROR_SCALE*mScreenH);
|
158
|
|
159
|
mScaleOffscreen1.set(offscreen1W, offscreen1H, 0);
|
160
|
mScaleOffscreen2.set(offscreen2W, offscreen2H, 0);
|
161
|
|
162
|
mOffScreen1 = new DistortedFramebuffer( offscreen1W, offscreen1H, 1, DistortedFramebuffer.NO_DEPTH_NO_STENCIL );
|
163
|
mOffScreen2 = new DistortedFramebuffer( offscreen2W, offscreen2H, 1, DistortedFramebuffer.NO_DEPTH_NO_STENCIL );
|
164
|
|
165
|
float headScale = HEAD_SCALE *mScreenH;
|
166
|
mScaleHead.set(headScale*mHeadW/mHeadH,headScale,0);
|
167
|
mScaleMirror.set( mScreenW, mScreenH, 0);
|
168
|
|
169
|
mMoveOffscreen2.set( (MIRROR_MOVE_H-0.5f+0.5f*MIRROR_SCALE)*mScreenW, (MIRROR_MOVE_V-0.5f+0.5f*MIRROR_SCALE)*mScreenH*mMirrorW/mMirrorH, 0);
|
170
|
mHeadPosition.set1( (0.5f*HEAD_SCALE - 0.5f + MIRROR_MARGIN*mMirrorW/mMirrorH)*mScreenH );
|
171
|
setPosition(mX);
|
172
|
|
173
|
mOffScreen1.attach( mTextureMirror, mEffectsMirror , mQuadMirror );
|
174
|
mOffScreen1.attach( mOffScreen2 , mEffectsOffscreen2, mQuad2 );
|
175
|
mOffScreen1.attach( mTextureHead , mEffectsHead , mQuadHead );
|
176
|
mOffScreen2.attach( mOffScreen1 , mEffectsOffscreen1, mQuad1 );
|
177
|
|
178
|
mScreen.detachAll();
|
179
|
mScreen.attach ( mOffScreen1 , mEffectsScreen , mQuad1 );
|
180
|
mScreen.resize(mScreenW,mScreenH);
|
181
|
}
|
182
|
}
|
183
|
|
184
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
185
|
|
186
|
public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
|
187
|
{
|
188
|
InputStream isM = mView.getContext().getResources().openRawResource(R.raw.mirror);
|
189
|
InputStream isH = mView.getContext().getResources().openRawResource(R.raw.messi);
|
190
|
|
191
|
Bitmap bitmapM, bitmapH;
|
192
|
|
193
|
try
|
194
|
{
|
195
|
bitmapM = BitmapFactory.decodeStream(isM);
|
196
|
bitmapH = BitmapFactory.decodeStream(isH);
|
197
|
}
|
198
|
finally
|
199
|
{
|
200
|
try
|
201
|
{
|
202
|
isM.close();
|
203
|
isH.close();
|
204
|
}
|
205
|
catch(IOException e) { }
|
206
|
}
|
207
|
|
208
|
mMirrorW = bitmapM.getWidth();
|
209
|
mMirrorH = bitmapM.getHeight();
|
210
|
mHeadW = bitmapH.getWidth();
|
211
|
mHeadH = bitmapH.getHeight();
|
212
|
|
213
|
if( mTextureMirror==null ) mTextureMirror = new DistortedTexture();
|
214
|
if( mTextureHead ==null ) mTextureHead = new DistortedTexture();
|
215
|
|
216
|
mTextureMirror.setTexture(bitmapM);
|
217
|
mTextureHead.setTexture(bitmapH);
|
218
|
|
219
|
VertexEffectScale.enable();
|
220
|
FragmentEffectBrightness.enable();
|
221
|
|
222
|
DistortedLibrary.onSurfaceCreated(mView.getContext(), this);
|
223
|
}
|
224
|
|
225
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
226
|
|
227
|
public void distortedException(Exception ex)
|
228
|
{
|
229
|
android.util.Log.e("Mirror", ex.getMessage() );
|
230
|
}
|
231
|
}
|