Project

General

Profile

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

examples / src / main / java / org / distorted / examples / mirror / MirrorRenderer.java @ 19938eb7

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