Project

General

Profile

« Previous | Next » 

Revision 69ed1eb4

Added by Leszek Koltunski almost 8 years ago

Progress with getting Mesh'es bounding rectangle.

View differences:

src/main/java/org/distorted/library/DistortedEffects.java
146 146
      }
147 147
    else
148 148
      {
149
      if( mV.mNumEffects==0 && mF.mNumEffects==0 && mesh.canUsePostprocessingShortcut() )
149
      if( mV.mNumEffects==0 && mF.mNumEffects==0 && (mesh instanceof MeshFlat) && mM.canUseShortcut() )
150 150
        {
151 151
        mM.constructMatrices(df,halfInputW,halfInputH);
152 152
        mP.render(2*halfInputW, 2*halfInputH, mM.getMVP(), df);
src/main/java/org/distorted/library/EffectQueueMatrix.java
204 204
    return mMVPMatrix;
205 205
    }
206 206

  
207
///////////////////////////////////////////////////////////////////////////////////////////////////
208
// return true if in the queue there are only translations, scales, x- and y-shears, and rotations along
209
// a vector which is perpendicular to the screen, i.e. if the resulting matrix keeps the front wall of the
210
// Mesh parallel to the screen.
211

  
212
  boolean canUseShortcut()
213
    {
214
    for(int i=0; i<mNumEffects; i++)
215
      {
216
      if (mName[i] == EffectNames.ROTATE.ordinal() )
217
        {
218
        if( mUniforms[NUM_UNIFORMS*i] != 0.0f || mUniforms[NUM_UNIFORMS*i+1] != 0.0f ) return false;  // rotation along vector with x or y non-zero
219
        }
220
      else if(mName[i] == EffectNames.QUATERNION.ordinal() )
221
        {
222
        if( mUniforms[NUM_UNIFORMS*i] != 0.0f || mUniforms[NUM_UNIFORMS*i+1] != 0.0f ) return false;  // quaternion rotation along vector with x or y non-zero
223
        }
224
      else if(mName[i] == EffectNames.SHEAR.ordinal() )
225
        {
226
        if( mUniforms[NUM_UNIFORMS*i+2] != 0.0f ) return false; // shear with non-zero Z
227
        }
228
      }
229

  
230
    return true;
231
    }
232

  
207 233
///////////////////////////////////////////////////////////////////////////////////////////////////
208 234

  
209 235
  synchronized void compute(long currTime) 
src/main/java/org/distorted/library/MeshCubes.java
72 72
   private int mEdgeNum;
73 73
   private int mSideWalls;
74 74

  
75
   private float[] mBoundingVert;
76

  
77
   private static float[] mBoundingVert1 = new float[] { -0.5f,-0.5f, FRONTZ,
78
                                                         +0.5f,-0.5f, FRONTZ,
79
                                                         -0.5f,+0.5f, FRONTZ,
80
                                                         +0.5f,+0.5f, FRONTZ };
81

  
82
   private static float[] mBoundingVert2 = new float[] { -0.5f,-0.5f, FRONTZ,
83
                                                         +0.5f,-0.5f, FRONTZ,
84
                                                         -0.5f,+0.5f, FRONTZ,
85
                                                         +0.5f,+0.5f, FRONTZ,
86
                                                         -0.5f,-0.5f, BACKZ ,
87
                                                         +0.5f,-0.5f, BACKZ ,
88
                                                         -0.5f,+0.5f, BACKZ ,
89
                                                         +0.5f,+0.5f, BACKZ  };
90

  
75 91
///////////////////////////////////////////////////////////////////////////////////////////////////
76 92
// a Block is split into two triangles along the NE-SW line iff it is in the top-right
77 93
// or bottom-left quadrant of the grid.
......
188 204
       dataLength = computeDataLength(frontOnly);
189 205

  
190 206
       remainingVert = dataLength;
207
       buildBoundingVert(mCubes,frontOnly);
191 208
       }
192 209
     }
193 210

  
......
213 230

  
214 231
       remainingVert = dataLength;
215 232
       }
233

  
234
     mBoundingVert = frontOnly ? mBoundingVert1 : mBoundingVert2;
216 235
     }
217 236

  
237
///////////////////////////////////////////////////////////////////////////////////////////////////
238

  
239
  private float retLeftmost(short[][] cubes, int row)
240
    {
241

  
242
    return 0.0f;
243
    }
244

  
245
///////////////////////////////////////////////////////////////////////////////////////////////////
246

  
247
 private float retRightmost(short[][] cubes, int row)
248
    {
249
    if( row==0 )
250
      {
251
      for(int i=0; i<mCols; i++)
252
        {
253

  
254
        }
255
      }
256
    else if(row==mRows)
257
      {
258
      for(int i=0; i<mCols; i++)
259
        {
260

  
261
        }
262
      }
263
    else
264
      {
265
      for(int i=0; i<mCols; i++)
266
        {
267

  
268
        }
269
      }
270

  
271
    return 0.0f;
272
    }
273

  
274
///////////////////////////////////////////////////////////////////////////////////////////////////
275

  
276
  private int addLeftmost(float[] temp, short[][] cubes, int row, int index)
277
    {
278
    float x2 = retLeftmost(cubes,row);
279
    float y2 = row/mRows - 0.5f;
280

  
281
    if( index>1 )
282
      {
283
      float x0 = temp[2*index-4];
284
      float y0 = temp[2*index-3];
285
      float x1 = temp[2*index-2];
286
      float y1 = temp[2*index-1];
287

  
288
      if( (x0-x2)*(y0-y1) <= (x0-x1)*(y0-y2) )
289
        {
290
        temp[2*index-2] = x2;
291
        temp[2*index-1] = y2;
292
        return index;
293
        }
294
      }
295

  
296
    temp[2*index+0] = x2;
297
    temp[2*index+1] = y2;
298
    return index+1;
299
    }
300

  
301
///////////////////////////////////////////////////////////////////////////////////////////////////
302

  
303
  private int addRightmost(float[] temp, short[][] cubes, int row, int index)
304
    {
305
    float x2 = retRightmost(cubes,row);
306
    float y2 = row/mRows - 0.5f;
307

  
308
    if( index>1 )
309
      {
310
      float x0 = temp[2*index-4];
311
      float y0 = temp[2*index-3];
312
      float x1 = temp[2*index-2];
313
      float y1 = temp[2*index-1];
314

  
315
      if( (x0-x2)*(y0-y1) >= (x0-x1)*(y0-y2) )
316
        {
317
        temp[2*index-2] = x2;
318
        temp[2*index-1] = y2;
319
        return index;
320
        }
321
      }
322

  
323
    temp[2*index+0] = x2;
324
    temp[2*index+1] = y2;
325
    return index+1;
326
    }
327

  
328
///////////////////////////////////////////////////////////////////////////////////////////////////
329
// fill up the mBoundingVert array with the smallest set of vertices which form the same convex hull
330
// like the whole thing.
331

  
332
  private void buildBoundingVert(short[][] cubes, boolean frontOnly)
333
    {
334
    int lVert=0, rVert=0;
335
    float[] tempL = new float[2*(mRows+1)];
336
    float[] tempR = new float[2*(mRows+1)];
337

  
338
    for(int i=0; i<=mRows; i++)
339
      {
340
      lVert = addLeftmost (tempL,cubes,i,lVert);
341
      rVert = addRightmost(tempR,cubes,i,rVert);
342
      }
343

  
344
    int numVert = lVert+rVert;
345

  
346
    mBoundingVert = new float[ numVert*(frontOnly ? 3:6) ];
347

  
348
    for(int i=0; i<lVert; i++)
349
      {
350
      mBoundingVert[3*i  ] = tempL[2*i  ];
351
      mBoundingVert[3*i+1] = tempL[2*i+1];
352
      mBoundingVert[3*i+2] = FRONTZ;
353
      }
354

  
355
    for(int i=0; i<rVert; i++)
356
      {
357
      mBoundingVert[3*(i+lVert)  ] = tempR[2*i  ];
358
      mBoundingVert[3*(i+lVert)+1] = tempR[2*i+1];
359
      mBoundingVert[3*(i+lVert)+2] = FRONTZ;
360
      }
361

  
362
    if( !frontOnly )
363
      {
364
      for(int i=0; i<numVert; i++)
365
        {
366
        mBoundingVert[3*(i+numVert)  ] = mBoundingVert[3*i  ];
367
        mBoundingVert[3*(i+numVert)+1] = mBoundingVert[3*i+1];
368
        mBoundingVert[3*(i+numVert)+2] = BACKZ;
369
        }
370
      }
371
    }
372

  
218 373
///////////////////////////////////////////////////////////////////////////////////////////////////
219 374
// Mark all the 'regions' of our grid  - i.e. separate pieces of 'land' (connected blocks that will 
220 375
// be rendered) and 'water' (connected holes in between) with integers. Each connected block of land
......
742 897

  
743 898
///////////////////////////////////////////////////////////////////////////////////////////////////
744 899

  
745
   boolean canUsePostprocessingShortcut()
746
      {
747
      return false;
748
      }
900
   float[] getBoundingVertices()
901
     {
902
     return mBoundingVert;
903
     }
749 904

  
750 905
///////////////////////////////////////////////////////////////////////////////////////////////////
751 906
// PUBLIC API
src/main/java/org/distorted/library/MeshFlat.java
34 34
  private int mCols, mRows;
35 35
  private int remainingVert;
36 36

  
37
  private static float[] mBoundingVert = new float[] { -0.5f,-0.5f,0.0f,
38
                                                       +0.5f,-0.5f,0.0f,
39
                                                       -0.5f,+0.5f,0.0f,
40
                                                       +0.5f,+0.5f,0.0f };
41

  
37 42
///////////////////////////////////////////////////////////////////////////////////////////////////
38 43
// Create a flat, full grid.
39 44

  
......
60 65

  
61 66
///////////////////////////////////////////////////////////////////////////////////////////////////
62 67

  
63
   private int addVertex(int vertex, int col, int row, float[] position, float[] normal, float[] texture)
68
   private int addVertex(int vertex, float x, float y, float[] position, float[] normal, float[] texture)
64 69
     {
65 70
     remainingVert--;
66 71

  
67
     float x= (float)col/mCols;
68
     float y= (float)row/mRows;
69

  
70 72
     position[3*vertex  ] = x-0.5f;
71 73
     position[3*vertex+1] = 0.5f-y;
72 74
     position[3*vertex+2] = 0;
......
115 117
     boolean currentBlockIsNE;
116 118
     int vertex = 0;
117 119

  
120
     float x,y;
121
     final float X = 1.0f/mCols;
122
     final float Y = 1.0f/mRows;
123

  
118 124
     //android.util.Log.d("BITMAP", "buildGrid");
119 125

  
126
     y = 0.0f;
127

  
120 128
     for(int row=0; row<mRows; row++)
121 129
       {
130
       x = 0.0f;
131

  
122 132
       for(int col=0; col<mCols; col++)
123 133
         {
124 134
         currentBlockIsNE = (2*row<=mRows-1)^(2*col<=mCols-1);
......
126 136
         if( col==0 || (lastBlockIsNE^currentBlockIsNE) )
127 137
           {
128 138
           if( row!=0 && col==0 ) vertex = repeatLast(vertex,position,normal,texture);
129
           vertex= addVertex( vertex, col, row+(currentBlockIsNE?0:1), position, normal, texture);
139
           vertex= addVertex( vertex, x, y+(currentBlockIsNE?0:Y), position, normal, texture);
130 140
           if( row!=0 && col==0 ) vertex = repeatLast(vertex,position,normal,texture);
131 141
           if( lastBlockIsNE^currentBlockIsNE)  vertex = repeatLast(vertex,position,normal,texture);
132
           vertex= addVertex( vertex, col, row+(currentBlockIsNE?1:0), position, normal, texture);
142
           vertex= addVertex( vertex, x, y+(currentBlockIsNE?Y:0), position, normal, texture);
133 143
           }
134
         vertex= addVertex( vertex, col+1, row+(currentBlockIsNE?0:1), position, normal, texture);
135
         vertex= addVertex( vertex, col+1, row+(currentBlockIsNE?1:0), position, normal, texture);
144
         vertex= addVertex( vertex, x+X, y+(currentBlockIsNE?0:Y), position, normal, texture);
145
         vertex= addVertex( vertex, x+X, y+(currentBlockIsNE?Y:0), position, normal, texture);
136 146

  
137 147
         lastBlockIsNE = currentBlockIsNE;
148
         x+=X;
138 149
         }
150

  
151
       y+=Y;
139 152
       }
140 153

  
141 154
     //android.util.Log.d("BITMAP", "buildGrid done");
......
159 172

  
160 173
///////////////////////////////////////////////////////////////////////////////////////////////////
161 174

  
162
   boolean canUsePostprocessingShortcut()
163
      {
164
      return true;
165
      }
175
   float[] getBoundingVertices()
176
     {
177
     return mBoundingVert;
178
     }
166 179

  
167 180
///////////////////////////////////////////////////////////////////////////////////////////////////
168 181
// PUBLIC API
src/main/java/org/distorted/library/MeshObject.java
50 50
     }
51 51

  
52 52
///////////////////////////////////////////////////////////////////////////////////////////////////
53

  
54
   abstract boolean canUsePostprocessingShortcut();
53
/**
54
 * Get the minimal set of Vertices which have the same convex hull as the whole set.
55
 * <p>
56
 * In case of Flat Meshes, the set is obviously just the 4 corners. In case of the Cubes Mesh,
57
 * it is a subset of the set of each rightmost- and leftmost- corners in each row.
58
 * <p>
59
 * This is used to be able to quickly compute, in window coordinates, the Mesh'es bounding rectangle.
60
 */
61
   abstract float[] getBoundingVertices();
55 62
   }

Also available in: Unified diff