Project

General

Profile

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

examples / src / main / java / org / distorted / examples / mirror / MirrorRenderer.java @ 061449ed

1 03a2fd30 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4 71c8884f Leszek Koltunski
// This file is part of Distorted.                                                               //
5 03a2fd30 Leszek Koltunski
//                                                                                               //
6 71c8884f Leszek Koltunski
// Distorted is free software: you can redistribute it and/or modify                             //
7 03a2fd30 Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// Distorted is distributed in the hope that it will be useful,                                  //
12 03a2fd30 Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18 03a2fd30 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 c8ecd3ac Leszek Koltunski
import org.distorted.library.effect.FragmentEffectBrightness;
28
import org.distorted.library.effect.MatrixEffectMove;
29
import org.distorted.library.effect.MatrixEffectScale;
30 f94110ca Leszek Koltunski
import org.distorted.library.effect.VertexEffectScale;
31 e3900503 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
32 01782e85 Leszek Koltunski
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 698ad0a8 Leszek Koltunski
import org.distorted.library.mesh.MeshQuad;
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 061449ed Leszek Koltunski
class MirrorRenderer implements GLSurfaceView.Renderer, DistortedLibrary.ExceptionListener
49 03a2fd30 Leszek Koltunski
{
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 a4d59c0b Leszek Koltunski
   private static final float MIRROR_MOVE_H    =0.12f;  // Each next mirror is moved to the left by 12% of
55 88048c61 Leszek Koltunski
                                                        // the length of the previous one
56 a4d59c0b Leszek Koltunski
   private static final float MIRROR_MOVE_V    =0.22f;  // Move the mirror up
57 03a2fd30 Leszek Koltunski
58
   private GLSurfaceView mView;
59 687263cc Leszek Koltunski
   private DistortedEffects mEffectsMirror, mEffectsHead, mEffectsScreen;
60 0fb42347 leszek
   private DistortedEffects mEffectsOffscreen1, mEffectsOffscreen2;
61 2c2616f1 Leszek Koltunski
   private DistortedTexture mTextureMirror, mTextureHead;
62 d218d64e leszek
   private DistortedFramebuffer mOffScreen1, mOffScreen2;
63
   private DistortedScreen mScreen;
64 698ad0a8 Leszek Koltunski
   private MeshQuad mQuad1, mQuad2, mQuadMirror, mQuadHead;
65 c8ecd3ac Leszek Koltunski
   private Static3D mHeadPosition, mScaleMirror, mMoveOffscreen2, mScaleHead;
66 f94110ca Leszek Koltunski
   private Static3D mScaleOffscreen1, mScaleOffscreen2;
67 03a2fd30 Leszek Koltunski
68 c8ecd3ac Leszek Koltunski
   private int mX;
69 0fb42347 leszek
   private int mMirrorW, mMirrorH, mHeadW, mHeadH;
70 03a2fd30 Leszek Koltunski
   private int mScreenW, mScreenH;
71
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73
74
   MirrorRenderer(GLSurfaceView view)
75
      { 
76
      mView    = view;
77 e4330c89 Leszek Koltunski
      mScreen  = new DistortedScreen();
78 03a2fd30 Leszek Koltunski
79 698ad0a8 Leszek Koltunski
      mQuad1     = new MeshQuad();
80
      mQuad2     = new MeshQuad();
81
      mQuadMirror= new MeshQuad();
82
      mQuadHead  = new MeshQuad();
83
84
      mEffectsMirror    = new DistortedEffects();
85
      mEffectsHead      = new DistortedEffects();
86
      mEffectsOffscreen1= new DistortedEffects();
87
      mEffectsOffscreen2= new DistortedEffects();
88
      mEffectsScreen    = new DistortedEffects();
89 7f9d5a91 Leszek Koltunski
90 76c9dd1d Leszek Koltunski
      mX = MirrorActivity.INIT_POSITION;
91 03a2fd30 Leszek Koltunski
92 c8ecd3ac Leszek Koltunski
      mHeadPosition    = new Static3D(0,0,0);
93
      mScaleMirror     = new Static3D(1,1,1);
94
      mMoveOffscreen2  = new Static3D(0,0,0);
95
      mScaleHead       = new Static3D(1,1,1);
96 f94110ca Leszek Koltunski
      mScaleOffscreen1 = new Static3D(1,1,1);
97
      mScaleOffscreen2 = new Static3D(1,1,1);
98 c8ecd3ac Leszek Koltunski
99 f94110ca Leszek Koltunski
      mEffectsMirror.apply( new VertexEffectScale(mScaleMirror));
100
      mEffectsScreen.apply( new VertexEffectScale(mScaleOffscreen1) );
101
      mEffectsOffscreen1.apply( new VertexEffectScale(mScaleOffscreen1) );
102 c8ecd3ac Leszek Koltunski
      mEffectsOffscreen1.apply( new MatrixEffectScale( new Static3D(MIRROR_SCALE,MIRROR_SCALE,MIRROR_SCALE)));
103 f94110ca Leszek Koltunski
      mEffectsOffscreen2.apply( new VertexEffectScale(mScaleOffscreen2) );
104 c8ecd3ac Leszek Koltunski
      mEffectsOffscreen2.apply( new MatrixEffectMove(mMoveOffscreen2) );
105 f94110ca Leszek Koltunski
      mEffectsHead.apply( new VertexEffectScale(mScaleHead) );
106 8664ea2e Leszek Koltunski
      mEffectsHead.apply( new MatrixEffectMove(mHeadPosition) );
107 c8ecd3ac Leszek Koltunski
      mEffectsOffscreen1.apply(new FragmentEffectBrightness(new Static1D(MIRROR_BRIGHTNESS)));
108 03a2fd30 Leszek Koltunski
      }
109
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111
112
   void setPosition(int pos)
113
      {
114 7f9d5a91 Leszek Koltunski
      mX = pos;
115 0fb42347 leszek
116 16b22aab Leszek Koltunski
      float headWdivScreenW = mScreenW >0 ? (float)mHeadW/mScreenW : 0.0f;
117
      float left = headWdivScreenW -0.5f + MIRROR_MARGIN;
118
      float right= -left;
119 0fb42347 leszek
120 386fd702 Leszek Koltunski
      mHeadPosition.set0( ((right-left)*(mX/100.0f) + left) * mScreenW );
121 03a2fd30 Leszek Koltunski
      }
122
   
123
///////////////////////////////////////////////////////////////////////////////////////////////////
124
125
   public void onResume()
126
     {
127
     mScreenW = 0;
128
     mScreenH = 0;
129
     }
130
131
///////////////////////////////////////////////////////////////////////////////////////////////////
132
133
   public void onDrawFrame(GL10 glUnused) 
134
      {
135
      long time = System.currentTimeMillis();
136
137 fe59d375 Leszek Koltunski
      mOffScreen1.render(time);
138
      mOffScreen2.render(time);
139
      mScreen.render(time);
140 03a2fd30 Leszek Koltunski
      }
141
142
///////////////////////////////////////////////////////////////////////////////////////////////////
143
    
144
   public void onSurfaceChanged(GL10 glUnused, int width, int height) 
145
      {
146
      if( mScreenW!=width || mScreenH!=height)
147
        {
148
        mScreenW = width;
149
        mScreenH = height;
150
151 59540ba2 Leszek Koltunski
        if( mOffScreen1!=null ) mOffScreen1.markForDeletion();
152
        if( mOffScreen2!=null ) mOffScreen2.markForDeletion();
153
154 687263cc Leszek Koltunski
        int offscreen1W = mScreenW;
155
        int offscreen1H = mScreenH;
156
        int offscreen2W = (int)(MIRROR_SCALE*mScreenW);
157
        int offscreen2H = (int)(MIRROR_SCALE*mScreenH);
158
159 f94110ca Leszek Koltunski
        mScaleOffscreen1.set(offscreen1W, offscreen1H, 0);
160
        mScaleOffscreen2.set(offscreen2W, offscreen2H, 0);
161 687263cc Leszek Koltunski
162
        mOffScreen1 = new DistortedFramebuffer( offscreen1W, offscreen1H, 1, DistortedFramebuffer.NO_DEPTH_NO_STENCIL );
163
        mOffScreen2 = new DistortedFramebuffer( offscreen2W, offscreen2H, 1, DistortedFramebuffer.NO_DEPTH_NO_STENCIL );
164 03a2fd30 Leszek Koltunski
165 f94110ca Leszek Koltunski
        float headScale = HEAD_SCALE *mScreenH;
166
        mScaleHead.set(headScale*mHeadW/mHeadH,headScale,0);
167
        mScaleMirror.set( mScreenW, mScreenH, 0);
168 16b22aab Leszek Koltunski
169
        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);
170 8664ea2e Leszek Koltunski
        mHeadPosition.set1( (0.5f*HEAD_SCALE - 0.5f + MIRROR_MARGIN*mMirrorW/mMirrorH)*mScreenH );
171 7f9d5a91 Leszek Koltunski
        setPosition(mX);
172 03a2fd30 Leszek Koltunski
173 698ad0a8 Leszek Koltunski
        mOffScreen1.attach( mTextureMirror, mEffectsMirror    , mQuadMirror );
174
        mOffScreen1.attach( mOffScreen2   , mEffectsOffscreen2, mQuad2      );
175
        mOffScreen1.attach( mTextureHead  , mEffectsHead      , mQuadHead   );
176
        mOffScreen2.attach( mOffScreen1   , mEffectsOffscreen1, mQuad1      );
177 fe59d375 Leszek Koltunski
178
        mScreen.detachAll();
179 698ad0a8 Leszek Koltunski
        mScreen.attach    ( mOffScreen1   , mEffectsScreen    , mQuad1      );
180 03a2fd30 Leszek Koltunski
        mScreen.resize(mScreenW,mScreenH);
181
        }
182
      }
183
184
///////////////////////////////////////////////////////////////////////////////////////////////////
185
    
186
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
187
      {
188
      InputStream isM = mView.getContext().getResources().openRawResource(R.raw.mirror);
189 2c2616f1 Leszek Koltunski
      InputStream isH = mView.getContext().getResources().openRawResource(R.raw.messi);
190 03a2fd30 Leszek Koltunski
191 2c2616f1 Leszek Koltunski
      Bitmap bitmapM, bitmapH;
192 03a2fd30 Leszek Koltunski
193
      try
194
        {
195
        bitmapM = BitmapFactory.decodeStream(isM);
196 2c2616f1 Leszek Koltunski
        bitmapH = BitmapFactory.decodeStream(isH);
197 03a2fd30 Leszek Koltunski
        }
198
      finally
199
        {
200
        try
201
          {
202
          isM.close();
203 2c2616f1 Leszek Koltunski
          isH.close();
204 03a2fd30 Leszek Koltunski
          }
205
        catch(IOException e) { }
206
        }
207
208 0fb42347 leszek
      mMirrorW = bitmapM.getWidth();
209
      mMirrorH = bitmapM.getHeight();
210
      mHeadW   = bitmapH.getWidth();
211
      mHeadH   = bitmapH.getHeight();
212 03a2fd30 Leszek Koltunski
213 687263cc Leszek Koltunski
      if( mTextureMirror==null ) mTextureMirror = new DistortedTexture();
214
      if( mTextureHead  ==null ) mTextureHead   = new DistortedTexture();
215 03a2fd30 Leszek Koltunski
216
      mTextureMirror.setTexture(bitmapM);
217 2c2616f1 Leszek Koltunski
      mTextureHead.setTexture(bitmapH);
218 03a2fd30 Leszek Koltunski
219 f94110ca Leszek Koltunski
      VertexEffectScale.enable();
220 885b9cca leszek
      FragmentEffectBrightness.enable();
221 6637d0f2 Leszek Koltunski
222 061449ed Leszek Koltunski
      DistortedLibrary.onCreate(mView.getContext(), this);
223
      }
224
225
///////////////////////////////////////////////////////////////////////////////////////////////////
226
227
    public void distortedException(Exception ex)
228
      {
229
      android.util.Log.e("Mirror", ex.getMessage() );
230 03a2fd30 Leszek Koltunski
      }
231
}