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