Project

General

Profile

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

library / src / main / java / org / distorted / library / EffectQueuePostprocess.java @ 8fa96e69

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
import android.opengl.GLES20;
23
24
import org.distorted.library.message.EffectMessage;
25 8fa96e69 Leszek Koltunski
import org.distorted.library.program.DistortedProgram;
26 4c1dd6e9 Leszek Koltunski
import org.distorted.library.type.Data1D;
27
import org.distorted.library.type.Data2D;
28
import org.distorted.library.type.Dynamic1D;
29
import org.distorted.library.type.Dynamic2D;
30
import org.distorted.library.type.Static1D;
31
import org.distorted.library.type.Static2D;
32
33 8fa96e69 Leszek Koltunski
import java.nio.ByteBuffer;
34
import java.nio.ByteOrder;
35
import java.nio.FloatBuffer;
36
37 4c1dd6e9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
38
39
class EffectQueuePostprocess extends EffectQueue
40
  {
41 8fa96e69 Leszek Koltunski
  private static final int BYTES_PER_FLOAT   = 4;
42
  private static final int POSITION_DATA_SIZE= 2; // Post Program: size of the position data in elements
43
  private static final int TEX_DATA_SIZE     = 2; // Post Program: size of the texture coordinate data in elements.
44
45 4c1dd6e9 Leszek Koltunski
  private static final int NUM_UNIFORMS = 3;
46
  private static final int NUM_CACHE    = 0;
47
  private static final int INDEX = EffectTypes.POSTPROCESS.ordinal();
48
49
  private static int mNumEffectsH;
50
  private static int mTypeH;
51
  private static int mUniformsH;
52
  private static int mObjDH;
53 bfe2c61b Leszek Koltunski
  private static int mMVPMatrixH;
54
55 8fa96e69 Leszek Koltunski
  private static final FloatBuffer mQuadPositions, mQuadTexture;
56
  private static DistortedProgram mProgram;
57
58
  static
59
    {
60
    int dataLength = 4;
61
62
    float[] positionData= { -0.5f, -0.5f,  -0.5f, 0.5f,  0.5f,-0.5f,  0.5f, 0.5f };
63
    float[] textureData = {  0.0f,  0.0f,   0.0f, 1.0f,  1.0f, 0.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
    mQuadTexture   = ByteBuffer.allocateDirect(TEX_DATA_SIZE     *dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
68
    mQuadTexture.put(textureData).position(0);
69
    }
70 4c1dd6e9 Leszek Koltunski
71
///////////////////////////////////////////////////////////////////////////////////////////////////
72
73
  EffectQueuePostprocess(long id)
74
    { 
75
    super(id,NUM_UNIFORMS,NUM_CACHE,INDEX );
76
    }
77
78
///////////////////////////////////////////////////////////////////////////////////////////////////
79
80
  static void getUniforms(int mProgramH)
81
    {
82
    mNumEffectsH= GLES20.glGetUniformLocation( mProgramH, "pNumEffects");
83
    mTypeH      = GLES20.glGetUniformLocation( mProgramH, "pType");
84
    mUniformsH  = GLES20.glGetUniformLocation( mProgramH, "pUniforms");
85
    mObjDH      = GLES20.glGetUniformLocation( mProgramH, "u_objD");
86 bfe2c61b Leszek Koltunski
    mMVPMatrixH = GLES20.glGetUniformLocation(mProgramH, "u_MVPMatrix");
87 4c1dd6e9 Leszek Koltunski
    }
88
89 8fa96e69 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
90
91
  static void setProgram(DistortedProgram p)
92
    {
93
    mProgram = p;
94
    }
95
96 4c1dd6e9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
97
  
98
  synchronized void compute(long currTime) 
99
    {
100
    if( currTime==mTime ) return;
101
    if( mTime==0 ) mTime = currTime;
102
    long step = (currTime-mTime);
103
   
104
    for(int i=0; i<mNumEffects; i++)
105
      {
106
      if( mInter[0][i]!=null && mInter[0][i].interpolateMain(mUniforms ,NUM_UNIFORMS*i, mCurrentDuration[i], step) )
107
        {
108
        for(int j=0; j<mNumListeners; j++)
109
          EffectMessageSender.newMessage( mListeners.elementAt(j),
110
                                          EffectMessage.EFFECT_FINISHED,
111
                                         (mID[i]<<EffectTypes.LENGTH)+EffectTypes.POSTPROCESS.type,
112
                                          mName[i],
113
                                          mObjectID);
114
115
        if( EffectNames.isUnity(mName[i], mUniforms, NUM_UNIFORMS*i) )
116
          {
117
          remove(i);
118
          i--;
119
          continue;
120
          }
121
        else mInter[0][i] = null;
122
        }
123
124
      if( mInter[1][i]!=null )
125
        {
126
        mInter[1][i].interpolateMain(mUniforms, NUM_UNIFORMS*i+1, mCurrentDuration[i], step);
127
        }
128
129
      mCurrentDuration[i] += step;
130
      }
131
     
132
    mTime = currTime;  
133
    }  
134
135
///////////////////////////////////////////////////////////////////////////////////////////////////
136
137
  protected void moveEffect(int index)
138
    {
139
    mUniforms[NUM_UNIFORMS*index  ] = mUniforms[NUM_UNIFORMS*(index+1)  ];
140
    mUniforms[NUM_UNIFORMS*index+1] = mUniforms[NUM_UNIFORMS*(index+1)+1];
141
    mUniforms[NUM_UNIFORMS*index+2] = mUniforms[NUM_UNIFORMS*(index+1)+2];
142
    }
143
144 02de77c9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
145
146 8fa96e69 Leszek Koltunski
  synchronized static void sendZero(float w, float h, float[] mvp)
147 02de77c9 Leszek Koltunski
    {
148 8fa96e69 Leszek Koltunski
    GLES20.glUniform1i( mNumEffectsH, 0);
149
    GLES20.glUniform2f( mObjDH , w, h);
150 02de77c9 Leszek Koltunski
    GLES20.glUniformMatrix4fv(mMVPMatrixH, 1, false, mvp , 0);
151
    }
152
153 4c1dd6e9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
154
155 8fa96e69 Leszek Koltunski
  synchronized void render(float w, float h, float[] mvp, DistortedFramebuffer df)
156 4c1dd6e9 Leszek Koltunski
    {
157 8fa96e69 Leszek Koltunski
    mProgram.useProgram();
158
    df.setAsOutput();
159 bfe2c61b Leszek Koltunski
160 4c1dd6e9 Leszek Koltunski
    GLES20.glUniform1i( mNumEffectsH, mNumEffects);
161 8fa96e69 Leszek Koltunski
    GLES20.glUniform2f( mObjDH , w, h );
162
    GLES20.glUniformMatrix4fv(mMVPMatrixH, 1, false, mvp , 0);
163 4c1dd6e9 Leszek Koltunski
164
    if( mNumEffects>0 )
165
      {
166
      GLES20.glUniform1iv( mTypeH    ,  mNumEffects, mName    ,0);
167
      GLES20.glUniform4fv( mUniformsH,2*mNumEffects, mUniforms,0);
168
      }
169 d6e94c84 Leszek Koltunski
170 8fa96e69 Leszek Koltunski
    GLES20.glVertexAttribPointer(mProgram.mAttribute[0], POSITION_DATA_SIZE, GLES20.GL_FLOAT, false, 0, mQuadPositions);
171
    GLES20.glVertexAttribPointer(mProgram.mAttribute[1], TEX_DATA_SIZE     , GLES20.GL_FLOAT, false, 0, mQuadTexture);
172
    GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
173 d6e94c84 Leszek Koltunski
    }
174
175 4c1dd6e9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
176
// blur
177
178
  synchronized long add(EffectNames eln, Data1D degree, Data2D center)
179
    {
180
    if( mMax[INDEX]>mNumEffects )
181
      {
182
      if( degree instanceof Dynamic1D)
183
        {
184
        mInter[0][mNumEffects] = (Dynamic1D)degree;
185
        }
186
      else if( degree instanceof Static1D)
187
        {
188
        mInter[0][mNumEffects] = null;
189
        mUniforms[NUM_UNIFORMS*mNumEffects] = ((Static1D)degree).getX();
190
        }
191
      else return -1;
192
193
      if( center instanceof Dynamic2D)
194
        {
195
        mInter[1][mNumEffects] = (Dynamic2D)center;
196
        }
197
      else if( center instanceof Static2D)
198
        {
199
        mInter[1][mNumEffects] = null;
200
        mUniforms[NUM_UNIFORMS*mNumEffects+1] = ((Static2D)center).getX();
201
        mUniforms[NUM_UNIFORMS*mNumEffects+2] = ((Static2D)center).getY();
202
        }
203
      else return -1;
204
205
      return addBase(eln);
206
      }
207
      
208
    return -1;
209
    }
210
  }