Project

General

Profile

« Previous | Next » 

Revision 77fcb24d

Added by Leszek Koltunski over 7 years ago

Fix for Bug #17: Regions of Fragment Effects migrate.

Fix is: don't send vertex position and the Fragment Effect region to the fragment shader already multiplied by the ModelView matrix (that introduces the imprecise interpolation because of the projection effect) but simply send both of them in local coords.

View differences:

src/main/java/org/distorted/library/DistortedObject.java
168 168
    mV.send();
169 169
        
170 170
    mF.compute(currTime);
171
    mF.postprocess(mViewMatrix);
172 171
    mF.send();
173 172
       
174 173
    mGrid.draw();
src/main/java/org/distorted/library/EffectQueueFragment.java
38 38
  {
39 39
  private static final int NUM_UNIFORMS = 8;
40 40
  private static final int INDEX = EffectTypes.FRAGMENT.ordinal();
41
  private float[] mBuf;
42 41
  private static int mNumEffectsH;
43 42
  private static int mTypeH;
44 43
  private static int mUniformsH;
......
48 47
  public EffectQueueFragment(DistortedObject obj)
49 48
    { 
50 49
    super(obj,NUM_UNIFORMS,INDEX);
51
   
52
    if( mMax[INDEX]>0 )
53
      {
54
      mBuf= new float[4*mMax[INDEX]];
55
      }
56 50
    }
57 51

  
58 52
///////////////////////////////////////////////////////////////////////////////////////////////////
......
93 87
        else mInter[0][i] = null;
94 88
        }
95 89

  
96
      if( mInter[1][i]!=null ) mInter[1][i].interpolateMain(     mBuf,            4*i  , mCurrentDuration[i]);
90
      if( mInter[1][i]!=null )
91
        {
92
        mInter[1][i].interpolateMain( mUniforms, NUM_UNIFORMS*i+4, mCurrentDuration[i]);
93

  
94
        mUniforms[NUM_UNIFORMS*i+4] = mUniforms[NUM_UNIFORMS*i+4]-mObjHalfX;
95
        mUniforms[NUM_UNIFORMS*i+5] =-mUniforms[NUM_UNIFORMS*i+5]+mObjHalfY;
96
        }
97 97

  
98 98
      if( mInter[2][i]!=null ) mInter[2][i].interpolateMain(mUniforms, NUM_UNIFORMS*i+1, mCurrentDuration[i]);
99 99

  
......
107 107

  
108 108
  protected void moveEffect(int index)
109 109
    {
110
    mBuf[4*index  ] = mBuf[4*index+4];
111
    mBuf[4*index+1] = mBuf[4*index+5];
112
    mBuf[4*index+2] = mBuf[4*index+6];
113
    mBuf[4*index+3] = mBuf[4*index+7];
114
              
115 110
    mUniforms[NUM_UNIFORMS*index  ] = mUniforms[NUM_UNIFORMS*(index+1)  ];
116 111
    mUniforms[NUM_UNIFORMS*index+1] = mUniforms[NUM_UNIFORMS*(index+1)+1];
117
    mUniforms[NUM_UNIFORMS*index+2] = mUniforms[NUM_UNIFORMS*(index+1)+2];  
118
    mUniforms[NUM_UNIFORMS*index+3] = mUniforms[NUM_UNIFORMS*(index+1)+3];  
112
    mUniforms[NUM_UNIFORMS*index+2] = mUniforms[NUM_UNIFORMS*(index+1)+2];
113
    mUniforms[NUM_UNIFORMS*index+3] = mUniforms[NUM_UNIFORMS*(index+1)+3];
114

  
115
    if( mInter[1][index]==null )
116
      {
117
      mUniforms[NUM_UNIFORMS*index+4] = mUniforms[NUM_UNIFORMS*(index+1)+4];
118
      mUniforms[NUM_UNIFORMS*index+5] = mUniforms[NUM_UNIFORMS*(index+1)+5];
119
      mUniforms[NUM_UNIFORMS*index+6] = mUniforms[NUM_UNIFORMS*(index+1)+6];
120
      mUniforms[NUM_UNIFORMS*index+7] = mUniforms[NUM_UNIFORMS*(index+1)+7];
121
      }
119 122
    }
120 123
  
121 124
///////////////////////////////////////////////////////////////////////////////////////////////////
......
126 129
      
127 130
    if( mNumEffects>0 )
128 131
      {     
129
      GLES20.glUniform1iv( mTypeH    ,  mNumEffects, mName,0);
132
      GLES20.glUniform1iv( mTypeH    ,  mNumEffects, mName    ,0);
130 133
      GLES20.glUniform4fv( mUniformsH,2*mNumEffects, mUniforms,0);
131 134
      }  
132 135
    }
......
137 140
    {
138 141
    GLES20.glUniform1i( mNumEffectsH, 0);
139 142
    }
140
    
141
///////////////////////////////////////////////////////////////////////////////////////////////////
142
// Do various post-processing on already computed effects.
143
// 1) move all Points and scale all Region radii by a ModelView matrix
144 143

  
145
  void postprocess(float[] MVmatrix)
146
    {
147
    if( mNumEffects>0 )
148
      {
149
      float tx,ty;   
150
      float w = (float)Math.sqrt(MVmatrix[0]*MVmatrix[0] + MVmatrix[4]*MVmatrix[4]);  // The scale factors are the lengths of the first 3 vectors of the upper-left 3x3 submatrix; here
151
      float h = (float)Math.sqrt(MVmatrix[1]*MVmatrix[1] + MVmatrix[5]*MVmatrix[5]);  // m[2]=m[6]=m[8]=m[9]=0 so it is really only the upper-left 2x2 matrix.
152
   
153
      for(int i=0; i<mNumEffects; i++)
154
        {   
155
        tx = mBuf[4*i  ]-mObjHalfX; // we have to invert y and move everything by (half of bmp width, half of bmp height)
156
        ty =-mBuf[4*i+1]+mObjHalfY; //
157

  
158
        mUniforms[NUM_UNIFORMS*i+4] = MVmatrix[0]*tx + MVmatrix[4]*ty + MVmatrix[12]; // multiply the ModelView matrix times the (x,y,0,1) point, i.e. the (x,y) point on the surface of the bitmap.
159
        mUniforms[NUM_UNIFORMS*i+5] = MVmatrix[1]*tx + MVmatrix[5]*ty + MVmatrix[13]; //
160
        mUniforms[NUM_UNIFORMS*i+6] = w*mBuf[4*i+2];                                  // in fragment shader rx and ry radii are the last two values of the second vec4
161
        mUniforms[NUM_UNIFORMS*i+7] = h*mBuf[4*i+3];                                  //
162
        }
163
      }
164
    }
165
  
166 144
///////////////////////////////////////////////////////////////////////////////////////////////////
167 145
// alpha, brightness, contrast, saturation
168 146

  
......
182 160
      else return -1;
183 161

  
184 162
      mInter[1][mNumEffects] = null;
185
      mBuf[4*mNumEffects+2] = 1000*mObjHalfX;
186
      mBuf[4*mNumEffects+3] = 1000*mObjHalfY;
163
      mUniforms[NUM_UNIFORMS*mNumEffects+6] = 1000*mObjHalfX;
164
      mUniforms[NUM_UNIFORMS*mNumEffects+7] = 1000*mObjHalfY;
187 165

  
188 166
      mInter[2][mNumEffects] = null;
189 167

  
......
216 194
      else if( region instanceof Static4D )
217 195
        {
218 196
        mInter[1][mNumEffects]  = null;
219
        mBuf[4*mNumEffects  ] = ((Static4D)region).getX();
220
        mBuf[4*mNumEffects+1] = ((Static4D)region).getY();
221
        mBuf[4*mNumEffects+2] = ((Static4D)region).getZ();
222
        mBuf[4*mNumEffects+3] = ((Static4D)region).getW();
197
        mUniforms[NUM_UNIFORMS*mNumEffects+4] = ((Static4D)region).getX()-mObjHalfX;
198
        mUniforms[NUM_UNIFORMS*mNumEffects+5] =-((Static4D)region).getY()+mObjHalfY;
199
        mUniforms[NUM_UNIFORMS*mNumEffects+6] = ((Static4D)region).getZ();
200
        mUniforms[NUM_UNIFORMS*mNumEffects+7] = ((Static4D)region).getW();
223 201
        }
224 202
      else return -1;
225 203

  
......
267 245
      else if( region instanceof Static4D )
268 246
        {
269 247
        mInter[1][mNumEffects]  = null;
270
        mBuf[4*mNumEffects  ] = ((Static4D)region).getX();
271
        mBuf[4*mNumEffects+1] = ((Static4D)region).getY();
272
        mBuf[4*mNumEffects+2] = ((Static4D)region).getZ();
273
        mBuf[4*mNumEffects+3] = ((Static4D)region).getW();
248
        mUniforms[NUM_UNIFORMS*mNumEffects+4] = ((Static4D)region).getX()-mObjHalfX;
249
        mUniforms[NUM_UNIFORMS*mNumEffects+5] =-((Static4D)region).getY()+mObjHalfY;
250
        mUniforms[NUM_UNIFORMS*mNumEffects+6] = ((Static4D)region).getZ();
251
        mUniforms[NUM_UNIFORMS*mNumEffects+7] = ((Static4D)region).getW();
274 252
        }
275 253
      else return -1;
276 254

  
......
311 289
        }
312 290
      else return -1;
313 291

  
314
      mInter[1][mNumEffects]  = null;          //
315
      mBuf[4*mNumEffects+2] = 1000*mObjHalfX;  // i.e. null region
316
      mBuf[4*mNumEffects+3] = 1000*mObjHalfY;  //
292
      mInter[1][mNumEffects]  = null;
293
      mUniforms[NUM_UNIFORMS*mNumEffects+6] = 1000*mObjHalfX;
294
      mUniforms[NUM_UNIFORMS*mNumEffects+7] = 1000*mObjHalfY;
317 295

  
318 296
      return addBase(eln);
319 297
      }
src/main/res/raw/main_vertex_shader.glsl
356 356
  restrict(v.z);  
357 357
#endif
358 358
   
359
  v_Position      = vec3(u_MVMatrix*v);           
359
  v_Position      = v.xyz;
360 360
  v_TexCoordinate = a_TexCoordinate;
361 361
  v_Normal        = normalize(vec3(u_MVMatrix*n));
362 362
  gl_Position     = u_MVPMatrix*v;      

Also available in: Unified diff