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