Project

General

Profile

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

examples / src / main / java / org / distorted / examples / blur / BlurRenderer.java @ 59540ba2

1 43bda3db 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.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
import org.distorted.library.Distorted;
28
import org.distorted.library.DistortedEffects;
29 895d2f4f leszek
import org.distorted.library.DistortedEffectsPostprocess;
30
import org.distorted.library.DistortedNode;
31 d218d64e leszek
import org.distorted.library.DistortedScreen;
32 43bda3db Leszek Koltunski
import org.distorted.library.DistortedTexture;
33 6637d0f2 Leszek Koltunski
import org.distorted.library.EffectNames;
34 43bda3db Leszek Koltunski
import org.distorted.library.EffectTypes;
35
import org.distorted.library.MeshFlat;
36
import org.distorted.library.type.Dynamic1D;
37
import org.distorted.library.type.Static1D;
38
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
class BlurRenderer implements GLSurfaceView.Renderer
49
{
50
    private GLSurfaceView mView;
51 59540ba2 Leszek Koltunski
    private DistortedTexture mTexture;
52 43bda3db Leszek Koltunski
    private DistortedEffects mEffects;
53 895d2f4f leszek
    private DistortedEffectsPostprocess mPostEffects;
54 d218d64e leszek
    private DistortedScreen mScreen;
55 43bda3db Leszek Koltunski
    private MeshFlat mMesh;
56
    private Static1D mRadiusSta;
57
    private int bmpHeight, bmpWidth;
58
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60
61
   BlurRenderer(GLSurfaceView v)
62
      {
63 895d2f4f leszek
      mView        = v;
64
      mMesh        = new MeshFlat(1,1);
65
      mEffects     = new DistortedEffects();
66
      mPostEffects = new DistortedEffectsPostprocess();
67
      mScreen      = new DistortedScreen();
68 43bda3db Leszek Koltunski
69
      mRadiusSta = new Static1D(5);
70
      Dynamic1D radiusDyn = new Dynamic1D();
71
      radiusDyn.add(mRadiusSta);
72
73 895d2f4f leszek
      mPostEffects.blur(radiusDyn);
74 43bda3db Leszek Koltunski
      }
75
76
///////////////////////////////////////////////////////////////////////////////////////////////////
77
78 a6da86cc Leszek Koltunski
   int setBlur(int blur)
79
     {
80 3365725b Leszek Koltunski
     int radius = blur/2;
81 a6da86cc Leszek Koltunski
     mRadiusSta.set(radius);
82
     return radius;
83 43bda3db Leszek Koltunski
     }
84
85
///////////////////////////////////////////////////////////////////////////////////////////////////
86
   
87
   public void onDrawFrame(GL10 glUnused) 
88
      {
89 fe59d375 Leszek Koltunski
      mScreen.render( System.currentTimeMillis() );
90 43bda3db Leszek Koltunski
      }
91
92
///////////////////////////////////////////////////////////////////////////////////////////////////
93
    
94
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
95
      { 
96
      mEffects.abortEffects(EffectTypes.MATRIX);
97
      
98
      if( (float)bmpHeight/bmpWidth > (float)height/width )
99
        {
100
        int w = (height*bmpWidth)/bmpHeight;
101
        float factor = (float)height/bmpHeight;
102
103
        mEffects.move( new Static3D((width-w)/2,0,0) );
104
        mEffects.scale(factor);
105
        }
106
      else
107
        {
108
        int h = (width*bmpHeight)/bmpWidth;
109
        float factor = (float)width/bmpWidth;
110
111
        mEffects.move( new Static3D(0,(height-h)/2,0) );
112
        mEffects.scale(factor);
113
        }
114
      
115
      mScreen.resize(width, height);
116
      }
117
118
///////////////////////////////////////////////////////////////////////////////////////////////////
119
    
120
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
121
      {
122 a6da86cc Leszek Koltunski
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.dog);
123 43bda3db Leszek Koltunski
      Bitmap bitmap;
124
        
125
      try 
126
        {
127
        bitmap = BitmapFactory.decodeStream(is);
128
        } 
129
      finally 
130
        {
131
        try 
132
          {
133
          is.close();
134
          } 
135
        catch(IOException e) { }
136
        }  
137
      
138
      bmpHeight = bitmap.getHeight();
139
      bmpWidth  = bitmap.getWidth();
140
141 59540ba2 Leszek Koltunski
      if( mTexture==null ) mTexture = new DistortedTexture(bmpWidth,bmpHeight);
142
      mTexture.setTexture(bitmap);
143 fe59d375 Leszek Koltunski
144
      mScreen.detachAll();
145 59540ba2 Leszek Koltunski
      DistortedNode node = new DistortedNode(mTexture,mEffects,mMesh);
146 895d2f4f leszek
      node.setPostprocessEffects(mPostEffects);
147
      mScreen.attach(node);
148 43bda3db Leszek Koltunski
149 6637d0f2 Leszek Koltunski
      DistortedEffects.enableEffect(EffectNames.BLUR);
150
151 43bda3db Leszek Koltunski
      try
152
        {
153
        Distorted.onCreate(mView.getContext());
154
        }
155
      catch(Exception ex)
156
        {
157 fe59d375 Leszek Koltunski
        android.util.Log.e("Blur", ex.getMessage() );
158 43bda3db Leszek Koltunski
        }
159
      }
160
}