Project

General

Profile

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

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

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 DistortedTexture mTexture;
52
    private DistortedEffects mEffects;
53
    private DistortedEffectsPostprocess mPostEffects;
54
    private DistortedScreen mScreen;
55
    private MeshFlat mMesh;
56
    private Static1D mRadiusSta;
57
    private int bmpHeight, bmpWidth;
58

    
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60

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

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

    
73
      mPostEffects.blur(radiusDyn);
74
      }
75

    
76
///////////////////////////////////////////////////////////////////////////////////////////////////
77

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

    
85
///////////////////////////////////////////////////////////////////////////////////////////////////
86
   
87
   public void onDrawFrame(GL10 glUnused) 
88
      {
89
      mScreen.render( System.currentTimeMillis() );
90
      }
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
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.dog);
123
      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
      if( mTexture==null ) mTexture = new DistortedTexture(bmpWidth,bmpHeight);
142
      mTexture.setTexture(bitmap);
143

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

    
149
      DistortedEffects.enableEffect(EffectNames.BLUR);
150

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