Project

General

Profile

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

examples / src / main / java / org / distorted / examples / blur / BlurRenderer.java @ 678c391d

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.MatrixEffectScale;
28
import org.distorted.library.effect.PostprocessEffectBlur;
29
import org.distorted.library.main.DistortedLibrary;
30
import org.distorted.library.main.DistortedEffects;
31
import org.distorted.library.main.DistortedFramebuffer;
32
import org.distorted.library.main.DistortedNode;
33
import org.distorted.library.main.DistortedScreen;
34
import org.distorted.library.main.DistortedTexture;
35
import org.distorted.library.mesh.MeshRectangles;
36
import org.distorted.library.type.Dynamic2D;
37
import org.distorted.library.type.Static2D;
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 final static int SIZE = 500;
51

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

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

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

    
72
      mHaloRadiusSta = new Static2D(10,5);
73
      Dynamic2D haloAndRadiusDyn = new Dynamic2D();
74
      haloAndRadiusDyn.add(mHaloRadiusSta);
75

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

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

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

    
87
///////////////////////////////////////////////////////////////////////////////////////////////////
88

    
89
   int setBlur(int blur)
90
     {
91
     int radius = blur/2;
92
     mHaloRadiusSta.set1(radius);
93
     return radius;
94
     }
95

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

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

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

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

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

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

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

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

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

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

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

    
158
     PostprocessEffectBlur.enable();
159

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