Project

General

Profile

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

examples / src / main / java / org / distorted / examples / mirror / MirrorRenderer.java @ 00191c89

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.effect.VertexEffectScale;
31
import org.distorted.library.main.DistortedLibrary;
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.mesh.MeshQuad;
37
import org.distorted.library.type.Static1D;
38
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, DistortedLibrary.ExceptionListener
49
{
50
   private static final float MIRROR_SCALE     =0.70f;  // each next mirror will be 70% of the size or the previous
51
   private static final float HEAD_SCALE       =0.30f;  // Head's height will be 30% of the height of the mirror
52
   private static final float MIRROR_BRIGHTNESS=0.70f;  // Each next mirror 30% darker
53
   private static final float MIRROR_MARGIN    =0.11f;  // The frame of the mirror takes up 11% of its width
54
   private static final float MIRROR_MOVE_H    =0.12f;  // Each next mirror is moved to the left by 12% of
55
                                                        // the length of the previous one
56
   private static final float MIRROR_MOVE_V    =0.22f;  // Move the mirror up
57

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

    
66
   private DistortedTexture mTextureMirror, mTextureHead;
67
   private DistortedFramebuffer mOffScreen1, mOffScreen2;
68

    
69
   private int mX;
70
   private int mMirrorW, mMirrorH, mHeadW, mHeadH;
71
   private int mScreenW, mScreenH;
72

    
73
///////////////////////////////////////////////////////////////////////////////////////////////////
74

    
75
   MirrorRenderer(GLSurfaceView view)
76
      { 
77
      mView    = view;
78
      mScreen  = new DistortedScreen();
79

    
80
      mQuad1     = new MeshQuad();
81
      mQuad2     = new MeshQuad();
82
      mQuadMirror= new MeshQuad();
83
      mQuadHead  = new MeshQuad();
84

    
85
      mEffectsMirror    = new DistortedEffects();
86
      mEffectsHead      = new DistortedEffects();
87
      mEffectsOffscreen1= new DistortedEffects();
88
      mEffectsOffscreen2= new DistortedEffects();
89
      mEffectsScreen    = new DistortedEffects();
90

    
91
      mX = MirrorActivity.INIT_POSITION;
92

    
93
      mHeadPosition    = new Static3D(0,0,0);
94
      mScaleMirror     = new Static3D(1,1,1);
95
      mMoveOffscreen2  = new Static3D(0,0,0);
96
      mScaleHead       = new Static3D(1,1,1);
97
      mScaleOffscreen1 = new Static3D(1,1,1);
98
      mScaleOffscreen2 = new Static3D(1,1,1);
99

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

    
111
///////////////////////////////////////////////////////////////////////////////////////////////////
112

    
113
   void setPosition(int pos)
114
      {
115
      mX = pos;
116

    
117
      float headWdivScreenW = mScreenW >0 ? (float)mHeadW/mScreenW : 0.0f;
118
      float left = headWdivScreenW -0.5f + MIRROR_MARGIN;
119
      float right= -left;
120

    
121
      mHeadPosition.set0( ((right-left)*(mX/100.0f) + left) * mScreenW );
122
      }
123
   
124
///////////////////////////////////////////////////////////////////////////////////////////////////
125

    
126
   public void onResume()
127
     {
128
     mScreenW = 0;
129
     mScreenH = 0;
130
     }
131

    
132
///////////////////////////////////////////////////////////////////////////////////////////////////
133

    
134
   public void onDrawFrame(GL10 glUnused) 
135
      {
136
      long time = System.currentTimeMillis();
137

    
138
      mOffScreen1.render(time);
139
      mOffScreen2.render(time);
140
      mScreen.render(time);
141
      }
142

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

    
152
        if( mOffScreen1!=null ) mOffScreen1.markForDeletion();
153
        if( mOffScreen2!=null ) mOffScreen2.markForDeletion();
154

    
155
        int offscreen1W = mScreenW;
156
        int offscreen1H = mScreenH;
157
        int offscreen2W = (int)(MIRROR_SCALE*mScreenW);
158
        int offscreen2H = (int)(MIRROR_SCALE*mScreenH);
159

    
160
        mScaleOffscreen1.set(offscreen1W, offscreen1H, 0);
161
        mScaleOffscreen2.set(offscreen2W, offscreen2H, 0);
162

    
163
        mOffScreen1 = new DistortedFramebuffer( offscreen1W, offscreen1H, 1, DistortedFramebuffer.NO_DEPTH_NO_STENCIL );
164
        mOffScreen2 = new DistortedFramebuffer( offscreen2W, offscreen2H, 1, DistortedFramebuffer.NO_DEPTH_NO_STENCIL );
165

    
166
        float headScale = HEAD_SCALE *mScreenH;
167
        mScaleHead.set(headScale*mHeadW/mHeadH,headScale,0);
168
        mScaleMirror.set( mScreenW, mScreenH, 0);
169

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

    
174
        mOffScreen1.attach( mTextureMirror, mEffectsMirror    , mQuadMirror );
175
        mOffScreen1.attach( mOffScreen2   , mEffectsOffscreen2, mQuad2      );
176
        mOffScreen1.attach( mTextureHead  , mEffectsHead      , mQuadHead   );
177
        mOffScreen2.attach( mOffScreen1   , mEffectsOffscreen1, mQuad1      );
178

    
179
        mScreen.detachAll();
180
        mScreen.attach    ( mOffScreen1   , mEffectsScreen    , mQuad1      );
181
        mScreen.resize(mScreenW,mScreenH);
182
        }
183
      }
184

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

    
192
      Bitmap bitmapM, bitmapH;
193

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

    
209
      mMirrorW = bitmapM.getWidth();
210
      mMirrorH = bitmapM.getHeight();
211
      mHeadW   = bitmapH.getWidth();
212
      mHeadH   = bitmapH.getHeight();
213

    
214
      if( mTextureMirror==null ) mTextureMirror = new DistortedTexture();
215
      if( mTextureHead  ==null ) mTextureHead   = new DistortedTexture();
216

    
217
      mTextureMirror.setTexture(bitmapM);
218
      mTextureHead.setTexture(bitmapH);
219

    
220
      VertexEffectScale.enable();
221
      FragmentEffectBrightness.enable();
222

    
223
      DistortedLibrary.onSurfaceCreated(mView.getContext(), this);
224
      }
225

    
226
///////////////////////////////////////////////////////////////////////////////////////////////////
227

    
228
    public void distortedException(Exception ex)
229
      {
230
      android.util.Log.e("Mirror", ex.getMessage() );
231
      }
232
}
(2-2/3)