Project

General

Profile

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

examples / src / main / java / org / distorted / examples / blur / BlurRenderer.java @ dc10a48d

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.blur;
21

    
22
import android.app.ActivityManager;
23
import android.content.Context;
24
import android.content.pm.ConfigurationInfo;
25
import android.content.res.Resources;
26
import android.graphics.Bitmap;
27
import android.graphics.BitmapFactory;
28
import android.opengl.GLSurfaceView;
29

    
30
import org.distorted.examples.R;
31
import org.distorted.library.effect.MatrixEffectMove;
32
import org.distorted.library.effect.MatrixEffectScale;
33
import org.distorted.library.effect.PostprocessEffectBlur;
34
import org.distorted.library.main.DistortedLibrary;
35
import org.distorted.library.main.DistortedEffects;
36
import org.distorted.library.main.DistortedFramebuffer;
37
import org.distorted.library.main.DistortedScreen;
38
import org.distorted.library.main.DistortedTexture;
39
import org.distorted.library.mesh.MeshSquare;
40
import org.distorted.library.type.Dynamic2D;
41
import org.distorted.library.type.Static2D;
42
import org.distorted.library.type.Static3D;
43

    
44
import java.io.IOException;
45
import java.io.InputStream;
46

    
47
import javax.microedition.khronos.egl.EGLConfig;
48
import javax.microedition.khronos.opengles.GL10;
49

    
50
///////////////////////////////////////////////////////////////////////////////////////////////////
51

    
52
class BlurRenderer implements GLSurfaceView.Renderer, DistortedLibrary.LibraryUser
53
{
54
    private final static int SIZE = 500;
55
    private final static float PART = 0.5f;
56

    
57
    private final GLSurfaceView mView;
58
    private final Resources mResources;
59
    private final DistortedEffects mEffects, mBufferEffects;
60
    private final DistortedScreen mScreen;
61
    private final DistortedFramebuffer mBuffer;
62
    private final MeshSquare mMesh, mMeshBuffer;
63
    private final Static2D mHaloRadiusSta;
64
    private final Static3D mMove, mScale, mBufferScale;
65

    
66
    private DistortedTexture mTexture;
67

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

    
70
   BlurRenderer(GLSurfaceView v)
71
      {
72
      mView = v;
73
      mResources = v.getResources();
74

    
75
      mMesh       = new MeshSquare(1,1);
76
      mMeshBuffer = new MeshSquare(1,1);
77
      mScreen     = new DistortedScreen();
78
      mBuffer     = new DistortedFramebuffer(SIZE,SIZE,1, DistortedFramebuffer.NO_DEPTH_NO_STENCIL);
79

    
80
      mHaloRadiusSta = new Static2D(10,5);
81
      Dynamic2D haloAndRadiusDyn = new Dynamic2D();
82
      haloAndRadiusDyn.add(mHaloRadiusSta);
83

    
84
      mMove = new Static3D(0,0,0);
85
      mScale= new Static3D(1,1,1);
86
      mBufferScale= new Static3D(1,1,1);
87

    
88
      mBufferEffects = new DistortedEffects();
89
      mBufferEffects.apply(new MatrixEffectScale(mBufferScale));
90
      mBufferEffects.apply(new PostprocessEffectBlur(haloAndRadiusDyn));
91

    
92
      mEffects = new DistortedEffects();
93
      mEffects.apply(new MatrixEffectScale(mScale));
94
      mEffects.apply(new MatrixEffectMove(mMove));
95
      }
96

    
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98
//   0 --> (SIZE-factor2) * -0.5
99
//  50 --> (SIZE-factor2) *  0.0
100
// 100 --> (SIZE-factor2) * +0.5
101

    
102
   int setMove(int move)
103
     {
104
     float xmove = (1-PART)*SIZE*(move-50)*0.01f;
105
     mMove.set0(xmove);
106
     return (int)xmove;
107
     }
108

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

    
111
   int setHalo(int halo)
112
     {
113
     int radius = halo;
114
     mHaloRadiusSta.set0(radius);
115
     return radius;
116
     }
117

    
118
///////////////////////////////////////////////////////////////////////////////////////////////////
119

    
120
   int setBlur(int blur)
121
     {
122
     int radius = blur/2;
123
     mHaloRadiusSta.set1(radius);
124
     return radius;
125
     }
126

    
127
///////////////////////////////////////////////////////////////////////////////////////////////////
128
   
129
   public void onDrawFrame(GL10 glUnused) 
130
      {
131
      long time = System.currentTimeMillis();
132

    
133
      mBuffer.render(time);
134
      mScreen.render(time);
135
      }
136

    
137
///////////////////////////////////////////////////////////////////////////////////////////////////
138
    
139
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
140
     {
141
     float factor1 = Math.min(width, height);
142
     mBufferScale.set( factor1,factor1,factor1 );
143

    
144
     float factor2 = PART*SIZE;
145
     mScale.set( factor2,factor2,factor2 );
146

    
147
     mScreen.resize(width, height);
148
     }
149

    
150
///////////////////////////////////////////////////////////////////////////////////////////////////
151
    
152
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
153
     {
154
     InputStream is = mView.getContext().getResources().openRawResource(R.raw.dog);
155
     Bitmap bitmap;
156
        
157
     try
158
       {
159
       bitmap = BitmapFactory.decodeStream(is);
160
       }
161
     finally
162
       {
163
       try
164
         {
165
         is.close();
166
         }
167
       catch(IOException ignored) { }
168
       }
169

    
170
     if( mTexture==null ) mTexture = new DistortedTexture();
171
     mTexture.setTexture(bitmap);
172

    
173
     mScreen.detachAll();
174
     mBuffer.detachAll();
175

    
176
     mScreen.attach(mBuffer, mBufferEffects, mMeshBuffer);
177
     mBuffer.attach(mTexture,mEffects,mMesh);
178

    
179
     PostprocessEffectBlur.enable();
180

    
181
     DistortedLibrary.onSurfaceCreated(this);
182
     }
183

    
184
///////////////////////////////////////////////////////////////////////////////////////////////////
185

    
186
   public void distortedException(Exception ex)
187
     {
188
     android.util.Log.e("Blur", ex.getMessage() );
189
     }
190

    
191
///////////////////////////////////////////////////////////////////////////////////////////////////
192

    
193
   public int openGlVersion()
194
     {
195
     Context context = mView.getContext();
196
     final ActivityManager activityManager     = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
197
     final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
198
     int glESversion = configurationInfo.reqGlEsVersion;
199
     int major = glESversion >> 16;
200
     int minor = glESversion & 0xff;
201

    
202
     return 100*major + 10*minor;
203
     }
204

    
205
///////////////////////////////////////////////////////////////////////////////////////////////////
206

    
207
   public InputStream localFile(int fileID)
208
     {
209
     return mResources.openRawResource(fileID);
210
     }
211

    
212
///////////////////////////////////////////////////////////////////////////////////////////////////
213

    
214
   public void logMessage(String message)
215
     {
216
     android.util.Log.e("Blur", message );
217
     }
218
}
(2-2/3)