Project

General

Profile

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

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

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.DistortedEffectsPostprocess;
34
import org.distorted.library.main.DistortedNode;
35
import org.distorted.library.main.DistortedScreen;
36
import org.distorted.library.main.DistortedTexture;
37
import org.distorted.library.main.MeshFlat;
38
import org.distorted.library.type.Dynamic1D;
39
import org.distorted.library.type.Static1D;
40
import org.distorted.library.type.Static3D;
41

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

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

    
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49

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

    
62
///////////////////////////////////////////////////////////////////////////////////////////////////
63

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

    
72
      mRadiusSta = new Static1D(5);
73
      Dynamic1D radiusDyn = new Dynamic1D();
74
      radiusDyn.add(mRadiusSta);
75

    
76
      mMove = new Static3D(0,0,0);
77
      mScale= new Static3D(1,1,1);
78

    
79
      mPostEffects.apply( new PostprocessEffectBlur(radiusDyn) );
80
      mEffects.apply(new MatrixEffectMove(mMove));
81
      mEffects.apply(new MatrixEffectScale(mScale));
82
      }
83

    
84
///////////////////////////////////////////////////////////////////////////////////////////////////
85

    
86
   int setBlur(int blur)
87
     {
88
     int radius = blur/2;
89
     mRadiusSta.set(radius);
90
     return radius;
91
     }
92

    
93
///////////////////////////////////////////////////////////////////////////////////////////////////
94
   
95
   public void onDrawFrame(GL10 glUnused) 
96
      {
97
      mScreen.render( System.currentTimeMillis() );
98
      }
99

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

    
119
     mScreen.resize(width,height);
120
     }
121

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

    
145
     if( mTexture==null ) mTexture = new DistortedTexture(mObjWidth,mObjHeight);
146
     mTexture.setTexture(bitmap);
147

    
148
     mScreen.detachAll();
149
     DistortedNode node = new DistortedNode(mTexture,mEffects,mMesh);
150
     node.setPostprocessEffects(mPostEffects);
151
     mScreen.attach(node);
152

    
153
     DistortedEffects.enableEffect(EffectName.BLUR);
154

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