Project

General

Profile

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

library / src / main / java / org / distorted / library / mesh / MeshBase.java @ 62c869ad

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