Project

General

Profile

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

examples / src / main / java / org / distorted / examples / mirror / MirrorRenderer.java @ 8664ea2e

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.main.DistortedLibrary;
31
import org.distorted.library.main.DistortedEffects;
32
import org.distorted.library.main.DistortedFramebuffer;
33
import org.distorted.library.main.DistortedScreen;
34
import org.distorted.library.main.DistortedTexture;
35
import org.distorted.library.mesh.MeshQuad;
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_H    =0.12f;  // Each next mirror is moved to the left by 12% of
54
                                                        // the length of the previous one
55
   private static final float MIRROR_MOVE_V    =0.22f;  // Move the mirror up
56

    
57
   private GLSurfaceView mView;
58
   private DistortedEffects mEffectsMirror, mEffectsHead, mEffectsScreen;
59
   private DistortedEffects mEffectsOffscreen1, mEffectsOffscreen2;
60
   private DistortedTexture mTextureMirror, mTextureHead;
61
   private DistortedFramebuffer mOffScreen1, mOffScreen2;
62
   private DistortedScreen mScreen;
63
   private MeshQuad mQuad1, mQuad2, mQuadMirror, mQuadHead;
64
   private Static3D mHeadPosition, mScaleMirror, mMoveOffscreen2, mScaleHead;
65

    
66
   private int mX;
67
   private int mMirrorW, mMirrorH, mHeadW, mHeadH;
68
   private int mScreenW, mScreenH;
69

    
70
///////////////////////////////////////////////////////////////////////////////////////////////////
71

    
72
   MirrorRenderer(GLSurfaceView view)
73
      { 
74
      mView    = view;
75
      mScreen  = new DistortedScreen();
76

    
77
      mQuad1     = new MeshQuad();
78
      mQuad2     = new MeshQuad();
79
      mQuadMirror= new MeshQuad();
80
      mQuadHead  = new MeshQuad();
81

    
82
      mEffectsMirror    = new DistortedEffects();
83
      mEffectsHead      = new DistortedEffects();
84
      mEffectsOffscreen1= new DistortedEffects();
85
      mEffectsOffscreen2= new DistortedEffects();
86
      mEffectsScreen    = new DistortedEffects();
87

    
88
      mX = MirrorActivity.INIT_POSITION;
89

    
90
      mHeadPosition    = new Static3D(0,0,0);
91
      mScaleMirror     = new Static3D(1,1,1);
92
      mMoveOffscreen2  = new Static3D(0,0,0);
93
      mScaleHead       = new Static3D(1,1,1);
94

    
95
      mEffectsMirror.apply( new MatrixEffectScale(mScaleMirror));
96
      mEffectsOffscreen1.apply( new MatrixEffectScale( new Static3D(MIRROR_SCALE,MIRROR_SCALE,MIRROR_SCALE)));
97
      mEffectsOffscreen2.apply( new MatrixEffectMove(mMoveOffscreen2) );
98
      mEffectsHead.apply( new MatrixEffectScale(mScaleHead) );
99
      mEffectsHead.apply( new MatrixEffectMove(mHeadPosition) );
100
      mEffectsOffscreen1.apply(new FragmentEffectBrightness(new Static1D(MIRROR_BRIGHTNESS)));
101
      }
102

    
103
///////////////////////////////////////////////////////////////////////////////////////////////////
104

    
105
   void setPosition(int pos)
106
      {
107
      mX = pos;
108

    
109
      float headWdivScreenW = mScreenW >0 ? (float)mHeadW/mScreenW : 0.0f;
110
      float left = headWdivScreenW -0.5f + MIRROR_MARGIN;
111
      float right= -left;
112

    
113
      mHeadPosition.set0( ((right-left)*(mX/100.0f) + left) * mScreenW );
114
      }
115
   
116
///////////////////////////////////////////////////////////////////////////////////////////////////
117

    
118
   public void onResume()
119
     {
120
     mScreenW = 0;
121
     mScreenH = 0;
122
     }
123

    
124
///////////////////////////////////////////////////////////////////////////////////////////////////
125

    
126
   public void onDrawFrame(GL10 glUnused) 
127
      {
128
      long time = System.currentTimeMillis();
129

    
130
      mOffScreen1.render(time);
131
      mOffScreen2.render(time);
132
      mScreen.render(time);
133
      }
134

    
135
///////////////////////////////////////////////////////////////////////////////////////////////////
136
    
137
   public void onSurfaceChanged(GL10 glUnused, int width, int height) 
138
      {
139
      if( mScreenW!=width || mScreenH!=height)
140
        {
141
        mScreenW = width;
142
        mScreenH = height;
143

    
144
        if( mOffScreen1!=null ) mOffScreen1.markForDeletion();
145
        if( mOffScreen2!=null ) mOffScreen2.markForDeletion();
146

    
147
        int offscreen1W = mScreenW;
148
        int offscreen1H = mScreenH;
149
        int offscreen2W = (int)(MIRROR_SCALE*mScreenW);
150
        int offscreen2H = (int)(MIRROR_SCALE*mScreenH);
151

    
152
        mQuad1.setStretch(offscreen1W, offscreen1H, 0);
153
        mQuad2.setStretch(offscreen2W, offscreen2H, 0);
154

    
155
        mOffScreen1 = new DistortedFramebuffer( offscreen1W, offscreen1H, 1, DistortedFramebuffer.NO_DEPTH_NO_STENCIL );
156
        mOffScreen2 = new DistortedFramebuffer( offscreen2W, offscreen2H, 1, DistortedFramebuffer.NO_DEPTH_NO_STENCIL );
157

    
158
        float headScale = HEAD_SCALE *mScreenH/ mHeadH;
159
        mScaleHead.set(headScale,headScale,headScale);
160

    
161
        float scaleW = (float)mScreenW/mMirrorW;
162
        float scaleH = (float)mScreenH/mMirrorH;
163

    
164
        mScaleMirror.set( scaleW, scaleH, 1.0f);
165

    
166
        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);
167
        mHeadPosition.set1( (0.5f*HEAD_SCALE - 0.5f + MIRROR_MARGIN*mMirrorW/mMirrorH)*mScreenH );
168
        setPosition(mX);
169

    
170
        mOffScreen1.attach( mTextureMirror, mEffectsMirror    , mQuadMirror );
171
        mOffScreen1.attach( mOffScreen2   , mEffectsOffscreen2, mQuad2      );
172
        mOffScreen1.attach( mTextureHead  , mEffectsHead      , mQuadHead   );
173
        mOffScreen2.attach( mOffScreen1   , mEffectsOffscreen1, mQuad1      );
174

    
175
        mScreen.detachAll();
176
        mScreen.attach    ( mOffScreen1   , mEffectsScreen    , mQuad1      );
177
        mScreen.resize(mScreenW,mScreenH);
178
        }
179
      }
180

    
181
///////////////////////////////////////////////////////////////////////////////////////////////////
182
    
183
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
184
      {
185
      InputStream isM = mView.getContext().getResources().openRawResource(R.raw.mirror);
186
      InputStream isH = mView.getContext().getResources().openRawResource(R.raw.messi);
187

    
188
      Bitmap bitmapM, bitmapH;
189

    
190
      try
191
        {
192
        bitmapM = BitmapFactory.decodeStream(isM);
193
        bitmapH = BitmapFactory.decodeStream(isH);
194
        }
195
      finally
196
        {
197
        try
198
          {
199
          isM.close();
200
          isH.close();
201
          }
202
        catch(IOException e) { }
203
        }
204

    
205
      mMirrorW = bitmapM.getWidth();
206
      mMirrorH = bitmapM.getHeight();
207
      mHeadW   = bitmapH.getWidth();
208
      mHeadH   = bitmapH.getHeight();
209

    
210
      mQuadMirror.setStretch(mMirrorW,mMirrorH,0);
211
      mQuadHead.setStretch(mHeadW, mHeadH,0);
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
      FragmentEffectBrightness.enable();
220

    
221
      try
222
        {
223
        DistortedLibrary.onCreate(mView.getContext());
224
        }
225
      catch(Exception ex)
226
        {
227
        android.util.Log.e("Mirror", ex.getMessage() );
228
        }
229
      }
230
}
(2-2/3)