Project

General

Profile

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

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

1 03a2fd30 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 06cd94ba Leszek Koltunski
import org.distorted.library.effect.EffectName;
28 c8ecd3ac Leszek Koltunski
import org.distorted.library.effect.FragmentEffectBrightness;
29
import org.distorted.library.effect.MatrixEffectMove;
30
import org.distorted.library.effect.MatrixEffectScale;
31 01782e85 Leszek Koltunski
import org.distorted.library.main.Distorted;
32
import org.distorted.library.main.DistortedEffects;
33
import org.distorted.library.main.DistortedFramebuffer;
34
import org.distorted.library.main.DistortedScreen;
35
import org.distorted.library.main.DistortedTexture;
36
import org.distorted.library.main.MeshFlat;
37 88048c61 Leszek Koltunski
import org.distorted.library.type.Static1D;
38 03a2fd30 Leszek Koltunski
import org.distorted.library.type.Static3D;
39
40
import java.io.IOException;
41
import java.io.InputStream;
42
43
import javax.microedition.khronos.egl.EGLConfig;
44
import javax.microedition.khronos.opengles.GL10;
45
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47
48
class MirrorRenderer implements GLSurfaceView.Renderer
49
{
50 88048c61 Leszek Koltunski
   private static final float MIRROR_SCALE     =0.70f;  // each next mirror will be 70% of the size or the previous
51 0fb42347 leszek
   private static final float HEAD_SCALE       =0.30f;  // Head's height will be 30% of the height of the mirror
52 88048c61 Leszek Koltunski
   private static final float MIRROR_BRIGHTNESS=0.70f;  // Each next mirror 30% darker
53 0fb42347 leszek
   private static final float MIRROR_MARGIN    =0.11f;  // The frame of the mirror takes up 11% of its width
54 88048c61 Leszek Koltunski
   private static final float MIRROR_MOVE      =0.12f;  // Each next mirror is moved to the right by 12% of
55
                                                        // the length of the previous one
56 03a2fd30 Leszek Koltunski
57
   private GLSurfaceView mView;
58 0fb42347 leszek
   private DistortedEffects mEffectsMirror, mEffectsHead, mEffectsNull;
59
   private DistortedEffects mEffectsOffscreen1, mEffectsOffscreen2;
60 2c2616f1 Leszek Koltunski
   private DistortedTexture mTextureMirror, mTextureHead;
61 d218d64e leszek
   private DistortedFramebuffer mOffScreen1, mOffScreen2;
62
   private DistortedScreen mScreen;
63 b01acdaf Leszek Koltunski
   private MeshFlat mQuad;
64 c8ecd3ac Leszek Koltunski
   private Static3D mHeadPosition, mScaleMirror, mMoveOffscreen2, mScaleHead;
65 03a2fd30 Leszek Koltunski
66 c8ecd3ac Leszek Koltunski
   private int mX;
67 0fb42347 leszek
   private int mMirrorW, mMirrorH, mHeadW, mHeadH;
68 03a2fd30 Leszek Koltunski
   private int mScreenW, mScreenH;
69
70
///////////////////////////////////////////////////////////////////////////////////////////////////
71
72
   MirrorRenderer(GLSurfaceView view)
73
      { 
74
      mView    = view;
75 b01acdaf Leszek Koltunski
      mQuad    = new MeshFlat(1,1);
76 e4330c89 Leszek Koltunski
      mScreen  = new DistortedScreen();
77 03a2fd30 Leszek Koltunski
78 0fb42347 leszek
      mEffectsMirror    = new DistortedEffects();
79
      mEffectsHead      = new DistortedEffects();
80
      mEffectsOffscreen1= new DistortedEffects();
81
      mEffectsOffscreen2= new DistortedEffects();
82
      mEffectsNull      = new DistortedEffects();
83 7f9d5a91 Leszek Koltunski
84 76c9dd1d Leszek Koltunski
      mX = MirrorActivity.INIT_POSITION;
85 03a2fd30 Leszek Koltunski
86 c8ecd3ac Leszek Koltunski
      mHeadPosition    = new Static3D(0,0,0);
87
      mScaleMirror     = new Static3D(1,1,1);
88
      mMoveOffscreen2  = new Static3D(0,0,0);
89
      mScaleHead       = new Static3D(1,1,1);
90
91
      mEffectsMirror.apply( new MatrixEffectScale(mScaleMirror));
92
      mEffectsOffscreen1.apply( new MatrixEffectScale( new Static3D(MIRROR_SCALE,MIRROR_SCALE,MIRROR_SCALE)));
93
      mEffectsOffscreen2.apply( new MatrixEffectMove(mMoveOffscreen2) );
94
      mEffectsHead.apply( new MatrixEffectMove(mHeadPosition) );
95
      mEffectsHead.apply( new MatrixEffectScale(mScaleHead) );
96
      mEffectsOffscreen1.apply(new FragmentEffectBrightness(new Static1D(MIRROR_BRIGHTNESS)));
97 03a2fd30 Leszek Koltunski
      }
98
99
///////////////////////////////////////////////////////////////////////////////////////////////////
100
101
   void setPosition(int pos)
102
      {
103 7f9d5a91 Leszek Koltunski
      mX = pos;
104 0fb42347 leszek
105
      float headW = (HEAD_SCALE *mScreenH* mHeadW) / (mScreenW* mHeadH);
106
107
      mHeadPosition.set1(mX*(1.0f-2*MIRROR_MARGIN-headW)*mScreenW / 100.0f + MIRROR_MARGIN*mScreenW);
108 03a2fd30 Leszek Koltunski
      }
109
   
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111
112
   public void onResume()
113
     {
114
     mScreenW = 0;
115
     mScreenH = 0;
116
     }
117
118
///////////////////////////////////////////////////////////////////////////////////////////////////
119
120
   public void onDrawFrame(GL10 glUnused) 
121
      {
122
      long time = System.currentTimeMillis();
123
124 fe59d375 Leszek Koltunski
      mOffScreen1.render(time);
125
      mOffScreen2.render(time);
126
      mScreen.render(time);
127 03a2fd30 Leszek Koltunski
      }
128
129
///////////////////////////////////////////////////////////////////////////////////////////////////
130
    
131
   public void onSurfaceChanged(GL10 glUnused, int width, int height) 
132
      {
133
      if( mScreenW!=width || mScreenH!=height)
134
        {
135
        mScreenW = width;
136
        mScreenH = height;
137
138 59540ba2 Leszek Koltunski
        if( mOffScreen1!=null ) mOffScreen1.markForDeletion();
139
        if( mOffScreen2!=null ) mOffScreen2.markForDeletion();
140
141 f5c28afc Leszek Koltunski
        mOffScreen1 = new DistortedFramebuffer(                     mScreenW,                     mScreenH, 0, DistortedFramebuffer.NO_DEPTH_NO_STENCIL );
142
        mOffScreen2 = new DistortedFramebuffer( (int)(MIRROR_SCALE*mScreenW), (int)(MIRROR_SCALE*mScreenH), 0, DistortedFramebuffer.NO_DEPTH_NO_STENCIL );
143 03a2fd30 Leszek Koltunski
144 c8ecd3ac Leszek Koltunski
        mScaleMirror.set( (float)mScreenW/mMirrorW, (float)mScreenH/mMirrorH, 1.0f);
145
        mMoveOffscreen2.set( MIRROR_MOVE*mScreenW, MIRROR_MOVE*mScreenH*mMirrorW/mMirrorH, 0);
146 7f9d5a91 Leszek Koltunski
147 0fb42347 leszek
        float headScale = HEAD_SCALE *mScreenH/ mHeadH;
148 c8ecd3ac Leszek Koltunski
        mScaleHead.set(headScale,headScale,headScale);
149 0fb42347 leszek
        mHeadPosition.set2( mScreenH*(1.0f-MIRROR_MARGIN*mMirrorW/mMirrorH) - headScale* mHeadH);
150 7f9d5a91 Leszek Koltunski
        setPosition(mX);
151 03a2fd30 Leszek Koltunski
152 0fb42347 leszek
        mOffScreen1.attach( mTextureMirror, mEffectsMirror    , mQuad );
153 fe59d375 Leszek Koltunski
        mOffScreen1.attach( mOffScreen2   , mEffectsOffscreen2, mQuad );
154
        mOffScreen1.attach( mTextureHead  , mEffectsHead      , mQuad );
155
        mOffScreen2.attach( mOffScreen1   , mEffectsOffscreen1, mQuad );
156
157
        mScreen.detachAll();
158 0fb42347 leszek
        mScreen.attach    ( mOffScreen1   , mEffectsNull      , mQuad );
159 03a2fd30 Leszek Koltunski
        mScreen.resize(mScreenW,mScreenH);
160
        }
161
      }
162
163
///////////////////////////////////////////////////////////////////////////////////////////////////
164
    
165
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
166
      {
167
      InputStream isM = mView.getContext().getResources().openRawResource(R.raw.mirror);
168 2c2616f1 Leszek Koltunski
      InputStream isH = mView.getContext().getResources().openRawResource(R.raw.messi);
169 03a2fd30 Leszek Koltunski
170 2c2616f1 Leszek Koltunski
      Bitmap bitmapM, bitmapH;
171 03a2fd30 Leszek Koltunski
172
      try
173
        {
174
        bitmapM = BitmapFactory.decodeStream(isM);
175 2c2616f1 Leszek Koltunski
        bitmapH = BitmapFactory.decodeStream(isH);
176 03a2fd30 Leszek Koltunski
        }
177
      finally
178
        {
179
        try
180
          {
181
          isM.close();
182 2c2616f1 Leszek Koltunski
          isH.close();
183 03a2fd30 Leszek Koltunski
          }
184
        catch(IOException e) { }
185
        }
186
187 0fb42347 leszek
      mMirrorW = bitmapM.getWidth();
188
      mMirrorH = bitmapM.getHeight();
189
      mHeadW   = bitmapH.getWidth();
190
      mHeadH   = bitmapH.getHeight();
191 03a2fd30 Leszek Koltunski
192 0fb42347 leszek
      if( mTextureMirror==null ) mTextureMirror = new DistortedTexture(mMirrorW,mMirrorH);
193 477fc548 leszek
      if( mTextureHead  ==null ) mTextureHead   = new DistortedTexture(mHeadW, mHeadH);
194 03a2fd30 Leszek Koltunski
195
      mTextureMirror.setTexture(bitmapM);
196 2c2616f1 Leszek Koltunski
      mTextureHead.setTexture(bitmapH);
197 03a2fd30 Leszek Koltunski
198 885b9cca leszek
      FragmentEffectBrightness.enable();
199 6637d0f2 Leszek Koltunski
200 03a2fd30 Leszek Koltunski
      try
201
        {
202 76f9798b Leszek Koltunski
        Distorted.onCreate(mView.getContext());
203 03a2fd30 Leszek Koltunski
        }
204
      catch(Exception ex)
205
        {
206
        android.util.Log.e("Mirror", ex.getMessage() );
207
        }
208
      }
209
}