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.mesh;
|
21
|
|
22
|
import android.opengl.GLES30;
|
23
|
import android.util.Log;
|
24
|
|
25
|
import org.distorted.library.effect.VertexEffect;
|
26
|
import org.distorted.library.effectqueue.EffectQueueVertex;
|
27
|
import org.distorted.library.main.DistortedLibrary;
|
28
|
import org.distorted.library.main.InternalBuffer;
|
29
|
import org.distorted.library.program.DistortedProgram;
|
30
|
import org.distorted.library.type.Static4D;
|
31
|
|
32
|
import java.nio.ByteBuffer;
|
33
|
import java.nio.ByteOrder;
|
34
|
import java.nio.FloatBuffer;
|
35
|
import java.util.ArrayList;
|
36
|
|
37
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
38
|
/**
|
39
|
* Abstract class which represents a Mesh, ie an array of vertices (rendered as a TRIANGLE_STRIP).
|
40
|
* <p>
|
41
|
* If you want to render to a particular shape, extend from here, construct a float array
|
42
|
* containing per-vertex attributes, and call back setAttribs().
|
43
|
*/
|
44
|
public abstract class MeshBase
|
45
|
{
|
46
|
// sizes of attributes of an individual vertex.
|
47
|
private static final int POS_DATA_SIZE= 3; // vertex coordinates: x,y,z
|
48
|
private static final int NOR_DATA_SIZE= 3; // normal vector: x,y,z
|
49
|
private static final int INF_DATA_SIZE= 3; // 'inflate' vector: x,y,z
|
50
|
private static final int TEX_DATA_SIZE= 2; // texture coordinates: s,t
|
51
|
|
52
|
static final int POS_ATTRIB = 0;
|
53
|
static final int NOR_ATTRIB = POS_DATA_SIZE;
|
54
|
static final int INF_ATTRIB = POS_DATA_SIZE + NOR_DATA_SIZE;
|
55
|
static final int TEX_ATTRIB = 0;
|
56
|
static final int VERT1_ATTRIBS= POS_DATA_SIZE + NOR_DATA_SIZE + INF_DATA_SIZE; // number of attributes of a vertex (the part changed by preapply)
|
57
|
static final int VERT2_ATTRIBS= TEX_DATA_SIZE; // number of attributes of a vertex (the 'preapply invariant' part)
|
58
|
static final int TRAN_ATTRIBS = POS_DATA_SIZE + NOR_DATA_SIZE + INF_DATA_SIZE; // number of attributes of a transform feedback vertex
|
59
|
|
60
|
private static final int BYTES_PER_FLOAT = 4;
|
61
|
|
62
|
private static final int OFFSET_POS = POS_ATTRIB*BYTES_PER_FLOAT;
|
63
|
private static final int OFFSET_NOR = NOR_ATTRIB*BYTES_PER_FLOAT;
|
64
|
private static final int OFFSET_INF = INF_ATTRIB*BYTES_PER_FLOAT;
|
65
|
private static final int OFFSET_TEX = TEX_ATTRIB*BYTES_PER_FLOAT;
|
66
|
private static final int TRAN_SIZE = TRAN_ATTRIBS*BYTES_PER_FLOAT;
|
67
|
private static final int VERT1_SIZE = VERT1_ATTRIBS*BYTES_PER_FLOAT;
|
68
|
private static final int VERT2_SIZE = VERT2_ATTRIBS*BYTES_PER_FLOAT;
|
69
|
|
70
|
private boolean mShowNormals; // when rendering this mesh, draw normal vectors?
|
71
|
private InternalBuffer mVBO1, mVBO2, mTFO; // main vertex buffer and transform feedback buffer
|
72
|
private int mNumVertices;
|
73
|
private float[] mVertAttribs1; // packed: PosX,PosY,PosZ, NorX,NorY,NorZ, InfX,InfY,InfZ
|
74
|
private float[] mVertAttribs2; // packed: TexS,TexT
|
75
|
private float mInflate;
|
76
|
private boolean mNeedsAdjustment;
|
77
|
|
78
|
private static class Component
|
79
|
{
|
80
|
private int mEndIndex;
|
81
|
private Static4D mTextureMap;
|
82
|
private EffectQueueVertex mQueue;
|
83
|
|
84
|
Component(int end)
|
85
|
{
|
86
|
mEndIndex = end;
|
87
|
mTextureMap= new Static4D(0,0,1,1);
|
88
|
mQueue = new EffectQueueVertex();
|
89
|
}
|
90
|
Component(Component original)
|
91
|
{
|
92
|
mEndIndex = original.mEndIndex;
|
93
|
|
94
|
float x = original.mTextureMap.get0();
|
95
|
float y = original.mTextureMap.get1();
|
96
|
float z = original.mTextureMap.get2();
|
97
|
float w = original.mTextureMap.get3();
|
98
|
mTextureMap = new Static4D(x,y,z,w);
|
99
|
mQueue = new EffectQueueVertex(original.mQueue);
|
100
|
}
|
101
|
|
102
|
void setMap(Static4D map)
|
103
|
{
|
104
|
mTextureMap.set(map.get0(),map.get1(),map.get2(),map.get3());
|
105
|
}
|
106
|
}
|
107
|
|
108
|
private ArrayList<Component> mComponent;
|
109
|
|
110
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
111
|
|
112
|
MeshBase()
|
113
|
{
|
114
|
mShowNormals = false;
|
115
|
mInflate = 0.0f;
|
116
|
mNeedsAdjustment = false;
|
117
|
mComponent = new ArrayList<>();
|
118
|
|
119
|
mVBO1= new InternalBuffer(GLES30.GL_ARRAY_BUFFER , GLES30.GL_STATIC_READ);
|
120
|
mVBO2= new InternalBuffer(GLES30.GL_ARRAY_BUFFER , GLES30.GL_STATIC_READ);
|
121
|
mTFO = new InternalBuffer(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, GLES30.GL_STATIC_READ);
|
122
|
}
|
123
|
|
124
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
125
|
// copy constructor
|
126
|
|
127
|
MeshBase(MeshBase original)
|
128
|
{
|
129
|
mShowNormals = original.mShowNormals;
|
130
|
mInflate = original.mInflate;
|
131
|
mNeedsAdjustment = original.mNeedsAdjustment;
|
132
|
|
133
|
int size = original.mComponent.size();
|
134
|
mComponent = new ArrayList<>();
|
135
|
for(int i=0; i<size; i++)
|
136
|
{
|
137
|
Component comp = new Component(original.mComponent.get(i));
|
138
|
mComponent.add(comp);
|
139
|
}
|
140
|
|
141
|
mVBO1= new InternalBuffer(GLES30.GL_ARRAY_BUFFER , GLES30.GL_STATIC_READ);
|
142
|
mVBO2= new InternalBuffer(GLES30.GL_ARRAY_BUFFER , GLES30.GL_STATIC_READ);
|
143
|
mTFO = new InternalBuffer(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, GLES30.GL_STATIC_READ);
|
144
|
|
145
|
mNumVertices = original.mNumVertices;
|
146
|
mVertAttribs1= new float[mNumVertices*VERT1_ATTRIBS];
|
147
|
mVertAttribs2= new float[mNumVertices*VERT2_ATTRIBS];
|
148
|
|
149
|
System.arraycopy(original.mVertAttribs1,0,mVertAttribs1,0,mNumVertices*VERT1_ATTRIBS);
|
150
|
System.arraycopy(original.mVertAttribs2,0,mVertAttribs2,0,mNumVertices*VERT2_ATTRIBS);
|
151
|
|
152
|
mVBO1.invalidate();
|
153
|
mVBO2.invalidate();
|
154
|
mTFO.invalidate();
|
155
|
}
|
156
|
|
157
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
158
|
|
159
|
int numComponents()
|
160
|
{
|
161
|
return mComponent.size();
|
162
|
}
|
163
|
|
164
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
165
|
// when a derived class is done computing its mesh, it has to call this method.
|
166
|
|
167
|
void setAttribs(float[] vert1Attribs, float[] vert2Attribs)
|
168
|
{
|
169
|
mNumVertices = vert1Attribs.length/VERT1_ATTRIBS;
|
170
|
mVertAttribs1= vert1Attribs;
|
171
|
mVertAttribs2= vert2Attribs;
|
172
|
|
173
|
mComponent.add(new Component(mNumVertices));
|
174
|
|
175
|
mVBO1.invalidate();
|
176
|
mVBO2.invalidate();
|
177
|
}
|
178
|
|
179
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
180
|
// called from MeshJoined
|
181
|
|
182
|
void join(MeshBase[] meshes)
|
183
|
{
|
184
|
MeshBase mesh;
|
185
|
Component comp;
|
186
|
int numComponents, numVertices, numMeshes = meshes.length;
|
187
|
int origVertices = mNumVertices;
|
188
|
|
189
|
// compute new numVertices; take care of Components
|
190
|
|
191
|
if( origVertices>0 )
|
192
|
{
|
193
|
numComponents = mComponent.size();
|
194
|
mNumVertices+= ( mNumVertices%2==1 ? 2:1 );
|
195
|
mComponent.get(numComponents-1).mEndIndex = mNumVertices;
|
196
|
}
|
197
|
|
198
|
for(int i=0; i<numMeshes; i++)
|
199
|
{
|
200
|
mesh = meshes[i];
|
201
|
numComponents = mesh.mComponent.size();
|
202
|
|
203
|
for(int j=0; j<numComponents; j++)
|
204
|
{
|
205
|
comp = new Component(mesh.mComponent.get(j));
|
206
|
comp.mEndIndex += mNumVertices;
|
207
|
mComponent.add(comp);
|
208
|
}
|
209
|
|
210
|
numVertices = mesh.mNumVertices;
|
211
|
|
212
|
if( mNumVertices==0 )
|
213
|
{
|
214
|
if( numMeshes>1 )
|
215
|
mNumVertices += (numVertices%2==1 ? numVertices+2 : numVertices+1);
|
216
|
else
|
217
|
mNumVertices += numVertices;
|
218
|
}
|
219
|
else if( i==numMeshes-1 ) mNumVertices += (numVertices+1);
|
220
|
else mNumVertices += (numVertices%2==1 ? numVertices+3 : numVertices+2);
|
221
|
}
|
222
|
|
223
|
// allocate new attrib array
|
224
|
float[] newAttribs1 = new float[VERT1_ATTRIBS*mNumVertices];
|
225
|
float[] newAttribs2 = new float[VERT2_ATTRIBS*mNumVertices];
|
226
|
numVertices = origVertices;
|
227
|
|
228
|
if( origVertices>0 )
|
229
|
{
|
230
|
System.arraycopy(mVertAttribs1, 0, newAttribs1, 0, VERT1_ATTRIBS*numVertices);
|
231
|
System.arraycopy(mVertAttribs2, 0, newAttribs2, 0, VERT2_ATTRIBS*numVertices);
|
232
|
System.arraycopy(mVertAttribs1, VERT1_ATTRIBS*(origVertices-1), newAttribs1, VERT1_ATTRIBS*origVertices, VERT1_ATTRIBS );
|
233
|
System.arraycopy(mVertAttribs2, VERT2_ATTRIBS*(origVertices-1), newAttribs2, VERT2_ATTRIBS*origVertices, VERT2_ATTRIBS );
|
234
|
origVertices++;
|
235
|
|
236
|
if( numVertices%2==1 )
|
237
|
{
|
238
|
System.arraycopy(mVertAttribs1, VERT1_ATTRIBS*(origVertices-1), newAttribs1, VERT1_ATTRIBS*origVertices, VERT1_ATTRIBS);
|
239
|
System.arraycopy(mVertAttribs2, VERT2_ATTRIBS*(origVertices-1), newAttribs2, VERT2_ATTRIBS*origVertices, VERT2_ATTRIBS);
|
240
|
origVertices++;
|
241
|
}
|
242
|
}
|
243
|
|
244
|
for(int i=0; i<numMeshes; i++)
|
245
|
{
|
246
|
mesh = meshes[i];
|
247
|
numVertices = mesh.mNumVertices;
|
248
|
|
249
|
if( origVertices>0 )
|
250
|
{
|
251
|
System.arraycopy(mesh.mVertAttribs1, 0, newAttribs1, VERT1_ATTRIBS*origVertices, VERT1_ATTRIBS );
|
252
|
System.arraycopy(mesh.mVertAttribs2, 0, newAttribs2, VERT2_ATTRIBS*origVertices, VERT2_ATTRIBS );
|
253
|
origVertices++;
|
254
|
}
|
255
|
System.arraycopy(mesh.mVertAttribs1, 0, newAttribs1, VERT1_ATTRIBS*origVertices, VERT1_ATTRIBS*numVertices);
|
256
|
System.arraycopy(mesh.mVertAttribs2, 0, newAttribs2, VERT2_ATTRIBS*origVertices, VERT2_ATTRIBS*numVertices);
|
257
|
origVertices+=numVertices;
|
258
|
|
259
|
if( i<numMeshes-1 )
|
260
|
{
|
261
|
System.arraycopy(mesh.mVertAttribs1, VERT1_ATTRIBS*(numVertices-1), newAttribs1, VERT1_ATTRIBS*origVertices, VERT1_ATTRIBS);
|
262
|
System.arraycopy(mesh.mVertAttribs2, VERT2_ATTRIBS*(numVertices-1), newAttribs2, VERT2_ATTRIBS*origVertices, VERT2_ATTRIBS);
|
263
|
origVertices++;
|
264
|
|
265
|
if( numVertices%2==1 )
|
266
|
{
|
267
|
System.arraycopy(mesh.mVertAttribs1, VERT1_ATTRIBS*(numVertices-1), newAttribs1, VERT1_ATTRIBS*origVertices, VERT1_ATTRIBS);
|
268
|
System.arraycopy(mesh.mVertAttribs2, VERT2_ATTRIBS*(numVertices-1), newAttribs2, VERT2_ATTRIBS*origVertices, VERT2_ATTRIBS);
|
269
|
origVertices++;
|
270
|
}
|
271
|
}
|
272
|
}
|
273
|
|
274
|
if( origVertices!=mNumVertices )
|
275
|
{
|
276
|
android.util.Log.e("mesh", "join: origVertices: "+origVertices+" numVertices: "+mNumVertices);
|
277
|
}
|
278
|
|
279
|
mVertAttribs1 = newAttribs1;
|
280
|
mVertAttribs2 = newAttribs2;
|
281
|
|
282
|
mVBO1.invalidate();
|
283
|
mVBO2.invalidate();
|
284
|
}
|
285
|
|
286
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
287
|
/**
|
288
|
* Not part of public API, do not document (public only because has to be used from the main package)
|
289
|
*
|
290
|
* @y.exclude
|
291
|
*/
|
292
|
public void copyTransformToVertex()
|
293
|
{
|
294
|
ByteBuffer buffer = (ByteBuffer)GLES30.glMapBufferRange( GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, 0,
|
295
|
TRAN_SIZE*mNumVertices, GLES30.GL_MAP_READ_BIT);
|
296
|
if( buffer!=null )
|
297
|
{
|
298
|
FloatBuffer feedback = buffer.order(ByteOrder.nativeOrder()).asFloatBuffer();
|
299
|
feedback.get(mVertAttribs1,0,VERT1_ATTRIBS*mNumVertices);
|
300
|
mVBO1.update(mVertAttribs1);
|
301
|
}
|
302
|
else
|
303
|
{
|
304
|
int error = GLES30.glGetError();
|
305
|
Log.e("mesh", "failed to map tf buffer, error="+error);
|
306
|
}
|
307
|
|
308
|
GLES30.glUnmapBuffer(GLES30.GL_TRANSFORM_FEEDBACK);
|
309
|
|
310
|
int numComp = mComponent.size();
|
311
|
|
312
|
for(int i=0; i<numComp; i++)
|
313
|
{
|
314
|
mComponent.get(i).mQueue.removeAll(false);
|
315
|
}
|
316
|
}
|
317
|
|
318
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
319
|
/**
|
320
|
* Not part of public API, do not document (public only because has to be used from the main package)
|
321
|
*
|
322
|
* @y.exclude
|
323
|
*/
|
324
|
public void computeQueue()
|
325
|
{
|
326
|
int numComp = mComponent.size();
|
327
|
|
328
|
for(int i=0; i<numComp; i++)
|
329
|
{
|
330
|
mComponent.get(i).mQueue.compute(1);
|
331
|
}
|
332
|
}
|
333
|
|
334
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
335
|
/**
|
336
|
* Not part of public API, do not document (public only because has to be used from the main package)
|
337
|
*
|
338
|
* @y.exclude
|
339
|
*/
|
340
|
public void sendQueue()
|
341
|
{
|
342
|
int numComp = mComponent.size();
|
343
|
|
344
|
for(int i=0; i<numComp; i++)
|
345
|
{
|
346
|
mComponent.get(i).mQueue.send(0.0f,3);
|
347
|
}
|
348
|
}
|
349
|
|
350
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
351
|
/**
|
352
|
* Not part of public API, do not document (public only because has to be used from the main package)
|
353
|
*
|
354
|
* @y.exclude
|
355
|
*/
|
356
|
public int getTFO()
|
357
|
{
|
358
|
return mTFO.createImmediately(mNumVertices*TRAN_SIZE, null);
|
359
|
}
|
360
|
|
361
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
362
|
/**
|
363
|
* Not part of public API, do not document (public only because has to be used from the main package)
|
364
|
*
|
365
|
* @y.exclude
|
366
|
*/
|
367
|
public int getNumVertices()
|
368
|
{
|
369
|
return mNumVertices;
|
370
|
}
|
371
|
|
372
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
373
|
/**
|
374
|
* Not part of public API, do not document (public only because has to be used from the main package)
|
375
|
*
|
376
|
* @y.exclude
|
377
|
*/
|
378
|
public void bindVertexAttribs(DistortedProgram program)
|
379
|
{
|
380
|
if( mNeedsAdjustment )
|
381
|
{
|
382
|
mNeedsAdjustment = false;
|
383
|
DistortedLibrary.adjustVertices(this);
|
384
|
}
|
385
|
|
386
|
int index1 = mVBO1.createImmediately(mNumVertices*VERT1_SIZE, mVertAttribs1);
|
387
|
int index2 = mVBO2.createImmediately(mNumVertices*VERT2_SIZE, mVertAttribs2);
|
388
|
|
389
|
GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, index1 );
|
390
|
GLES30.glVertexAttribPointer(program.mAttribute[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, VERT1_SIZE, OFFSET_POS);
|
391
|
GLES30.glVertexAttribPointer(program.mAttribute[1], NOR_DATA_SIZE, GLES30.GL_FLOAT, false, VERT1_SIZE, OFFSET_NOR);
|
392
|
GLES30.glVertexAttribPointer(program.mAttribute[2], INF_DATA_SIZE, GLES30.GL_FLOAT, false, VERT1_SIZE, OFFSET_INF);
|
393
|
GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, index2 );
|
394
|
GLES30.glVertexAttribPointer(program.mAttribute[3], TEX_DATA_SIZE, GLES30.GL_FLOAT, false, VERT2_SIZE, OFFSET_TEX);
|
395
|
GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, 0);
|
396
|
}
|
397
|
|
398
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
399
|
/**
|
400
|
* Not part of public API, do not document (public only because has to be used from the main package)
|
401
|
*
|
402
|
* @y.exclude
|
403
|
*/
|
404
|
public void bindTransformAttribs(DistortedProgram program)
|
405
|
{
|
406
|
int index = mTFO.createImmediately(mNumVertices*TRAN_SIZE, null);
|
407
|
|
408
|
GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, index );
|
409
|
GLES30.glVertexAttribPointer(program.mAttribute[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, 0, 0);
|
410
|
GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, 0);
|
411
|
}
|
412
|
|
413
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
414
|
/**
|
415
|
* Not part of public API, do not document (public only because has to be used from the main package)
|
416
|
*
|
417
|
* @y.exclude
|
418
|
*/
|
419
|
public void setInflate(float inflate)
|
420
|
{
|
421
|
mInflate = inflate;
|
422
|
}
|
423
|
|
424
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
425
|
/**
|
426
|
* Not part of public API, do not document (public only because has to be used from the main package)
|
427
|
*
|
428
|
* @y.exclude
|
429
|
*/
|
430
|
public float getInflate()
|
431
|
{
|
432
|
return mInflate;
|
433
|
}
|
434
|
|
435
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
436
|
// PUBLIC API
|
437
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
438
|
/**
|
439
|
* When rendering this Mesh, do we want to render the Normal vectors as well?
|
440
|
* <p>
|
441
|
* Will work only on OpenGL ES >= 3.0 devices.
|
442
|
*
|
443
|
* @param show Controls if we render the Normal vectors or not.
|
444
|
*/
|
445
|
public void setShowNormals(boolean show)
|
446
|
{
|
447
|
mShowNormals = show;
|
448
|
}
|
449
|
|
450
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
451
|
/**
|
452
|
* When rendering this mesh, should we also draw the normal vectors?
|
453
|
*
|
454
|
* @return <i>true</i> if we do render normal vectors
|
455
|
*/
|
456
|
public boolean getShowNormals()
|
457
|
{
|
458
|
return mShowNormals;
|
459
|
}
|
460
|
|
461
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
462
|
/**
|
463
|
* Release all internal resources.
|
464
|
*/
|
465
|
public void markForDeletion()
|
466
|
{
|
467
|
mVertAttribs1 = null;
|
468
|
mVertAttribs2 = null;
|
469
|
|
470
|
mVBO1.markForDeletion();
|
471
|
mVBO2.markForDeletion();
|
472
|
mTFO.markForDeletion();
|
473
|
}
|
474
|
|
475
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
476
|
/**
|
477
|
* Apply a Vertex Effect to the vertex mesh.
|
478
|
* <p>
|
479
|
* This is a static, permanent modification of the vertices contained in this Mesh. If the effects
|
480
|
* contain any Dynamics, the Dynamics will be evaluated at 0.
|
481
|
*
|
482
|
* We would call this several times building up a list of Effects to do. This list of effects gets
|
483
|
* lazily executed only when the Mesh is used for rendering for the first time.
|
484
|
*
|
485
|
* @param effect Vertex Effect to apply to the Mesh.
|
486
|
*/
|
487
|
public void apply(VertexEffect effect)
|
488
|
{
|
489
|
int numComp = mComponent.size();
|
490
|
|
491
|
for(int i=0; i<numComp; i++)
|
492
|
{
|
493
|
mComponent.get(i).mQueue.add(effect);
|
494
|
}
|
495
|
|
496
|
mNeedsAdjustment = true;
|
497
|
}
|
498
|
|
499
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
500
|
/**
|
501
|
* Sets texture maps for (some of) the components of this mesh.
|
502
|
* <p>
|
503
|
* Format: ( x of lower-left corner, y of lower-left corner, width, height ).
|
504
|
* For example maps[0] = new Static4D( 0.0, 0.5, 0.5, 0.5 ) sets the 0th component texture map to the
|
505
|
* upper-left quadrant of the texture.
|
506
|
* <p>
|
507
|
* Probably the most common user case would be sending as many maps as there are components in this
|
508
|
* Mesh. One can also send less, or more (the extraneous ones will be ignored) and set some of them
|
509
|
* to null (those will be ignored as well). So if there are 5 components, and we want to set the map
|
510
|
* of the 2nd and 4rd one, call this with
|
511
|
* maps = new Static4D[4]
|
512
|
* maps[0] = null
|
513
|
* maps[1] = the map for the 2nd component
|
514
|
* maps[2] = null
|
515
|
* maps[3] = the map for the 4th component
|
516
|
*
|
517
|
* A map's width and height have to be non-zero (but can be negative!)
|
518
|
*
|
519
|
* @param maps List of texture maps to apply to the texture's components.
|
520
|
*/
|
521
|
public void setTextureMap(Static4D[] maps)
|
522
|
{
|
523
|
int num_comp = mComponent.size();
|
524
|
int num_maps = maps.length;
|
525
|
int min = Math.min(num_comp, num_maps);
|
526
|
int vertex = 0;
|
527
|
Static4D newMap, oldMap;
|
528
|
Component comp;
|
529
|
float newW, newH, ratW, ratH, movX, movY;
|
530
|
|
531
|
for(int i=0; i<min; i++)
|
532
|
{
|
533
|
newMap = maps[i];
|
534
|
comp = mComponent.get(i);
|
535
|
|
536
|
if( newMap!=null )
|
537
|
{
|
538
|
newW = newMap.get2();
|
539
|
newH = newMap.get3();
|
540
|
|
541
|
if( newW!=0.0f && newH!=0.0f )
|
542
|
{
|
543
|
oldMap = comp.mTextureMap;
|
544
|
ratW = newW/oldMap.get2();
|
545
|
ratH = newH/oldMap.get3();
|
546
|
movX = newMap.get0() - ratW*oldMap.get0();
|
547
|
movY = newMap.get1() - ratH*oldMap.get1();
|
548
|
|
549
|
for( int index=vertex*VERT2_ATTRIBS+TEX_ATTRIB ; vertex<=comp.mEndIndex; vertex++, index+=VERT2_ATTRIBS)
|
550
|
{
|
551
|
mVertAttribs2[index ] = ratW*mVertAttribs2[index ] + movX;
|
552
|
mVertAttribs2[index+1] = ratH*mVertAttribs2[index+1] + movY;
|
553
|
}
|
554
|
comp.setMap(newMap);
|
555
|
}
|
556
|
}
|
557
|
|
558
|
vertex= comp.mEndIndex+1;
|
559
|
}
|
560
|
|
561
|
mVBO2.invalidate();
|
562
|
}
|
563
|
|
564
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
565
|
/**
|
566
|
* Return the texture map of one of the components.
|
567
|
*
|
568
|
* @param component The component number whose texture map we want to retrieve.
|
569
|
*/
|
570
|
public Static4D getTextureMap(int component)
|
571
|
{
|
572
|
return (component>=0 && component<mComponent.size()) ? mComponent.get(component).mTextureMap : null;
|
573
|
}
|
574
|
|
575
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
576
|
/**
|
577
|
* Deep copy
|
578
|
*/
|
579
|
public abstract MeshBase deepCopy();
|
580
|
}
|
581
|
|
582
|
|
583
|
|