Project

General

Profile

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

examples / src / main / java / org / distorted / examples / blur / BlurRenderer.java @ 061449ed

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 fa9b6494 Leszek Koltunski
import org.distorted.library.effect.MatrixEffectScale;
28
import org.distorted.library.effect.PostprocessEffectBlur;
29 e3900503 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
30 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedEffects;
31 da57b7da Leszek Koltunski
import org.distorted.library.main.DistortedFramebuffer;
32 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedNode;
33
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
52 43bda3db Leszek Koltunski
    private GLSurfaceView mView;
53 59540ba2 Leszek Koltunski
    private DistortedTexture mTexture;
54 da57b7da Leszek Koltunski
    private DistortedEffects mEffects, mBufferEffects;
55 d218d64e leszek
    private DistortedScreen mScreen;
56 da57b7da Leszek Koltunski
    private DistortedFramebuffer mBuffer;
57 698ad0a8 Leszek Koltunski
    private MeshRectangles mMesh, mMeshBuffer;
58 678c391d Leszek Koltunski
    private Static2D mHaloRadiusSta;
59 16b22aab Leszek Koltunski
    private Static3D mScale, mBufferScale;
60 43bda3db Leszek Koltunski
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62
63
   BlurRenderer(GLSurfaceView v)
64
      {
65 698ad0a8 Leszek Koltunski
      mView       = v;
66
      mMesh       = new MeshRectangles(1,1);
67
      mMeshBuffer = new MeshRectangles(1,1);
68
      mScreen     = new DistortedScreen();
69
      mBuffer     = new DistortedFramebuffer(SIZE,SIZE,1, DistortedFramebuffer.NO_DEPTH_NO_STENCIL);
70 43bda3db Leszek Koltunski
71 678c391d Leszek Koltunski
      mHaloRadiusSta = new Static2D(10,5);
72
      Dynamic2D haloAndRadiusDyn = new Dynamic2D();
73
      haloAndRadiusDyn.add(mHaloRadiusSta);
74 43bda3db Leszek Koltunski
75 fa9b6494 Leszek Koltunski
      mScale= new Static3D(1,1,1);
76 da57b7da Leszek Koltunski
      mBufferScale= new Static3D(1,1,1);
77
78 698ad0a8 Leszek Koltunski
      mBufferEffects = new DistortedEffects();
79 da57b7da Leszek Koltunski
      mBufferEffects.apply(new MatrixEffectScale(mBufferScale));
80 fa9b6494 Leszek Koltunski
81 698ad0a8 Leszek Koltunski
      mEffects = new DistortedEffects();
82 678c391d Leszek Koltunski
      mEffects.apply( new PostprocessEffectBlur(haloAndRadiusDyn) );
83 fa9b6494 Leszek Koltunski
      mEffects.apply(new MatrixEffectScale(mScale));
84 43bda3db Leszek Koltunski
      }
85
86
///////////////////////////////////////////////////////////////////////////////////////////////////
87
88 a6da86cc Leszek Koltunski
   int setBlur(int blur)
89
     {
90 3365725b Leszek Koltunski
     int radius = blur/2;
91 678c391d Leszek Koltunski
     mHaloRadiusSta.set1(radius);
92 a6da86cc Leszek Koltunski
     return radius;
93 43bda3db Leszek Koltunski
     }
94
95
///////////////////////////////////////////////////////////////////////////////////////////////////
96
   
97
   public void onDrawFrame(GL10 glUnused) 
98
      {
99 da57b7da Leszek Koltunski
      long time = System.currentTimeMillis();
100
101
      mBuffer.render(time);
102
      mScreen.render(time);
103 43bda3db Leszek Koltunski
      }
104
105
///////////////////////////////////////////////////////////////////////////////////////////////////
106
    
107 fa9b6494 Leszek Koltunski
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
108
     {
109 278a214c Leszek Koltunski
     float factor1 = 0.8f* (Math.min(width, height));
110 da57b7da Leszek Koltunski
111
     mBufferScale.set( factor1,factor1,factor1 );
112
113 278a214c Leszek Koltunski
     float factor2 = 0.9f*SIZE;
114 da57b7da Leszek Koltunski
115
     mScale.set( factor2,factor2,factor2 );
116
117 463f1f9c leszek
     mScreen.resize(width, height);
118 fa9b6494 Leszek Koltunski
     }
119 43bda3db Leszek Koltunski
120
///////////////////////////////////////////////////////////////////////////////////////////////////
121
    
122 fa9b6494 Leszek Koltunski
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
123
     {
124
     InputStream is = mView.getContext().getResources().openRawResource(R.raw.dog);
125
     Bitmap bitmap;
126 43bda3db Leszek Koltunski
        
127 fa9b6494 Leszek Koltunski
     try
128
       {
129
       bitmap = BitmapFactory.decodeStream(is);
130
       }
131
     finally
132
       {
133
       try
134
         {
135
         is.close();
136
         }
137
       catch(IOException e) { }
138
       }
139 687263cc Leszek Koltunski
140
     if( mTexture==null ) mTexture = new DistortedTexture();
141 fa9b6494 Leszek Koltunski
     mTexture.setTexture(bitmap);
142
143
     mScreen.detachAll();
144 698ad0a8 Leszek Koltunski
     mScreen.attach(mBuffer, mBufferEffects, mMeshBuffer);
145 da57b7da Leszek Koltunski
     mBuffer.attach(new DistortedNode(mTexture,mEffects,mMesh));
146 fa9b6494 Leszek Koltunski
147 885b9cca leszek
     PostprocessEffectBlur.enable();
148 fa9b6494 Leszek Koltunski
149 061449ed Leszek Koltunski
     DistortedLibrary.onCreate(mView.getContext(),this);
150
     }
151
152
///////////////////////////////////////////////////////////////////////////////////////////////////
153
154
   public void distortedException(Exception ex)
155
     {
156
     android.util.Log.e("Blur", ex.getMessage() );
157 fa9b6494 Leszek Koltunski
     }
158 43bda3db Leszek Koltunski
}