Project

General

Profile

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

library / src / main / java / org / distorted / library / EffectQueuePostprocess.java @ 5ccafcca

1 4c1dd6e9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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.library;
21
22 c90b9e01 Leszek Koltunski
import android.content.res.Resources;
23 194ab46f Leszek Koltunski
import android.opengl.GLES30;
24 1c67457f Leszek Koltunski
import android.opengl.Matrix;
25 4c1dd6e9 Leszek Koltunski
26
import org.distorted.library.message.EffectMessage;
27 8fa96e69 Leszek Koltunski
import org.distorted.library.program.DistortedProgram;
28 c90b9e01 Leszek Koltunski
import org.distorted.library.program.FragmentCompilationException;
29
import org.distorted.library.program.FragmentUniformsException;
30
import org.distorted.library.program.LinkingException;
31
import org.distorted.library.program.VertexCompilationException;
32
import org.distorted.library.program.VertexUniformsException;
33 4c1dd6e9 Leszek Koltunski
import org.distorted.library.type.Data1D;
34
import org.distorted.library.type.Dynamic1D;
35
import org.distorted.library.type.Static1D;
36
37 c90b9e01 Leszek Koltunski
import java.io.InputStream;
38 8fa96e69 Leszek Koltunski
import java.nio.ByteBuffer;
39
import java.nio.ByteOrder;
40
import java.nio.FloatBuffer;
41
42 4c1dd6e9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
43
44
class EffectQueuePostprocess extends EffectQueue
45
  {
46 0d81d0fb Leszek Koltunski
  private static final int MAX_BLUR = 50;
47
48 8fa96e69 Leszek Koltunski
  private static final int POSITION_DATA_SIZE= 2; // Post Program: size of the position data in elements
49
  private static final int TEX_DATA_SIZE     = 2; // Post Program: size of the texture coordinate data in elements.
50
51 667a1ad9 Leszek Koltunski
  private static final int NUM_UNIFORMS = 4;
52 4c1dd6e9 Leszek Koltunski
  private static final int NUM_CACHE    = 0;
53
  private static final int INDEX = EffectTypes.POSTPROCESS.ordinal();
54
55 1c67457f Leszek Koltunski
  private static final FloatBuffer mQuadPositions, mQuadTextureNor, mQuadTextureInv;
56 8fa96e69 Leszek Koltunski
57
  static
58
    {
59 667a1ad9 Leszek Koltunski
    int dataLength      = 4;
60
    int bytes_per_float = 4;
61 8fa96e69 Leszek Koltunski
62 1c67457f Leszek Koltunski
    float[] position  = { -0.5f, -0.5f,  -0.5f, 0.5f,  0.5f,-0.5f,  0.5f, 0.5f };
63
    float[] textureNor= {  0.0f,  0.0f,   0.0f, 1.0f,  1.0f, 0.0f,  1.0f, 1.0f };
64
    float[] textureInv= {  0.0f,  0.0f,   1.0f, 0.0f,  0.0f, 1.0f,  1.0f, 1.0f };
65 8fa96e69 Leszek Koltunski
66 667a1ad9 Leszek Koltunski
    mQuadPositions = ByteBuffer.allocateDirect(POSITION_DATA_SIZE*dataLength*bytes_per_float).order(ByteOrder.nativeOrder()).asFloatBuffer();
67 1c67457f Leszek Koltunski
    mQuadPositions.put(position).position(0);
68
    mQuadTextureNor  = ByteBuffer.allocateDirect(TEX_DATA_SIZE     *dataLength*bytes_per_float).order(ByteOrder.nativeOrder()).asFloatBuffer();
69
    mQuadTextureNor.put(textureNor).position(0);
70
    mQuadTextureInv  = ByteBuffer.allocateDirect(TEX_DATA_SIZE     *dataLength*bytes_per_float).order(ByteOrder.nativeOrder()).asFloatBuffer();
71
    mQuadTextureInv.put(textureInv).position(0);
72 8fa96e69 Leszek Koltunski
    }
73 4c1dd6e9 Leszek Koltunski
74 0d81d0fb Leszek Koltunski
  private static DistortedFramebuffer mBufferFBO = new DistortedFramebuffer(1,1);
75 1c67457f Leszek Koltunski
  private static float[] mMVPMatrix = new float[16];
76
  private static float[] mTmpMatrix = new float[16];
77 0d81d0fb Leszek Koltunski
78
  // BLUR effect
79
  private static DistortedProgram mBlurProgram;
80 5ccafcca Leszek Koltunski
  private static int mRadiusH,mOffsetsH,mWeightsH,mObjDH,mMVPMatrixH;
81 0d81d0fb Leszek Koltunski
  private float[] mWeights = new float[MAX_BLUR];
82 5ccafcca Leszek Koltunski
  private float[] mOffsets = new float[MAX_BLUR];
83 0d81d0fb Leszek Koltunski
  // another effect ....
84
85 4c1dd6e9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
86
87
  EffectQueuePostprocess(long id)
88
    { 
89
    super(id,NUM_UNIFORMS,NUM_CACHE,INDEX );
90
    }
91
92 c90b9e01 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
93
94
  static void createProgram(Resources resources)
95
  throws FragmentCompilationException,VertexCompilationException,VertexUniformsException,FragmentUniformsException,LinkingException
96
    {
97 f1d5ea12 Leszek Koltunski
    final InputStream postVertexStream   = resources.openRawResource(R.raw.blur_vertex_shader);
98
    final InputStream postFragmentStream = resources.openRawResource(R.raw.blur_fragment_shader);
99 c90b9e01 Leszek Koltunski
100 0d81d0fb Leszek Koltunski
    mBlurProgram = new DistortedProgram(postVertexStream,postFragmentStream,
101
                                        "#version 100\n",
102
                                        "#version 100\n#define MAX_BLUR "+MAX_BLUR);
103 c90b9e01 Leszek Koltunski
104 f1d5ea12 Leszek Koltunski
    int blurProgramH = mBlurProgram.getProgramHandle();
105
    mRadiusH    = GLES30.glGetUniformLocation( blurProgramH, "u_Radius");
106 5ccafcca Leszek Koltunski
    mOffsetsH   = GLES30.glGetUniformLocation( blurProgramH, "u_Offsets");
107 0d81d0fb Leszek Koltunski
    mWeightsH   = GLES30.glGetUniformLocation( blurProgramH, "u_Weights");
108 f1d5ea12 Leszek Koltunski
    mObjDH      = GLES30.glGetUniformLocation( blurProgramH, "u_objD");
109
    mMVPMatrixH = GLES30.glGetUniformLocation( blurProgramH, "u_MVPMatrix");
110 4c1dd6e9 Leszek Koltunski
    }
111
112
///////////////////////////////////////////////////////////////////////////////////////////////////
113
  
114
  synchronized void compute(long currTime) 
115
    {
116
    if( currTime==mTime ) return;
117
    if( mTime==0 ) mTime = currTime;
118
    long step = (currTime-mTime);
119
   
120
    for(int i=0; i<mNumEffects; i++)
121
      {
122
      if( mInter[0][i]!=null && mInter[0][i].interpolateMain(mUniforms ,NUM_UNIFORMS*i, mCurrentDuration[i], step) )
123
        {
124
        for(int j=0; j<mNumListeners; j++)
125
          EffectMessageSender.newMessage( mListeners.elementAt(j),
126
                                          EffectMessage.EFFECT_FINISHED,
127
                                         (mID[i]<<EffectTypes.LENGTH)+EffectTypes.POSTPROCESS.type,
128
                                          mName[i],
129
                                          mObjectID);
130
131
        if( EffectNames.isUnity(mName[i], mUniforms, NUM_UNIFORMS*i) )
132
          {
133
          remove(i);
134
          i--;
135
          continue;
136
          }
137
        else mInter[0][i] = null;
138
        }
139
140
      mCurrentDuration[i] += step;
141
      }
142
     
143
    mTime = currTime;  
144
    }  
145
146
///////////////////////////////////////////////////////////////////////////////////////////////////
147
148
  protected void moveEffect(int index)
149
    {
150
    mUniforms[NUM_UNIFORMS*index  ] = mUniforms[NUM_UNIFORMS*(index+1)  ];
151
    mUniforms[NUM_UNIFORMS*index+1] = mUniforms[NUM_UNIFORMS*(index+1)+1];
152
    mUniforms[NUM_UNIFORMS*index+2] = mUniforms[NUM_UNIFORMS*(index+1)+2];
153 667a1ad9 Leszek Koltunski
    mUniforms[NUM_UNIFORMS*index+3] = mUniforms[NUM_UNIFORMS*(index+1)+3];
154 4c1dd6e9 Leszek Koltunski
    }
155
156
///////////////////////////////////////////////////////////////////////////////////////////////////
157 1c67457f Leszek Koltunski
// w,h - width and height of the input texture. MVP - Model-View-Projection matrix to apply to the
158 11fb6ce0 Leszek Koltunski
// texture; df - output FBO.
159 4c1dd6e9 Leszek Koltunski
160 8fa96e69 Leszek Koltunski
  synchronized void render(float w, float h, float[] mvp, DistortedFramebuffer df)
161 4c1dd6e9 Leszek Koltunski
    {
162 f1d5ea12 Leszek Koltunski
    mBlurProgram.useProgram();
163 bfe2c61b Leszek Koltunski
164 0d81d0fb Leszek Koltunski
    int radius = (int)mUniforms[0];
165
    if( radius>=MAX_BLUR ) radius = MAX_BLUR-1;
166
167
    for(int i=0; i<=radius; i++)
168
      {
169
      mWeights[i] = 1.0f / (2.0f*radius+1.0f);
170 5ccafcca Leszek Koltunski
      mOffsets[i] = i/h;
171 0d81d0fb Leszek Koltunski
      }
172
173
    GLES30.glUniform1fv( mWeightsH, radius+1, mWeights,0);
174 5ccafcca Leszek Koltunski
    GLES30.glUniform1fv( mOffsetsH ,radius+1, mOffsets,0);
175 0d81d0fb Leszek Koltunski
    GLES30.glUniform1i( mRadiusH, radius);
176 194ab46f Leszek Koltunski
    GLES30.glUniform2f( mObjDH , w, h );
177 4c1dd6e9 Leszek Koltunski
178 1c67457f Leszek Koltunski
    mBufferFBO.resizeFast( (int)w, (int)h);
179 0d81d0fb Leszek Koltunski
    mBufferFBO.setAsOutput();
180 1c67457f Leszek Koltunski
    GLES30.glViewport(0, 0, (int)w, (int)h);
181
182
    Matrix.setIdentityM(mTmpMatrix, 0);
183
    Matrix.translateM(mTmpMatrix, 0, 0, 0, -mBufferFBO.mDistance);
184
    Matrix.multiplyMM(mMVPMatrix, 0, mBufferFBO.mProjectionMatrix, 0, mTmpMatrix, 0);
185 0d81d0fb Leszek Koltunski
186
    // horizontal blur
187 5ccafcca Leszek Koltunski
188 1c67457f Leszek Koltunski
    GLES30.glUniformMatrix4fv(mMVPMatrixH, 1, false, mMVPMatrix , 0);
189 0d81d0fb Leszek Koltunski
    GLES30.glVertexAttribPointer(mBlurProgram.mAttribute[0], POSITION_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadPositions);
190 1c67457f Leszek Koltunski
    GLES30.glVertexAttribPointer(mBlurProgram.mAttribute[1], TEX_DATA_SIZE     , GLES30.GL_FLOAT, false, 0, mQuadTextureInv);
191 0d81d0fb Leszek Koltunski
    GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
192
193
    mBufferFBO.setAsInput();
194
    df.setAsOutput();
195 1c67457f Leszek Koltunski
    GLES30.glViewport(0, 0, df.mWidth, df.mHeight);
196 0d81d0fb Leszek Koltunski
197 5ccafcca Leszek Koltunski
    float adjust = h/w;
198
    for(int i=0; i<=radius; i++) mOffsets[i] *= adjust;
199
200 0d81d0fb Leszek Koltunski
    // vertical blur
201 5ccafcca Leszek Koltunski
    GLES30.glUniform1fv( mOffsetsH ,radius+1, mOffsets,0);
202 1c67457f Leszek Koltunski
    GLES30.glUniformMatrix4fv(mMVPMatrixH, 1, false, mvp , 0);
203 f1d5ea12 Leszek Koltunski
    GLES30.glVertexAttribPointer(mBlurProgram.mAttribute[0], POSITION_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadPositions);
204 1c67457f Leszek Koltunski
    GLES30.glVertexAttribPointer(mBlurProgram.mAttribute[1], TEX_DATA_SIZE     , GLES30.GL_FLOAT, false, 0, mQuadTextureInv);
205 194ab46f Leszek Koltunski
    GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
206 d6e94c84 Leszek Koltunski
    }
207
208 4c1dd6e9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
209
// blur
210
211 85cbbc5e Leszek Koltunski
  synchronized long add(EffectNames eln, Data1D degree)
212 4c1dd6e9 Leszek Koltunski
    {
213
    if( mMax[INDEX]>mNumEffects )
214
      {
215
      if( degree instanceof Dynamic1D)
216
        {
217
        mInter[0][mNumEffects] = (Dynamic1D)degree;
218
        }
219
      else if( degree instanceof Static1D)
220
        {
221
        mInter[0][mNumEffects] = null;
222
        mUniforms[NUM_UNIFORMS*mNumEffects] = ((Static1D)degree).getX();
223
        }
224
      else return -1;
225
226 85cbbc5e Leszek Koltunski
      mInter[1][mNumEffects] = null;
227
      mInter[2][mNumEffects] = null;
228 4c1dd6e9 Leszek Koltunski
229
      return addBase(eln);
230
      }
231
      
232
    return -1;
233
    }
234
  }