Project

General

Profile

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

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

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
     float qw = (float)width /mObjWidth;
102
     float qh = (float)height/mObjHeight;
103
     float factor = 0.8f* (qw<qh ? qw:qh);
104
     int w = (int)(factor*mObjWidth);
105
     int h = (int)(factor*mObjHeight);
106

    
107
     mMove.set((width-w)/2 ,(height-h)/2, 0);
108
     mScale.set( factor,factor,factor );
109
     mScreen.resize(width, height);
110
     }
111

    
112
///////////////////////////////////////////////////////////////////////////////////////////////////
113
    
114
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
115
     {
116
     InputStream is = mView.getContext().getResources().openRawResource(R.raw.dog);
117
     Bitmap bitmap;
118
        
119
     try
120
       {
121
       bitmap = BitmapFactory.decodeStream(is);
122
       }
123
     finally
124
       {
125
       try
126
         {
127
         is.close();
128
         }
129
       catch(IOException e) { }
130
       }
131
      
132
     mObjHeight = bitmap.getHeight();
133
     mObjWidth  = bitmap.getWidth();
134

    
135
     if( mTexture==null ) mTexture = new DistortedTexture(mObjWidth,mObjHeight);
136
     mTexture.setTexture(bitmap);
137

    
138
     mScreen.detachAll();
139
     mScreen.attach(new DistortedNode(mTexture,mEffects,mMesh));
140

    
141
     PostprocessEffectBlur.enable();
142

    
143
     try
144
       {
145
       Distorted.onCreate(mView.getContext());
146
       }
147
     catch(Exception ex)
148
       {
149
       android.util.Log.e("Blur", ex.getMessage() );
150
       }
151
     }
152
}
(2-2/3)