Project

General

Profile

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

examples / src / main / java / org / distorted / examples / blur / BlurRenderer.java @ 77e9d967

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.MatrixEffectMove;
28
import org.distorted.library.effect.MatrixEffectScale;
29
import org.distorted.library.effect.PostprocessEffectBlur;
30
import org.distorted.library.main.DistortedLibrary;
31
import org.distorted.library.main.DistortedEffects;
32
import org.distorted.library.main.DistortedFramebuffer;
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.mesh.MeshRectangles;
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 final static int SIZE = 500;
52

    
53
    private GLSurfaceView mView;
54
    private DistortedTexture mTexture;
55
    private DistortedEffects mEffects, mBufferEffects;
56
    private DistortedScreen mScreen;
57
    private DistortedFramebuffer mBuffer;
58
    private MeshRectangles mMesh, mMeshBuffer;
59
    private Static1D mRadiusSta;
60
    private int mObjHeight, mObjWidth;
61
    private Static3D mScale, mBufferScale;
62

    
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64

    
65
   BlurRenderer(GLSurfaceView v)
66
      {
67
      mView       = v;
68
      mMesh       = new MeshRectangles(1,1);
69
      mMeshBuffer = new MeshRectangles(1,1);
70
      mScreen     = new DistortedScreen();
71
      mBuffer     = new DistortedFramebuffer(SIZE,SIZE,1, DistortedFramebuffer.NO_DEPTH_NO_STENCIL);
72

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

    
77
      mScale= new Static3D(1,1,1);
78
      mBufferScale= new Static3D(1,1,1);
79

    
80
      mBufferEffects = new DistortedEffects();
81
      mBufferEffects.apply(new MatrixEffectScale(mBufferScale));
82

    
83
      mEffects = new DistortedEffects();
84
      mEffects.apply( new PostprocessEffectBlur(radiusDyn) );
85
      mEffects.apply(new MatrixEffectScale(mScale));
86
      }
87

    
88
///////////////////////////////////////////////////////////////////////////////////////////////////
89

    
90
   int setBlur(int blur)
91
     {
92
     int radius = blur/2;
93
     mRadiusSta.set(radius);
94
     return radius;
95
     }
96

    
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98
   
99
   public void onDrawFrame(GL10 glUnused) 
100
      {
101
      long time = System.currentTimeMillis();
102

    
103
      mBuffer.render(time);
104
      mScreen.render(time);
105
      }
106

    
107
///////////////////////////////////////////////////////////////////////////////////////////////////
108
    
109
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
110
     {
111
     float qw1 = (float)width /SIZE;
112
     float qh1 = (float)height/SIZE;
113
     float factor1 = 0.8f* (qw1<qh1 ? qw1:qh1);
114

    
115
     mBufferScale.set( factor1,factor1,factor1 );
116

    
117
     float qw2 = (float)SIZE/mObjWidth;
118
     float qh2 = (float)SIZE/mObjHeight;
119
     float factor2 = 0.9f* (qw2<qh2 ? qw2:qh2);
120

    
121
     mScale.set( factor2,factor2,factor2 );
122

    
123
     mScreen.resize(width, height);
124
     }
125

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

    
149
     mMesh.setStretch(mObjWidth,mObjHeight,0);
150
     mMeshBuffer.setStretch(SIZE,SIZE,0);
151

    
152
     if( mTexture==null ) mTexture = new DistortedTexture();
153
     mTexture.setTexture(bitmap);
154

    
155
     mScreen.detachAll();
156
     mScreen.attach(mBuffer, mBufferEffects, mMeshBuffer);
157
     mBuffer.attach(new DistortedNode(mTexture,mEffects,mMesh));
158

    
159
     PostprocessEffectBlur.enable();
160

    
161
     try
162
       {
163
       DistortedLibrary.onCreate(mView.getContext());
164
       }
165
     catch(Exception ex)
166
       {
167
       android.util.Log.e("Blur", ex.getMessage() );
168
       }
169
     }
170
}
(2-2/3)