Project

General

Profile

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

library / src / main / java / org / distorted / library / mesh / MeshBase.java @ 524e4fe7

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