Revision a8dfedcc
Added by Leszek Koltunski over 4 years ago
src/main/java/org/distorted/library/mesh/DeferredJobs.java | ||
---|---|---|
45 | 45 |
private static class Job |
46 | 46 |
{ |
47 | 47 |
private int mType; |
48 |
private int mArg; |
|
48 | 49 |
private MeshBase mTarget; |
49 | 50 |
private MeshBase[] mSource; |
50 | 51 |
private EffectQueueVertex mEffects; |
51 | 52 |
|
52 |
Job(int type, MeshBase target, MeshBase[] source, VertexEffect effect) |
|
53 |
Job(int type, MeshBase target, MeshBase[] source, VertexEffect effect, int arg)
|
|
53 | 54 |
{ |
54 | 55 |
mType = type; |
55 | 56 |
mTarget = target; |
56 | 57 |
mSource = source; |
58 |
mArg = arg; |
|
57 | 59 |
mEffects= new EffectQueueVertex(); |
58 | 60 |
mEffects.add(effect); |
59 | 61 |
} |
60 | 62 |
|
61 |
Job(Job job) |
|
62 |
{ |
|
63 |
mType = job.mType; |
|
64 |
mTarget = job.mTarget; |
|
65 |
mSource = job.mSource; |
|
66 |
mEffects= new EffectQueueVertex(job.mEffects); |
|
67 |
} |
|
68 |
|
|
69 | 63 |
void addEffect(VertexEffect effect) |
70 | 64 |
{ |
71 | 65 |
mEffects.add(effect); |
... | ... | |
77 | 71 |
{ |
78 | 72 |
case JOB_TYPE_VERTEX: DistortedLibrary.adjustVertices(mTarget, mEffects); |
79 | 73 |
break; |
80 |
case JOB_TYPE_MERGE : break; |
|
81 |
case JOB_TYPE_JOIN : break; |
|
74 |
case JOB_TYPE_MERGE : mTarget.merge(); |
|
75 |
break; |
|
76 |
case JOB_TYPE_JOIN : mTarget.joinAttrib1(mSource,mArg); |
|
77 |
break; |
|
82 | 78 |
case JOB_TYPE_COPY : mTarget.deepCopyAttribs1(mSource[0]); |
83 | 79 |
break; |
84 | 80 |
} |
... | ... | |
105 | 101 |
mJob = job; |
106 | 102 |
} |
107 | 103 |
|
108 |
JobNode(JobNode node) |
|
109 |
{ |
|
110 |
mPrevJobs = new ArrayList<>(); |
|
111 |
mNextJobs = new ArrayList<>(); |
|
112 |
mJob = new Job(node.mJob); |
|
113 |
|
|
114 |
int numPrev = node.mPrevJobs.size(); |
|
115 |
|
|
116 |
for(int i=0; i<numPrev; i++) |
|
117 |
{ |
|
118 |
mPrevJobs.add(node.mPrevJobs.get(i)); |
|
119 |
} |
|
120 |
|
|
121 |
int numNext = node.mNextJobs.size(); |
|
122 |
|
|
123 |
for(int i=0; i<numNext; i++) |
|
124 |
{ |
|
125 |
mNextJobs.add(node.mNextJobs.get(i)); |
|
126 |
} |
|
127 |
} |
|
128 |
|
|
129 | 104 |
void execute() |
130 | 105 |
{ |
131 | 106 |
JobNode node; |
... | ... | |
187 | 162 |
|
188 | 163 |
if( jn==null ) |
189 | 164 |
{ |
190 |
Job job = new Job(JOB_TYPE_VERTEX,target,null,effect); |
|
165 |
Job job = new Job(JOB_TYPE_VERTEX,target,null,effect,0);
|
|
191 | 166 |
JobNode node = new JobNode(job); |
192 | 167 |
mJobs.add(node); |
193 | 168 |
return node; |
... | ... | |
201 | 176 |
} |
202 | 177 |
else |
203 | 178 |
{ |
204 |
Job job = new Job(JOB_TYPE_VERTEX,target,null,effect); |
|
179 |
Job job = new Job(JOB_TYPE_VERTEX,target,null,effect,0);
|
|
205 | 180 |
JobNode node = new JobNode(job); |
206 | 181 |
node.mPrevJobs.add(jn); |
207 | 182 |
jn.mNextJobs.add(node); |
... | ... | |
215 | 190 |
|
216 | 191 |
static JobNode merge(MeshBase target) |
217 | 192 |
{ |
218 |
return null; |
|
193 |
JobNode jn = target.mJobNode[0]; |
|
194 |
|
|
195 |
if( jn==null ) |
|
196 |
{ |
|
197 |
android.util.Log.e("deferredJobs", "Error: trying to add a merge job to an empty graph!"); |
|
198 |
return jn; |
|
199 |
} |
|
200 |
else |
|
201 |
{ |
|
202 |
if( jn.mJob.mType==JOB_TYPE_MERGE ) |
|
203 |
{ |
|
204 |
android.util.Log.e("deferredJobs", "Error: trying to add a second consecutive merge job"); |
|
205 |
return jn; |
|
206 |
} |
|
207 |
else |
|
208 |
{ |
|
209 |
Job job = new Job(JOB_TYPE_MERGE,target,null,null,0); |
|
210 |
JobNode node = new JobNode(job); |
|
211 |
node.mPrevJobs.add(jn); |
|
212 |
jn.mNextJobs.add(node); |
|
213 |
mJobs.add(node); |
|
214 |
return node; |
|
215 |
} |
|
216 |
} |
|
219 | 217 |
} |
220 | 218 |
|
221 | 219 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
222 | 220 |
|
223 |
static JobNode join(MeshBase target, MeshBase[] meshes) |
|
221 |
static JobNode join(MeshBase target, MeshBase[] meshes, int origVertices)
|
|
224 | 222 |
{ |
225 |
return null; |
|
223 |
JobNode jn = target.mJobNode[0]; |
|
224 |
|
|
225 |
if( jn==null ) |
|
226 |
{ |
|
227 |
android.util.Log.e("deferredJobs", "Error: trying to add a join job to an empty graph!"); |
|
228 |
return jn; |
|
229 |
} |
|
230 |
else |
|
231 |
{ |
|
232 |
if( jn.mJob.mType==JOB_TYPE_JOIN ) |
|
233 |
{ |
|
234 |
android.util.Log.e("deferredJobs", "Error: trying to add a second consecutive join job"); |
|
235 |
return jn; |
|
236 |
} |
|
237 |
else |
|
238 |
{ |
|
239 |
Job job = new Job(JOB_TYPE_JOIN,target,meshes,null,origVertices); |
|
240 |
JobNode node = new JobNode(job); |
|
241 |
node.mPrevJobs.add(jn); |
|
242 |
jn.mNextJobs.add(node); |
|
243 |
mJobs.add(node); |
|
244 |
return node; |
|
245 |
} |
|
246 |
} |
|
226 | 247 |
} |
227 | 248 |
|
228 | 249 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
229 | 250 |
|
230 | 251 |
static JobNode copy(MeshBase target, MeshBase mesh) |
231 | 252 |
{ |
232 |
return null; |
|
253 |
JobNode jn = target.mJobNode[0]; |
|
254 |
|
|
255 |
if( jn==null ) |
|
256 |
{ |
|
257 |
android.util.Log.e("deferredJobs", "Error: trying to add a copy job to an empty graph!"); |
|
258 |
return jn; |
|
259 |
} |
|
260 |
else |
|
261 |
{ |
|
262 |
if( jn.mJob.mType==JOB_TYPE_COPY ) |
|
263 |
{ |
|
264 |
android.util.Log.e("deferredJobs", "Error: trying to add a second consecutive copy job"); |
|
265 |
return jn; |
|
266 |
} |
|
267 |
else |
|
268 |
{ |
|
269 |
MeshBase[] meshes = new MeshBase[1]; |
|
270 |
meshes[0] = mesh; |
|
271 |
Job job = new Job(JOB_TYPE_COPY,target,meshes,null,0); |
|
272 |
JobNode node = new JobNode(job); |
|
273 |
node.mPrevJobs.add(jn); |
|
274 |
jn.mNextJobs.add(node); |
|
275 |
mJobs.add(node); |
|
276 |
return node; |
|
277 |
} |
|
278 |
} |
|
233 | 279 |
} |
234 | 280 |
|
235 | 281 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
src/main/java/org/distorted/library/mesh/MeshBase.java | ||
---|---|---|
208 | 208 |
mVBO1.invalidate(); |
209 | 209 |
} |
210 | 210 |
|
211 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
212 |
|
|
213 |
void merge() |
|
214 |
{ |
|
215 |
int num = mComponent.size(); |
|
216 |
|
|
217 |
if( num>1 ) |
|
218 |
{ |
|
219 |
mComponent.clear(); |
|
220 |
mComponent.add(new Component(mNumVertices-1)); |
|
221 |
|
|
222 |
for(int index=0; index<mNumVertices; index++) |
|
223 |
{ |
|
224 |
mVertAttribs2[VERT2_ATTRIBS*index+COM_ATTRIB] = 0; |
|
225 |
} |
|
226 |
|
|
227 |
mVBO2.invalidate(); |
|
228 |
} |
|
229 |
} |
|
230 |
|
|
211 | 231 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
212 | 232 |
|
213 | 233 |
int numComponents() |
... | ... | |
231 | 251 |
} |
232 | 252 |
|
233 | 253 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
234 |
// called from MeshJoined |
|
235 | 254 |
|
236 |
void join(MeshBase[] meshes)
|
|
255 |
void joinAttrib1(MeshBase[] meshes, int origVertices)
|
|
237 | 256 |
{ |
238 | 257 |
MeshBase mesh; |
239 |
Component comp; |
|
240 |
int origComponents=0, numComponents, numVertices, numMeshes = meshes.length; |
|
241 |
int origVertices = mNumVertices; |
|
258 |
int numMeshes = meshes.length; |
|
259 |
int numVertices = origVertices; |
|
242 | 260 |
|
243 |
// compute new numVertices; take care of Components
|
|
261 |
float[] newAttribs1 = new float[VERT1_ATTRIBS*mNumVertices];
|
|
244 | 262 |
|
245 | 263 |
if( origVertices>0 ) |
246 | 264 |
{ |
247 |
origComponents = mComponent.size(); |
|
248 |
mNumVertices+= ( mNumVertices%2==1 ? 2:1 ); |
|
249 |
mComponent.get(origComponents-1).mEndIndex = mNumVertices-1; |
|
265 |
System.arraycopy(mVertAttribs1, 0, newAttribs1, 0, VERT1_ATTRIBS*numVertices); |
|
266 |
System.arraycopy(mVertAttribs1, VERT1_ATTRIBS*(origVertices-1), newAttribs1, VERT1_ATTRIBS*origVertices, VERT1_ATTRIBS ); |
|
267 |
origVertices++; |
|
268 |
|
|
269 |
if( numVertices%2==1 ) |
|
270 |
{ |
|
271 |
System.arraycopy(mVertAttribs1, VERT1_ATTRIBS*(origVertices-1), newAttribs1, VERT1_ATTRIBS*origVertices, VERT1_ATTRIBS); |
|
272 |
origVertices++; |
|
273 |
} |
|
250 | 274 |
} |
251 | 275 |
|
252 | 276 |
for(int i=0; i<numMeshes; i++) |
253 | 277 |
{ |
254 | 278 |
mesh = meshes[i]; |
255 |
numComponents = mesh.mComponent.size(); |
|
256 |
|
|
257 | 279 |
numVertices = mesh.mNumVertices; |
258 | 280 |
|
259 |
int extraVerticesBefore = mNumVertices==0 ? 0:1; |
|
260 |
int extraVerticesAfter = (i==numMeshes-1) ? 0 : (numVertices%2==1 ? 2:1); |
|
281 |
if( origVertices>0 ) |
|
282 |
{ |
|
283 |
System.arraycopy(mesh.mVertAttribs1, 0, newAttribs1, VERT1_ATTRIBS*origVertices, VERT1_ATTRIBS ); |
|
284 |
origVertices++; |
|
285 |
} |
|
286 |
System.arraycopy(mesh.mVertAttribs1, 0, newAttribs1, VERT1_ATTRIBS*origVertices, VERT1_ATTRIBS*numVertices); |
|
287 |
origVertices+=numVertices; |
|
261 | 288 |
|
262 |
for(int j=0; j<numComponents; j++)
|
|
289 |
if( i<numMeshes-1 )
|
|
263 | 290 |
{ |
264 |
comp = new Component(mesh.mComponent.get(j)); |
|
265 |
comp.mEndIndex += (extraVerticesBefore+mNumVertices+extraVerticesAfter); |
|
266 |
mComponent.add(comp); |
|
291 |
System.arraycopy(mesh.mVertAttribs1, VERT1_ATTRIBS*(numVertices-1), newAttribs1, VERT1_ATTRIBS*origVertices, VERT1_ATTRIBS); |
|
292 |
origVertices++; |
|
267 | 293 |
|
268 |
if( origComponents<MAX_COMPONENTS )
|
|
294 |
if( numVertices%2==1 )
|
|
269 | 295 |
{ |
270 |
mAssociation[origComponents] = mesh.mAssociation[j];
|
|
271 |
origComponents++;
|
|
296 |
System.arraycopy(mesh.mVertAttribs1, VERT1_ATTRIBS*(numVertices-1), newAttribs1, VERT1_ATTRIBS*origVertices, VERT1_ATTRIBS);
|
|
297 |
origVertices++;
|
|
272 | 298 |
} |
273 | 299 |
} |
274 |
|
|
275 |
mNumVertices += (extraVerticesBefore+numVertices+extraVerticesAfter); |
|
276 | 300 |
} |
277 | 301 |
|
278 |
// allocate new attrib array |
|
279 |
float[] newAttribs1 = new float[VERT1_ATTRIBS*mNumVertices]; |
|
302 |
mVertAttribs1 = newAttribs1; |
|
303 |
mVBO1.invalidate(); |
|
304 |
} |
|
305 |
|
|
306 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
307 |
|
|
308 |
private void joinAttrib2(MeshBase[] meshes, int origVertices) |
|
309 |
{ |
|
310 |
MeshBase mesh; |
|
311 |
int numMeshes = meshes.length; |
|
312 |
int numVertices = origVertices; |
|
313 |
|
|
280 | 314 |
float[] newAttribs2 = new float[VERT2_ATTRIBS*mNumVertices]; |
281 |
numVertices = origVertices; |
|
282 | 315 |
|
283 | 316 |
if( origVertices>0 ) |
284 | 317 |
{ |
285 |
System.arraycopy(mVertAttribs1, 0, newAttribs1, 0, VERT1_ATTRIBS*numVertices); |
|
286 | 318 |
System.arraycopy(mVertAttribs2, 0, newAttribs2, 0, VERT2_ATTRIBS*numVertices); |
287 |
System.arraycopy(mVertAttribs1, VERT1_ATTRIBS*(origVertices-1), newAttribs1, VERT1_ATTRIBS*origVertices, VERT1_ATTRIBS ); |
|
288 | 319 |
System.arraycopy(mVertAttribs2, VERT2_ATTRIBS*(origVertices-1), newAttribs2, VERT2_ATTRIBS*origVertices, VERT2_ATTRIBS ); |
289 | 320 |
origVertices++; |
290 | 321 |
|
291 | 322 |
if( numVertices%2==1 ) |
292 | 323 |
{ |
293 |
System.arraycopy(mVertAttribs1, VERT1_ATTRIBS*(origVertices-1), newAttribs1, VERT1_ATTRIBS*origVertices, VERT1_ATTRIBS); |
|
294 | 324 |
System.arraycopy(mVertAttribs2, VERT2_ATTRIBS*(origVertices-1), newAttribs2, VERT2_ATTRIBS*origVertices, VERT2_ATTRIBS); |
295 | 325 |
origVertices++; |
296 | 326 |
} |
... | ... | |
303 | 333 |
|
304 | 334 |
if( origVertices>0 ) |
305 | 335 |
{ |
306 |
System.arraycopy(mesh.mVertAttribs1, 0, newAttribs1, VERT1_ATTRIBS*origVertices, VERT1_ATTRIBS ); |
|
307 | 336 |
System.arraycopy(mesh.mVertAttribs2, 0, newAttribs2, VERT2_ATTRIBS*origVertices, VERT2_ATTRIBS ); |
308 | 337 |
origVertices++; |
309 | 338 |
} |
310 |
System.arraycopy(mesh.mVertAttribs1, 0, newAttribs1, VERT1_ATTRIBS*origVertices, VERT1_ATTRIBS*numVertices); |
|
311 | 339 |
System.arraycopy(mesh.mVertAttribs2, 0, newAttribs2, VERT2_ATTRIBS*origVertices, VERT2_ATTRIBS*numVertices); |
312 | 340 |
origVertices+=numVertices; |
313 | 341 |
|
314 | 342 |
if( i<numMeshes-1 ) |
315 | 343 |
{ |
316 |
System.arraycopy(mesh.mVertAttribs1, VERT1_ATTRIBS*(numVertices-1), newAttribs1, VERT1_ATTRIBS*origVertices, VERT1_ATTRIBS); |
|
317 | 344 |
System.arraycopy(mesh.mVertAttribs2, VERT2_ATTRIBS*(numVertices-1), newAttribs2, VERT2_ATTRIBS*origVertices, VERT2_ATTRIBS); |
318 | 345 |
origVertices++; |
319 | 346 |
|
320 | 347 |
if( numVertices%2==1 ) |
321 | 348 |
{ |
322 |
System.arraycopy(mesh.mVertAttribs1, VERT1_ATTRIBS*(numVertices-1), newAttribs1, VERT1_ATTRIBS*origVertices, VERT1_ATTRIBS); |
|
323 | 349 |
System.arraycopy(mesh.mVertAttribs2, VERT2_ATTRIBS*(numVertices-1), newAttribs2, VERT2_ATTRIBS*origVertices, VERT2_ATTRIBS); |
324 | 350 |
origVertices++; |
325 | 351 |
} |
... | ... | |
340 | 366 |
for( ; index<=endIndex; index++) newAttribs2[VERT2_ATTRIBS*index+COM_ATTRIB] = component; |
341 | 367 |
} |
342 | 368 |
|
343 |
mVertAttribs1 = newAttribs1; |
|
344 | 369 |
mVertAttribs2 = newAttribs2; |
345 |
|
|
346 |
mVBO1.invalidate(); |
|
347 | 370 |
mVBO2.invalidate(); |
348 | 371 |
} |
349 | 372 |
|
373 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
374 |
// called from MeshJoined |
|
375 |
|
|
376 |
void join(MeshBase[] meshes) |
|
377 |
{ |
|
378 |
MeshBase mesh; |
|
379 |
Component comp; |
|
380 |
int origComponents=0, numComponents, numVertices, numMeshes = meshes.length; |
|
381 |
int origVertices = mNumVertices; |
|
382 |
|
|
383 |
if( origVertices>0 ) |
|
384 |
{ |
|
385 |
origComponents = mComponent.size(); |
|
386 |
mNumVertices+= ( mNumVertices%2==1 ? 2:1 ); |
|
387 |
mComponent.get(origComponents-1).mEndIndex = mNumVertices-1; |
|
388 |
} |
|
389 |
|
|
390 |
for(int i=0; i<numMeshes; i++) |
|
391 |
{ |
|
392 |
mesh = meshes[i]; |
|
393 |
numComponents = mesh.mComponent.size(); |
|
394 |
|
|
395 |
numVertices = mesh.mNumVertices; |
|
396 |
|
|
397 |
int extraVerticesBefore = mNumVertices==0 ? 0:1; |
|
398 |
int extraVerticesAfter = (i==numMeshes-1) ? 0 : (numVertices%2==1 ? 2:1); |
|
399 |
|
|
400 |
for(int j=0; j<numComponents; j++) |
|
401 |
{ |
|
402 |
comp = new Component(mesh.mComponent.get(j)); |
|
403 |
comp.mEndIndex += (extraVerticesBefore+mNumVertices+extraVerticesAfter); |
|
404 |
mComponent.add(comp); |
|
405 |
|
|
406 |
if( origComponents<MAX_COMPONENTS ) |
|
407 |
{ |
|
408 |
mAssociation[origComponents] = mesh.mAssociation[j]; |
|
409 |
origComponents++; |
|
410 |
} |
|
411 |
} |
|
412 |
|
|
413 |
mNumVertices += (extraVerticesBefore+numVertices+extraVerticesAfter); |
|
414 |
} |
|
415 |
|
|
416 |
if( mJobNode[0]!=null ) |
|
417 |
{ |
|
418 |
mJobNode[0] = DeferredJobs.join(this,meshes,origVertices); |
|
419 |
} |
|
420 |
else |
|
421 |
{ |
|
422 |
joinAttrib1(meshes,origVertices); |
|
423 |
} |
|
424 |
|
|
425 |
joinAttrib2(meshes,origVertices); |
|
426 |
} |
|
427 |
|
|
350 | 428 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
351 | 429 |
/** |
352 | 430 |
* Not part of public API, do not document (public only because has to be used from the main package) |
... | ... | |
494 | 572 |
return mShowNormals; |
495 | 573 |
} |
496 | 574 |
|
575 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
576 |
/** |
|
577 |
* Merge all components of this Mesh into a single one. |
|
578 |
*/ |
|
579 |
public void mergeComponents() |
|
580 |
{ |
|
581 |
if( mJobNode==null ) |
|
582 |
{ |
|
583 |
merge(); |
|
584 |
} |
|
585 |
else |
|
586 |
{ |
|
587 |
mJobNode[0] = DeferredJobs.merge(this); |
|
588 |
} |
|
589 |
} |
|
590 |
|
|
497 | 591 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
498 | 592 |
/** |
499 | 593 |
* Release all internal resources. |
Also available in: Unified diff
Progress with DeferredJobs (apply, copy, merge & join should be working now - untested)