Project

General

Profile

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

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

1 43bda3db Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4 71c8884f Leszek Koltunski
// This file is part of Distorted.                                                               //
5 43bda3db Leszek Koltunski
//                                                                                               //
6 71c8884f Leszek Koltunski
// Distorted is free software: you can redistribute it and/or modify                             //
7 43bda3db Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// Distorted is distributed in the hope that it will be useful,                                  //
12 43bda3db Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18 43bda3db Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
19
20
package org.distorted.examples.blur;
21
22
import android.graphics.Bitmap;
23
import android.graphics.BitmapFactory;
24
import android.opengl.GLSurfaceView;
25
26
import org.distorted.examples.R;
27 bf2e8f97 Leszek Koltunski
import org.distorted.library.effect.MatrixEffectMove;
28 fa9b6494 Leszek Koltunski
import org.distorted.library.effect.MatrixEffectScale;
29
import org.distorted.library.effect.PostprocessEffectBlur;
30 e3900503 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
31 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedEffects;
32 da57b7da Leszek Koltunski
import org.distorted.library.main.DistortedFramebuffer;
33 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedScreen;
34
import org.distorted.library.main.DistortedTexture;
35 f3ca895f Leszek Koltunski
import org.distorted.library.mesh.MeshRectangles;
36 678c391d Leszek Koltunski
import org.distorted.library.type.Dynamic2D;
37
import org.distorted.library.type.Static2D;
38 43bda3db Leszek Koltunski
import org.distorted.library.type.Static3D;
39
40
import java.io.IOException;
41
import java.io.InputStream;
42
43
import javax.microedition.khronos.egl.EGLConfig;
44
import javax.microedition.khronos.opengles.GL10;
45
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47
48 061449ed Leszek Koltunski
class BlurRenderer implements GLSurfaceView.Renderer, DistortedLibrary.ExceptionListener
49 43bda3db Leszek Koltunski
{
50 da57b7da Leszek Koltunski
    private final static int SIZE = 500;
51 bf2e8f97 Leszek Koltunski
    private final static float PART = 0.5f;
52 da57b7da Leszek Koltunski
53 43bda3db Leszek Koltunski
    private GLSurfaceView mView;
54 59540ba2 Leszek Koltunski
    private DistortedTexture mTexture;
55 da57b7da Leszek Koltunski
    private DistortedEffects mEffects, mBufferEffects;
56 d218d64e leszek
    private DistortedScreen mScreen;
57 da57b7da Leszek Koltunski
    private DistortedFramebuffer mBuffer;
58 698ad0a8 Leszek Koltunski
    private MeshRectangles mMesh, mMeshBuffer;
59 678c391d Leszek Koltunski
    private Static2D mHaloRadiusSta;
60 bf2e8f97 Leszek Koltunski
    private Static3D mMove, mScale, mBufferScale;
61 43bda3db Leszek Koltunski
62
///////////////////////////////////////////////////////////////////////////////////////////////////
63
64
   BlurRenderer(GLSurfaceView v)
65
      {
66 698ad0a8 Leszek Koltunski
      mView       = v;
67
      mMesh       = new MeshRectangles(1,1);
68
      mMeshBuffer = new MeshRectangles(1,1);
69
      mScreen     = new DistortedScreen();
70
      mBuffer     = new DistortedFramebuffer(SIZE,SIZE,1, DistortedFramebuffer.NO_DEPTH_NO_STENCIL);
71 43bda3db Leszek Koltunski
72 678c391d Leszek Koltunski
      mHaloRadiusSta = new Static2D(10,5);
73
      Dynamic2D haloAndRadiusDyn = new Dynamic2D();
74
      haloAndRadiusDyn.add(mHaloRadiusSta);
75 43bda3db Leszek Koltunski
76 bf2e8f97 Leszek Koltunski
      mMove = new Static3D(0,0,0);
77 fa9b6494 Leszek Koltunski
      mScale= new Static3D(1,1,1);
78 da57b7da Leszek Koltunski
      mBufferScale= new Static3D(1,1,1);
79
80 698ad0a8 Leszek Koltunski
      mBufferEffects = new DistortedEffects();
81 da57b7da Leszek Koltunski
      mBufferEffects.apply(new MatrixEffectScale(mBufferScale));
82 bf2e8f97 Leszek Koltunski
      mBufferEffects.apply(new PostprocessEffectBlur(haloAndRadiusDyn));
83 fa9b6494 Leszek Koltunski
84 698ad0a8 Leszek Koltunski
      mEffects = new DistortedEffects();
85 fa9b6494 Leszek Koltunski
      mEffects.apply(new MatrixEffectScale(mScale));
86 bf2e8f97 Leszek Koltunski
      mEffects.apply(new MatrixEffectMove(mMove));
87 43bda3db Leszek Koltunski
      }
88
89 bf2e8f97 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
90
//   0 --> (SIZE-factor2) * -0.5
91
//  50 --> (SIZE-factor2) *  0.0
92
// 100 --> (SIZE-factor2) * +0.5
93
94
   int setMove(int move)
95
     {
96
     float xmove = (1-PART)*SIZE*(move-50)*0.01f;
97
     mMove.set0(xmove);
98
     return (int)xmove;
99
     }
100
101
///////////////////////////////////////////////////////////////////////////////////////////////////
102
103
   int setHalo(int halo)
104
     {
105
     int radius = halo;
106
     mHaloRadiusSta.set0(radius);
107
     return radius;
108
     }
109
110 43bda3db Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
111
112 a6da86cc Leszek Koltunski
   int setBlur(int blur)
113
     {
114 3365725b Leszek Koltunski
     int radius = blur/2;
115 678c391d Leszek Koltunski
     mHaloRadiusSta.set1(radius);
116 a6da86cc Leszek Koltunski
     return radius;
117 43bda3db Leszek Koltunski
     }
118
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120
   
121
   public void onDrawFrame(GL10 glUnused) 
122
      {
123 da57b7da Leszek Koltunski
      long time = System.currentTimeMillis();
124
125
      mBuffer.render(time);
126
      mScreen.render(time);
127 43bda3db Leszek Koltunski
      }
128
129
///////////////////////////////////////////////////////////////////////////////////////////////////
130
    
131 fa9b6494 Leszek Koltunski
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
132
     {
133 bf2e8f97 Leszek Koltunski
     float factor1 = Math.min(width, height);
134 da57b7da Leszek Koltunski
     mBufferScale.set( factor1,factor1,factor1 );
135
136 bf2e8f97 Leszek Koltunski
     float factor2 = PART*SIZE;
137 da57b7da Leszek Koltunski
     mScale.set( factor2,factor2,factor2 );
138
139 463f1f9c leszek
     mScreen.resize(width, height);
140 fa9b6494 Leszek Koltunski
     }
141 43bda3db Leszek Koltunski
142
///////////////////////////////////////////////////////////////////////////////////////////////////
143
    
144 fa9b6494 Leszek Koltunski
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
145
     {
146
     InputStream is = mView.getContext().getResources().openRawResource(R.raw.dog);
147
     Bitmap bitmap;
148 43bda3db Leszek Koltunski
        
149 fa9b6494 Leszek Koltunski
     try
150
       {
151
       bitmap = BitmapFactory.decodeStream(is);
152
       }
153
     finally
154
       {
155
       try
156
         {
157
         is.close();
158
         }
159
       catch(IOException e) { }
160
       }
161 687263cc Leszek Koltunski
162
     if( mTexture==null ) mTexture = new DistortedTexture();
163 fa9b6494 Leszek Koltunski
     mTexture.setTexture(bitmap);
164
165
     mScreen.detachAll();
166 bf2e8f97 Leszek Koltunski
     mBuffer.detachAll();
167
168 698ad0a8 Leszek Koltunski
     mScreen.attach(mBuffer, mBufferEffects, mMeshBuffer);
169 bf2e8f97 Leszek Koltunski
     mBuffer.attach(mTexture,mEffects,mMesh);
170 fa9b6494 Leszek Koltunski
171 885b9cca leszek
     PostprocessEffectBlur.enable();
172 fa9b6494 Leszek Koltunski
173 061449ed Leszek Koltunski
     DistortedLibrary.onCreate(mView.getContext(),this);
174
     }
175
176
///////////////////////////////////////////////////////////////////////////////////////////////////
177
178
   public void distortedException(Exception ex)
179
     {
180
     android.util.Log.e("Blur", ex.getMessage() );
181 fa9b6494 Leszek Koltunski
     }
182 43bda3db Leszek Koltunski
}