Project

General

Profile

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

examples / src / main / java / org / distorted / examples / mirror / MirrorRenderer.java @ 30f337e5

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.content.res.Resources;
23
import android.graphics.Bitmap;
24
import android.graphics.BitmapFactory;
25
import android.opengl.GLSurfaceView;
26

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

    
41
import java.io.IOException;
42
import java.io.InputStream;
43

    
44
import javax.microedition.khronos.egl.EGLConfig;
45
import javax.microedition.khronos.opengles.GL10;
46

    
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48

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

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

    
68
   private DistortedTexture mTextureMirror, mTextureHead;
69
   private DistortedFramebuffer mOffScreen1, mOffScreen2;
70

    
71
   private int mX;
72
   private int mMirrorW, mMirrorH, mHeadW, mHeadH;
73
   private int mScreenW, mScreenH;
74

    
75
///////////////////////////////////////////////////////////////////////////////////////////////////
76

    
77
   MirrorRenderer(GLSurfaceView view)
78
      { 
79
      mView     = view;
80
      mResources= view.getResources();
81
      mScreen   = new DistortedScreen();
82

    
83
      mQuad1     = new MeshQuad();
84
      mQuad2     = new MeshQuad();
85
      mQuadMirror= new MeshQuad();
86
      mQuadHead  = new MeshQuad();
87

    
88
      mEffectsMirror    = new DistortedEffects();
89
      mEffectsHead      = new DistortedEffects();
90
      mEffectsOffscreen1= new DistortedEffects();
91
      mEffectsOffscreen2= new DistortedEffects();
92
      mEffectsScreen    = new DistortedEffects();
93

    
94
      mX = MirrorActivity.INIT_POSITION;
95

    
96
      mHeadPosition    = new Static3D(0,0,0);
97
      mScaleMirror     = new Static3D(1,1,1);
98
      mMoveOffscreen2  = new Static3D(0,0,0);
99
      mScaleHead       = new Static3D(1,1,1);
100
      mScaleOffscreen1 = new Static3D(1,1,1);
101
      mScaleOffscreen2 = new Static3D(1,1,1);
102

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

    
114
///////////////////////////////////////////////////////////////////////////////////////////////////
115

    
116
   void setPosition(int pos)
117
      {
118
      mX = pos;
119

    
120
      float headWdivScreenW = mScreenW >0 ? (float)mHeadW/mScreenW : 0.0f;
121
      float left = headWdivScreenW -0.5f + MIRROR_MARGIN;
122
      float right= -left;
123

    
124
      mHeadPosition.set0( ((right-left)*(mX/100.0f) + left) * mScreenW );
125
      }
126
   
127
///////////////////////////////////////////////////////////////////////////////////////////////////
128

    
129
   public void onResume()
130
     {
131
     mScreenW = 0;
132
     mScreenH = 0;
133
     }
134

    
135
///////////////////////////////////////////////////////////////////////////////////////////////////
136

    
137
   public void onDrawFrame(GL10 glUnused) 
138
      {
139
      long time = System.currentTimeMillis();
140

    
141
      mOffScreen1.render(time);
142
      mOffScreen2.render(time);
143
      mScreen.render(time);
144
      }
145

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

    
155
        if( mOffScreen1!=null ) mOffScreen1.markForDeletion();
156
        if( mOffScreen2!=null ) mOffScreen2.markForDeletion();
157

    
158
        int offscreen1W = mScreenW;
159
        int offscreen1H = mScreenH;
160
        int offscreen2W = (int)(MIRROR_SCALE*mScreenW);
161
        int offscreen2H = (int)(MIRROR_SCALE*mScreenH);
162

    
163
        mScaleOffscreen1.set(offscreen1W, offscreen1H, 0);
164
        mScaleOffscreen2.set(offscreen2W, offscreen2H, 0);
165

    
166
        mOffScreen1 = new DistortedFramebuffer( offscreen1W, offscreen1H, 1, DistortedFramebuffer.NO_DEPTH_NO_STENCIL );
167
        mOffScreen2 = new DistortedFramebuffer( offscreen2W, offscreen2H, 1, DistortedFramebuffer.NO_DEPTH_NO_STENCIL );
168

    
169
        float headScale = HEAD_SCALE *mScreenH;
170
        mScaleHead.set(headScale*mHeadW/mHeadH,headScale,0);
171
        mScaleMirror.set( mScreenW, mScreenH, 0);
172

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

    
177
        mOffScreen1.attach( mTextureMirror, mEffectsMirror    , mQuadMirror );
178
        mOffScreen1.attach( mOffScreen2   , mEffectsOffscreen2, mQuad2      );
179
        mOffScreen1.attach( mTextureHead  , mEffectsHead      , mQuadHead   );
180
        mOffScreen2.attach( mOffScreen1   , mEffectsOffscreen1, mQuad1      );
181

    
182
        mScreen.detachAll();
183
        mScreen.attach    ( mOffScreen1   , mEffectsScreen    , mQuad1      );
184
        mScreen.resize(mScreenW,mScreenH);
185
        }
186
      }
187

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

    
195
      Bitmap bitmapM, bitmapH;
196

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

    
212
      mMirrorW = bitmapM.getWidth();
213
      mMirrorH = bitmapM.getHeight();
214
      mHeadW   = bitmapH.getWidth();
215
      mHeadH   = bitmapH.getHeight();
216

    
217
      if( mTextureMirror==null ) mTextureMirror = new DistortedTexture();
218
      if( mTextureHead  ==null ) mTextureHead   = new DistortedTexture();
219

    
220
      mTextureMirror.setTexture(bitmapM);
221
      mTextureHead.setTexture(bitmapH);
222

    
223
      VertexEffectScale.enable();
224
      FragmentEffectBrightness.enable();
225

    
226
      DistortedLibrary.onSurfaceCreated(this);
227
      }
228

    
229
///////////////////////////////////////////////////////////////////////////////////////////////////
230

    
231
    public void distortedException(Exception ex)
232
      {
233
      android.util.Log.e("Mirror", ex.getMessage() );
234
      }
235

    
236
///////////////////////////////////////////////////////////////////////////////////////////////////
237

    
238
    public InputStream localFile(int fileID)
239
      {
240
      return mResources.openRawResource(fileID);
241
      }
242

    
243
///////////////////////////////////////////////////////////////////////////////////////////////////
244

    
245
    public void logMessage(String message)
246
      {
247
      android.util.Log.e("Mirror", message );
248
      }
249
}
(2-2/3)