Project

General

Profile

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

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

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

    
22
import android.content.res.Resources;
23
import android.opengl.GLES31;
24
import android.util.Log;
25

    
26
import org.distorted.library.R;
27
import org.distorted.library.effect.EffectType;
28
import org.distorted.library.effect.PostprocessEffect;
29
import org.distorted.library.effect.VertexEffect;
30
import org.distorted.library.mesh.MeshBase;
31
import org.distorted.library.message.EffectMessage;
32
import org.distorted.library.message.EffectMessageSender;
33
import org.distorted.library.program.DistortedProgram;
34

    
35
import java.io.InputStream;
36

    
37
///////////////////////////////////////////////////////////////////////////////////////////////////
38

    
39
class EffectQueuePostprocess extends EffectQueue
40
  {
41
  private static final int NUM_UNIFORMS = PostprocessEffect.NUM_UNIFORMS;
42
  private static final int INDEX = EffectType.POSTPROCESS.ordinal();
43

    
44
  private int mHalo;
45
  private float mR, mG, mB, mA;
46

    
47
  private static DistortedProgram mPreProgram;
48
  private static int mPreColorH;
49
  private static int mPreTextureH;
50

    
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52

    
53
  EffectQueuePostprocess(long id)
54
    { 
55
    super(id,NUM_UNIFORMS,INDEX );
56
    }
57

    
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59

    
60
  void compute(long currTime)
61
    {
62
    if( currTime==mTime ) return;
63
    if( mTime==0 ) mTime = currTime;
64
    long step = (currTime-mTime);
65

    
66
    mR = mG = mB = mA = 0.0f;
67
    mHalo = 0;
68
    int halo;
69

    
70
    for(int i=0; i<mNumEffects; i++)
71
      {
72
      mCurrentDuration[i] += step;
73

    
74
      // first zero out the 'alpha' because BLUR effect will not overwrite this (it is a 1D effect)
75
      // and if previously there was a GLOW effect here then mA would be non-zero and we don't want
76
      // that (see preprocess())
77
      mUniforms[NUM_UNIFORMS*i+4]=0.0f;
78

    
79
      if( mEffects[i].compute(mUniforms, NUM_UNIFORMS*i, mCurrentDuration[i], step) )
80
        {
81
        for(int j=0; j<mNumListeners; j++)
82
          EffectMessageSender.newMessage( mListeners.get(j), EffectMessage.EFFECT_FINISHED, mEffects[i].getID(), mDistortedEffectsID);
83
        }
84

    
85
      halo = (int)mUniforms[NUM_UNIFORMS*i];
86
      if( halo>mHalo ) mHalo = halo;
87
      }
88

    
89
    // TODO  (now only really works in case of 1 effect!)
90
    if( mNumEffects>0 )
91
      {
92
      mR = mUniforms[1];
93
      mG = mUniforms[2];
94
      mB = mUniforms[3];
95
      mA = mUniforms[4];
96
      }
97

    
98
    mTime = currTime;
99
    }
100

    
101
///////////////////////////////////////////////////////////////////////////////////////////////////
102

    
103
  static void createPrograms(Resources resources)
104
    {
105
    final InputStream mainVertStream = resources.openRawResource(R.raw.main_vertex_shader);
106
    final InputStream mainFragStream = resources.openRawResource(R.raw.preprocess_fragment_shader);
107

    
108
    int numV = VertexEffect.getNumEnabled();
109

    
110
    String mainVertHeader= Distorted.GLSL_VERSION + ("#define NUM_VERTEX "   + ( numV>0 ? Distorted.getMax(EffectType.VERTEX  ) : 0 ) + "\n");
111
    String mainFragHeader= Distorted.GLSL_VERSION + "\n";
112

    
113
    String enabledEffectV= VertexEffect.getGLSL();
114

    
115
    try
116
      {
117
      mPreProgram = new DistortedProgram(mainVertStream, mainFragStream, mainVertHeader, mainFragHeader,
118
                                         enabledEffectV, null, Distorted.GLSL, null);
119
      }
120
    catch(Exception e)
121
      {
122
      Log.e("POSTPROCESS", e.getClass().getSimpleName()+" trying to compile PRE program: "+e.getMessage());
123
      throw new RuntimeException(e.getMessage());
124
      }
125

    
126
    int preProgramH = mPreProgram.getProgramHandle();
127
    EffectQueueVertex.getUniforms( preProgramH,2 );
128
    EffectQueueMatrix.getUniforms( preProgramH,2 );
129
    mPreColorH  = GLES31.glGetUniformLocation( preProgramH, "u_Color"  );
130
    mPreTextureH= GLES31.glGetUniformLocation( preProgramH, "u_Texture");
131
    }
132

    
133
///////////////////////////////////////////////////////////////////////////////////////////////////
134
// TODO  (now only really works in case of 1 effect!)
135

    
136
  int getQuality()
137
    {
138
    return mNumEffects>0 ? ((PostprocessEffect)mEffects[0]).getQuality() : 0;
139
    }
140

    
141
///////////////////////////////////////////////////////////////////////////////////////////////////
142
// TODO  (now only really works in case of 1 effect!)
143

    
144
  boolean getRender()
145
    {
146
    return mNumEffects>0 ? ((PostprocessEffect)mEffects[0]).getRender() : false;
147
    }
148

    
149
///////////////////////////////////////////////////////////////////////////////////////////////////
150

    
151
  int preprocess(DistortedOutputSurface buffer, DistortedNode node)
152
    {
153
    buffer.setAsOutput();
154
    DistortedSurface input = node.getInternalSurface();
155

    
156
    if( input.setAsInput() )
157
      {
158
      MeshBase mesh = node.getMesh();
159

    
160
      float halfW = input.getWidth() / 2.0f;
161
      float halfH = input.getHeight()/ 2.0f;
162
      float halfZ = halfW*mesh.getZFactor();
163

    
164
      DistortedRenderState.setUpStencilMark(mA!=0.0f);
165
      DistortedRenderState.disableBlending();
166

    
167
      GLES31.glViewport(0, 0, buffer.mWidth, buffer.mHeight );
168

    
169
      mPreProgram.useProgram();
170

    
171
      mesh.bindVertexAttribs(mPreProgram);
172

    
173
      EffectQueue[] queues = node.getEffects().getQueues();
174
      EffectQueueMatrix matrix = (EffectQueueMatrix)queues[0];
175
      EffectQueueVertex vertex = (EffectQueueVertex)queues[1];
176

    
177
      float inflate=0.0f;
178

    
179
      matrix.send(buffer,halfW,halfH,halfZ,2);
180

    
181
      if( mHalo!=0.0f )
182
        {
183
        inflate = matrix.magnify(buffer,halfW,halfH,halfZ,mHalo);
184
        }
185

    
186
      vertex.send(inflate,2);
187

    
188
      if( mA!=0.0f )
189
        {
190
        GLES31.glUniform4f(mPreColorH, mR, mG, mB, mA);
191
        GLES31.glUniform1i(mPreTextureH, 0);
192
        }
193

    
194
      GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, mesh.getNumVertices() );
195

    
196
      DistortedRenderState.restoreBlending();
197
      DistortedRenderState.unsetUpStencilMark();
198

    
199
      return 1;
200
      }
201
    return 0;
202
    }
203

    
204
///////////////////////////////////////////////////////////////////////////////////////////////////
205

    
206
  int postprocess(DistortedFramebuffer buffer)
207
    {
208
    int numRenders = 0;
209

    
210
    GLES31.glDisable(GLES31.GL_BLEND);
211

    
212
    for(int i=0; i<mNumEffects; i++)
213
      {
214
      numRenders += ((PostprocessEffect)mEffects[i]).apply(mUniforms,NUM_UNIFORMS*i, buffer);
215
      }
216

    
217
    GLES31.glEnable(GLES31.GL_BLEND);
218

    
219
    return numRenders;
220
    }
221
  }
(18-18/19)