Project

General

Profile

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

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

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