Project

General

Profile

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

library / src / main / java / org / distorted / library / EffectQueuePostprocess.java @ 0d81d0fb

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.library;
21

    
22
import android.content.res.Resources;
23
import android.opengl.GLES30;
24

    
25
import org.distorted.library.message.EffectMessage;
26
import org.distorted.library.program.DistortedProgram;
27
import org.distorted.library.program.FragmentCompilationException;
28
import org.distorted.library.program.FragmentUniformsException;
29
import org.distorted.library.program.LinkingException;
30
import org.distorted.library.program.VertexCompilationException;
31
import org.distorted.library.program.VertexUniformsException;
32
import org.distorted.library.type.Data1D;
33
import org.distorted.library.type.Dynamic1D;
34
import org.distorted.library.type.Static1D;
35

    
36
import java.io.InputStream;
37
import java.nio.ByteBuffer;
38
import java.nio.ByteOrder;
39
import java.nio.FloatBuffer;
40

    
41
///////////////////////////////////////////////////////////////////////////////////////////////////
42

    
43
class EffectQueuePostprocess extends EffectQueue
44
  {
45
  private static final int MAX_BLUR = 50;
46

    
47
  private static final int POSITION_DATA_SIZE= 2; // Post Program: size of the position data in elements
48
  private static final int TEX_DATA_SIZE     = 2; // Post Program: size of the texture coordinate data in elements.
49

    
50
  private static final int NUM_UNIFORMS = 4;
51
  private static final int NUM_CACHE    = 0;
52
  private static final int INDEX = EffectTypes.POSTPROCESS.ordinal();
53

    
54
  private static final FloatBuffer mQuadPositions, mQuadTexture1, mQuadTexture2;
55

    
56
  static
57
    {
58
    int dataLength      = 4;
59
    int bytes_per_float = 4;
60

    
61
    float[] positionData= { -0.5f, -0.5f,  -0.5f, 0.5f,  0.5f,-0.5f,  0.5f, 0.5f };
62
    float[] textureData1= {  0.0f,  0.0f,   0.0f, 1.0f,  1.0f, 0.0f,  1.0f, 1.0f };
63
    float[] textureData2= {  0.0f,  0.0f,   1.0f, 0.0f,  0.0f, 1.0f,  1.0f, 1.0f };
64

    
65
    mQuadPositions = ByteBuffer.allocateDirect(POSITION_DATA_SIZE*dataLength*bytes_per_float).order(ByteOrder.nativeOrder()).asFloatBuffer();
66
    mQuadPositions.put(positionData).position(0);
67
    mQuadTexture1  = ByteBuffer.allocateDirect(TEX_DATA_SIZE     *dataLength*bytes_per_float).order(ByteOrder.nativeOrder()).asFloatBuffer();
68
    mQuadTexture1.put(textureData1).position(0);
69
    mQuadTexture2  = ByteBuffer.allocateDirect(TEX_DATA_SIZE     *dataLength*bytes_per_float).order(ByteOrder.nativeOrder()).asFloatBuffer();
70
    mQuadTexture2.put(textureData2).position(0);
71
    }
72

    
73
  private static DistortedFramebuffer mBufferFBO = new DistortedFramebuffer(1,1);
74

    
75
  // BLUR effect
76
  private static DistortedProgram mBlurProgram;
77
  private static int mRadiusH,mStepH,mWeightsH,mObjDH,mMVPMatrixH;
78
  private float[] mWeights = new float[MAX_BLUR];
79

    
80
  // another effect ....
81

    
82
///////////////////////////////////////////////////////////////////////////////////////////////////
83

    
84
  EffectQueuePostprocess(long id)
85
    { 
86
    super(id,NUM_UNIFORMS,NUM_CACHE,INDEX );
87
    }
88

    
89
///////////////////////////////////////////////////////////////////////////////////////////////////
90

    
91
  static void createProgram(Resources resources)
92
  throws FragmentCompilationException,VertexCompilationException,VertexUniformsException,FragmentUniformsException,LinkingException
93
    {
94
    final InputStream postVertexStream   = resources.openRawResource(R.raw.blur_vertex_shader);
95
    final InputStream postFragmentStream = resources.openRawResource(R.raw.blur_fragment_shader);
96

    
97
    mBlurProgram = new DistortedProgram(postVertexStream,postFragmentStream,
98
                                        "#version 100\n",
99
                                        "#version 100\n#define MAX_BLUR "+MAX_BLUR);
100

    
101
    int blurProgramH = mBlurProgram.getProgramHandle();
102
    mRadiusH    = GLES30.glGetUniformLocation( blurProgramH, "u_Radius");
103
    mStepH      = GLES30.glGetUniformLocation( blurProgramH, "u_Step");
104
    mWeightsH   = GLES30.glGetUniformLocation( blurProgramH, "u_Weights");
105
    mObjDH      = GLES30.glGetUniformLocation( blurProgramH, "u_objD");
106
    mMVPMatrixH = GLES30.glGetUniformLocation( blurProgramH, "u_MVPMatrix");
107
    }
108

    
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110
  
111
  synchronized void compute(long currTime) 
112
    {
113
    if( currTime==mTime ) return;
114
    if( mTime==0 ) mTime = currTime;
115
    long step = (currTime-mTime);
116
   
117
    for(int i=0; i<mNumEffects; i++)
118
      {
119
      if( mInter[0][i]!=null && mInter[0][i].interpolateMain(mUniforms ,NUM_UNIFORMS*i, mCurrentDuration[i], step) )
120
        {
121
        for(int j=0; j<mNumListeners; j++)
122
          EffectMessageSender.newMessage( mListeners.elementAt(j),
123
                                          EffectMessage.EFFECT_FINISHED,
124
                                         (mID[i]<<EffectTypes.LENGTH)+EffectTypes.POSTPROCESS.type,
125
                                          mName[i],
126
                                          mObjectID);
127

    
128
        if( EffectNames.isUnity(mName[i], mUniforms, NUM_UNIFORMS*i) )
129
          {
130
          remove(i);
131
          i--;
132
          continue;
133
          }
134
        else mInter[0][i] = null;
135
        }
136

    
137
      mCurrentDuration[i] += step;
138
      }
139
     
140
    mTime = currTime;  
141
    }  
142

    
143
///////////////////////////////////////////////////////////////////////////////////////////////////
144

    
145
  protected void moveEffect(int index)
146
    {
147
    mUniforms[NUM_UNIFORMS*index  ] = mUniforms[NUM_UNIFORMS*(index+1)  ];
148
    mUniforms[NUM_UNIFORMS*index+1] = mUniforms[NUM_UNIFORMS*(index+1)+1];
149
    mUniforms[NUM_UNIFORMS*index+2] = mUniforms[NUM_UNIFORMS*(index+1)+2];
150
    mUniforms[NUM_UNIFORMS*index+3] = mUniforms[NUM_UNIFORMS*(index+1)+3];
151
    }
152

    
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154
// w,h - width and height of hte input texture. MVP - Model-View-Projection matrix to apply to the
155
// texture; df - output FBO.
156

    
157
  synchronized void render(float w, float h, float[] mvp, DistortedFramebuffer df)
158
    {
159
    mBlurProgram.useProgram();
160

    
161
    int radius = (int)mUniforms[0];
162
    if( radius>=MAX_BLUR ) radius = MAX_BLUR-1;
163

    
164
    for(int i=0; i<=radius; i++)
165
      {
166
      mWeights[i] = 1.0f / (2.0f*radius+1.0f);
167
      }
168

    
169
    GLES30.glUniform1fv( mWeightsH, radius+1, mWeights,0);
170
    GLES30.glUniform1i( mRadiusH, radius);
171
    GLES30.glUniform2f( mObjDH , w, h );
172
    GLES30.glUniformMatrix4fv(mMVPMatrixH, 1, false, mvp , 0);
173

    
174
    mBufferFBO.resizeFast(df.mWidth, df.mHeight);
175
    mBufferFBO.setAsOutput();
176

    
177
    // horizontal blur
178
    GLES30.glUniform1f( mStepH , 1/h );
179
    GLES30.glVertexAttribPointer(mBlurProgram.mAttribute[0], POSITION_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadPositions);
180
    GLES30.glVertexAttribPointer(mBlurProgram.mAttribute[1], TEX_DATA_SIZE     , GLES30.GL_FLOAT, false, 0, mQuadTexture2);
181
    GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
182

    
183
    mBufferFBO.setAsInput();
184
    df.setAsOutput();
185

    
186
    // vertical blur
187
    GLES30.glUniform1f( mStepH , 1/w );
188
    GLES30.glVertexAttribPointer(mBlurProgram.mAttribute[0], POSITION_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadPositions);
189
    GLES30.glVertexAttribPointer(mBlurProgram.mAttribute[1], TEX_DATA_SIZE     , GLES30.GL_FLOAT, false, 0, mQuadTexture2);
190
    GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
191
    }
192

    
193
///////////////////////////////////////////////////////////////////////////////////////////////////
194
// blur
195

    
196
  synchronized long add(EffectNames eln, Data1D degree)
197
    {
198
    if( mMax[INDEX]>mNumEffects )
199
      {
200
      if( degree instanceof Dynamic1D)
201
        {
202
        mInter[0][mNumEffects] = (Dynamic1D)degree;
203
        }
204
      else if( degree instanceof Static1D)
205
        {
206
        mInter[0][mNumEffects] = null;
207
        mUniforms[NUM_UNIFORMS*mNumEffects] = ((Static1D)degree).getX();
208
        }
209
      else return -1;
210

    
211
      mInter[1][mNumEffects] = null;
212
      mInter[2][mNumEffects] = null;
213

    
214
      return addBase(eln);
215
      }
216
      
217
    return -1;
218
    }
219
  }
(11-11/16)