Project

General

Profile

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

library / src / main / java / org / distorted / library / mesh / MeshBase.java @ 2acab35b

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