Project

General

Profile

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

library / src / main / java / org / distorted / library / EffectQueuePostprocess.java @ e2b2d95f

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
import org.distorted.library.type.Data1D;
26
import org.distorted.library.type.Data2D;
27
import org.distorted.library.type.Dynamic1D;
28
import org.distorted.library.type.Dynamic2D;
29
import org.distorted.library.type.Static1D;
30
import org.distorted.library.type.Static2D;
31
32
///////////////////////////////////////////////////////////////////////////////////////////////////
33
34
class EffectQueuePostprocess extends EffectQueue
35
  {
36
  private static final int NUM_UNIFORMS = 3;
37
  private static final int NUM_CACHE    = 0;
38
  private static final int INDEX = EffectTypes.POSTPROCESS.ordinal();
39
40
  private static int mNumEffectsH;
41
  private static int mTypeH;
42
  private static int mUniformsH;
43
  private static int mObjDH;
44
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46
47
  EffectQueuePostprocess(long id)
48
    { 
49
    super(id,NUM_UNIFORMS,NUM_CACHE,INDEX );
50
    }
51
52
///////////////////////////////////////////////////////////////////////////////////////////////////
53
54
  static void getUniforms(int mProgramH)
55
    {
56
    mNumEffectsH= GLES20.glGetUniformLocation( mProgramH, "pNumEffects");
57
    mTypeH      = GLES20.glGetUniformLocation( mProgramH, "pType");
58
    mUniformsH  = GLES20.glGetUniformLocation( mProgramH, "pUniforms");
59
    mObjDH      = GLES20.glGetUniformLocation( mProgramH, "u_objD");
60
    }
61
62
///////////////////////////////////////////////////////////////////////////////////////////////////
63
  
64
  synchronized void compute(long currTime) 
65
    {
66
    if( currTime==mTime ) return;
67
    if( mTime==0 ) mTime = currTime;
68
    long step = (currTime-mTime);
69
   
70
    for(int i=0; i<mNumEffects; i++)
71
      {
72
      if( mInter[0][i]!=null && mInter[0][i].interpolateMain(mUniforms ,NUM_UNIFORMS*i, mCurrentDuration[i], step) )
73
        {
74
        for(int j=0; j<mNumListeners; j++)
75
          EffectMessageSender.newMessage( mListeners.elementAt(j),
76
                                          EffectMessage.EFFECT_FINISHED,
77
                                         (mID[i]<<EffectTypes.LENGTH)+EffectTypes.POSTPROCESS.type,
78
                                          mName[i],
79
                                          mObjectID);
80
81
        if( EffectNames.isUnity(mName[i], mUniforms, NUM_UNIFORMS*i) )
82
          {
83
          remove(i);
84
          i--;
85
          continue;
86
          }
87
        else mInter[0][i] = null;
88
        }
89
90
      if( mInter[1][i]!=null )
91
        {
92
        mInter[1][i].interpolateMain(mUniforms, NUM_UNIFORMS*i+1, mCurrentDuration[i], step);
93
        }
94
95
      mCurrentDuration[i] += step;
96
      }
97
     
98
    mTime = currTime;  
99
    }  
100
101 d6e94c84 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
102
103
  void postprocess(DistortedFramebuffer input, DistortedFramebuffer output)
104
    {
105 e2b2d95f Leszek Koltunski
    // 1. Set input's COLOR0 as input texture
106
    // 2. Set output as output FBO
107
    // 3. set Vieport
108
    // 4. call send()
109
    // 5. render a quad:
110
    /*
111
    GLES20.glVertexAttribPointer(Distorted.mPostProgramAttributes[0], MeshObject.POSITION_DATA_SIZE, GLES20.GL_FLOAT, false, 0, mMeshPositions);
112
    GLES20.glVertexAttribPointer(Distorted.mPostProgramAttributes[1], MeshObject.TEX_DATA_SIZE     , GLES20.GL_FLOAT, false, 0, mMeshTexture);
113
    GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
114
    */
115 d6e94c84 Leszek Koltunski
    }
116
117 4c1dd6e9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
118
119
  protected void moveEffect(int index)
120
    {
121
    mUniforms[NUM_UNIFORMS*index  ] = mUniforms[NUM_UNIFORMS*(index+1)  ];
122
    mUniforms[NUM_UNIFORMS*index+1] = mUniforms[NUM_UNIFORMS*(index+1)+1];
123
    mUniforms[NUM_UNIFORMS*index+2] = mUniforms[NUM_UNIFORMS*(index+1)+2];
124
    }
125
126
///////////////////////////////////////////////////////////////////////////////////////////////////
127
128
  synchronized void send(float objX, float objY)
129
    {
130
    GLES20.glUniform1i( mNumEffectsH, mNumEffects);
131
    GLES20.glUniform2f( mObjDH , objX, objY);
132
133
    if( mNumEffects>0 )
134
      {
135
      GLES20.glUniform1iv( mTypeH    ,  mNumEffects, mName    ,0);
136
      GLES20.glUniform4fv( mUniformsH,2*mNumEffects, mUniforms,0);
137
      }
138
    }
139
140 d6e94c84 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
141
142
  synchronized static void sendZero(float objX, float objY)
143
    {
144
    GLES20.glUniform1i( mNumEffectsH, 0);
145
    GLES20.glUniform2f( mObjDH , objX, objY);
146
    }
147
148 4c1dd6e9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
149
// blur
150
151
  synchronized long add(EffectNames eln, Data1D degree, Data2D center)
152
    {
153
    if( mMax[INDEX]>mNumEffects )
154
      {
155
      if( degree instanceof Dynamic1D)
156
        {
157
        mInter[0][mNumEffects] = (Dynamic1D)degree;
158
        }
159
      else if( degree instanceof Static1D)
160
        {
161
        mInter[0][mNumEffects] = null;
162
        mUniforms[NUM_UNIFORMS*mNumEffects] = ((Static1D)degree).getX();
163
        }
164
      else return -1;
165
166
      if( center instanceof Dynamic2D)
167
        {
168
        mInter[1][mNumEffects] = (Dynamic2D)center;
169
        }
170
      else if( center instanceof Static2D)
171
        {
172
        mInter[1][mNumEffects] = null;
173
        mUniforms[NUM_UNIFORMS*mNumEffects+1] = ((Static2D)center).getX();
174
        mUniforms[NUM_UNIFORMS*mNumEffects+2] = ((Static2D)center).getY();
175
        }
176
      else return -1;
177
178
      return addBase(eln);
179
      }
180
      
181
    return -1;
182
    }
183
  }