Project

General

Profile

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

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

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