Project

General

Profile

Download (21.2 KB) Statistics
| Branch: | Revision:

library / src / main / java / org / distorted / library / mesh / MeshBase.java @ 0d4aae88

1 d333eb6b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4 46b572b5 Leszek Koltunski
// This file is part of Distorted.                                                               //
5 d333eb6b Leszek Koltunski
//                                                                                               //
6 46b572b5 Leszek Koltunski
// Distorted is free software: you can redistribute it and/or modify                             //
7 d333eb6b Leszek Koltunski
// 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 46b572b5 Leszek Koltunski
// Distorted is distributed in the hope that it will be useful,                                  //
12 d333eb6b Leszek Koltunski
// 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 46b572b5 Leszek Koltunski
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18 d333eb6b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
19
20 6c00149d Leszek Koltunski
package org.distorted.library.mesh;
21 6a06a912 Leszek Koltunski
22 e6519ac8 Leszek Koltunski
import android.opengl.GLES31;
23 c90aca24 Leszek Koltunski
import android.opengl.Matrix;
24 6c00149d Leszek Koltunski
25 fa8bc998 Leszek Koltunski
import org.distorted.library.effect.MatrixEffect;
26 7602a827 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
27
import org.distorted.library.main.InternalBuffer;
28 da681e7e Leszek Koltunski
import org.distorted.library.program.DistortedProgram;
29 6c00149d Leszek Koltunski
30 da681e7e Leszek Koltunski
import java.nio.ByteBuffer;
31
import java.nio.ByteOrder;
32 6a06a912 Leszek Koltunski
import java.nio.FloatBuffer;
33 c90aca24 Leszek Koltunski
import java.util.ArrayList;
34 6a06a912 Leszek Koltunski
35
///////////////////////////////////////////////////////////////////////////////////////////////////
36 e0b6c593 Leszek Koltunski
/**
37 e5ba319d Leszek Koltunski
 * Abstract class which represents a Mesh, ie an array of vertices (rendered as a TRIANGLE_STRIP).
38 e0b6c593 Leszek Koltunski
 * <p>
39 da681e7e Leszek Koltunski
 * If you want to render to a particular shape, extend from here, construct a float array
40 7a5e538a Leszek Koltunski
 * containing per-vertex attributes, and call back setAttribs().
41 e0b6c593 Leszek Koltunski
 */
42 715e7726 Leszek Koltunski
public abstract class MeshBase
43 6a06a912 Leszek Koltunski
   {
44 227b9bca Leszek Koltunski
   // sizes of attributes of an individual vertex.
45
   private static final int POS_DATA_SIZE= 3; // vertex coordinates: x,y,z
46
   private static final int NOR_DATA_SIZE= 3; // normal vector: x,y,z
47
   private static final int INF_DATA_SIZE= 3; // 'inflate' vector: x,y,z
48 7a5e538a Leszek Koltunski
   private static final int TEX_DATA_SIZE= 2; // texture coordinates: s,t
49 6f2d931d Leszek Koltunski
50
   static final int POS_ATTRIB   = 0;
51
   static final int NOR_ATTRIB   = POS_DATA_SIZE;
52
   static final int INF_ATTRIB   = POS_DATA_SIZE + NOR_DATA_SIZE;
53
   static final int TEX_ATTRIB   = POS_DATA_SIZE + NOR_DATA_SIZE + INF_DATA_SIZE;
54 227b9bca Leszek Koltunski
   static final int VERT_ATTRIBS = POS_DATA_SIZE + NOR_DATA_SIZE + INF_DATA_SIZE + TEX_DATA_SIZE;  // number of attributes of a 'normal' vertex
55
   static final int TRAN_ATTRIBS = POS_DATA_SIZE + POS_DATA_SIZE;                                  // number of attributes of a transform feedback vertex
56 6a06a912 Leszek Koltunski
57 da681e7e Leszek Koltunski
   private static final int BYTES_PER_FLOAT = 4;
58 3ef3364d Leszek Koltunski
59 6f2d931d Leszek Koltunski
   private static final int OFFSET_POS = POS_ATTRIB*BYTES_PER_FLOAT;
60
   private static final int OFFSET_NOR = NOR_ATTRIB*BYTES_PER_FLOAT;
61
   private static final int OFFSET_INF = INF_ATTRIB*BYTES_PER_FLOAT;
62
   private static final int OFFSET_TEX = TEX_ATTRIB*BYTES_PER_FLOAT;
63 227b9bca Leszek Koltunski
   private static final int TRAN_SIZE  = TRAN_ATTRIBS*BYTES_PER_FLOAT;
64
   private static final int VERT_SIZE  = VERT_ATTRIBS*BYTES_PER_FLOAT;
65 a51fe521 Leszek Koltunski
66 fa8bc998 Leszek Koltunski
   private boolean mShowNormals;      // when rendering this mesh, draw normal vectors?
67
   private InternalBuffer mVBO, mTFO; // main vertex buffer and transform feedback buffer
68 da681e7e Leszek Koltunski
   private int mNumVertices;
69 fa8bc998 Leszek Koltunski
   private float[] mVertAttribs;      // packed: PosX,PosY,PosZ, NorX,NorY,NorZ, InfX,InfY,InfZ, TexS,TexT
70 7a5e538a Leszek Koltunski
   private float mInflate;
71 1b059065 Leszek Koltunski
   private float mBoundingX, mBoundingY, mBoundingZ;
72 23b733db Leszek Koltunski
   private float mStretchX, mStretchY, mStretchZ;
73 3ef3364d Leszek Koltunski
74 c90aca24 Leszek Koltunski
   private class Component
75
     {
76
     private int mEndIndex;
77
     private float[] mTextureMap;
78
79 0d4aae88 Leszek Koltunski
     Component(int end)
80 c90aca24 Leszek Koltunski
       {
81 0d4aae88 Leszek Koltunski
       mEndIndex = end;
82
83
       mTextureMap    = new float[4];
84
       mTextureMap[0] = 0.0f;  // LowerLeft_X
85
       mTextureMap[1] = 0.0f;  // LowerLeft_Y
86
       mTextureMap[2] = 1.0f;  // Width
87
       mTextureMap[3] = 1.0f;  // Height
88 c90aca24 Leszek Koltunski
       }
89
     Component(Component original)
90
       {
91
       mEndIndex = original.mEndIndex;
92 0d4aae88 Leszek Koltunski
       mTextureMap = new float[4];
93
       System.arraycopy(original.mTextureMap,0,mTextureMap,0,4);
94
       }
95
96
     void setMap(float[] newMap)
97
       {
98
       mTextureMap = newMap;
99 c90aca24 Leszek Koltunski
       }
100
     }
101
102
   private ArrayList<Component> mComponent;
103
104 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
105 3ef3364d Leszek Koltunski
106 1b059065 Leszek Koltunski
   MeshBase(float bx, float by, float bz)
107 3ef3364d Leszek Koltunski
     {
108 1b059065 Leszek Koltunski
     mBoundingX = bx/2;
109
     mBoundingY = by/2;
110
     mBoundingZ = bz/2;
111 044b5494 Leszek Koltunski
112 23b733db Leszek Koltunski
     mStretchX = 1.0f;
113
     mStretchY = 1.0f;
114
     mStretchZ = 1.0f;
115
116 3fc9327a Leszek Koltunski
     mShowNormals = false;
117 c90aca24 Leszek Koltunski
     mInflate     = 0.0f;
118 0d4aae88 Leszek Koltunski
     mComponent   = new ArrayList<>();
119 c90aca24 Leszek Koltunski
120
     mVBO = new InternalBuffer(GLES31.GL_ARRAY_BUFFER             , GLES31.GL_STATIC_READ);
121
     mTFO = new InternalBuffer(GLES31.GL_TRANSFORM_FEEDBACK_BUFFER, GLES31.GL_STATIC_READ);
122
     }
123
124
///////////////////////////////////////////////////////////////////////////////////////////////////
125
// copy constructor
126
127
   MeshBase(MeshBase original)
128
     {
129 1b059065 Leszek Koltunski
     mBoundingX = original.mBoundingX;
130
     mBoundingY = original.mBoundingY;
131
     mBoundingZ = original.mBoundingZ;
132 044b5494 Leszek Koltunski
133 23b733db Leszek Koltunski
     mStretchX = original.mStretchX;
134
     mStretchY = original.mStretchY;
135
     mStretchZ = original.mStretchZ;
136
137 c90aca24 Leszek Koltunski
     mShowNormals = original.mShowNormals;
138
     mInflate     = original.mInflate;
139
140
     int size = original.mComponent.size();
141
     mComponent = new ArrayList<>();
142
     for(int i=0; i<size; i++)
143
       {
144
       Component comp = new Component(original.mComponent.get(i));
145
       mComponent.add(comp);
146
       }
147 42571056 Leszek Koltunski
148 7602a827 Leszek Koltunski
     mVBO = new InternalBuffer(GLES31.GL_ARRAY_BUFFER             , GLES31.GL_STATIC_READ);
149
     mTFO = new InternalBuffer(GLES31.GL_TRANSFORM_FEEDBACK_BUFFER, GLES31.GL_STATIC_READ);
150 c90aca24 Leszek Koltunski
151
     System.arraycopy(original.mVertAttribs,0,mVertAttribs,0,original.mNumVertices*VERT_ATTRIBS);
152
     setAttribs(mVertAttribs);
153 42571056 Leszek Koltunski
     }
154
155 0d4aae88 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
156
// change mVertAttribs from index 'begin' to index 'end' (inclusive) to the new Texture Map.
157
// x varies from -mBoundingX to +mBoundingX; y accordingly.
158
159
   private void changeTextureMap( float[] newMap, float[] oldMap, int begin, int end)
160
     {
161
162
     }
163
164
///////////////////////////////////////////////////////////////////////////////////////////////////
165
166
   int numComponents()
167
     {
168
     return mComponent.size();
169
     }
170
171
///////////////////////////////////////////////////////////////////////////////////////////////////
172
173
   void setBounding(float bx, float by, float bz)
174
     {
175
     mBoundingX = bx/2;
176
     mBoundingY = by/2;
177
     mBoundingZ = bz/2;
178
     }
179
180 42571056 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
181 6c00149d Leszek Koltunski
// when a derived class is done computing its mesh, it has to call this method.
182 42571056 Leszek Koltunski
183 227b9bca Leszek Koltunski
   void setAttribs(float[] vertexAttribs)
184 42571056 Leszek Koltunski
     {
185 227b9bca Leszek Koltunski
     mNumVertices = vertexAttribs.length/VERT_ATTRIBS;
186 fa8bc998 Leszek Koltunski
     mVertAttribs = vertexAttribs;
187 da681e7e Leszek Koltunski
188 0d4aae88 Leszek Koltunski
     mComponent.add(new Component(mNumVertices));
189 c90aca24 Leszek Koltunski
190 fa8bc998 Leszek Koltunski
     FloatBuffer attribs = ByteBuffer.allocateDirect(mNumVertices*VERT_SIZE).order(ByteOrder.nativeOrder()).asFloatBuffer();
191
     attribs.put(vertexAttribs).position(0);
192 da681e7e Leszek Koltunski
193 fa8bc998 Leszek Koltunski
     mVBO.setData(mNumVertices*VERT_SIZE, attribs);
194
     mTFO.setData(mNumVertices*TRAN_SIZE, null   );
195 42571056 Leszek Koltunski
     }
196
197
///////////////////////////////////////////////////////////////////////////////////////////////////
198 0d4aae88 Leszek Koltunski
// called from MeshJoined
199 42571056 Leszek Koltunski
200 0d4aae88 Leszek Koltunski
   void join(MeshBase[] meshes)
201 226144d0 leszek
     {
202 0d4aae88 Leszek Koltunski
     MeshBase mesh;
203
     Component comp;
204
     int com, num, len = meshes.length;
205
     int origVertices = mNumVertices;
206 42571056 Leszek Koltunski
207 0d4aae88 Leszek Koltunski
     // compute new numVertices; take care of Components
208 044b5494 Leszek Koltunski
209 0d4aae88 Leszek Koltunski
     if( origVertices>0 )
210
       {
211
       com = mComponent.size();
212
       mNumVertices+= ( mNumVertices%2==1 ? 2:1 );
213
       mComponent.get(com-1).mEndIndex = mNumVertices;
214
       }
215 044b5494 Leszek Koltunski
216 0d4aae88 Leszek Koltunski
     for(int i=0; i<len; i++)
217
       {
218
       mesh = meshes[i];
219
       com = mesh.mComponent.size();
220 044b5494 Leszek Koltunski
221 0d4aae88 Leszek Koltunski
       for(int j=0; j<com; j++)
222
         {
223
         comp = new Component(mesh.mComponent.get(j));
224
         comp.mEndIndex += mNumVertices;
225
         mComponent.add(comp);
226
         }
227 23b733db Leszek Koltunski
228 0d4aae88 Leszek Koltunski
       num = mesh.mNumVertices;
229
       mNumVertices+= (i<len-1 ? ( num%2==1 ? num+2 : num+1 ) : num);
230
       }
231
232
     // allocate new attrib array
233
     float[] newAttribs = new float[VERT_ATTRIBS*mNumVertices];
234
     num = origVertices;
235
236
     if( origVertices>0 )
237
       {
238
       System.arraycopy(mVertAttribs,                             0, newAttribs,                         0, VERT_ATTRIBS*num);
239
       System.arraycopy(mVertAttribs, VERT_ATTRIBS*(origVertices-1), newAttribs, VERT_ATTRIBS*origVertices, VERT_ATTRIBS    );
240
       origVertices++;
241
242
       if( num%2==1 )
243
         {
244
         System.arraycopy(mVertAttribs, VERT_ATTRIBS*(origVertices-1), newAttribs, VERT_ATTRIBS*origVertices, VERT_ATTRIBS);
245
         origVertices++;
246
         }
247
       }
248
249
     for(int i=0; i<len; i++)
250
       {
251
       mesh = meshes[i];
252
       num = mesh.mNumVertices;
253
254
       System.arraycopy(mesh.mVertAttribs, 0, newAttribs, VERT_ATTRIBS*origVertices, VERT_ATTRIBS    );
255
       origVertices++;
256
       System.arraycopy(mesh.mVertAttribs, 0, newAttribs, VERT_ATTRIBS*origVertices, VERT_ATTRIBS*num);
257
       origVertices+=num;
258
259
       if( i<len-1 )
260
         {
261
         System.arraycopy(mesh.mVertAttribs, VERT_ATTRIBS*(num-1), newAttribs, VERT_ATTRIBS*origVertices, VERT_ATTRIBS);
262
         origVertices++;
263
264
         if( num%2==1 )
265
           {
266
           System.arraycopy(mesh.mVertAttribs, VERT_ATTRIBS*(num-1), newAttribs, VERT_ATTRIBS*origVertices, VERT_ATTRIBS);
267
           origVertices++;
268
           }
269
         }
270
       }
271
272
     if( origVertices!=mNumVertices )
273
       {
274
       android.util.Log.e("mesh", "join: origVertices: "+origVertices+" numVertices: "+mNumVertices);
275
       }
276
277
     mVertAttribs = newAttribs;
278
279
     FloatBuffer attribs = ByteBuffer.allocateDirect(mNumVertices*VERT_SIZE).order(ByteOrder.nativeOrder()).asFloatBuffer();
280
     attribs.put(mVertAttribs).position(0);
281
282
     mVBO.setData(mNumVertices*VERT_SIZE, attribs);
283
     mTFO.setData(mNumVertices*TRAN_SIZE, null   );
284 23b733db Leszek Koltunski
     }
285
286
///////////////////////////////////////////////////////////////////////////////////////////////////
287
/**
288 0d4aae88 Leszek Koltunski
 * Not part of public API, do not document (public only because has to be used from the main package)
289
 *
290
 * @y.exclude
291 23b733db Leszek Koltunski
 */
292 0d4aae88 Leszek Koltunski
   public int getTFO()
293 23b733db Leszek Koltunski
     {
294 0d4aae88 Leszek Koltunski
     return mTFO.mIndex[0];
295 23b733db Leszek Koltunski
     }
296
297
///////////////////////////////////////////////////////////////////////////////////////////////////
298
/**
299 0d4aae88 Leszek Koltunski
 * Not part of public API, do not document (public only because has to be used from the main package)
300
 *
301
 * @y.exclude
302 23b733db Leszek Koltunski
 */
303 0d4aae88 Leszek Koltunski
   public int getNumVertices()
304 23b733db Leszek Koltunski
     {
305 0d4aae88 Leszek Koltunski
     return mNumVertices;
306 23b733db Leszek Koltunski
     }
307
308 044b5494 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
309 6c00149d Leszek Koltunski
/**
310
 * Not part of public API, do not document (public only because has to be used from the main package)
311
 *
312
 * @y.exclude
313
 */
314 da681e7e Leszek Koltunski
   public void bindVertexAttribs(DistortedProgram program)
315 6c00149d Leszek Koltunski
     {
316 da681e7e Leszek Koltunski
     GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, mVBO.mIndex[0] );
317 227b9bca Leszek Koltunski
     GLES31.glVertexAttribPointer(program.mAttribute[0], POS_DATA_SIZE, GLES31.GL_FLOAT, false, VERT_SIZE, OFFSET_POS);
318
     GLES31.glVertexAttribPointer(program.mAttribute[1], NOR_DATA_SIZE, GLES31.GL_FLOAT, false, VERT_SIZE, OFFSET_NOR);
319
     GLES31.glVertexAttribPointer(program.mAttribute[2], INF_DATA_SIZE, GLES31.GL_FLOAT, false, VERT_SIZE, OFFSET_INF);
320
     GLES31.glVertexAttribPointer(program.mAttribute[3], TEX_DATA_SIZE, GLES31.GL_FLOAT, false, VERT_SIZE, OFFSET_TEX);
321 da681e7e Leszek Koltunski
     GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, 0);
322
     }
323
324
///////////////////////////////////////////////////////////////////////////////////////////////////
325
/**
326
 * Not part of public API, do not document (public only because has to be used from the main package)
327
 *
328
 * @y.exclude
329
 */
330
   public void bindTransformAttribs(DistortedProgram program)
331
     {
332
     GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, mTFO.mIndex[0] );
333 227b9bca Leszek Koltunski
     GLES31.glVertexAttribPointer(program.mAttribute[0], POS_DATA_SIZE, GLES31.GL_FLOAT, false, 0, 0);
334 da681e7e Leszek Koltunski
     GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, 0);
335 6c00149d Leszek Koltunski
     }
336
337 7a5e538a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
338
/**
339
 * Not part of public API, do not document (public only because has to be used from the main package)
340
 *
341
 * @y.exclude
342
 */
343
   public void setInflate(float inflate)
344
     {
345
     mInflate = inflate;
346
     }
347
348
///////////////////////////////////////////////////////////////////////////////////////////////////
349
/**
350
 * Not part of public API, do not document (public only because has to be used from the main package)
351
 *
352
 * @y.exclude
353
 */
354
   public float getInflate()
355
     {
356
     return mInflate;
357
     }
358
359 466450b5 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
360
// PUBLIC API
361 3fc9327a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
362
/**
363
 * When rendering this Mesh, do we want to render the Normal vectors as well?
364 420836fc leszek
 * <p>
365
 * Will work only on OpenGL ES >= 3.0 devices.
366 3fc9327a Leszek Koltunski
 *
367
 * @param show Controls if we render the Normal vectors or not.
368
 */
369
   public void setShowNormals(boolean show)
370
     {
371 7602a827 Leszek Koltunski
     mShowNormals = (DistortedLibrary.GLSL >= 300 && show);
372 3fc9327a Leszek Koltunski
     }
373 466450b5 Leszek Koltunski
374 6c00149d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
375
/**
376
 * When rendering this mesh, should we also draw the normal vectors?
377
 *
378
 * @return <i>true</i> if we do render normal vectors
379
 */
380
   public boolean getShowNormals()
381
     {
382
     return mShowNormals;
383
     }
384
385 466450b5 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
386
/**
387
 * Release all internal resources.
388
 */
389
   public void markForDeletion()
390
     {
391 fa8bc998 Leszek Koltunski
     mVertAttribs = null;
392 466450b5 Leszek Koltunski
393
     mVBO.markForDeletion();
394
     mTFO.markForDeletion();
395
     }
396 fa8bc998 Leszek Koltunski
397
///////////////////////////////////////////////////////////////////////////////////////////////////
398
/**
399 c90aca24 Leszek Koltunski
 * Apply all Effects to the vertex mesh. Overwrite the mesh in place.
400 fa8bc998 Leszek Koltunski
 * <p>
401 c90aca24 Leszek Koltunski
 * This is a static, permanent modification of the vertices contained in this Mesh. If the effects
402
 * contain any Dynamics, they will be evaluated at 0.
403 fa8bc998 Leszek Koltunski
 *
404
 * Please note that calling this once with the complete list of Effects will be much faster than
405
 * calling it repeatedly with one Effect at a time, as we have to reallocate the array of vertices
406
 * each time.
407
 */
408
   public void apply(MatrixEffect[] effects)
409
     {
410 c90aca24 Leszek Koltunski
     float[][] matrix = new float[effects.length][16];
411
     float[] tmp;
412 0d4aae88 Leszek Koltunski
     float[] array = new float[7];
413 c90aca24 Leszek Koltunski
     float x,y,z;
414
     int numEffects = 0;
415
416
     for(MatrixEffect eff: effects)
417 fa8bc998 Leszek Koltunski
       {
418 c90aca24 Leszek Koltunski
       if( eff!=null )
419 fa8bc998 Leszek Koltunski
         {
420 c90aca24 Leszek Koltunski
         Matrix.setIdentityM(matrix[numEffects],0);
421
         eff.compute(array,0,0,0);
422
         eff.apply(matrix[numEffects], array, 0);
423
         numEffects++;
424 fa8bc998 Leszek Koltunski
         }
425
       }
426
427 7594a5d2 Leszek Koltunski
     for(int index=0; index<mNumVertices*VERT_ATTRIBS; index+=VERT_ATTRIBS )
428 c90aca24 Leszek Koltunski
       {
429
       for(int mat=0; mat<numEffects; mat++)
430
         {
431
         tmp = matrix[mat];
432
433
         x = mVertAttribs[index+POS_ATTRIB  ];
434
         y = mVertAttribs[index+POS_ATTRIB+1];
435
         z = mVertAttribs[index+POS_ATTRIB+2];
436
437
         mVertAttribs[index+POS_ATTRIB  ] = tmp[0]*x + tmp[4]*y + tmp[ 8]*z + tmp[12];
438
         mVertAttribs[index+POS_ATTRIB+1] = tmp[1]*x + tmp[5]*y + tmp[ 9]*z + tmp[13];
439
         mVertAttribs[index+POS_ATTRIB+2] = tmp[2]*x + tmp[6]*y + tmp[10]*z + tmp[14];
440
441
         x = mVertAttribs[index+NOR_ATTRIB  ];
442
         y = mVertAttribs[index+NOR_ATTRIB+1];
443
         z = mVertAttribs[index+NOR_ATTRIB+2];
444
445
         mVertAttribs[index+NOR_ATTRIB  ] = tmp[0]*x + tmp[4]*y + tmp[ 8]*z;
446
         mVertAttribs[index+NOR_ATTRIB+1] = tmp[1]*x + tmp[5]*y + tmp[ 9]*z;
447
         mVertAttribs[index+NOR_ATTRIB+2] = tmp[2]*x + tmp[6]*y + tmp[10]*z;
448
449
         x = mVertAttribs[index+INF_ATTRIB  ];
450
         y = mVertAttribs[index+INF_ATTRIB+1];
451
         z = mVertAttribs[index+INF_ATTRIB+2];
452
453
         mVertAttribs[index+INF_ATTRIB  ] = tmp[0]*x + tmp[4]*y + tmp[ 8]*z;
454
         mVertAttribs[index+INF_ATTRIB+1] = tmp[1]*x + tmp[5]*y + tmp[ 9]*z;
455
         mVertAttribs[index+INF_ATTRIB+2] = tmp[2]*x + tmp[6]*y + tmp[10]*z;
456
         }
457
       }
458
459
     FloatBuffer attribs = ByteBuffer.allocateDirect(mNumVertices*VERT_SIZE).order(ByteOrder.nativeOrder()).asFloatBuffer();
460
     attribs.put(mVertAttribs).position(0);
461
462
     mVBO.setData(mNumVertices*VERT_SIZE, attribs);
463
     mTFO.setData(mNumVertices*TRAN_SIZE, null   );
464
     }
465
466
///////////////////////////////////////////////////////////////////////////////////////////////////
467
/**
468 0d4aae88 Leszek Koltunski
 * Sets texture maps for all components of this mesh.
469 c90aca24 Leszek Koltunski
 * <p>
470 0d4aae88 Leszek Koltunski
 * Please note that calling this once with the complete list of Maps will be much faster than
471
 * calling it repeatedly with one Maps at a time, as we have to reallocate the array of vertices
472 c90aca24 Leszek Koltunski
 * each time.
473 0d4aae88 Leszek Koltunski
 * 'maps' needs to be maps[NumComponentsInThisMesh][4]. [0] is the lower-left corner's X, [1]- its Y,
474
 * [2] - width, [3] - height of the map.
475
 * For example map[0] = new float { 0.0, 0.5, 0.5, 0.5 } sets the 0th component texture map to the
476
 * upper-left quadrant of the texture.
477 c90aca24 Leszek Koltunski
 */
478 0d4aae88 Leszek Koltunski
   public void setTextureMap(float[][] maps)
479 c90aca24 Leszek Koltunski
     {
480 0d4aae88 Leszek Koltunski
     int num_comp = mComponent.size();
481
     int num_maps = maps.length;
482
     int min = num_comp<num_maps ? num_comp : num_maps;
483
     int lastEnd = 0;
484 ea88d502 Leszek Koltunski
485 0d4aae88 Leszek Koltunski
     for(int i=0; i<min; i++)
486 ea88d502 Leszek Koltunski
       {
487 0d4aae88 Leszek Koltunski
       if( maps[i]!=null )
488 ea88d502 Leszek Koltunski
         {
489 0d4aae88 Leszek Koltunski
         Component comp = mComponent.get(i);
490
         changeTextureMap(maps[i],comp.mTextureMap,lastEnd,comp.mEndIndex);
491
         comp.setMap(maps[i]);
492
         lastEnd = comp.mEndIndex;
493 ea88d502 Leszek Koltunski
         }
494
       }
495 c90aca24 Leszek Koltunski
496 ea88d502 Leszek Koltunski
     FloatBuffer attribs = ByteBuffer.allocateDirect(mNumVertices*VERT_SIZE).order(ByteOrder.nativeOrder()).asFloatBuffer();
497
     attribs.put(mVertAttribs).position(0);
498
499
     mVBO.setData(mNumVertices*VERT_SIZE, attribs);
500
     mTFO.setData(mNumVertices*TRAN_SIZE, null   );
501 fa8bc998 Leszek Koltunski
     }
502 9099e567 Leszek Koltunski
503
///////////////////////////////////////////////////////////////////////////////////////////////////
504
/**
505 0d4aae88 Leszek Koltunski
 * Each mesh has its 'bounding box' - return half of its X-length.
506 9099e567 Leszek Koltunski
 * <p>
507 0d4aae88 Leszek Koltunski
 * In case of all 'simple' Meshes, the bounding box is always 1x1x1 (Sphere, Cubes) or 1x1x0
508
 * (Rectangles, Triangles, Quad - i.e. all 'flat' Meshes). But this can be something else in case of
509
 * MeshComponent.
510 9099e567 Leszek Koltunski
 */
511 0d4aae88 Leszek Koltunski
   public float getBoundingX()
512
    {
513
    return mBoundingX*mStretchX;
514
    }
515
516
///////////////////////////////////////////////////////////////////////////////////////////////////
517
/**
518
 * Each mesh has its 'bounding box' - return half of its Y-length.
519
 */
520
   public float getBoundingY()
521
    {
522
    return mBoundingY*mStretchY;
523
    }
524
525
///////////////////////////////////////////////////////////////////////////////////////////////////
526
/**
527
 * Each mesh has its 'bounding box' - return half of its Z-length.
528
 */
529
   public float getBoundingZ()
530
    {
531
    return mBoundingZ*mStretchZ;
532
    }
533
534
///////////////////////////////////////////////////////////////////////////////////////////////////
535
/**
536
 * Sometimes we want to display a Mesh on a rectangular screen. Then we need to stretch it by
537
 * different factors in x and y (or z) directions. If we also wanted do display some vertex effects
538
 * done on this mesh, let's say a bulge done by a Distort effect, and wanted the bulge to be round,
539
 * (i.e the same in x and y directions) then doing so without this method would be impossible.
540
 *
541
 * This sets 'stretch' factors in each 3 dimensions. All vertices of this Mesh will be premultiplied
542
 * by those factors in the very first line of the Vertex Shader, before any Effects are done on it.
543
 * Using this we can thus pre-stretch the mesh to aspect ratio equal to the surface we eventually
544
 * want to display the Mesh on, and this way we can achieve a round Distort bulge!
545
 *
546
 * This could also be used to pre-stretch a Rectangles Mesh to a size equal (in pixels) to the bitmap
547
 * this mesh is textured with - and this lets us work with all Effects in natural, pixel units.
548
 *
549
 * @param sx stretch factor in x.
550
 * @param sy stretch factor in y.
551
 * @param sz stretch factor in z.
552
 */
553
   public void setStretch(float sx, float sy, float sz)
554 9099e567 Leszek Koltunski
     {
555 0d4aae88 Leszek Koltunski
     mStretchX = sx;
556
     mStretchY = sy;
557
     mStretchZ = sz;
558
     }
559 9099e567 Leszek Koltunski
560 0d4aae88 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
561
/**
562
 * Returns the x-factor set by setStretch().
563
 */
564
   public float getStretchX()
565
     {
566
     return mStretchX;
567
     }
568 9099e567 Leszek Koltunski
569 0d4aae88 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
570
/**
571
 * Returns the y-factor set by setStretch().
572
 */
573
   public float getStretchY()
574
     {
575
     return mStretchY;
576
     }
577
578
///////////////////////////////////////////////////////////////////////////////////////////////////
579
/**
580
 * Returns the z-factor set by setStretch().
581
 */
582
   public float getStretchZ()
583
     {
584
     return mStretchZ;
585 9099e567 Leszek Koltunski
     }
586 6a06a912 Leszek Koltunski
   }
587 226144d0 leszek
588 3fc9327a Leszek Koltunski