Project

General

Profile

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

examples / src / main / java / org / distorted / examples / blur / BlurRenderer.java @ 30f337e5

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
     mHaloRadiusSta.set0(halo);
114
     return halo;
115
     }
116

    
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118

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

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

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

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

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

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

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

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

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

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

    
178
     PostprocessEffectBlur.enable();
179

    
180
     DistortedLibrary.onSurfaceCreated(this);
181
     }
182

    
183
///////////////////////////////////////////////////////////////////////////////////////////////////
184

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

    
190
///////////////////////////////////////////////////////////////////////////////////////////////////
191

    
192
   public InputStream localFile(int fileID)
193
     {
194
     return mResources.openRawResource(fileID);
195
     }
196

    
197
///////////////////////////////////////////////////////////////////////////////////////////////////
198

    
199
   public void logMessage(String message)
200
     {
201
     android.util.Log.e("Blur", message );
202
     }
203
}
(2-2/3)