Project

General

Profile

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

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

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.Distorted;
28
import org.distorted.library.DistortedEffects;
29
import org.distorted.library.DistortedFramebuffer;
30
import org.distorted.library.DistortedScreen;
31
import org.distorted.library.DistortedTexture;
32
import org.distorted.library.EffectNames;
33
import org.distorted.library.MeshFlat;
34
import org.distorted.library.type.Dynamic3D;
35
import org.distorted.library.type.Static1D;
36
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
   private static final float MIRROR_SCALE     =0.70f;  // each next mirror will be 70% of the size or the previous
49
   private static final float HEAD_SCALE       =0.40f;  // Head's height will be 30% of the height of the mirror
50
   private static final float MIRROR_BRIGHTNESS=0.70f;  // Each next mirror 30% darker
51
   private static final float MIRROR_MARGIN    =0.09f;  // The frame of the mirror takes up 9% of its width
52
   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

    
55
   private GLSurfaceView mView;
56
   private DistortedEffects mEffectsHead, mEffectsNull, mEffectsOffscreen1, mEffectsOffscreen2;
57
   private DistortedTexture mTextureMirror, mTextureHead;
58
   private DistortedFramebuffer mOffScreen1, mOffScreen2;
59
   private DistortedScreen mScreen;
60
   private MeshFlat mQuad;
61
   private Static3D mHeadPosition;
62
   private Dynamic3D mHeadDyn;
63
   private int mX;
64

    
65
   private int mHeadW, mHeadH;
66
   private int mScreenW, mScreenH;
67

    
68
///////////////////////////////////////////////////////////////////////////////////////////////////
69

    
70
   MirrorRenderer(GLSurfaceView view)
71
      { 
72
      mView    = view;
73
      mQuad    = new MeshFlat(1,1);
74
      mScreen  = new DistortedScreen();
75

    
76
      mEffectsHead       = new DistortedEffects();
77
      mEffectsOffscreen1 = new DistortedEffects();
78
      mEffectsOffscreen2 = new DistortedEffects();
79
      mEffectsNull       = new DistortedEffects();
80

    
81
      mX = MirrorActivity.INIT_POSITION;
82

    
83
      mHeadPosition = new Static3D(0,0,0);
84
      mHeadDyn = new Dynamic3D();
85
      mHeadDyn.add(mHeadPosition);
86
      }
87

    
88
///////////////////////////////////////////////////////////////////////////////////////////////////
89

    
90
   void setPosition(int pos)
91
      {
92
      mX = pos;
93
      mHeadPosition.set1( (mX/50.0f-1)*(0.5f-MIRROR_MARGIN-HEAD_SCALE/2) );
94
      }
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
      mOffScreen1.render(time);
111
      mOffScreen2.render(time);
112
      mScreen.render(time);
113
      }
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
        if( mOffScreen1!=null ) mOffScreen1.markForDeletion();
125
        if( mOffScreen2!=null ) mOffScreen2.markForDeletion();
126

    
127
        mOffScreen1 = new DistortedFramebuffer(mScreenW,mScreenH);
128
        mOffScreen2 = new DistortedFramebuffer( (int)(MIRROR_SCALE*mScreenW), (int)(MIRROR_SCALE*mScreenH) );
129

    
130
        mEffectsHead.abortAllEffects();
131
        mEffectsOffscreen1.abortAllEffects();
132
        mEffectsOffscreen2.abortAllEffects();
133

    
134
        mEffectsOffscreen1.brightness(new Static1D(MIRROR_BRIGHTNESS));
135
        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

    
138
        mEffectsHead.move(mHeadDyn);
139
        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
        setPosition(mX);
154

    
155
        mOffScreen1.attach( mTextureMirror, mEffectsNull      , mQuad );
156
        mOffScreen1.attach( mOffScreen2   , mEffectsOffscreen2, mQuad );
157
        mOffScreen1.attach( mTextureHead  , mEffectsHead      , mQuad );
158
        mOffScreen2.attach( mOffScreen1   , mEffectsOffscreen1, mQuad );
159

    
160
        mScreen.detachAll();
161
        mScreen.attach    ( mOffScreen1   , mEffectsNull     , mQuad );
162
        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
      InputStream isH = mView.getContext().getResources().openRawResource(R.raw.messi);
172

    
173
      Bitmap bitmapM, bitmapH;
174

    
175
      try
176
        {
177
        bitmapM = BitmapFactory.decodeStream(isM);
178
        bitmapH = BitmapFactory.decodeStream(isH);
179
        }
180
      finally
181
        {
182
        try
183
          {
184
          isM.close();
185
          isH.close();
186
          }
187
        catch(IOException e) { }
188
        }
189

    
190
      mHeadW = bitmapH.getWidth();
191
      mHeadH = bitmapH.getHeight();
192

    
193
      if( mTextureMirror==null ) mTextureMirror = new DistortedTexture(bitmapM.getWidth(),bitmapM.getHeight());
194
      if( mTextureHead  ==null ) mTextureHead   = new DistortedTexture(mHeadW, mHeadH);
195

    
196
      mTextureMirror.setTexture(bitmapM);
197
      mTextureHead.setTexture(bitmapH);
198

    
199
      DistortedEffects.enableEffect(EffectNames.BRIGHTNESS);
200

    
201
      try
202
        {
203
        Distorted.onCreate(mView.getContext());
204
        }
205
      catch(Exception ex)
206
        {
207
        android.util.Log.e("Mirror", ex.getMessage() );
208
        }
209
      }
210
}
(2-2/3)