Project

General

Profile

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

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

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.DistortedScreen;
32
import org.distorted.library.DistortedTexture;
33
import org.distorted.library.EffectTypes;
34
import org.distorted.library.MeshFlat;
35
import org.distorted.library.type.Dynamic1D;
36
import org.distorted.library.type.Static1D;
37
import org.distorted.library.type.Static3D;
38

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

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

    
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46

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

    
57
///////////////////////////////////////////////////////////////////////////////////////////////////
58

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

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

    
70
      mEffects.blur(radiusDyn);
71
      }
72

    
73
///////////////////////////////////////////////////////////////////////////////////////////////////
74

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

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

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

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

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

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

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

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

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