Project

General

Profile

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

library / src / main / java / org / distorted / library / mesh / MeshBase.java @ 5eb4cc34

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