Project

General

Profile

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

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

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 POSITION_DATA_SIZE= 2; // Post Program: size of the position data in elements
46
  private static final int TEX_DATA_SIZE     = 2; // Post Program: size of the texture coordinate data in elements.
47

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

    
52
  private static int mRadiusH;
53
  private static int mOneOverH;
54
  private static int mObjDH;
55
  private static int mMVPMatrixH;
56

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

    
60
  static
61
    {
62
    int dataLength      = 4;
63
    int bytes_per_float = 4;
64

    
65
    float[] positionData= { -0.5f, -0.5f,  -0.5f, 0.5f,  0.5f,-0.5f,  0.5f, 0.5f };
66
    float[] textureData = {  0.0f,  0.0f,   0.0f, 1.0f,  1.0f, 0.0f,  1.0f, 1.0f };
67

    
68
    mQuadPositions = ByteBuffer.allocateDirect(POSITION_DATA_SIZE*dataLength*bytes_per_float).order(ByteOrder.nativeOrder()).asFloatBuffer();
69
    mQuadPositions.put(positionData).position(0);
70
    mQuadTexture   = ByteBuffer.allocateDirect(TEX_DATA_SIZE     *dataLength*bytes_per_float).order(ByteOrder.nativeOrder()).asFloatBuffer();
71
    mQuadTexture.put(textureData).position(0);
72
    }
73

    
74
///////////////////////////////////////////////////////////////////////////////////////////////////
75

    
76
  EffectQueuePostprocess(long id)
77
    { 
78
    super(id,NUM_UNIFORMS,NUM_CACHE,INDEX );
79
    }
80

    
81
///////////////////////////////////////////////////////////////////////////////////////////////////
82

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

    
89
    mBlurProgram = new DistortedProgram(postVertexStream,postFragmentStream, "", "");
90

    
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");
96
    }
97

    
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99
  
100
  synchronized void compute(long currTime) 
101
    {
102
    if( currTime==mTime ) return;
103
    if( mTime==0 ) mTime = currTime;
104
    long step = (currTime-mTime);
105
   
106
    for(int i=0; i<mNumEffects; i++)
107
      {
108
      if( mInter[0][i]!=null && mInter[0][i].interpolateMain(mUniforms ,NUM_UNIFORMS*i, mCurrentDuration[i], step) )
109
        {
110
        for(int j=0; j<mNumListeners; j++)
111
          EffectMessageSender.newMessage( mListeners.elementAt(j),
112
                                          EffectMessage.EFFECT_FINISHED,
113
                                         (mID[i]<<EffectTypes.LENGTH)+EffectTypes.POSTPROCESS.type,
114
                                          mName[i],
115
                                          mObjectID);
116

    
117
        if( EffectNames.isUnity(mName[i], mUniforms, NUM_UNIFORMS*i) )
118
          {
119
          remove(i);
120
          i--;
121
          continue;
122
          }
123
        else mInter[0][i] = null;
124
        }
125

    
126
      mCurrentDuration[i] += step;
127
      }
128
     
129
    mTime = currTime;  
130
    }  
131

    
132
///////////////////////////////////////////////////////////////////////////////////////////////////
133

    
134
  protected void moveEffect(int index)
135
    {
136
    mUniforms[NUM_UNIFORMS*index  ] = mUniforms[NUM_UNIFORMS*(index+1)  ];
137
    mUniforms[NUM_UNIFORMS*index+1] = mUniforms[NUM_UNIFORMS*(index+1)+1];
138
    mUniforms[NUM_UNIFORMS*index+2] = mUniforms[NUM_UNIFORMS*(index+1)+2];
139
    mUniforms[NUM_UNIFORMS*index+3] = mUniforms[NUM_UNIFORMS*(index+1)+3];
140
    }
141

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

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

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

    
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);
158
    GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
159
    }
160

    
161
///////////////////////////////////////////////////////////////////////////////////////////////////
162
// blur
163

    
164
  synchronized long add(EffectNames eln, Data1D degree)
165
    {
166
    if( mMax[INDEX]>mNumEffects )
167
      {
168
      if( degree instanceof Dynamic1D)
169
        {
170
        mInter[0][mNumEffects] = (Dynamic1D)degree;
171
        }
172
      else if( degree instanceof Static1D)
173
        {
174
        mInter[0][mNumEffects] = null;
175
        mUniforms[NUM_UNIFORMS*mNumEffects] = ((Static1D)degree).getX();
176
        }
177
      else return -1;
178

    
179
      mInter[1][mNumEffects] = null;
180
      mInter[2][mNumEffects] = null;
181

    
182
      return addBase(eln);
183
      }
184
      
185
    return -1;
186
    }
187
  }
(11-11/16)