Project

General

Profile

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

library / src / main / java / org / distorted / library / mesh / MeshBase.java @ a2878a67

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