Project

General

Profile

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

examples / src / main / java / org / distorted / examples / mirror / MirrorRenderer.java @ 88048c61

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