Project

General

Profile

« Previous | Next » 

Revision 270c27bc

Added by Leszek Koltunski over 7 years ago

Progress with Stencils in postprocessing.

View differences:

src/main/java/org/distorted/library/DistortedEffects.java
245 245

  
246 246
///////////////////////////////////////////////////////////////////////////////////////////////////
247 247

  
248
  void drawPriv(float halfW, float halfH, MeshObject mesh, DistortedOutputSurface surface, long currTime)
248
  void drawPriv(float halfW, float halfH, MeshObject mesh, DistortedOutputSurface surface, long currTime, float marginInPixels)
249 249
    {
250 250
    mM.compute(currTime);
251 251
    mV.compute(currTime);
......
277 277
      GLES30.glVertexAttribPointer(mMainProgram.mAttribute[2], MeshObject.TEX_DATA_SIZE, GLES30.GL_FLOAT, false, MeshObject.VERTSIZE, mesh.mVertAttribs);
278 278
      }
279 279

  
280
    mM.send(surface,halfW,halfH,halfZ);
280
    mM.send(surface,halfW,halfH,halfZ,marginInPixels);
281 281
    mV.send(halfW,halfH,halfZ);
282 282
    mF.send(halfW,halfH);
283 283

  
src/main/java/org/distorted/library/DistortedNode.java
257 257
    if( mParent!=null ) mParent.adjustIsomorphism();
258 258
    }
259 259

  
260
///////////////////////////////////////////////////////////////////////////////////////////////////
261

  
262
  int markStencilAndDraw(long currTime, DistortedOutputSurface surface, float marginInPixels)
263
    {
264
    DistortedInputSurface input = mNumChildren[0]==0 ? mSurface : mData.mFBO;
265

  
266
    if( input.setAsInput() )
267
      {
268
      // Mark area of our object + marginInPixels pixels around with 1s in Stencil buffer
269
      DistortedRenderState.setUpStencilMark();
270
      mEffects.drawPriv(mSurface.getWidth()/2.0f, mSurface.getHeight()/2.0f, mMesh, surface, currTime, marginInPixels);
271

  
272
      // Actually draw our object.
273
      mState.apply();
274
      mEffects.drawPriv(mSurface.getWidth()/2.0f, mSurface.getHeight()/2.0f, mMesh, surface, currTime, 0);
275
      return 1;
276
      }
277
    return 0;
278
    }
279

  
260 280
///////////////////////////////////////////////////////////////////////////////////////////////////
261 281
// return the total number of render calls issued
262 282

  
......
267 287
    if( input.setAsInput() )
268 288
      {
269 289
      mState.apply();
270
      mEffects.drawPriv(mSurface.getWidth()/2.0f, mSurface.getHeight()/2.0f, mMesh, surface, currTime);
290
      mEffects.drawPriv(mSurface.getWidth()/2.0f, mSurface.getHeight()/2.0f, mMesh, surface, currTime, 0);
271 291
      return 1;
272 292
      }
273 293

  
src/main/java/org/distorted/library/DistortedOutputSurface.java
80 80
  private float mClearDepth;
81 81
  private int mClearStencil;
82 82
  private int mClear;
83

  
84
//private String sNew="", sOld="";
85

  
86 83
  float mMipmap;
87 84

  
88 85
///////////////////////////////////////////////////////////////////////////////////////////////////
......
171 168
    DistortedEffectsPostprocess lastP=null, currP;
172 169
    long lastB=0, currB;
173 170

  
174
//sNew = "";
175

  
176 171
    for(int i=0; i<num; i++)
177 172
      {
178 173
      child = children.get(i);
179 174
      currP = child.getEffectsPostprocess();
180 175
      currB = currP==null ? 0 : currP.getBucket();
181 176

  
182
//sNew += currB;
183

  
184 177
      if( lastB!=currB && lastB!=0 )
185 178
        {
186 179
        numRenders += lastP.postprocess(time,this);
......
204 197
            mBuffer1[j].glClear(GLES30.GL_COLOR_BUFFER_BIT|GLES30.GL_DEPTH_BUFFER_BIT|GLES30.GL_STENCIL_BUFFER_BIT);
205 198
            mipmap *= EffectQuality.MULTIPLIER;
206 199
            }
207
          DistortedObject.toDo();  // create immediately
200
          DistortedObject.toDo(); // create the FBOs immediately. This is safe as we must be holding the OpenGL context now.
208 201
          }
209 202

  
210
        numRenders += child.draw(time,mBuffer1[currP.getQuality()]);
203
        numRenders += child.markStencilAndDraw(time,mBuffer1[currP.getQuality()],20);
211 204

  
212 205
        if( i==num-1 )
213 206
          {
......
218 211
      lastP = currP;
219 212
      lastB = currB;
220 213
      }
221
/*
222
if( !sNew.equals(sOld) )
223
  {
224
  sOld = sNew;
225
  android.util.Log.e("surface", "Surface"+getID()+": "+sOld);
226
  }
227
*/
214

  
228 215
    return numRenders;
229 216
    }
230 217

  
src/main/java/org/distorted/library/DistortedRenderState.java
56 56
  private int mBlendSrc, mBlendDst;                                        //
57 57
  private int mClear;                                                      // This does not have a 'static' compatriot
58 58

  
59
  private static int rStencilTest;                                         //
60
  private static int rStencilFuncFunc;                                     //
61
  private static int rStencilFuncRef;                                      // Remember values of Stencil.
62
  private static int rStencilFuncMask;                                     //
63
  private static int rStencilMask;                                         //
64

  
59 65
///////////////////////////////////////////////////////////////////////////////////////////////////
60 66
// default: color writes on, depth test and writes on, blending on, stencil off.
61 67

  
......
153 159
    GLES30.glDisable(GLES30.GL_SCISSOR_TEST);
154 160
    }
155 161

  
162
///////////////////////////////////////////////////////////////////////////////////////////////////
163

  
164
  static void setUpStencilMark()
165
    {
166
    if( sStencilTest!=1 )
167
      {
168
      sStencilTest = 1;
169
      GLES30.glEnable(GLES30.GL_STENCIL_TEST);
170
      }
171
    if( sStencilFuncFunc!=GLES30.GL_ALWAYS || sStencilFuncRef!=1 || sStencilFuncMask!=STENCIL_MASK )
172
      {
173
      sStencilFuncFunc = GLES30.GL_ALWAYS;
174
      sStencilFuncRef  = 1;
175
      sStencilFuncMask = STENCIL_MASK;
176
      GLES30.glStencilFunc(sStencilFuncFunc,sStencilFuncRef,sStencilFuncMask);
177
      }
178
    if( sStencilOpSfail!=GLES30.GL_KEEP || sStencilOpDpfail!=GLES30.GL_KEEP || sStencilOpDppass!=GLES30.GL_REPLACE )
179
      {
180
      sStencilOpSfail = GLES30.GL_KEEP;
181
      sStencilOpDpfail= GLES30.GL_KEEP;
182
      sStencilOpDppass= GLES30.GL_REPLACE;
183
      GLES30.glStencilOp(sStencilOpSfail,sStencilOpDpfail,sStencilOpDppass);
184
      }
185
    if( sColorMaskR!=0 || sColorMaskG!=0 || sColorMaskB!=0 || sColorMaskA!=0 )
186
      {
187
      sColorMaskR = 0;
188
      sColorMaskG = 0;
189
      sColorMaskB = 0;
190
      sColorMaskA = 0;
191
      GLES30.glColorMask(false,false,false,false);
192
      }
193
    if( sDepthMask!=0 )
194
      {
195
      sDepthMask = 0;
196
      GLES30.glDepthMask(false);
197
      }
198
    if( sStencilMask!= STENCIL_MASK )
199
      {
200
      sStencilMask = STENCIL_MASK;
201
      GLES30.glStencilMask(sStencilMask);
202
      }
203
    }
204

  
205
///////////////////////////////////////////////////////////////////////////////////////////////////
206

  
207
  static void useStencilMark()
208
    {
209
    if( sStencilTest!=1 )
210
      {
211
      rStencilTest = sStencilTest;
212
      sStencilTest = 1;
213
      GLES30.glEnable(GLES30.GL_STENCIL_TEST);
214
      }
215
    if( sStencilFuncFunc!=GLES30.GL_EQUAL || sStencilFuncRef!=1 || sStencilFuncMask!=STENCIL_MASK )
216
      {
217
      rStencilFuncFunc = sStencilFuncFunc;
218
      rStencilFuncRef  = sStencilFuncRef;
219
      rStencilFuncMask = sStencilFuncMask;
220
      sStencilFuncFunc = GLES30.GL_EQUAL;
221
      sStencilFuncRef  = 1;
222
      sStencilFuncMask = STENCIL_MASK;
223
      GLES30.glStencilFunc(sStencilFuncFunc,sStencilFuncRef,sStencilFuncMask);
224
      }
225

  
226
    // TODO: Debatable
227
    if( sStencilMask!= 0x00 )
228
      {
229
      rStencilMask = sStencilMask;
230
      sStencilMask = 0x00;
231
      GLES30.glStencilMask(sStencilMask);
232
      }
233
    }
234

  
235
///////////////////////////////////////////////////////////////////////////////////////////////////
236

  
237
  static void restoreStencilMark()
238
    {
239
    if( rStencilTest!=sStencilTest )
240
      {
241
      sStencilTest = rStencilTest;
242

  
243
      if (sStencilTest == 0)
244
        {
245
        GLES30.glDisable(GLES30.GL_STENCIL_TEST);
246
        }
247
      else
248
        {
249
        GLES30.glEnable(GLES30.GL_STENCIL_TEST);
250
        }
251
      }
252

  
253
    if( rStencilFuncFunc!=sStencilFuncFunc || rStencilFuncRef!=sStencilFuncRef || rStencilFuncMask!=sStencilFuncMask )
254
      {
255
      sStencilFuncFunc = rStencilFuncFunc;
256
      sStencilFuncRef  = rStencilFuncRef ;
257
      sStencilFuncMask = rStencilFuncMask;
258
      GLES30.glStencilFunc(sStencilFuncFunc,sStencilFuncRef,sStencilFuncMask);
259
      }
260

  
261
    if( rStencilMask!=sStencilMask )
262
      {
263
      sStencilMask = rStencilMask;
264
      GLES30.glStencilMask(sStencilMask);
265
      }
266
    }
267

  
156 268
///////////////////////////////////////////////////////////////////////////////////////////////////
157 269

  
158 270
  void apply()
src/main/java/org/distorted/library/EffectQueueMatrix.java
105 105

  
106 106
///////////////////////////////////////////////////////////////////////////////////////////////////
107 107

  
108
  private void magnify(float halfX, float halfY, float halfZ, DistortedOutputSurface projection)
108
  private void magnify(DistortedOutputSurface projection, float halfX, float halfY, float halfZ, float marginInPixels)
109 109
    {
110
    float scale;
110
    float scale, nx, ny;
111 111
    float[] result= new float[4];
112 112
    float[] point = new float[4];
113 113
    float[] matrix= new float[16];
114
    double d1, d2, d3, d4, d5, d6, d7, d8;
115
    float nx, ny;
116
    float w = projection.mWidth/2;
117
    float h = projection.mHeight/2;
118
    double sum = 0.0;
119
    point[3] = 0.0f;
114
    float minx = Integer.MAX_VALUE;
115
    float maxx = Integer.MIN_VALUE;
116
    float miny = Integer.MAX_VALUE;
117
    float maxy = Integer.MIN_VALUE;
118

  
119
    point[3] = 1.0f;
120 120

  
121 121
    Matrix.multiplyMM(matrix, 0, projection.mProjectionMatrix, 0, mViewMatrix, 0);
122 122

  
123 123
    point[0] = +halfX; point[1] = +halfY; point[2] = +halfZ;
124 124
    Matrix.multiplyMV(result,0,matrix,0,point,0);
125
    nx = w*result[0]/result[3];
126
    ny = h*result[1]/result[3];
127
    d1 = Math.sqrt(nx*nx+ny*ny);
125
    nx = result[0]/result[3];
126
    ny = result[1]/result[3];
127
    if( nx<minx ) minx = nx;
128
    if( nx>maxx ) maxx = nx;
129
    if( ny<miny ) miny = ny;
130
    if( ny>maxy ) maxy = ny;
128 131

  
129 132
    point[0] = +halfX; point[1] = +halfY; point[2] = -halfZ;
130 133
    Matrix.multiplyMV(result,0,matrix,0,point,0);
131
    nx = w*result[0]/result[3];
132
    ny = h*result[1]/result[3];
133
    d2 = Math.sqrt(nx*nx+ny*ny);
134
    nx = result[0]/result[3];
135
    ny = result[1]/result[3];
136
    if( nx<minx ) minx = nx;
137
    if( nx>maxx ) maxx = nx;
138
    if( ny<miny ) miny = ny;
139
    if( ny>maxy ) maxy = ny;
134 140

  
135 141
    point[0] = +halfX; point[1] = -halfY; point[2] = +halfZ;
136 142
    Matrix.multiplyMV(result,0,matrix,0,point,0);
137
    nx = w*result[0]/result[3];
138
    ny = h*result[1]/result[3];
139
    d3 = Math.sqrt(nx*nx+ny*ny);
143
    nx = result[0]/result[3];
144
    ny = result[1]/result[3];
145
    if( nx<minx ) minx = nx;
146
    if( nx>maxx ) maxx = nx;
147
    if( ny<miny ) miny = ny;
148
    if( ny>maxy ) maxy = ny;
140 149

  
141 150
    point[0] = +halfX; point[1] = -halfY; point[2] = -halfZ;
142 151
    Matrix.multiplyMV(result,0,matrix,0,point,0);
143
    nx = w*result[0]/result[3];
144
    ny = h*result[1]/result[3];
145
    d4 = Math.sqrt(nx*nx+ny*ny);
152
    nx = result[0]/result[3];
153
    ny = result[1]/result[3];
154
    if( nx<minx ) minx = nx;
155
    if( nx>maxx ) maxx = nx;
156
    if( ny<miny ) miny = ny;
157
    if( ny>maxy ) maxy = ny;
146 158

  
147 159
    point[0] = -halfX; point[1] = +halfY; point[2] = +halfZ;
148 160
    Matrix.multiplyMV(result,0,matrix,0,point,0);
149
    nx = w*result[0]/result[3];
150
    ny = h*result[1]/result[3];
151
    d5 = Math.sqrt(nx*nx+ny*ny);
161
    nx = result[0]/result[3];
162
    ny = result[1]/result[3];
163
    if( nx<minx ) minx = nx;
164
    if( nx>maxx ) maxx = nx;
165
    if( ny<miny ) miny = ny;
166
    if( ny>maxy ) maxy = ny;
152 167

  
153 168
    point[0] = -halfX; point[1] = +halfY; point[2] = -halfZ;
154 169
    Matrix.multiplyMV(result,0,matrix,0,point,0);
155
    nx = w*result[0]/result[3];
156
    ny = h*result[1]/result[3];
157
    d6 = Math.sqrt(nx*nx+ny*ny);
170
    nx = result[0]/result[3];
171
    ny = result[1]/result[3];
172
    if( nx<minx ) minx = nx;
173
    if( nx>maxx ) maxx = nx;
174
    if( ny<miny ) miny = ny;
175
    if( ny>maxy ) maxy = ny;
158 176

  
159 177
    point[0] = -halfX; point[1] = -halfY; point[2] = +halfZ;
160 178
    Matrix.multiplyMV(result,0,matrix,0,point,0);
161
    nx = w*result[0]/result[3];
162
    ny = h*result[1]/result[3];
163
    d7 = Math.sqrt(nx*nx+ny*ny);
179
    nx = result[0]/result[3];
180
    ny = result[1]/result[3];
181
    if( nx<minx ) minx = nx;
182
    if( nx>maxx ) maxx = nx;
183
    if( ny<miny ) miny = ny;
184
    if( ny>maxy ) maxy = ny;
164 185

  
165 186
    point[0] = -halfX; point[1] = -halfY; point[2] = -halfZ;
166 187
    Matrix.multiplyMV(result,0,matrix,0,point,0);
167
    nx = w*result[0]/result[3];
168
    ny = h*result[1]/result[3];
169
    d8 = Math.sqrt(nx*nx+ny*ny);
188
    nx = result[0]/result[3];
189
    ny = result[1]/result[3];
190
    if( nx<minx ) minx = nx;
191
    if( nx>maxx ) maxx = nx;
192
    if( ny<miny ) miny = ny;
193
    if( ny>maxy ) maxy = ny;
170 194

  
171
    sum = (d1+d2+d3+d4+d5+d6+d7+d8)/8;
195
    float xlen = projection.mWidth *(maxx-minx)/2;
196
    float ylen = projection.mHeight*(maxy-miny)/2;
172 197

  
173
    scale = 1.0f + (float)(Distorted.mMargin/sum);
198
    scale = 1.0f + marginInPixels/( xlen>ylen ? ylen:xlen );
174 199

  
175
    android.util.Log.d("scale", "scale="+scale+" sum="+sum+" "+d1+" "+d2+" "+d3+" "+d4+" "+d5+" "+d6+" "+d7+" "+d8);
200
   // android.util.Log.d("scale", "scale= "+scale+" xlen="+xlen+" ylen="+ylen);
176 201

  
177 202
    Matrix.scaleM(mViewMatrix, 0, scale, scale, scale);
178 203
    }
......
180 205
///////////////////////////////////////////////////////////////////////////////////////////////////
181 206
// here construct the ModelView and the ModelViewProjection Matrices
182 207

  
183
  private void constructMatrices(DistortedOutputSurface projection, float halfX, float halfY, float halfZ)
208
  private void constructMatrices(DistortedOutputSurface projection, float halfX, float halfY, float halfZ, float marginInPixels)
184 209
    {
185 210
    Matrix.setIdentityM(mViewMatrix, 0);
186 211
    Matrix.translateM(mViewMatrix, 0, -projection.mWidth/2, projection.mHeight/2, -projection.mDistance);
......
261 286

  
262 287
    Matrix.translateM(mViewMatrix, 0, halfX,-halfY,-halfZ);
263 288

  
264
    if( Distorted.mMargin!=0 ) magnify(halfX,halfY,halfZ,projection);
289
    if( marginInPixels!=0 ) magnify(projection,halfX,halfY,halfZ, marginInPixels);
265 290

  
266 291
    Matrix.multiplyMM(mMVPMatrix, 0, projection.mProjectionMatrix, 0, mViewMatrix, 0);
267 292
    }
......
336 361

  
337 362
///////////////////////////////////////////////////////////////////////////////////////////////////
338 363

  
339
  synchronized void send(DistortedOutputSurface projection, float halfX, float halfY, float halfZ)
364
  synchronized void send(DistortedOutputSurface projection, float halfX, float halfY, float halfZ, float marginInPixels)
340 365
    {
341
    constructMatrices(projection,halfX,halfY,halfZ);
366
    constructMatrices(projection,halfX,halfY,halfZ, marginInPixels);
342 367

  
343 368
    GLES30.glUniform3f( mObjDH , halfX, halfY, halfZ);
344 369
    GLES30.glUniformMatrix4fv(mMVMatrixH , 1, false, mViewMatrix, 0);

Also available in: Unified diff