Project

General

Profile

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

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

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.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
import org.distorted.library.DistortedEffectsPostprocess;
30
import org.distorted.library.DistortedNode;
31
import org.distorted.library.DistortedScreen;
32
import org.distorted.library.DistortedTexture;
33
import org.distorted.library.EffectNames;
34
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
    private DistortedEffects mEffects;
52
    private DistortedEffectsPostprocess mPostEffects;
53
    private DistortedScreen mScreen;
54
    private MeshFlat mMesh;
55
    private Static1D mRadiusSta;
56
    private int bmpHeight, bmpWidth;
57

    
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59

    
60
   BlurRenderer(GLSurfaceView v)
61
      {
62
      mView        = v;
63
      mMesh        = new MeshFlat(1,1);
64
      mEffects     = new DistortedEffects();
65
      mPostEffects = new DistortedEffectsPostprocess();
66
      mScreen      = new DistortedScreen();
67

    
68
      mRadiusSta = new Static1D(5);
69
      Dynamic1D radiusDyn = new Dynamic1D();
70
      radiusDyn.add(mRadiusSta);
71

    
72
      mPostEffects.blur(radiusDyn);
73
      }
74

    
75
///////////////////////////////////////////////////////////////////////////////////////////////////
76

    
77
   int setBlur(int blur)
78
     {
79
     int radius = blur/2;
80
     mRadiusSta.set(radius);
81
     return radius;
82
     }
83

    
84
///////////////////////////////////////////////////////////////////////////////////////////////////
85
   
86
   public void onDrawFrame(GL10 glUnused) 
87
      {
88
      mScreen.render( System.currentTimeMillis() );
89
      }
90

    
91
///////////////////////////////////////////////////////////////////////////////////////////////////
92
    
93
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
94
      { 
95
      mEffects.abortEffects(EffectTypes.MATRIX);
96
      
97
      if( (float)bmpHeight/bmpWidth > (float)height/width )
98
        {
99
        int w = (height*bmpWidth)/bmpHeight;
100
        float factor = (float)height/bmpHeight;
101

    
102
        mEffects.move( new Static3D((width-w)/2,0,0) );
103
        mEffects.scale(factor);
104
        }
105
      else
106
        {
107
        int h = (width*bmpHeight)/bmpWidth;
108
        float factor = (float)width/bmpWidth;
109

    
110
        mEffects.move( new Static3D(0,(height-h)/2,0) );
111
        mEffects.scale(factor);
112
        }
113
      
114
      mScreen.resize(width, height);
115
      }
116

    
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118
    
119
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
120
      {
121
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.dog);
122
      Bitmap bitmap;
123
        
124
      try 
125
        {
126
        bitmap = BitmapFactory.decodeStream(is);
127
        } 
128
      finally 
129
        {
130
        try 
131
          {
132
          is.close();
133
          } 
134
        catch(IOException e) { }
135
        }  
136
      
137
      bmpHeight = bitmap.getHeight();
138
      bmpWidth  = bitmap.getWidth();
139

    
140
      DistortedTexture texture = new DistortedTexture(bmpWidth,bmpHeight);
141
      texture.setTexture(bitmap);
142

    
143
      mScreen.detachAll();
144
      DistortedNode node = new DistortedNode(texture,mEffects,mMesh);
145
      node.setPostprocessEffects(mPostEffects);
146
      mScreen.attach(node);
147

    
148
      DistortedEffects.enableEffect(EffectNames.BLUR);
149

    
150
      try
151
        {
152
        Distorted.onCreate(mView.getContext());
153
        }
154
      catch(Exception ex)
155
        {
156
        android.util.Log.e("Blur", ex.getMessage() );
157
        }
158
      }
159
}
(2-2/3)