Project

General

Profile

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

examples / src / main / java / org / distorted / examples / mirror / MirrorRenderer.java @ 59540ba2

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.30f;  // 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.11f;  // The frame of the mirror takes up 11% 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 mEffectsMirror, mEffectsHead, mEffectsNull;
57
   private DistortedEffects mEffectsOffscreen1, mEffectsOffscreen2;
58
   private DistortedTexture mTextureMirror, mTextureHead;
59
   private DistortedFramebuffer mOffScreen1, mOffScreen2;
60
   private DistortedScreen mScreen;
61
   private MeshFlat mQuad;
62
   private Static3D mHeadPosition;
63
   private Dynamic3D mHeadDyn;
64
   private int mX;
65

    
66
   private int mMirrorW, mMirrorH, mHeadW, mHeadH;
67
   private int mScreenW, mScreenH;
68

    
69
///////////////////////////////////////////////////////////////////////////////////////////////////
70

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

    
77
      mEffectsMirror    = new DistortedEffects();
78
      mEffectsHead      = new DistortedEffects();
79
      mEffectsOffscreen1= new DistortedEffects();
80
      mEffectsOffscreen2= new DistortedEffects();
81
      mEffectsNull      = new DistortedEffects();
82

    
83
      mX = MirrorActivity.INIT_POSITION;
84

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

    
90
///////////////////////////////////////////////////////////////////////////////////////////////////
91

    
92
   void setPosition(int pos)
93
      {
94
      mX = pos;
95

    
96
      float headW = (HEAD_SCALE *mScreenH* mHeadW) / (mScreenW* mHeadH);
97

    
98
      mHeadPosition.set1(mX*(1.0f-2*MIRROR_MARGIN-headW)*mScreenW / 100.0f + MIRROR_MARGIN*mScreenW);
99
      }
100
   
101
///////////////////////////////////////////////////////////////////////////////////////////////////
102

    
103
   public void onResume()
104
     {
105
     mScreenW = 0;
106
     mScreenH = 0;
107
     }
108

    
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110

    
111
   public void onDrawFrame(GL10 glUnused) 
112
      {
113
      long time = System.currentTimeMillis();
114

    
115
      mOffScreen1.render(time);
116
      mOffScreen2.render(time);
117
      mScreen.render(time);
118
      }
119

    
120
///////////////////////////////////////////////////////////////////////////////////////////////////
121
    
122
   public void onSurfaceChanged(GL10 glUnused, int width, int height) 
123
      {
124
      if( mScreenW!=width || mScreenH!=height)
125
        {
126
        mScreenW = width;
127
        mScreenH = height;
128

    
129
        if( mOffScreen1!=null ) mOffScreen1.markForDeletion();
130
        if( mOffScreen2!=null ) mOffScreen2.markForDeletion();
131

    
132
        mOffScreen1 = new DistortedFramebuffer(mScreenW,mScreenH);
133
        mOffScreen2 = new DistortedFramebuffer( (int)(MIRROR_SCALE*mScreenW), (int)(MIRROR_SCALE*mScreenH) );
134

    
135
        mEffectsHead.abortAllEffects();
136
        mEffectsMirror.abortAllEffects();
137
        mEffectsOffscreen1.abortAllEffects();
138
        mEffectsOffscreen2.abortAllEffects();
139

    
140
        mEffectsMirror.scale( new Static3D( (float)mScreenW/mMirrorW, (float)mScreenH/mMirrorH, 1.0f) );
141
        mEffectsOffscreen1.scale(MIRROR_SCALE);
142
        mEffectsOffscreen1.brightness(new Static1D(MIRROR_BRIGHTNESS));
143
        mEffectsOffscreen2.move( new Static3D( MIRROR_MOVE*mScreenW, MIRROR_MOVE*mScreenH*mMirrorW/mMirrorH, 0) );
144

    
145
        mEffectsHead.move(mHeadDyn);
146
        float headScale = HEAD_SCALE *mScreenH/ mHeadH;
147
        mEffectsHead.scale(headScale);
148
        mHeadPosition.set2( mScreenH*(1.0f-MIRROR_MARGIN*mMirrorW/mMirrorH) - headScale* mHeadH);
149
        setPosition(mX);
150

    
151
        mOffScreen1.attach( mTextureMirror, mEffectsMirror    , mQuad );
152
        mOffScreen1.attach( mOffScreen2   , mEffectsOffscreen2, mQuad );
153
        mOffScreen1.attach( mTextureHead  , mEffectsHead      , mQuad );
154
        mOffScreen2.attach( mOffScreen1   , mEffectsOffscreen1, mQuad );
155

    
156
        mScreen.detachAll();
157
        mScreen.attach    ( mOffScreen1   , mEffectsNull      , mQuad );
158
        mScreen.resize(mScreenW,mScreenH);
159
        }
160
      }
161

    
162
///////////////////////////////////////////////////////////////////////////////////////////////////
163
    
164
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
165
      {
166
      InputStream isM = mView.getContext().getResources().openRawResource(R.raw.mirror);
167
      InputStream isH = mView.getContext().getResources().openRawResource(R.raw.messi);
168

    
169
      Bitmap bitmapM, bitmapH;
170

    
171
      try
172
        {
173
        bitmapM = BitmapFactory.decodeStream(isM);
174
        bitmapH = BitmapFactory.decodeStream(isH);
175
        }
176
      finally
177
        {
178
        try
179
          {
180
          isM.close();
181
          isH.close();
182
          }
183
        catch(IOException e) { }
184
        }
185

    
186
      mMirrorW = bitmapM.getWidth();
187
      mMirrorH = bitmapM.getHeight();
188
      mHeadW   = bitmapH.getWidth();
189
      mHeadH   = bitmapH.getHeight();
190

    
191
      if( mTextureMirror==null ) mTextureMirror = new DistortedTexture(mMirrorW,mMirrorH);
192
      if( mTextureHead  ==null ) mTextureHead   = new DistortedTexture(mHeadW, mHeadH);
193

    
194
      mTextureMirror.setTexture(bitmapM);
195
      mTextureHead.setTexture(bitmapH);
196

    
197
      DistortedEffects.enableEffect(EffectNames.BRIGHTNESS);
198

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