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