Project

General

Profile

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

examples / src / main / java / org / distorted / examples / blur / BlurRenderer.java @ 41a81a14

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.GLES30;
25
import android.opengl.GLSurfaceView;
26

    
27
import org.distorted.examples.R;
28
import org.distorted.library.Distorted;
29
import org.distorted.library.DistortedEffects;
30
import org.distorted.library.DistortedFramebuffer;
31
import org.distorted.library.DistortedTexture;
32
import org.distorted.library.EffectTypes;
33
import org.distorted.library.MeshFlat;
34
import org.distorted.library.type.Dynamic1D;
35
import org.distorted.library.type.Static1D;
36
import org.distorted.library.type.Static3D;
37

    
38
import java.io.IOException;
39
import java.io.InputStream;
40

    
41
import javax.microedition.khronos.egl.EGLConfig;
42
import javax.microedition.khronos.opengles.GL10;
43

    
44
///////////////////////////////////////////////////////////////////////////////////////////////////
45

    
46
class BlurRenderer implements GLSurfaceView.Renderer
47
{
48
    private GLSurfaceView mView;
49
    private DistortedTexture mTexture;
50
    private DistortedEffects mEffects;
51
    private DistortedFramebuffer mScreen;
52
    private MeshFlat mMesh;
53
    private Static1D mRadiusSta;
54
    private int bmpHeight, bmpWidth;
55

    
56
///////////////////////////////////////////////////////////////////////////////////////////////////
57

    
58
   BlurRenderer(GLSurfaceView v)
59
      {
60
      mView    = v;
61
      mMesh    = new MeshFlat(1,1);
62
      mEffects = new DistortedEffects();
63
      mScreen  = new DistortedFramebuffer(0);
64

    
65
      mRadiusSta = new Static1D(5);
66
      Dynamic1D radiusDyn = new Dynamic1D();
67
      radiusDyn.add(mRadiusSta);
68

    
69
      mEffects.blur(radiusDyn);
70
      }
71

    
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73

    
74
   int setBlur(int blur)
75
     {
76
     int radius = blur/10;
77
     mRadiusSta.set(radius);
78
     return radius;
79
     }
80

    
81
///////////////////////////////////////////////////////////////////////////////////////////////////
82
   
83
   public void onDrawFrame(GL10 glUnused) 
84
      {
85
      GLES30.glClear( GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_COLOR_BUFFER_BIT);
86
      mScreen.renderTo(mTexture, mMesh, mEffects, System.currentTimeMillis() );
87
      }
88

    
89
///////////////////////////////////////////////////////////////////////////////////////////////////
90
    
91
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
92
      { 
93
      mEffects.abortEffects(EffectTypes.MATRIX);
94
      
95
      if( (float)bmpHeight/bmpWidth > (float)height/width )
96
        {
97
        int w = (height*bmpWidth)/bmpHeight;
98
        float factor = (float)height/bmpHeight;
99

    
100
        mEffects.move( new Static3D((width-w)/2,0,0) );
101
        mEffects.scale(factor);
102
        }
103
      else
104
        {
105
        int h = (width*bmpHeight)/bmpWidth;
106
        float factor = (float)width/bmpWidth;
107

    
108
        mEffects.move( new Static3D(0,(height-h)/2,0) );
109
        mEffects.scale(factor);
110
        }
111
      
112
      mScreen.resize(width, height);
113
      }
114

    
115
///////////////////////////////////////////////////////////////////////////////////////////////////
116
    
117
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
118
      {
119
      GLES30.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
120

    
121
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.dog);
122
      Bitmap bitmap;
123
        
124
      try 
125
        {
126
        bitmap = BitmapFactory.decodeStream(is);
127
        } 
128
      finally 
129
        {
130
        try 
131
          {
132
          is.close();
133
          } 
134
        catch(IOException e) { }
135
        }  
136
      
137
      bmpHeight = bitmap.getHeight();
138
      bmpWidth  = bitmap.getWidth();
139

    
140
      mTexture = new DistortedTexture(bmpWidth,bmpHeight);
141
      mTexture.setTexture(bitmap);
142

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