Project

General

Profile

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

library / src / main / java / org / distorted / library / mesh / MeshBase.java @ 41b3ada0

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 b7074bc6 Leszek Koltunski
import android.opengl.GLES30;
23 e8925fcd Leszek Koltunski
import android.opengl.Matrix;
24 f046b159 Leszek Koltunski
import android.util.Log;
25 6c00149d Leszek Koltunski
26 e8925fcd Leszek Koltunski
import org.distorted.library.effect.MatrixEffect;
27 f046b159 Leszek Koltunski
import org.distorted.library.effect.VertexEffect;
28 7602a827 Leszek Koltunski
import org.distorted.library.main.InternalBuffer;
29 da681e7e Leszek Koltunski
import org.distorted.library.program.DistortedProgram;
30 a3a05347 Leszek Koltunski
import org.distorted.library.type.Static4D;
31 6c00149d Leszek Koltunski
32 bc2ab8c5 Leszek Koltunski
import java.io.DataOutputStream;
33 22422a76 Leszek Koltunski
import java.io.IOException;
34 bc2ab8c5 Leszek Koltunski
import java.io.DataInputStream;
35 f046b159 Leszek Koltunski
import java.nio.ByteBuffer;
36
import java.nio.ByteOrder;
37
import java.nio.FloatBuffer;
38 c90aca24 Leszek Koltunski
import java.util.ArrayList;
39 6a06a912 Leszek Koltunski
40
///////////////////////////////////////////////////////////////////////////////////////////////////
41 e0b6c593 Leszek Koltunski
/**
42 e5ba319d Leszek Koltunski
 * Abstract class which represents a Mesh, ie an array of vertices (rendered as a TRIANGLE_STRIP).
43 e0b6c593 Leszek Koltunski
 * <p>
44 da681e7e Leszek Koltunski
 * If you want to render to a particular shape, extend from here, construct a float array
45 7a5e538a Leszek Koltunski
 * containing per-vertex attributes, and call back setAttribs().
46 e0b6c593 Leszek Koltunski
 */
47 715e7726 Leszek Koltunski
public abstract class MeshBase
48 6a06a912 Leszek Koltunski
   {
49 41b3ada0 Leszek Koltunski
   private static final int UBO_BINDING = 3;
50 97755c02 Leszek Koltunski
           static final int MAX_EFFECT_COMPONENTS= 100;
51 bc208a9c Leszek Koltunski
52 227b9bca Leszek Koltunski
   // sizes of attributes of an individual vertex.
53
   private static final int POS_DATA_SIZE= 3; // vertex coordinates: x,y,z
54
   private static final int NOR_DATA_SIZE= 3; // normal vector: x,y,z
55
   private static final int INF_DATA_SIZE= 3; // 'inflate' vector: x,y,z
56 7a5e538a Leszek Koltunski
   private static final int TEX_DATA_SIZE= 2; // texture coordinates: s,t
57 36d65d88 Leszek Koltunski
   private static final int COM_DATA_SIZE= 1; // component number, a single float
58 6f2d931d Leszek Koltunski
59
   static final int POS_ATTRIB   = 0;
60
   static final int NOR_ATTRIB   = POS_DATA_SIZE;
61
   static final int INF_ATTRIB   = POS_DATA_SIZE + NOR_DATA_SIZE;
62 e54bfada Leszek Koltunski
   static final int TEX_ATTRIB   = 0;
63 36d65d88 Leszek Koltunski
   static final int COM_ATTRIB   = TEX_DATA_SIZE;
64 bc208a9c Leszek Koltunski
65 e54bfada Leszek Koltunski
   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)
66 36d65d88 Leszek Koltunski
   static final int VERT2_ATTRIBS= TEX_DATA_SIZE + COM_DATA_SIZE;                  // number of attributes of a vertex (the 'preapply invariant' part)
67 e54bfada Leszek Koltunski
   static final int TRAN_ATTRIBS = POS_DATA_SIZE + NOR_DATA_SIZE + INF_DATA_SIZE;  // number of attributes of a transform feedback vertex
68 6a06a912 Leszek Koltunski
69 da681e7e Leszek Koltunski
   private static final int BYTES_PER_FLOAT = 4;
70 3ef3364d Leszek Koltunski
71 6f2d931d Leszek Koltunski
   private static final int OFFSET_POS = POS_ATTRIB*BYTES_PER_FLOAT;
72
   private static final int OFFSET_NOR = NOR_ATTRIB*BYTES_PER_FLOAT;
73
   private static final int OFFSET_INF = INF_ATTRIB*BYTES_PER_FLOAT;
74
   private static final int OFFSET_TEX = TEX_ATTRIB*BYTES_PER_FLOAT;
75 36d65d88 Leszek Koltunski
   private static final int OFFSET_COM = COM_ATTRIB*BYTES_PER_FLOAT;
76 bc208a9c Leszek Koltunski
77 227b9bca Leszek Koltunski
   private static final int TRAN_SIZE  = TRAN_ATTRIBS*BYTES_PER_FLOAT;
78 e54bfada Leszek Koltunski
   private static final int VERT1_SIZE = VERT1_ATTRIBS*BYTES_PER_FLOAT;
79
   private static final int VERT2_SIZE = VERT2_ATTRIBS*BYTES_PER_FLOAT;
80 a51fe521 Leszek Koltunski
81 e54bfada Leszek Koltunski
   private boolean mShowNormals;              // when rendering this mesh, draw normal vectors?
82
   private InternalBuffer mVBO1, mVBO2, mTFO; // main vertex buffer and transform feedback buffer
83 da681e7e Leszek Koltunski
   private int mNumVertices;
84 e54bfada Leszek Koltunski
   private float[] mVertAttribs1;             // packed: PosX,PosY,PosZ, NorX,NorY,NorZ, InfX,InfY,InfZ
85 36d65d88 Leszek Koltunski
   private float[] mVertAttribs2;             // packed: TexS,TexT, Component
86 7a5e538a Leszek Koltunski
   private float mInflate;
87 97755c02 Leszek Koltunski
   private AssociationUniformBlock mAUB;
88 36d65d88 Leszek Koltunski
89 71d8aba1 Leszek Koltunski
   DeferredJobs.JobNode[] mJobNode;
90 07206c71 Leszek Koltunski
91 22422a76 Leszek Koltunski
   private static final int TEX_COMP_SIZE = 5; // 5 four-bytes entities inside the component
92
93 e8925fcd Leszek Koltunski
   private static class TexComponent
94 c90aca24 Leszek Koltunski
     {
95
     private int mEndIndex;
96 a3a05347 Leszek Koltunski
     private Static4D mTextureMap;
97 c90aca24 Leszek Koltunski
98 e8925fcd Leszek Koltunski
     TexComponent(int end)
99 c90aca24 Leszek Koltunski
       {
100 a3a05347 Leszek Koltunski
       mEndIndex  = end;
101
       mTextureMap= new Static4D(0,0,1,1);
102 c90aca24 Leszek Koltunski
       }
103 e8925fcd Leszek Koltunski
     TexComponent(TexComponent original)
104 c90aca24 Leszek Koltunski
       {
105
       mEndIndex = original.mEndIndex;
106 a3a05347 Leszek Koltunski
107
       float x = original.mTextureMap.get0();
108
       float y = original.mTextureMap.get1();
109
       float z = original.mTextureMap.get2();
110
       float w = original.mTextureMap.get3();
111
       mTextureMap = new Static4D(x,y,z,w);
112 0d4aae88 Leszek Koltunski
       }
113
114 a3a05347 Leszek Koltunski
     void setMap(Static4D map)
115 0d4aae88 Leszek Koltunski
       {
116 a3a05347 Leszek Koltunski
       mTextureMap.set(map.get0(),map.get1(),map.get2(),map.get3());
117 c90aca24 Leszek Koltunski
       }
118
     }
119
120 e8925fcd Leszek Koltunski
   private ArrayList<TexComponent> mTexComponent;
121
   private ArrayList<Integer> mEffComponent;
122 c90aca24 Leszek Koltunski
123 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
124 3ef3364d Leszek Koltunski
125 0f10a0b6 Leszek Koltunski
   MeshBase()
126 3ef3364d Leszek Koltunski
     {
127 e8925fcd Leszek Koltunski
     mShowNormals  = false;
128
     mInflate      = 0.0f;
129
     mTexComponent = new ArrayList<>();
130
     mEffComponent = new ArrayList<>();
131
132 71d8aba1 Leszek Koltunski
     mJobNode = new DeferredJobs.JobNode[1];
133
134 97755c02 Leszek Koltunski
     mAUB = new AssociationUniformBlock();
135 4f81e0c8 Leszek Koltunski
136 e54bfada Leszek Koltunski
     mVBO1= new InternalBuffer(GLES30.GL_ARRAY_BUFFER             , GLES30.GL_STATIC_READ);
137
     mVBO2= new InternalBuffer(GLES30.GL_ARRAY_BUFFER             , GLES30.GL_STATIC_READ);
138 b7074bc6 Leszek Koltunski
     mTFO = new InternalBuffer(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, GLES30.GL_STATIC_READ);
139 c90aca24 Leszek Koltunski
     }
140
141
///////////////////////////////////////////////////////////////////////////////////////////////////
142
// copy constructor
143
144 4f81e0c8 Leszek Koltunski
   MeshBase(MeshBase original, boolean deep)
145 c90aca24 Leszek Koltunski
     {
146 1fad573e Leszek Koltunski
     mShowNormals= original.mShowNormals;
147
     mInflate    = original.mInflate;
148
     mNumVertices= original.mNumVertices;
149 42571056 Leszek Koltunski
150 97755c02 Leszek Koltunski
     mAUB = new AssociationUniformBlock(original.mAUB);
151 36d65d88 Leszek Koltunski
152 4f81e0c8 Leszek Koltunski
     if( deep )
153
       {
154 71d8aba1 Leszek Koltunski
       mJobNode = new DeferredJobs.JobNode[1];
155 1fad573e Leszek Koltunski
       if( original.mJobNode[0]==null ) copy(original);
156
       else mJobNode[0] = DeferredJobs.copy(this,original);
157 4f81e0c8 Leszek Koltunski
       }
158
     else
159
       {
160 71d8aba1 Leszek Koltunski
       mJobNode      = original.mJobNode;
161
       mVBO1         = original.mVBO1;
162
       mVertAttribs1 = original.mVertAttribs1;
163 1fad573e Leszek Koltunski
       shallowCopy(original);
164 4f81e0c8 Leszek Koltunski
       }
165 cbd502ec Leszek Koltunski
166 4f81e0c8 Leszek Koltunski
     mTFO = new InternalBuffer(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, GLES30.GL_STATIC_READ);
167 42571056 Leszek Koltunski
     }
168
169 36d65d88 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
170
171 1fad573e Leszek Koltunski
   void copy(MeshBase original)
172 36d65d88 Leszek Koltunski
     {
173 1fad573e Leszek Koltunski
     shallowCopy(original);
174
175
     mVBO1= new InternalBuffer(GLES30.GL_ARRAY_BUFFER, GLES30.GL_STATIC_READ);
176
     mVertAttribs1= new float[mNumVertices*VERT1_ATTRIBS];
177
     System.arraycopy(original.mVertAttribs1,0,mVertAttribs1,0,mNumVertices*VERT1_ATTRIBS);
178
     mVBO1.invalidate();
179 36d65d88 Leszek Koltunski
     }
180
181
///////////////////////////////////////////////////////////////////////////////////////////////////
182
183 1fad573e Leszek Koltunski
   private void shallowCopy(MeshBase original)
184 36d65d88 Leszek Koltunski
     {
185 e8925fcd Leszek Koltunski
     int texComSize = original.mTexComponent.size();
186
     mTexComponent = new ArrayList<>();
187 36d65d88 Leszek Koltunski
188 e8925fcd Leszek Koltunski
     for(int i=0; i<texComSize; i++)
189 1fad573e Leszek Koltunski
       {
190 e8925fcd Leszek Koltunski
       TexComponent comp = new TexComponent(original.mTexComponent.get(i));
191
       mTexComponent.add(comp);
192 1fad573e Leszek Koltunski
       }
193 f4c5a46e Leszek Koltunski
194 e8925fcd Leszek Koltunski
     mEffComponent = new ArrayList<>();
195
     mEffComponent.addAll(original.mEffComponent);
196
197 1fad573e Leszek Koltunski
     mVBO2= new InternalBuffer(GLES30.GL_ARRAY_BUFFER, GLES30.GL_STATIC_READ);
198
     mVertAttribs2= new float[mNumVertices*VERT2_ATTRIBS];
199
     System.arraycopy(original.mVertAttribs2,0,mVertAttribs2,0,mNumVertices*VERT2_ATTRIBS);
200
     mVBO2.invalidate();
201 f4c5a46e Leszek Koltunski
     }
202
203 a8dfedcc Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
204
205 e8925fcd Leszek Koltunski
   void mergeTexComponentsNow()
206 a8dfedcc Leszek Koltunski
     {
207 e8925fcd Leszek Koltunski
     int num = mTexComponent.size();
208 a8dfedcc Leszek Koltunski
209
     if( num>1 )
210
       {
211 e8925fcd Leszek Koltunski
       mTexComponent.clear();
212
       mTexComponent.add(new TexComponent(mNumVertices-1));
213
214
       mVBO2.invalidate();
215
       }
216
     }
217
218
///////////////////////////////////////////////////////////////////////////////////////////////////
219
220
   void mergeEffComponentsNow()
221
     {
222
     int num = mEffComponent.size();
223
224
     if( num>1 )
225
       {
226
       mEffComponent.clear();
227
       mEffComponent.add(mNumVertices-1);
228 a8dfedcc Leszek Koltunski
229
       for(int index=0; index<mNumVertices; index++)
230
         {
231
         mVertAttribs2[VERT2_ATTRIBS*index+COM_ATTRIB] = 0;
232
         }
233
234
       mVBO2.invalidate();
235
       }
236
     }
237
238 0d4aae88 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
239
240 e8925fcd Leszek Koltunski
   void applyMatrix(MatrixEffect effect, int andAssoc, int equAssoc)
241
     {
242 62c869ad Leszek Koltunski
     float[] matrixP  = new float[16];
243
     float[] matrixV  = new float[16];
244 e8925fcd Leszek Koltunski
     float[] uniforms = new float[7];
245
     int start, end=-1, numComp = mEffComponent.size();
246
247 62c869ad Leszek Koltunski
     Matrix.setIdentityM(matrixP,0);
248
     Matrix.setIdentityM(matrixV,0);
249 e8925fcd Leszek Koltunski
     effect.compute(uniforms,0,0,0);
250 62c869ad Leszek Koltunski
     effect.apply(matrixP, matrixV, uniforms, 0);
251 e8925fcd Leszek Koltunski
252 97755c02 Leszek Koltunski
     for(int comp=0; comp<numComp; comp++)
253 e8925fcd Leszek Koltunski
       {
254 f45e4279 Leszek Koltunski
       start = end+1;
255 97755c02 Leszek Koltunski
       end   = mEffComponent.get(comp);
256 e8925fcd Leszek Koltunski
257 97755c02 Leszek Koltunski
       if( mAUB.matchesAssociation(comp, andAssoc, equAssoc) )
258 e8925fcd Leszek Koltunski
         {
259 62c869ad Leszek Koltunski
         applyMatrixToComponent(matrixP, matrixV, start, end);
260 e8925fcd Leszek Koltunski
         }
261
       }
262
263
     mVBO1.invalidate();
264
     }
265
266
///////////////////////////////////////////////////////////////////////////////////////////////////
267
268 62c869ad Leszek Koltunski
   private void applyMatrixToComponent(float[] matrixP, float[] matrixV, int start, int end)
269 e8925fcd Leszek Koltunski
     {
270
     float x,y,z;
271
272 f45e4279 Leszek Koltunski
     for(int index=start*VERT1_ATTRIBS; index<=end*VERT1_ATTRIBS; index+=VERT1_ATTRIBS )
273 e8925fcd Leszek Koltunski
       {
274
       x = mVertAttribs1[index+POS_ATTRIB  ];
275
       y = mVertAttribs1[index+POS_ATTRIB+1];
276
       z = mVertAttribs1[index+POS_ATTRIB+2];
277
278 62c869ad Leszek Koltunski
       mVertAttribs1[index+POS_ATTRIB  ] = matrixP[0]*x + matrixP[4]*y + matrixP[ 8]*z + matrixP[12];
279
       mVertAttribs1[index+POS_ATTRIB+1] = matrixP[1]*x + matrixP[5]*y + matrixP[ 9]*z + matrixP[13];
280
       mVertAttribs1[index+POS_ATTRIB+2] = matrixP[2]*x + matrixP[6]*y + matrixP[10]*z + matrixP[14];
281 e8925fcd Leszek Koltunski
282
       x = mVertAttribs1[index+NOR_ATTRIB  ];
283
       y = mVertAttribs1[index+NOR_ATTRIB+1];
284
       z = mVertAttribs1[index+NOR_ATTRIB+2];
285
286 62c869ad Leszek Koltunski
       mVertAttribs1[index+NOR_ATTRIB  ] = matrixV[0]*x + matrixV[4]*y + matrixV[ 8]*z;
287
       mVertAttribs1[index+NOR_ATTRIB+1] = matrixV[1]*x + matrixV[5]*y + matrixV[ 9]*z;
288
       mVertAttribs1[index+NOR_ATTRIB+2] = matrixV[2]*x + matrixV[6]*y + matrixV[10]*z;
289
290
       x = mVertAttribs1[index+NOR_ATTRIB  ];
291
       y = mVertAttribs1[index+NOR_ATTRIB+1];
292
       z = mVertAttribs1[index+NOR_ATTRIB+2];
293
294
       float len1 = (float)Math.sqrt(x*x + y*y + z*z);
295
296
       if( len1>0.0f )
297
         {
298
         mVertAttribs1[index+NOR_ATTRIB  ] /= len1;
299
         mVertAttribs1[index+NOR_ATTRIB+1] /= len1;
300
         mVertAttribs1[index+NOR_ATTRIB+2] /= len1;
301
         }
302 e8925fcd Leszek Koltunski
303
       x = mVertAttribs1[index+INF_ATTRIB  ];
304
       y = mVertAttribs1[index+INF_ATTRIB+1];
305
       z = mVertAttribs1[index+INF_ATTRIB+2];
306
307 62c869ad Leszek Koltunski
       mVertAttribs1[index+INF_ATTRIB  ] = matrixV[0]*x + matrixV[4]*y + matrixV[ 8]*z;
308
       mVertAttribs1[index+INF_ATTRIB+1] = matrixV[1]*x + matrixV[5]*y + matrixV[ 9]*z;
309
       mVertAttribs1[index+INF_ATTRIB+2] = matrixV[2]*x + matrixV[6]*y + matrixV[10]*z;
310
311
       x = mVertAttribs1[index+INF_ATTRIB  ];
312
       y = mVertAttribs1[index+INF_ATTRIB+1];
313
       z = mVertAttribs1[index+INF_ATTRIB+2];
314
315
       float len2 = (float)Math.sqrt(x*x + y*y + z*z);
316
317
       if( len2>0.0f )
318
         {
319
         mVertAttribs1[index+INF_ATTRIB  ] /= len2;
320
         mVertAttribs1[index+INF_ATTRIB+1] /= len2;
321
         mVertAttribs1[index+INF_ATTRIB+2] /= len2;
322
         }
323 e8925fcd Leszek Koltunski
       }
324
     }
325
326 26671ef8 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
327
328
   void setEffectAssociationNow(int component, int andAssociation, int equAssociation)
329
     {
330 97755c02 Leszek Koltunski
     mAUB.setEffectAssociationNow(component, andAssociation, equAssociation);
331 26671ef8 Leszek Koltunski
     }
332
333 42571056 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
334 6c00149d Leszek Koltunski
// when a derived class is done computing its mesh, it has to call this method.
335 42571056 Leszek Koltunski
336 e54bfada Leszek Koltunski
   void setAttribs(float[] vert1Attribs, float[] vert2Attribs)
337 42571056 Leszek Koltunski
     {
338 e54bfada Leszek Koltunski
     mNumVertices = vert1Attribs.length/VERT1_ATTRIBS;
339
     mVertAttribs1= vert1Attribs;
340
     mVertAttribs2= vert2Attribs;
341 da681e7e Leszek Koltunski
342 e8925fcd Leszek Koltunski
     mTexComponent.add(new TexComponent(mNumVertices-1));
343
     mEffComponent.add(mNumVertices-1);
344 c90aca24 Leszek Koltunski
345 e54bfada Leszek Koltunski
     mVBO1.invalidate();
346
     mVBO2.invalidate();
347 42571056 Leszek Koltunski
     }
348
349
///////////////////////////////////////////////////////////////////////////////////////////////////
350
351 1fad573e Leszek Koltunski
   void joinAttribs(MeshBase[] meshes)
352 226144d0 leszek
     {
353 0d4aae88 Leszek Koltunski
     MeshBase mesh;
354 e8925fcd Leszek Koltunski
     TexComponent comp;
355 a8dfedcc Leszek Koltunski
     int numMeshes = meshes.length;
356 e8925fcd Leszek Koltunski
     int numVertices,origVertices = mNumVertices;
357 26671ef8 Leszek Koltunski
     int origTexComponents,numTexComponents;
358 e8925fcd Leszek Koltunski
     int origEffComponents=0,numEffComponents;
359 044b5494 Leszek Koltunski
360 0d4aae88 Leszek Koltunski
     if( origVertices>0 )
361
       {
362 e8925fcd Leszek Koltunski
       origTexComponents = mTexComponent.size();
363 1fad573e Leszek Koltunski
       mNumVertices+= ( mNumVertices%2==1 ? 2:1 );
364 e8925fcd Leszek Koltunski
       mTexComponent.get(origTexComponents-1).mEndIndex = mNumVertices-1;
365
       origEffComponents = mEffComponent.size();
366
       mEffComponent.set(origEffComponents-1,mNumVertices-1);
367 0d4aae88 Leszek Koltunski
       }
368 044b5494 Leszek Koltunski
369 e7f85322 Leszek Koltunski
     for(int i=0; i<numMeshes; i++)
370 0d4aae88 Leszek Koltunski
       {
371
       mesh = meshes[i];
372 e8925fcd Leszek Koltunski
       numTexComponents = mesh.mTexComponent.size();
373
       numEffComponents = mesh.mEffComponent.size();
374 36d65d88 Leszek Koltunski
       numVertices = mesh.mNumVertices;
375
376 1fad573e Leszek Koltunski
       int extraVerticesBefore = mNumVertices==0 ? 0:1;
377
       int extraVerticesAfter  = (i==numMeshes-1) ? 0 : (numVertices%2==1 ? 2:1);
378 36d65d88 Leszek Koltunski
379 e8925fcd Leszek Koltunski
       for(int j=0; j<numTexComponents; j++)
380 0d4aae88 Leszek Koltunski
         {
381 e8925fcd Leszek Koltunski
         comp = new TexComponent(mesh.mTexComponent.get(j));
382 e0343804 Leszek Koltunski
         comp.mEndIndex += (extraVerticesBefore+mNumVertices);
383
         if( j==numTexComponents-1) comp.mEndIndex += extraVerticesAfter;
384 e8925fcd Leszek Koltunski
         mTexComponent.add(comp);
385
         }
386 23b733db Leszek Koltunski
387 e8925fcd Leszek Koltunski
       for(int j=0; j<numEffComponents; j++)
388
         {
389
         int index = mesh.mEffComponent.get(j);
390 e0343804 Leszek Koltunski
         index += (extraVerticesBefore+mNumVertices);
391
         if( j==numEffComponents-1 ) index += extraVerticesAfter;
392 e8925fcd Leszek Koltunski
         mEffComponent.add(index);
393
394
         if( origEffComponents<MAX_EFFECT_COMPONENTS )
395 36d65d88 Leszek Koltunski
           {
396 97755c02 Leszek Koltunski
           mAUB.copy(origEffComponents, mesh.mAUB, j);
397 e8925fcd Leszek Koltunski
           origEffComponents++;
398 36d65d88 Leszek Koltunski
           }
399 e7f85322 Leszek Koltunski
         }
400 a8dfedcc Leszek Koltunski
401 1fad573e Leszek Koltunski
       mNumVertices += (extraVerticesBefore+numVertices+extraVerticesAfter);
402
       }
403 a8dfedcc Leszek Koltunski
404 1fad573e Leszek Koltunski
     float[] newAttribs1 = new float[VERT1_ATTRIBS*mNumVertices];
405 e54bfada Leszek Koltunski
     float[] newAttribs2 = new float[VERT2_ATTRIBS*mNumVertices];
406 1fad573e Leszek Koltunski
     numVertices = origVertices;
407 0d4aae88 Leszek Koltunski
408
     if( origVertices>0 )
409
       {
410 e8925fcd Leszek Koltunski
       System.arraycopy(mVertAttribs1,                              0, newAttribs1,                          0, VERT1_ATTRIBS*numVertices);
411
       System.arraycopy(mVertAttribs1, VERT1_ATTRIBS*(origVertices-1), newAttribs1, VERT1_ATTRIBS*origVertices, VERT1_ATTRIBS            );
412
       System.arraycopy(mVertAttribs2,                              0, newAttribs2,                          0, VERT2_ATTRIBS*numVertices);
413
       System.arraycopy(mVertAttribs2, VERT2_ATTRIBS*(origVertices-1), newAttribs2, VERT2_ATTRIBS*origVertices, VERT2_ATTRIBS            );
414 0d4aae88 Leszek Koltunski
       origVertices++;
415
416 e7f85322 Leszek Koltunski
       if( numVertices%2==1 )
417 0d4aae88 Leszek Koltunski
         {
418 1fad573e Leszek Koltunski
         System.arraycopy(mVertAttribs1, VERT1_ATTRIBS*(origVertices-1), newAttribs1, VERT1_ATTRIBS*origVertices, VERT1_ATTRIBS);
419 e54bfada Leszek Koltunski
         System.arraycopy(mVertAttribs2, VERT2_ATTRIBS*(origVertices-1), newAttribs2, VERT2_ATTRIBS*origVertices, VERT2_ATTRIBS);
420 0d4aae88 Leszek Koltunski
         origVertices++;
421
         }
422
       }
423
424 e7f85322 Leszek Koltunski
     for(int i=0; i<numMeshes; i++)
425 0d4aae88 Leszek Koltunski
       {
426
       mesh = meshes[i];
427 e7f85322 Leszek Koltunski
       numVertices = mesh.mNumVertices;
428 0d4aae88 Leszek Koltunski
429 22e60fba Leszek Koltunski
       if( origVertices>0 )
430
         {
431 e8925fcd Leszek Koltunski
         System.arraycopy(mesh.mVertAttribs1, 0, newAttribs1, VERT1_ATTRIBS*origVertices, VERT1_ATTRIBS);
432
         System.arraycopy(mesh.mVertAttribs2, 0, newAttribs2, VERT2_ATTRIBS*origVertices, VERT2_ATTRIBS);
433 22e60fba Leszek Koltunski
         origVertices++;
434
         }
435 1fad573e Leszek Koltunski
       System.arraycopy(mesh.mVertAttribs1, 0, newAttribs1, VERT1_ATTRIBS*origVertices, VERT1_ATTRIBS*numVertices);
436 e54bfada Leszek Koltunski
       System.arraycopy(mesh.mVertAttribs2, 0, newAttribs2, VERT2_ATTRIBS*origVertices, VERT2_ATTRIBS*numVertices);
437 e7f85322 Leszek Koltunski
       origVertices+=numVertices;
438 0d4aae88 Leszek Koltunski
439 e7f85322 Leszek Koltunski
       if( i<numMeshes-1 )
440 0d4aae88 Leszek Koltunski
         {
441 1fad573e Leszek Koltunski
         System.arraycopy(mesh.mVertAttribs1, VERT1_ATTRIBS*(numVertices-1), newAttribs1, VERT1_ATTRIBS*origVertices, VERT1_ATTRIBS);
442 e54bfada Leszek Koltunski
         System.arraycopy(mesh.mVertAttribs2, VERT2_ATTRIBS*(numVertices-1), newAttribs2, VERT2_ATTRIBS*origVertices, VERT2_ATTRIBS);
443 0d4aae88 Leszek Koltunski
         origVertices++;
444
445 e7f85322 Leszek Koltunski
         if( numVertices%2==1 )
446 0d4aae88 Leszek Koltunski
           {
447 1fad573e Leszek Koltunski
           System.arraycopy(mesh.mVertAttribs1, VERT1_ATTRIBS*(numVertices-1), newAttribs1, VERT1_ATTRIBS*origVertices, VERT1_ATTRIBS);
448 e54bfada Leszek Koltunski
           System.arraycopy(mesh.mVertAttribs2, VERT2_ATTRIBS*(numVertices-1), newAttribs2, VERT2_ATTRIBS*origVertices, VERT2_ATTRIBS);
449 0d4aae88 Leszek Koltunski
           origVertices++;
450
           }
451
         }
452
       }
453
454
     if( origVertices!=mNumVertices )
455
       {
456
       android.util.Log.e("mesh", "join: origVertices: "+origVertices+" numVertices: "+mNumVertices);
457
       }
458
459 e8925fcd Leszek Koltunski
     int endIndex, index=0, numEffComp = mEffComponent.size();
460 36d65d88 Leszek Koltunski
461 e8925fcd Leszek Koltunski
     for(int component=0; component<numEffComp; component++)
462 36d65d88 Leszek Koltunski
       {
463 e8925fcd Leszek Koltunski
       endIndex = mEffComponent.get(component);
464 36d65d88 Leszek Koltunski
465
       for( ; index<=endIndex; index++) newAttribs2[VERT2_ATTRIBS*index+COM_ATTRIB] = component;
466
       }
467
468 1fad573e Leszek Koltunski
     mVertAttribs1 = newAttribs1;
469 e54bfada Leszek Koltunski
     mVertAttribs2 = newAttribs2;
470 1fad573e Leszek Koltunski
     mVBO1.invalidate();
471 e54bfada Leszek Koltunski
     mVBO2.invalidate();
472 23b733db Leszek Koltunski
     }
473
474 a8dfedcc Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
475
// called from MeshJoined
476
477
   void join(MeshBase[] meshes)
478
     {
479 1fad573e Leszek Koltunski
     boolean immediateJoin = true;
480 a8dfedcc Leszek Koltunski
481 1fad573e Leszek Koltunski
     for (MeshBase mesh : meshes)
482 a8dfedcc Leszek Koltunski
       {
483 1fad573e Leszek Koltunski
       if (mesh.mJobNode[0] != null)
484
         {
485
         immediateJoin = false;
486
         break;
487
         }
488 a8dfedcc Leszek Koltunski
       }
489
490 1fad573e Leszek Koltunski
     if( immediateJoin ) joinAttribs(meshes);
491
     else                mJobNode[0] = DeferredJobs.join(this,meshes);
492
     }
493 a8dfedcc Leszek Koltunski
494 1fad573e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
495 a8dfedcc Leszek Koltunski
496 dbe3079d Leszek Koltunski
   void textureMap(Static4D[] maps, int startComponent)
497 1fad573e Leszek Koltunski
     {
498 e8925fcd Leszek Koltunski
     int num_comp = mTexComponent.size();
499 1fad573e Leszek Koltunski
     int num_maps = maps.length;
500 dbe3079d Leszek Koltunski
     int min = Math.min(num_comp-startComponent, num_maps);
501 96877ab4 Leszek Koltunski
     int vertex = startComponent>0 ? mTexComponent.get(startComponent-1).mEndIndex+1 : 0;
502 1fad573e Leszek Koltunski
     Static4D newMap, oldMap;
503 e8925fcd Leszek Koltunski
     TexComponent comp;
504 1fad573e Leszek Koltunski
     float newW, newH, ratW, ratH, movX, movY;
505 a8dfedcc Leszek Koltunski
506 1fad573e Leszek Koltunski
     for(int i=0; i<min; i++)
507
       {
508
       newMap = maps[i];
509 dbe3079d Leszek Koltunski
       comp = mTexComponent.get(i+startComponent);
510 1fad573e Leszek Koltunski
511
       if( newMap!=null )
512 a8dfedcc Leszek Koltunski
         {
513 1fad573e Leszek Koltunski
         newW = newMap.get2();
514
         newH = newMap.get3();
515 a8dfedcc Leszek Koltunski
516 1fad573e Leszek Koltunski
         if( newW!=0.0f && newH!=0.0f )
517 a8dfedcc Leszek Koltunski
           {
518 1fad573e Leszek Koltunski
           oldMap = comp.mTextureMap;
519
           ratW = newW/oldMap.get2();
520
           ratH = newH/oldMap.get3();
521
           movX = newMap.get0() - ratW*oldMap.get0();
522
           movY = newMap.get1() - ratH*oldMap.get1();
523
524
           for( int index=vertex*VERT2_ATTRIBS+TEX_ATTRIB ; vertex<=comp.mEndIndex; vertex++, index+=VERT2_ATTRIBS)
525
             {
526
             mVertAttribs2[index  ] = ratW*mVertAttribs2[index  ] + movX;
527
             mVertAttribs2[index+1] = ratH*mVertAttribs2[index+1] + movY;
528
             }
529
           comp.setMap(newMap);
530 a8dfedcc Leszek Koltunski
           }
531
         }
532
533 1fad573e Leszek Koltunski
       vertex= comp.mEndIndex+1;
534 a8dfedcc Leszek Koltunski
       }
535
536 1fad573e Leszek Koltunski
     mVBO2.invalidate();
537
     }
538
539
///////////////////////////////////////////////////////////////////////////////////////////////////
540 a8dfedcc Leszek Koltunski
541 e8925fcd Leszek Koltunski
   public static int getMaxEffComponents()
542 1fad573e Leszek Koltunski
     {
543 e8925fcd Leszek Koltunski
     return MAX_EFFECT_COMPONENTS;
544 1fad573e Leszek Koltunski
     }
545
546 f046b159 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
547
/**
548
 * Not part of public API, do not document (public only because has to be used from the main package)
549
 *
550
 * @y.exclude
551
 */
552
   public void copyTransformToVertex()
553
     {
554 e54bfada Leszek Koltunski
     ByteBuffer buffer = (ByteBuffer)GLES30.glMapBufferRange( GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, 0,
555
                                                              TRAN_SIZE*mNumVertices, GLES30.GL_MAP_READ_BIT);
556 f046b159 Leszek Koltunski
     if( buffer!=null )
557
       {
558 e54bfada Leszek Koltunski
       FloatBuffer feedback = buffer.order(ByteOrder.nativeOrder()).asFloatBuffer();
559
       feedback.get(mVertAttribs1,0,VERT1_ATTRIBS*mNumVertices);
560 0bd9f644 Leszek Koltunski
       mVBO1.updateFloat(mVertAttribs1);
561 f046b159 Leszek Koltunski
       }
562
     else
563
       {
564 b5be333a Leszek Koltunski
       int error = GLES30.glGetError();
565
       Log.e("mesh", "failed to map tf buffer, error="+error);
566 f046b159 Leszek Koltunski
       }
567
568 b5be333a Leszek Koltunski
     GLES30.glUnmapBuffer(GLES30.GL_TRANSFORM_FEEDBACK);
569 f046b159 Leszek Koltunski
     }
570
571 1fad573e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
572
/**
573
 * Not part of public API, do not document (public only because has to be used from the main package)
574
 *
575
 * @y.exclude
576
 */
577
   public void debug()
578
     {
579
     if( mJobNode[0]!=null )
580
       {
581
       mJobNode[0].print(0);
582
       }
583
     else
584
       {
585
       android.util.Log.e("mesh", "JobNode null");
586
       }
587
     }
588
589
///////////////////////////////////////////////////////////////////////////////////////////////////
590
/**
591
 * Not part of public API, do not document (public only because has to be used from the main package)
592
 *
593
 * @y.exclude
594
 */
595 e0343804 Leszek Koltunski
   public void printPos()
596 1fad573e Leszek Koltunski
     {
597 26671ef8 Leszek Koltunski
     StringBuilder sb = new StringBuilder();
598 1fad573e Leszek Koltunski
599
     for(int i=0; i<mNumVertices; i++)
600
       {
601
       sb.append('(');
602
       sb.append(mVertAttribs1[VERT1_ATTRIBS*i+POS_ATTRIB  ]);
603
       sb.append(',');
604
       sb.append(mVertAttribs1[VERT1_ATTRIBS*i+POS_ATTRIB+1]);
605
       sb.append(',');
606
       sb.append(mVertAttribs1[VERT1_ATTRIBS*i+POS_ATTRIB+2]);
607
       sb.append(") ");
608
       }
609 e0343804 Leszek Koltunski
     Log.d("mesh", sb.toString());
610
     }
611 1fad573e Leszek Koltunski
612 e0343804 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
613
/**
614
 * Not part of public API, do not document (public only because has to be used from the main package)
615
 *
616
 * @y.exclude
617
 */
618
   public void printCom()
619
     {
620
     StringBuilder sb = new StringBuilder();
621 1fad573e Leszek Koltunski
622
     for(int i=0; i<mNumVertices; i++)
623
       {
624 e0343804 Leszek Koltunski
       sb.append(mVertAttribs2[VERT2_ATTRIBS*i+COM_ATTRIB]);
625
       sb.append(' ');
626
       }
627
628
     Log.d("mesh", sb.toString());
629
     }
630
631
///////////////////////////////////////////////////////////////////////////////////////////////////
632
/**
633
 * Not part of public API, do not document (public only because has to be used from the main package)
634
 *
635
 * @y.exclude
636
 */
637
   public void printTex()
638
     {
639
     int vert=0;
640
     int num = numTexComponents();
641
     StringBuilder sb = new StringBuilder();
642
     sb.append("tex components: ").append(num);
643
644
     for(int i=0; i<num; i++)
645
       {
646
       int end = mTexComponent.get(i).mEndIndex;
647
       sb.append("\n");
648
649
       for( ; vert<=end; vert++)
650
         {
651
         sb.append(' ');
652
         sb.append('(');
653
         sb.append(mVertAttribs2[VERT2_ATTRIBS*vert+TEX_ATTRIB]);
654
         sb.append(',');
655
         sb.append(mVertAttribs2[VERT2_ATTRIBS*vert+TEX_ATTRIB+1]);
656
         sb.append(')');
657
         }
658 1fad573e Leszek Koltunski
       }
659
660 e0343804 Leszek Koltunski
     Log.d("mesh", sb.toString());
661 1fad573e Leszek Koltunski
     }
662
663 23b733db Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
664
/**
665 0d4aae88 Leszek Koltunski
 * Not part of public API, do not document (public only because has to be used from the main package)
666
 *
667
 * @y.exclude
668 23b733db Leszek Koltunski
 */
669 0d4aae88 Leszek Koltunski
   public int getTFO()
670 23b733db Leszek Koltunski
     {
671 0bd9f644 Leszek Koltunski
     return mTFO.createImmediatelyFloat(mNumVertices*TRAN_SIZE, null);
672 23b733db Leszek Koltunski
     }
673
674
///////////////////////////////////////////////////////////////////////////////////////////////////
675
/**
676 0d4aae88 Leszek Koltunski
 * Not part of public API, do not document (public only because has to be used from the main package)
677
 *
678
 * @y.exclude
679 23b733db Leszek Koltunski
 */
680 0d4aae88 Leszek Koltunski
   public int getNumVertices()
681 23b733db Leszek Koltunski
     {
682 0d4aae88 Leszek Koltunski
     return mNumVertices;
683 23b733db Leszek Koltunski
     }
684
685 36d65d88 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
686
/**
687
 * Not part of public API, do not document (public only because has to be used from the main package)
688
 *
689
 * @y.exclude
690
 */
691 0bd9f644 Leszek Koltunski
   public void send(int programH)
692 36d65d88 Leszek Koltunski
     {
693 97755c02 Leszek Koltunski
     int index = mAUB.getIndex();
694 0bd9f644 Leszek Koltunski
     GLES30.glBindBufferBase(GLES30.GL_UNIFORM_BUFFER, UBO_BINDING, index);
695
     GLES30.glUniformBlockBinding(programH, UBO_BINDING, index);
696 36d65d88 Leszek Koltunski
     }
697
698 044b5494 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
699 6c00149d Leszek Koltunski
/**
700
 * Not part of public API, do not document (public only because has to be used from the main package)
701
 *
702
 * @y.exclude
703
 */
704 da681e7e Leszek Koltunski
   public void bindVertexAttribs(DistortedProgram program)
705 6c00149d Leszek Koltunski
     {
706 71d8aba1 Leszek Koltunski
     if( mJobNode[0]!=null )
707 f046b159 Leszek Koltunski
       {
708 71d8aba1 Leszek Koltunski
       mJobNode[0].execute();  // this will set itself to null
709 bc2ab8c5 Leszek Koltunski
/*
710
       try
711
         {
712
         String name = "/sdcard/"+mNumVertices+".dmesh";
713
         DataOutputStream dos = new DataOutputStream(new FileOutputStream(name));
714
         write(dos);
715 9f28e9b6 Leszek Koltunski
         android.util.Log.e("mesh", "file written: "+name);
716 bc2ab8c5 Leszek Koltunski
         }
717
       catch(FileNotFoundException ex)
718
         {
719
         android.util.Log.e("mesh", "file not found exception: "+ex.toString());
720
         }
721
 */
722 f046b159 Leszek Koltunski
       }
723
724 0bd9f644 Leszek Koltunski
     int index1 = mVBO1.createImmediatelyFloat(mNumVertices*VERT1_SIZE, mVertAttribs1);
725
     int index2 = mVBO2.createImmediatelyFloat(mNumVertices*VERT2_SIZE, mVertAttribs2);
726 22e60fba Leszek Koltunski
727 e54bfada Leszek Koltunski
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, index1 );
728
     GLES30.glVertexAttribPointer(program.mAttribute[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, VERT1_SIZE, OFFSET_POS);
729
     GLES30.glVertexAttribPointer(program.mAttribute[1], NOR_DATA_SIZE, GLES30.GL_FLOAT, false, VERT1_SIZE, OFFSET_NOR);
730
     GLES30.glVertexAttribPointer(program.mAttribute[2], INF_DATA_SIZE, GLES30.GL_FLOAT, false, VERT1_SIZE, OFFSET_INF);
731
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, index2 );
732
     GLES30.glVertexAttribPointer(program.mAttribute[3], TEX_DATA_SIZE, GLES30.GL_FLOAT, false, VERT2_SIZE, OFFSET_TEX);
733 36d65d88 Leszek Koltunski
     GLES30.glVertexAttribPointer(program.mAttribute[4], COM_DATA_SIZE, GLES30.GL_FLOAT, false, VERT2_SIZE, OFFSET_COM);
734 b7074bc6 Leszek Koltunski
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, 0);
735 da681e7e Leszek Koltunski
     }
736
737
///////////////////////////////////////////////////////////////////////////////////////////////////
738
/**
739
 * Not part of public API, do not document (public only because has to be used from the main package)
740
 *
741
 * @y.exclude
742
 */
743
   public void bindTransformAttribs(DistortedProgram program)
744
     {
745 0bd9f644 Leszek Koltunski
     int index = mTFO.createImmediatelyFloat(mNumVertices*TRAN_SIZE, null);
746 22e60fba Leszek Koltunski
747 b7074bc6 Leszek Koltunski
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, index );
748
     GLES30.glVertexAttribPointer(program.mAttribute[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, 0, 0);
749
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, 0);
750 6c00149d Leszek Koltunski
     }
751
752 7a5e538a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
753
/**
754
 * Not part of public API, do not document (public only because has to be used from the main package)
755
 *
756
 * @y.exclude
757
 */
758
   public void setInflate(float inflate)
759
     {
760
     mInflate = inflate;
761
     }
762
763
///////////////////////////////////////////////////////////////////////////////////////////////////
764
/**
765
 * Not part of public API, do not document (public only because has to be used from the main package)
766
 *
767
 * @y.exclude
768
 */
769
   public float getInflate()
770
     {
771
     return mInflate;
772
     }
773
774 466450b5 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
775 54ce4c6f Leszek Koltunski
// Return the number of bytes read.
776 22422a76 Leszek Koltunski
777 54ce4c6f Leszek Koltunski
   int read(DataInputStream stream)
778 10836924 Leszek Koltunski
     {
779 bc2ab8c5 Leszek Koltunski
     byte[] buffer = new byte[BYTES_PER_FLOAT*4];
780
     int version, numEff, numTex;
781 22422a76 Leszek Koltunski
782
     try
783
       {
784 bc2ab8c5 Leszek Koltunski
       stream.read(buffer);
785
       ByteBuffer byteBuf = ByteBuffer.wrap(buffer);
786 22422a76 Leszek Koltunski
787 bc2ab8c5 Leszek Koltunski
       version = byteBuf.getInt(0);
788 22422a76 Leszek Koltunski
789
       if( version==1 )
790
         {
791 bc2ab8c5 Leszek Koltunski
         mNumVertices= byteBuf.getInt(4);
792
         numTex      = byteBuf.getInt(8);
793
         numEff      = byteBuf.getInt(12);
794 22422a76 Leszek Koltunski
         }
795
       else
796
         {
797 bc2ab8c5 Leszek Koltunski
         android.util.Log.e("mesh", "Error: unknown mesh file version "+String.format("0x%08X", version));
798 54ce4c6f Leszek Koltunski
         return 0;
799 22422a76 Leszek Koltunski
         }
800
       }
801
     catch(IOException e)
802
       {
803 bc2ab8c5 Leszek Koltunski
       android.util.Log.e("mesh", "IOException1 trying to read file: "+e.toString());
804 54ce4c6f Leszek Koltunski
       return 0;
805 22422a76 Leszek Koltunski
       }
806
807 bc2ab8c5 Leszek Koltunski
     if( mNumVertices>0 && numEff>0 && numTex>0 )
808 22422a76 Leszek Koltunski
       {
809
       int size = numEff+TEX_COMP_SIZE*numTex;
810
       float[] tmp = new float[size];
811 bc2ab8c5 Leszek Koltunski
       mVertAttribs1 = new float[VERT1_ATTRIBS*mNumVertices];
812
       mVertAttribs2 = new float[VERT2_ATTRIBS*mNumVertices];
813 22422a76 Leszek Koltunski
814 bc2ab8c5 Leszek Koltunski
       buffer = new byte[BYTES_PER_FLOAT*(size + mNumVertices*(VERT1_ATTRIBS+VERT2_ATTRIBS))];
815 22422a76 Leszek Koltunski
816 bc2ab8c5 Leszek Koltunski
       try
817
         {
818
         stream.read(buffer);
819
         }
820
       catch(IOException e)
821
         {
822
         android.util.Log.e("mesh", "IOException2 trying to read file: "+e.toString());
823 54ce4c6f Leszek Koltunski
         return 0;
824 bc2ab8c5 Leszek Koltunski
         }
825
826
       ByteBuffer byteBuf = ByteBuffer.wrap(buffer);
827
       FloatBuffer floatBuf = byteBuf.asFloatBuffer();
828 22422a76 Leszek Koltunski
829 bc2ab8c5 Leszek Koltunski
       floatBuf.get(tmp,0,size);
830
       floatBuf.get(mVertAttribs1, 0, VERT1_ATTRIBS*mNumVertices);
831
       floatBuf.get(mVertAttribs2, 0, VERT2_ATTRIBS*mNumVertices);
832 54ce4c6f Leszek Koltunski
833 22422a76 Leszek Koltunski
       TexComponent tex;
834
       int index, texComp;
835
       float x, y, z, w;
836
837
       for(texComp=0; texComp<numTex; texComp++)
838
         {
839
         index = (int)tmp[TEX_COMP_SIZE*texComp];
840
841
         x= tmp[TEX_COMP_SIZE*texComp+1];
842
         y= tmp[TEX_COMP_SIZE*texComp+2];
843
         z= tmp[TEX_COMP_SIZE*texComp+3];
844
         w= tmp[TEX_COMP_SIZE*texComp+4];
845
846
         tex = new TexComponent(index);
847
         tex.setMap(new Static4D(x,y,z,w));
848 10836924 Leszek Koltunski
849 22422a76 Leszek Koltunski
         mTexComponent.add(tex);
850
         }
851
852
       for(int effComp=0; effComp<numEff; effComp++)
853
         {
854
         index = (int)tmp[TEX_COMP_SIZE*texComp + effComp ];
855
         mEffComponent.add(index);
856
         }
857
       }
858 54ce4c6f Leszek Koltunski
859
     return BYTES_PER_FLOAT*( 4 + numEff+TEX_COMP_SIZE*numTex + VERT1_ATTRIBS*mNumVertices + VERT2_ATTRIBS*mNumVertices );
860 10836924 Leszek Koltunski
     }
861
862 524e4fe7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
863
/**
864
 * @y.exclude
865
 */
866
   public int getLastVertexEff(int effComponent)
867
     {
868
     if( effComponent>=0 && effComponent<mTexComponent.size() )
869
       {
870
       return mEffComponent.get(effComponent);
871
       }
872
873
     return 0;
874
     }
875
876
///////////////////////////////////////////////////////////////////////////////////////////////////
877
/**
878
 * @y.exclude
879
 */
880
   public int getLastVertexTex(int texComponent)
881
     {
882
     if( texComponent>=0 && texComponent<mTexComponent.size() )
883
       {
884
       return mTexComponent.get(texComponent).mEndIndex;
885
       }
886
887
     return 0;
888
     }
889
890 22422a76 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
891
// PUBLIC API
892 10836924 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
893
/**
894 22422a76 Leszek Koltunski
 * Write the Mesh to a File.
895 10836924 Leszek Koltunski
 */
896 bc2ab8c5 Leszek Koltunski
   public void write(DataOutputStream stream)
897 10836924 Leszek Koltunski
     {
898 22422a76 Leszek Koltunski
     TexComponent tex;
899
900
     int numTex = mTexComponent.size();
901
     int numEff = mEffComponent.size();
902
903 bc2ab8c5 Leszek Koltunski
     try
904 22422a76 Leszek Koltunski
       {
905 bc2ab8c5 Leszek Koltunski
       stream.writeInt(1);  // version
906
       stream.writeInt(mNumVertices);
907
       stream.writeInt(numTex);
908
       stream.writeInt(numEff);
909 22422a76 Leszek Koltunski
910 bc2ab8c5 Leszek Koltunski
       for(int i=0; i<numTex; i++)
911
         {
912
         tex = mTexComponent.get(i);
913 22422a76 Leszek Koltunski
914 bc2ab8c5 Leszek Koltunski
         stream.writeFloat((float)tex.mEndIndex);
915
         stream.writeFloat(tex.mTextureMap.get0());
916
         stream.writeFloat(tex.mTextureMap.get1());
917
         stream.writeFloat(tex.mTextureMap.get2());
918
         stream.writeFloat(tex.mTextureMap.get3());
919
         }
920 22422a76 Leszek Koltunski
921 bc2ab8c5 Leszek Koltunski
       for(int i=0; i<numEff; i++)
922
         {
923
         stream.writeFloat((float)mEffComponent.get(i));
924
         }
925 10836924 Leszek Koltunski
926 bc2ab8c5 Leszek Koltunski
       ByteBuffer vertBuf1 = ByteBuffer.allocate(VERT1_SIZE*mNumVertices);
927
       vertBuf1.asFloatBuffer().put(mVertAttribs1);
928
       ByteBuffer vertBuf2 = ByteBuffer.allocate(VERT2_SIZE*mNumVertices);
929
       vertBuf2.asFloatBuffer().put(mVertAttribs2);
930
931
       stream.write(vertBuf1.array());
932
       stream.write(vertBuf2.array());
933 22422a76 Leszek Koltunski
       }
934 bc2ab8c5 Leszek Koltunski
     catch(IOException ex)
935 22422a76 Leszek Koltunski
       {
936 bc2ab8c5 Leszek Koltunski
       android.util.Log.e("mesh", "IOException trying to write a mesh: "+ex.toString());
937 22422a76 Leszek Koltunski
       }
938 10836924 Leszek Koltunski
     }
939
940
///////////////////////////////////////////////////////////////////////////////////////////////////
941 3fc9327a Leszek Koltunski
/**
942
 * When rendering this Mesh, do we want to render the Normal vectors as well?
943 420836fc leszek
 * <p>
944
 * Will work only on OpenGL ES >= 3.0 devices.
945 3fc9327a Leszek Koltunski
 *
946
 * @param show Controls if we render the Normal vectors or not.
947
 */
948
   public void setShowNormals(boolean show)
949
     {
950 b7074bc6 Leszek Koltunski
     mShowNormals = show;
951 3fc9327a Leszek Koltunski
     }
952 466450b5 Leszek Koltunski
953 6c00149d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
954
/**
955
 * When rendering this mesh, should we also draw the normal vectors?
956
 *
957
 * @return <i>true</i> if we do render normal vectors
958
 */
959
   public boolean getShowNormals()
960
     {
961
     return mShowNormals;
962
     }
963
964 a8dfedcc Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
965
/**
966 e8925fcd Leszek Koltunski
 * Merge all texture components of this Mesh into a single one.
967 a8dfedcc Leszek Koltunski
 */
968 e8925fcd Leszek Koltunski
   public void mergeTexComponents()
969 a8dfedcc Leszek Koltunski
     {
970 1fad573e Leszek Koltunski
     if( mJobNode[0]==null )
971 a8dfedcc Leszek Koltunski
       {
972 e8925fcd Leszek Koltunski
       mergeTexComponentsNow();
973 a8dfedcc Leszek Koltunski
       }
974
     else
975
       {
976 e8925fcd Leszek Koltunski
       mJobNode[0] = DeferredJobs.mergeTex(this);
977
       }
978
     }
979
980
///////////////////////////////////////////////////////////////////////////////////////////////////
981
/**
982
 * Merge all effect components of this Mesh into a single one.
983
 */
984
   public void mergeEffComponents()
985
     {
986
     if( mJobNode[0]==null )
987
       {
988
       mergeEffComponentsNow();
989
       }
990
     else
991
       {
992
       mJobNode[0] = DeferredJobs.mergeEff(this);
993 a8dfedcc Leszek Koltunski
       }
994
     }
995
996 466450b5 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
997
/**
998
 * Release all internal resources.
999
 */
1000
   public void markForDeletion()
1001
     {
1002 e54bfada Leszek Koltunski
     mVertAttribs1 = null;
1003
     mVertAttribs2 = null;
1004 466450b5 Leszek Koltunski
1005 e54bfada Leszek Koltunski
     mVBO1.markForDeletion();
1006
     mVBO2.markForDeletion();
1007 466450b5 Leszek Koltunski
     mTFO.markForDeletion();
1008
     }
1009 fa8bc998 Leszek Koltunski
1010 e8925fcd Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1011
/**
1012
 * Apply a Matrix Effect to the components which match the (addAssoc,equAssoc) association.
1013
 * <p>
1014
 * This is a static, permanent modification of the vertices contained in this Mesh. If the effect
1015
 * contains any Dynamics, they will be evaluated at 0.
1016
 *
1017
 * @param effect List of Matrix Effects to apply to the Mesh.
1018
 * @param andAssoc 'Logical AND' association which defines which components will be affected.
1019
 * @param equAssoc 'equality' association which defines which components will be affected.
1020
 */
1021
   public void apply(MatrixEffect effect, int andAssoc, int equAssoc)
1022
     {
1023
     if( mJobNode[0]==null )
1024
       {
1025
       applyMatrix(effect,andAssoc,equAssoc);
1026
       }
1027
     else
1028
       {
1029
       mJobNode[0] = DeferredJobs.matrix(this,effect,andAssoc,equAssoc);
1030
       }
1031
     }
1032
1033 fa8bc998 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1034
/**
1035 f046b159 Leszek Koltunski
 * Apply a Vertex Effect to the vertex mesh.
1036 fa8bc998 Leszek Koltunski
 * <p>
1037 c90aca24 Leszek Koltunski
 * This is a static, permanent modification of the vertices contained in this Mesh. If the effects
1038 f046b159 Leszek Koltunski
 * contain any Dynamics, the Dynamics will be evaluated at 0.
1039 fa8bc998 Leszek Koltunski
 *
1040 f046b159 Leszek Koltunski
 * We would call this several times building up a list of Effects to do. This list of effects gets
1041
 * lazily executed only when the Mesh is used for rendering for the first time.
1042 e172985c Leszek Koltunski
 *
1043 f046b159 Leszek Koltunski
 * @param effect Vertex Effect to apply to the Mesh.
1044 fa8bc998 Leszek Koltunski
 */
1045 f046b159 Leszek Koltunski
   public void apply(VertexEffect effect)
1046 fa8bc998 Leszek Koltunski
     {
1047 71d8aba1 Leszek Koltunski
     mJobNode[0] = DeferredJobs.vertex(this,effect);
1048 c90aca24 Leszek Koltunski
     }
1049
1050
///////////////////////////////////////////////////////////////////////////////////////////////////
1051
/**
1052 a3a05347 Leszek Koltunski
 * Sets texture maps for (some of) the components of this mesh.
1053 c90aca24 Leszek Koltunski
 * <p>
1054 a3a05347 Leszek Koltunski
 * Format: ( x of lower-left corner, y of lower-left corner, width, height ).
1055
 * For example maps[0] = new Static4D( 0.0, 0.5, 0.5, 0.5 ) sets the 0th component texture map to the
1056 0d4aae88 Leszek Koltunski
 * upper-left quadrant of the texture.
1057 a3a05347 Leszek Koltunski
 * <p>
1058
 * Probably the most common user case would be sending as many maps as there are components in this
1059
 * Mesh. One can also send less, or more (the extraneous ones will be ignored) and set some of them
1060
 * to null (those will be ignored as well). So if there are 5 components, and we want to set the map
1061
 * of the 2nd and 4rd one, call this with
1062
 * maps = new Static4D[4]
1063
 * maps[0] = null
1064
 * maps[1] = the map for the 2nd component
1065
 * maps[2] = null
1066
 * maps[3] = the map for the 4th component
1067
 *
1068
 * A map's width and height have to be non-zero (but can be negative!)
1069 e172985c Leszek Koltunski
 *
1070 dbe3079d Leszek Koltunski
 * @param maps            List of texture maps to apply to the texture's components.
1071
 * @param startComponent  the component the first of the maps refers to.
1072 c90aca24 Leszek Koltunski
 */
1073 dbe3079d Leszek Koltunski
   public void setTextureMap(Static4D[] maps, int startComponent)
1074 c90aca24 Leszek Koltunski
     {
1075 1fad573e Leszek Koltunski
     if( mJobNode[0]==null )
1076 ea88d502 Leszek Koltunski
       {
1077 dbe3079d Leszek Koltunski
       textureMap(maps,startComponent);
1078 1fad573e Leszek Koltunski
       }
1079
     else
1080
       {
1081 dbe3079d Leszek Koltunski
       mJobNode[0] = DeferredJobs.textureMap(this,maps,startComponent);
1082 ea88d502 Leszek Koltunski
       }
1083 fa8bc998 Leszek Koltunski
     }
1084 9099e567 Leszek Koltunski
1085 e172985c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1086
/**
1087
 * Return the texture map of one of the components.
1088
 *
1089
 * @param component The component number whose texture map we want to retrieve.
1090
 */
1091
   public Static4D getTextureMap(int component)
1092
     {
1093 e8925fcd Leszek Koltunski
     return (component>=0 && component<mTexComponent.size()) ? mTexComponent.get(component).mTextureMap : null;
1094 e172985c Leszek Koltunski
     }
1095 cbd502ec Leszek Koltunski
1096
///////////////////////////////////////////////////////////////////////////////////////////////////
1097
/**
1098 1e672c1d Leszek Koltunski
 * Set Effect association.
1099 bc208a9c Leszek Koltunski
 *
1100 1e672c1d Leszek Koltunski
 * This creates an association between a Component of this Mesh and a Vertex Effect.
1101 2aeb75aa Leszek Koltunski
 * One can set two types of associations - an 'logical and' and a 'equal' associations and the Effect
1102
 * will only be active on vertices of Components such that
1103 36d65d88 Leszek Koltunski
 *
1104 2aeb75aa Leszek Koltunski
 * (effect andAssoc) & (component andAssoc) != 0 || (effect equAssoc) == (mesh equAssoc)
1105 36d65d88 Leszek Koltunski
 *
1106
 * (see main_vertex_shader)
1107 bc208a9c Leszek Koltunski
 *
1108
 * The point: this way we can configure the system so that each Vertex Effect acts only on a certain
1109 1e672c1d Leszek Koltunski
 * subset of a Mesh, thus potentially significantly reducing the number of render calls.
1110 cbd502ec Leszek Koltunski
 */
1111 26671ef8 Leszek Koltunski
   public void setEffectAssociation(int component, int andAssociation, int equAssociation)
1112
     {
1113
     if( component>=0 && component<MAX_EFFECT_COMPONENTS )
1114
       {
1115
       if( mJobNode[0]==null )
1116
         {
1117
         setEffectAssociationNow(component, andAssociation, equAssociation);
1118
         }
1119
       else
1120
         {
1121
         mJobNode[0] = DeferredJobs.effectAssoc(this,component,andAssociation,equAssociation);
1122
         }
1123
       }
1124
     }
1125 bc208a9c Leszek Koltunski
1126 524e4fe7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1127
/**
1128
 * Return the number of texture components, i.e. individual subsets of the whole set of vertices
1129
 * which can be independently textured.
1130
 *
1131
 * @return The number of Texture Components of this Mesh.
1132
 */
1133
   public int numTexComponents()
1134
     {
1135
     return mTexComponent.size();
1136
     }
1137
1138
///////////////////////////////////////////////////////////////////////////////////////////////////
1139
/**
1140
 * Return the number of 'effect' components, i.e. individual subsets of the whole set of vertices
1141
 * to which a VertexEffect can be addressed, independently of other vertices.
1142
 *
1143
 * @return The number of Effect Components of this Mesh.
1144
 */
1145
   public int numEffComponents()
1146
     {
1147
     return mEffComponent.size();
1148
     }
1149
1150 bc208a9c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1151
/**
1152 4f81e0c8 Leszek Koltunski
 * Copy the Mesh.
1153
 *
1154
 * @param deep If to be a deep or shallow copy of mVertAttribs1, i.e. the array holding vertices,
1155
 *             normals and inflates (the rest, in particular the mVertAttribs2 containing texture
1156 36d65d88 Leszek Koltunski
 *             coordinates and effect associations, is always deep copied)
1157 bc208a9c Leszek Koltunski
 */
1158 4f81e0c8 Leszek Koltunski
   public abstract MeshBase copy(boolean deep);
1159 bc208a9c Leszek Koltunski
   }