Project

General

Profile

« Previous | Next » 

Revision f1d5ea12

Added by Leszek Koltunski over 7 years ago

Small progress with Blur.

View differences:

src/main/java/org/distorted/library/DistortedEffects.java
305 305
    EffectQueueMatrix.sendZero(df,halfInputW,halfInputH,halfInputW*mesh.zFactor);
306 306
    EffectQueueVertex.sendZero();
307 307
    EffectQueueFragment.sendZero();
308
    EffectQueuePostprocess.sendZero(df.mWidth, df.mHeight, mMVPMatrix);
309 308

  
310 309
    GLES30.glVertexAttribPointer(mProgram.mAttribute[0], POSITION_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mesh.mMeshPositions);
311 310
    GLES30.glVertexAttribPointer(mProgram.mAttribute[1], NORMAL_DATA_SIZE  , GLES30.GL_FLOAT, false, 0, mesh.mMeshNormals);
src/main/java/org/distorted/library/EffectQueuePostprocess.java
49 49
  private static final int NUM_CACHE    = 0;
50 50
  private static final int INDEX = EffectTypes.POSTPROCESS.ordinal();
51 51

  
52
  private static int mNumEffectsH;
53
  private static int mTypeH;
54
  private static int mUniformsH;
52
  private static int mRadiusH;
53
  private static int mOneOverH;
55 54
  private static int mObjDH;
56 55
  private static int mMVPMatrixH;
57 56

  
58 57
  private static final FloatBuffer mQuadPositions, mQuadTexture;
59
  private static DistortedProgram mProgram;
58
  private static DistortedProgram mBlurProgram;
60 59

  
61 60
  static
62 61
    {
......
84 83
  static void createProgram(Resources resources)
85 84
  throws FragmentCompilationException,VertexCompilationException,VertexUniformsException,FragmentUniformsException,LinkingException
86 85
    {
87
    final InputStream postVertexStream   = resources.openRawResource(R.raw.post_vertex_shader);
88
    final InputStream postFragmentStream = resources.openRawResource(R.raw.post_fragment_shader);
86
    final InputStream postVertexStream   = resources.openRawResource(R.raw.blur_vertex_shader);
87
    final InputStream postFragmentStream = resources.openRawResource(R.raw.blur_fragment_shader);
89 88

  
90
    String postFragmentHeader= ("#version 100\n#define NUM_POSTPROCESS "  + DistortedEffects.getMaxPostprocess()+"\n");
89
    mBlurProgram = new DistortedProgram(postVertexStream,postFragmentStream, "", "");
91 90

  
92
    for(EffectNames name: EffectNames.values() )
93
      {
94
      if( name.getType()== EffectTypes.POSTPROCESS)
95
        postFragmentHeader += ("#define "+name.name()+" "+name.ordinal()+"\n");
96
      }
97

  
98
    mProgram = new DistortedProgram(postVertexStream,postFragmentStream, "", postFragmentHeader);
99

  
100
    int postProgramH = mProgram.getProgramHandle();
101
    getUniforms(postProgramH);
102
    }
103

  
104
///////////////////////////////////////////////////////////////////////////////////////////////////
105

  
106
  private static void getUniforms(int mProgramH)
107
    {
108
    mNumEffectsH= GLES30.glGetUniformLocation( mProgramH, "pNumEffects");
109
    mTypeH      = GLES30.glGetUniformLocation( mProgramH, "pType");
110
    mUniformsH  = GLES30.glGetUniformLocation( mProgramH, "pUniforms");
111
    mObjDH      = GLES30.glGetUniformLocation( mProgramH, "u_objD");
112
    mMVPMatrixH = GLES30.glGetUniformLocation(mProgramH, "u_MVPMatrix");
91
    int blurProgramH = mBlurProgram.getProgramHandle();
92
    mRadiusH    = GLES30.glGetUniformLocation( blurProgramH, "u_Radius");
93
    mOneOverH   = GLES30.glGetUniformLocation( blurProgramH, "one_over_objD");
94
    mObjDH      = GLES30.glGetUniformLocation( blurProgramH, "u_objD");
95
    mMVPMatrixH = GLES30.glGetUniformLocation( blurProgramH, "u_MVPMatrix");
113 96
    }
114 97

  
115 98
///////////////////////////////////////////////////////////////////////////////////////////////////
......
156 139
    mUniforms[NUM_UNIFORMS*index+3] = mUniforms[NUM_UNIFORMS*(index+1)+3];
157 140
    }
158 141

  
159
///////////////////////////////////////////////////////////////////////////////////////////////////
160

  
161
  synchronized static void sendZero(float w, float h, float[] mvp)
162
    {
163
    GLES30.glUniform1i( mNumEffectsH, 0);
164
    GLES30.glUniform2f( mObjDH , w, h);
165
    GLES30.glUniformMatrix4fv(mMVPMatrixH, 1, false, mvp , 0);
166
    }
167

  
168 142
///////////////////////////////////////////////////////////////////////////////////////////////////
169 143
// w,h - width and height of hte input texture. MVP - Model-View-Projection matrix to apply to the
170 144
// texture; df - output FBO.
171 145

  
172 146
  synchronized void render(float w, float h, float[] mvp, DistortedFramebuffer df)
173 147
    {
174
    mProgram.useProgram();
148
    mBlurProgram.useProgram();
175 149
    df.setAsOutput();
176 150

  
177
    GLES30.glUniform1i( mNumEffectsH, mNumEffects);
151
    GLES30.glUniform1f( mRadiusH, mUniforms[0]);
152
    GLES30.glUniform2f( mOneOverH, 1/w,1/h);
178 153
    GLES30.glUniform2f( mObjDH , w, h );
179 154
    GLES30.glUniformMatrix4fv(mMVPMatrixH, 1, false, mvp , 0);
180 155

  
181
    if( mNumEffects>0 )
182
      {
183
      GLES30.glUniform1iv( mTypeH    ,                  mNumEffects, mName    ,0);
184
      GLES30.glUniform4fv( mUniformsH, (NUM_UNIFORMS/4)*mNumEffects, mUniforms,0);
185
      }
186

  
187
    GLES30.glVertexAttribPointer(mProgram.mAttribute[0], POSITION_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadPositions);
188
    GLES30.glVertexAttribPointer(mProgram.mAttribute[1], TEX_DATA_SIZE     , GLES30.GL_FLOAT, false, 0, mQuadTexture);
156
    GLES30.glVertexAttribPointer(mBlurProgram.mAttribute[0], POSITION_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadPositions);
157
    GLES30.glVertexAttribPointer(mBlurProgram.mAttribute[1], TEX_DATA_SIZE     , GLES30.GL_FLOAT, false, 0, mQuadTexture);
189 158
    GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
190 159
    }
191 160

  
src/main/res/raw/blur_fragment_shader.glsl
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
precision lowp float;
21

  
22
varying vec2 v_TexCoordinate;
23
uniform sampler2D u_Texture;
24
uniform vec2 one_over_objD;
25
uniform float u_Radius;
26

  
27
//////////////////////////////////////////////////////////////////////////////////////////////
28

  
29
void main()
30
  {
31
  vec2 tex;
32
  vec4 pixel = vec4(0.0);
33
  float denom = 1.0 / ((2.0*u_Radius+1.0)*(2.0*u_Radius+1.0));
34

  
35
  for (float x = -u_Radius; x <= u_Radius; x+=1.0)
36
    for (float y = -u_Radius; y <= u_Radius; y+=1.0)
37
      {
38
      tex = vec2(v_TexCoordinate.x + x*one_over_objD.x, v_TexCoordinate.y + y*one_over_objD.y);
39
      pixel += texture2D(u_Texture,tex)*denom;
40
      }
41

  
42
  gl_FragColor = pixel;
43
  }
src/main/res/raw/blur_vertex_shader.glsl
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
precision lowp float;
21

  
22
uniform vec2 u_objD;             // object width X object height.
23
uniform mat4 u_MVPMatrix;        // the combined model/view/projection matrix.
24

  
25
attribute vec2 a_Position;       // Per-vertex position information we will pass in.
26
attribute vec2 a_TexCoordinate;  // Per-vertex texture coordinate information we will pass in.
27
varying   vec2 v_TexCoordinate;  //
28

  
29
//////////////////////////////////////////////////////////////////////////////////////////////
30
// empty 2D vertex shader for postprocessing
31

  
32
void main()
33
  {
34
  v_TexCoordinate = a_TexCoordinate;
35
  gl_Position     = u_MVPMatrix*vec4( u_objD*a_Position, 0.0, 1.0);
36
  }
src/main/res/raw/post_fragment_shader.glsl
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
precision lowp float;
21

  
22
varying vec2 v_TexCoordinate;
23
uniform sampler2D u_Texture;
24
uniform vec2 u_objD;
25
uniform int pNumEffects;                 // total number of postprocessing effects
26

  
27
#if NUM_POSTPROCESS>0
28
uniform int pType[NUM_POSTPROCESS];      // their types.
29
uniform vec4 pUniforms[NUM_POSTPROCESS]; // i-th effect is 1 vec4: [i].
30

  
31
//////////////////////////////////////////////////////////////////////////////////////////////
32
// BLUR EFFECT
33

  
34
void blur(out vec4 pixel,float radius)
35
  {
36
  pixel = vec4(0.0);
37

  
38
  float blurSizeH = 1.0 / u_objD.x;
39
  float blurSizeV = 1.0 / u_objD.y;
40
  float denom     = 1.0 / ((2.0*radius+1.0)*(2.0*radius+1.0));
41

  
42
  for (float x = -radius; x <= radius; x+=1.0)
43
    for (float y = -radius; y <= radius; y+=1.0)
44
      {
45
      pixel += texture2D( u_Texture, vec2(v_TexCoordinate.x + x*blurSizeH, v_TexCoordinate.y + y*blurSizeV) ) *denom;
46
      }
47
  }
48

  
49
#endif
50

  
51
//////////////////////////////////////////////////////////////////////////////////////////////
52

  
53
void main()
54
  {
55
  vec4 pixel = texture2D(u_Texture,v_TexCoordinate);
56

  
57
#if NUM_POSTPROCESS>0
58
  for(int i=0; i<pNumEffects; i++)
59
    {
60
    if( pType[i]==BLUR ) blur(pixel,pUniforms[i].x);
61
    }
62
#endif
63

  
64
  gl_FragColor = pixel;
65
  }
src/main/res/raw/post_vertex_shader.glsl
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
precision lowp float;
21

  
22
uniform vec2 u_objD;             // object width X object height.
23
                                 // point (0,0) is the center of the object
24
uniform mat4 u_MVPMatrix;        // the combined model/view/projection matrix.
25

  
26
attribute vec2 a_Position;       // Per-vertex position information we will pass in.
27
attribute vec2 a_TexCoordinate;  // Per-vertex texture coordinate information we will pass in.
28
varying   vec2 v_TexCoordinate;  //
29

  
30
//////////////////////////////////////////////////////////////////////////////////////////////
31
// empty 2D vertex shader for postprocessing
32

  
33
void main()
34
  {
35
  v_TexCoordinate = a_TexCoordinate;
36
  gl_Position     = u_MVPMatrix*vec4( u_objD*a_Position, 0.0, 1.0);
37
  }

Also available in: Unified diff