Project

General

Profile

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

examples / src / main / java / org / distorted / examples / blur / BlurRenderer.java @ 51554e47

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
     float qx = (float)width /bmpWidth;
97
     float qy = (float)height/bmpHeight;
98

    
99
     mEffects.abortEffects(EffectTypes.MATRIX);
100
     mEffects.scale(  qx<qy ? (new Static3D(1,qx/qy,1)) : (new Static3D(qy/qx,1,1)) );
101
      
102
     mScreen.resize(width, height);
103
     }
104

    
105
///////////////////////////////////////////////////////////////////////////////////////////////////
106
    
107
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
108
     {
109
     InputStream is = mView.getContext().getResources().openRawResource(R.raw.dog);
110
     Bitmap bitmap;
111
        
112
     try
113
       {
114
       bitmap = BitmapFactory.decodeStream(is);
115
       }
116
     finally
117
       {
118
       try
119
         {
120
         is.close();
121
         }
122
       catch(IOException e) { }
123
       }
124
      
125
     bmpHeight = bitmap.getHeight();
126
     bmpWidth  = bitmap.getWidth();
127

    
128
     if( mTexture==null ) mTexture = new DistortedTexture(bmpWidth,bmpHeight);
129
     mTexture.setTexture(bitmap);
130

    
131
     mScreen.detachAll();
132
     DistortedNode node = new DistortedNode(mTexture,mEffects,mMesh);
133
     node.setPostprocessEffects(mPostEffects);
134
     mScreen.attach(node);
135

    
136
     DistortedEffects.enableEffect(EffectNames.BLUR);
137

    
138
     try
139
       {
140
       Distorted.onCreate(mView.getContext());
141
       }
142
     catch(Exception ex)
143
       {
144
       android.util.Log.e("Blur", ex.getMessage() );
145
       }
146
     }
147
}
(2-2/3)