Project

General

Profile

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

examples / src / main / java / org / distorted / examples / mirror / MirrorRenderer.java @ 03a2fd30

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
   private static final float SCALE = 0.5f;
47
48
   private GLSurfaceView mView;
49
   private DistortedEffects mEffectsMirror, mEffectsGirl, mEffectsOffscreen, mEffectsNull;
50
   private DistortedTexture mTextureMirror, mTextureGirl;
51
   private DistortedFramebuffer mScreen, mOffScreen1, mOffScreen2;
52
   private GridFlat mQuad;
53
   private Static3D mGirlPosition;
54
   private Dynamic3D mGirlDyn;
55
56
   private int mMirrorW, mMirrorH, mGirlW, mGirlH;
57
   private int mScreenW, mScreenH;
58
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60
61
   MirrorRenderer(GLSurfaceView view)
62
      { 
63
      mView    = view;
64
      mQuad    = new GridFlat(1,1);
65
      mScreen  = new DistortedFramebuffer(0);
66
67
      mEffectsMirror   = new DistortedEffects();
68
      mEffectsGirl     = new DistortedEffects();
69
      mEffectsOffscreen= new DistortedEffects();
70
      mEffectsNull     = new DistortedEffects();
71
72
      mGirlPosition = new Static3D(0,0,0);
73
      mGirlDyn      = new Dynamic3D();
74
      mGirlDyn.add(mGirlPosition);
75
      }
76
77
///////////////////////////////////////////////////////////////////////////////////////////////////
78
79
   void setPosition(int pos)
80
      {
81
      mGirlPosition.set1(pos*mScreenW / 100.0f);
82
      }
83
   
84
///////////////////////////////////////////////////////////////////////////////////////////////////
85
86
   public void onResume()
87
     {
88
     mScreenW = 0;
89
     mScreenH = 0;
90
     }
91
92
///////////////////////////////////////////////////////////////////////////////////////////////////
93
94
   public void onDrawFrame(GL10 glUnused) 
95
      {
96
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
97
98
      long time = System.currentTimeMillis();
99
100
      mOffScreen1.renderTo( mTextureMirror, mQuad, mEffectsMirror   , time );
101
      mOffScreen1.renderTo( mOffScreen2   , mQuad, mEffectsOffscreen, time );
102
      mOffScreen1.renderTo( mTextureGirl  , mQuad, mEffectsGirl     , time );
103
      mOffScreen2.renderTo( mOffScreen1   , mQuad, mEffectsNull     , time );
104
      mScreen.renderTo    ( mOffScreen1   , mQuad, mEffectsMirror   , time );
105
      }
106
107
///////////////////////////////////////////////////////////////////////////////////////////////////
108
    
109
   public void onSurfaceChanged(GL10 glUnused, int width, int height) 
110
      {
111
      if( mScreenW!=width || mScreenH!=height)
112
        {
113
        mScreenW = width;
114
        mScreenH = height;
115
116
        mOffScreen1 = new DistortedFramebuffer(mScreenW,mScreenH);
117
        mOffScreen2 = new DistortedFramebuffer( (int)(SCALE*mScreenW), (int)(SCALE*mScreenH) );
118
119
        mEffectsGirl.abortAllEffects();
120
        mEffectsMirror.abortAllEffects();
121
        mEffectsOffscreen.abortAllEffects();
122
123
        mEffectsMirror.scale( new Static3D( (float)mScreenW/mMirrorW, (float)mScreenH/mMirrorH, 1.0f) );
124
        mEffectsOffscreen.move( new Static3D( mScreenW/10, mScreenH/10, 0) );
125
        mEffectsGirl.move(mGirlDyn);
126
        mGirlPosition.set2(mScreenH*0.9f);
127
128
        mScreen.resize(mScreenW,mScreenH);
129
        }
130
      }
131
132
///////////////////////////////////////////////////////////////////////////////////////////////////
133
    
134
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
135
      {
136
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
137
138
      InputStream isM = mView.getContext().getResources().openRawResource(R.raw.mirror);
139
      InputStream isG = mView.getContext().getResources().openRawResource(R.raw.face);
140
141
      Bitmap bitmapM, bitmapG;
142
143
      try
144
        {
145
        bitmapM = BitmapFactory.decodeStream(isM);
146
        bitmapG = BitmapFactory.decodeStream(isG);
147
        }
148
      finally
149
        {
150
        try
151
          {
152
          isM.close();
153
          isG.close();
154
          }
155
        catch(IOException e) { }
156
        }
157
158
      mMirrorW = bitmapM.getWidth();
159
      mMirrorH = bitmapM.getHeight();
160
      mGirlW   = bitmapG.getWidth();
161
      mGirlH   = bitmapG.getHeight();
162
163
      mTextureMirror = new DistortedTexture(mMirrorW,mMirrorH);
164
      mTextureGirl   = new DistortedTexture(mGirlW, mGirlH);
165
166
      mTextureMirror.setTexture(bitmapM);
167
      mTextureGirl.setTexture(bitmapG);
168
169
      try
170
        {
171
        Distorted.onSurfaceCreated(mView.getContext());
172
        }
173
      catch(Exception ex)
174
        {
175
        android.util.Log.e("Mirror", ex.getMessage() );
176
        }
177
      }
178
}