Project

General

Profile

Download (11.1 KB) Statistics
| Branch: | Revision:

examples / src / main / java / org / distorted / examples / mirror / MirrorRenderer.java @ dc10a48d

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.app.ActivityManager;
23
import android.content.Context;
24
import android.content.pm.ConfigurationInfo;
25
import android.content.res.Resources;
26
import android.graphics.Bitmap;
27
import android.graphics.BitmapFactory;
28
import android.opengl.GLSurfaceView;
29

    
30
import org.distorted.examples.R;
31
import org.distorted.library.effect.FragmentEffectBrightness;
32
import org.distorted.library.effect.MatrixEffectMove;
33
import org.distorted.library.effect.MatrixEffectScale;
34
import org.distorted.library.effect.VertexEffectScale;
35
import org.distorted.library.main.DistortedLibrary;
36
import org.distorted.library.main.DistortedEffects;
37
import org.distorted.library.main.DistortedFramebuffer;
38
import org.distorted.library.main.DistortedScreen;
39
import org.distorted.library.main.DistortedTexture;
40
import org.distorted.library.mesh.MeshQuad;
41
import org.distorted.library.type.Static1D;
42
import org.distorted.library.type.Static3D;
43

    
44
import java.io.IOException;
45
import java.io.InputStream;
46

    
47
import javax.microedition.khronos.egl.EGLConfig;
48
import javax.microedition.khronos.opengles.GL10;
49

    
50
///////////////////////////////////////////////////////////////////////////////////////////////////
51

    
52
class MirrorRenderer implements GLSurfaceView.Renderer, DistortedLibrary.LibraryUser
53
{
54
   private static final float MIRROR_SCALE     =0.70f;  // each next mirror will be 70% of the size or the previous
55
   private static final float HEAD_SCALE       =0.30f;  // Head's height will be 30% of the height of the mirror
56
   private static final float MIRROR_BRIGHTNESS=0.70f;  // Each next mirror 30% darker
57
   private static final float MIRROR_MARGIN    =0.11f;  // The frame of the mirror takes up 11% of its width
58
   private static final float MIRROR_MOVE_H    =0.12f;  // Each next mirror is moved to the left by 12% of
59
                                                        // the length of the previous one
60
   private static final float MIRROR_MOVE_V    =0.22f;  // Move the mirror up
61

    
62
   private final GLSurfaceView mView;
63
   private final Resources mResources;
64
   private final DistortedEffects mEffectsMirror, mEffectsHead, mEffectsScreen;
65
   private final DistortedEffects mEffectsOffscreen1, mEffectsOffscreen2;
66
   private final DistortedScreen mScreen;
67
   private final MeshQuad mQuad1, mQuad2, mQuadMirror, mQuadHead;
68
   private final Static3D mHeadPosition, mScaleMirror, mMoveOffscreen2, mScaleHead;
69
   private final Static3D mScaleOffscreen1, mScaleOffscreen2;
70

    
71
   private DistortedTexture mTextureMirror, mTextureHead;
72
   private DistortedFramebuffer mOffScreen1, mOffScreen2;
73

    
74
   private int mX;
75
   private int mMirrorW, mMirrorH, mHeadW, mHeadH;
76
   private int mScreenW, mScreenH;
77

    
78
///////////////////////////////////////////////////////////////////////////////////////////////////
79

    
80
   MirrorRenderer(GLSurfaceView view)
81
      { 
82
      mView     = view;
83
      mResources= view.getResources();
84
      mScreen   = new DistortedScreen();
85

    
86
      mQuad1     = new MeshQuad();
87
      mQuad2     = new MeshQuad();
88
      mQuadMirror= new MeshQuad();
89
      mQuadHead  = new MeshQuad();
90

    
91
      mEffectsMirror    = new DistortedEffects();
92
      mEffectsHead      = new DistortedEffects();
93
      mEffectsOffscreen1= new DistortedEffects();
94
      mEffectsOffscreen2= new DistortedEffects();
95
      mEffectsScreen    = new DistortedEffects();
96

    
97
      mX = MirrorActivity.INIT_POSITION;
98

    
99
      mHeadPosition    = new Static3D(0,0,0);
100
      mScaleMirror     = new Static3D(1,1,1);
101
      mMoveOffscreen2  = new Static3D(0,0,0);
102
      mScaleHead       = new Static3D(1,1,1);
103
      mScaleOffscreen1 = new Static3D(1,1,1);
104
      mScaleOffscreen2 = new Static3D(1,1,1);
105

    
106
      mEffectsMirror.apply( new VertexEffectScale(mScaleMirror));
107
      mEffectsScreen.apply( new VertexEffectScale(mScaleOffscreen1) );
108
      mEffectsOffscreen1.apply( new VertexEffectScale(mScaleOffscreen1) );
109
      mEffectsOffscreen1.apply( new MatrixEffectScale( new Static3D(MIRROR_SCALE,MIRROR_SCALE,MIRROR_SCALE)));
110
      mEffectsOffscreen2.apply( new VertexEffectScale(mScaleOffscreen2) );
111
      mEffectsOffscreen2.apply( new MatrixEffectMove(mMoveOffscreen2) );
112
      mEffectsHead.apply( new VertexEffectScale(mScaleHead) );
113
      mEffectsHead.apply( new MatrixEffectMove(mHeadPosition) );
114
      mEffectsOffscreen1.apply(new FragmentEffectBrightness(new Static1D(MIRROR_BRIGHTNESS)));
115
      }
116

    
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118

    
119
   void setPosition(int pos)
120
      {
121
      mX = pos;
122

    
123
      float headWdivScreenW = mScreenW >0 ? (float)mHeadW/mScreenW : 0.0f;
124
      float left = headWdivScreenW -0.5f + MIRROR_MARGIN;
125
      float right= -left;
126

    
127
      mHeadPosition.set0( ((right-left)*(mX/100.0f) + left) * mScreenW );
128
      }
129
   
130
///////////////////////////////////////////////////////////////////////////////////////////////////
131

    
132
   public void onResume()
133
     {
134
     mScreenW = 0;
135
     mScreenH = 0;
136
     }
137

    
138
///////////////////////////////////////////////////////////////////////////////////////////////////
139

    
140
   public void onDrawFrame(GL10 glUnused) 
141
      {
142
      long time = System.currentTimeMillis();
143

    
144
      mOffScreen1.render(time);
145
      mOffScreen2.render(time);
146
      mScreen.render(time);
147
      }
148

    
149
///////////////////////////////////////////////////////////////////////////////////////////////////
150
    
151
   public void onSurfaceChanged(GL10 glUnused, int width, int height) 
152
      {
153
      if( mScreenW!=width || mScreenH!=height)
154
        {
155
        mScreenW = width;
156
        mScreenH = height;
157

    
158
        if( mOffScreen1!=null ) mOffScreen1.markForDeletion();
159
        if( mOffScreen2!=null ) mOffScreen2.markForDeletion();
160

    
161
        int offscreen1W = mScreenW;
162
        int offscreen1H = mScreenH;
163
        int offscreen2W = (int)(MIRROR_SCALE*mScreenW);
164
        int offscreen2H = (int)(MIRROR_SCALE*mScreenH);
165

    
166
        mScaleOffscreen1.set(offscreen1W, offscreen1H, 0);
167
        mScaleOffscreen2.set(offscreen2W, offscreen2H, 0);
168

    
169
        mOffScreen1 = new DistortedFramebuffer( offscreen1W, offscreen1H, 1, DistortedFramebuffer.NO_DEPTH_NO_STENCIL );
170
        mOffScreen2 = new DistortedFramebuffer( offscreen2W, offscreen2H, 1, DistortedFramebuffer.NO_DEPTH_NO_STENCIL );
171

    
172
        float headScale = HEAD_SCALE *mScreenH;
173
        mScaleHead.set(headScale*mHeadW/mHeadH,headScale,0);
174
        mScaleMirror.set( mScreenW, mScreenH, 0);
175

    
176
        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);
177
        mHeadPosition.set1( (0.5f*HEAD_SCALE - 0.5f + MIRROR_MARGIN*mMirrorW/mMirrorH)*mScreenH );
178
        setPosition(mX);
179

    
180
        mOffScreen1.attach( mTextureMirror, mEffectsMirror    , mQuadMirror );
181
        mOffScreen1.attach( mOffScreen2   , mEffectsOffscreen2, mQuad2      );
182
        mOffScreen1.attach( mTextureHead  , mEffectsHead      , mQuadHead   );
183
        mOffScreen2.attach( mOffScreen1   , mEffectsOffscreen1, mQuad1      );
184

    
185
        mScreen.detachAll();
186
        mScreen.attach    ( mOffScreen1   , mEffectsScreen    , mQuad1      );
187
        mScreen.resize(mScreenW,mScreenH);
188
        }
189
      }
190

    
191
///////////////////////////////////////////////////////////////////////////////////////////////////
192
    
193
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
194
      {
195
      InputStream isM = mView.getContext().getResources().openRawResource(R.raw.mirror);
196
      InputStream isH = mView.getContext().getResources().openRawResource(R.raw.messi);
197

    
198
      Bitmap bitmapM, bitmapH;
199

    
200
      try
201
        {
202
        bitmapM = BitmapFactory.decodeStream(isM);
203
        bitmapH = BitmapFactory.decodeStream(isH);
204
        }
205
      finally
206
        {
207
        try
208
          {
209
          isM.close();
210
          isH.close();
211
          }
212
        catch(IOException ignored) { }
213
        }
214

    
215
      mMirrorW = bitmapM.getWidth();
216
      mMirrorH = bitmapM.getHeight();
217
      mHeadW   = bitmapH.getWidth();
218
      mHeadH   = bitmapH.getHeight();
219

    
220
      if( mTextureMirror==null ) mTextureMirror = new DistortedTexture();
221
      if( mTextureHead  ==null ) mTextureHead   = new DistortedTexture();
222

    
223
      mTextureMirror.setTexture(bitmapM);
224
      mTextureHead.setTexture(bitmapH);
225

    
226
      VertexEffectScale.enable();
227
      FragmentEffectBrightness.enable();
228

    
229
      DistortedLibrary.onSurfaceCreated(this);
230
      }
231

    
232
///////////////////////////////////////////////////////////////////////////////////////////////////
233

    
234
    public void distortedException(Exception ex)
235
      {
236
      android.util.Log.e("Mirror", ex.getMessage() );
237
      }
238

    
239
///////////////////////////////////////////////////////////////////////////////////////////////////
240

    
241
    public int openGlVersion()
242
      {
243
      Context context = mView.getContext();
244
      final ActivityManager activityManager     = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
245
      final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
246
      int glESversion = configurationInfo.reqGlEsVersion;
247
      int major = glESversion >> 16;
248
      int minor = glESversion & 0xff;
249

    
250
      return 100*major + 10*minor;
251
      }
252

    
253
///////////////////////////////////////////////////////////////////////////////////////////////////
254

    
255
    public InputStream localFile(int fileID)
256
      {
257
      return mResources.openRawResource(fileID);
258
      }
259

    
260
///////////////////////////////////////////////////////////////////////////////////////////////////
261

    
262
    public void logMessage(String message)
263
      {
264
      android.util.Log.e("Mirror", message );
265
      }
266
}
(2-2/3)