Project

General

Profile

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

examples / src / main / java / org / distorted / examples / mirror / MirrorRenderer.java @ 2c2616f1

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