Project

General

Profile

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

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

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.effect.EffectName;
28
import org.distorted.library.effect.MatrixEffectMove;
29
import org.distorted.library.effect.MatrixEffectScale;
30
import org.distorted.library.effect.PostprocessEffectBlur;
31
import org.distorted.library.main.Distorted;
32
import org.distorted.library.main.DistortedEffects;
33
import org.distorted.library.main.DistortedNode;
34
import org.distorted.library.main.DistortedScreen;
35
import org.distorted.library.main.DistortedTexture;
36
import org.distorted.library.main.MeshFlat;
37
import org.distorted.library.type.Dynamic1D;
38
import org.distorted.library.type.Static1D;
39
import org.distorted.library.type.Static3D;
40

    
41
import java.io.IOException;
42
import java.io.InputStream;
43

    
44
import javax.microedition.khronos.egl.EGLConfig;
45
import javax.microedition.khronos.opengles.GL10;
46

    
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48

    
49
class BlurRenderer implements GLSurfaceView.Renderer
50
{
51
    private GLSurfaceView mView;
52
    private DistortedTexture mTexture;
53
    private DistortedEffects mEffects;
54
    private DistortedScreen mScreen;
55
    private MeshFlat mMesh;
56
    private Static1D mRadiusSta;
57
    private int mObjHeight, mObjWidth;
58
    private Static3D mMove, mScale;
59

    
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61

    
62
   BlurRenderer(GLSurfaceView v)
63
      {
64
      mView   = v;
65
      mMesh   = new MeshFlat(1,1);
66
      mScreen = new DistortedScreen();
67

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

    
72
      mMove = new Static3D(0,0,0);
73
      mScale= new Static3D(1,1,1);
74

    
75
      mEffects = new DistortedEffects();
76
      mEffects.apply( new PostprocessEffectBlur(radiusDyn) );
77
      mEffects.apply(new MatrixEffectMove(mMove));
78
      mEffects.apply(new MatrixEffectScale(mScale));
79
      }
80

    
81
///////////////////////////////////////////////////////////////////////////////////////////////////
82

    
83
   int setBlur(int blur)
84
     {
85
     int radius = blur/2;
86
     mRadiusSta.set(radius);
87
     return radius;
88
     }
89

    
90
///////////////////////////////////////////////////////////////////////////////////////////////////
91
   
92
   public void onDrawFrame(GL10 glUnused) 
93
      {
94
      mScreen.render( System.currentTimeMillis() );
95
      }
96

    
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98
    
99
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
100
     {
101
     if( (float)mObjHeight/mObjWidth > (float)height/width )
102
       {
103
       int w = (height*mObjWidth)/mObjHeight;
104
       float factor = (float)height/mObjHeight;
105
       mMove.set((width-w)/2,0,0);
106
       mScale.set(factor,factor,factor);
107
       }
108
     else
109
       {
110
       int h = (width*mObjHeight)/mObjWidth;
111
       float factor = (float)width/mObjWidth;
112
       mMove.set(0,(height-h)/2,0);
113
       mScale.set(factor,factor,factor);
114
       }
115

    
116
     mScreen.resize(width,height);
117
     }
118

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

    
142
     if( mTexture==null ) mTexture = new DistortedTexture(mObjWidth,mObjHeight);
143
     mTexture.setTexture(bitmap);
144

    
145
     mScreen.detachAll();
146
     mScreen.attach(new DistortedNode(mTexture,mEffects,mMesh));
147

    
148
     DistortedEffects.enableEffect(EffectName.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)