Project

General

Profile

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

library / src / main / java / org / distorted / library / mesh / MeshBase.java @ 1fad573e

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 f046b159 Leszek Koltunski
import android.util.Log;
24 6c00149d Leszek Koltunski
25 f046b159 Leszek Koltunski
import org.distorted.library.effect.VertexEffect;
26 36d65d88 Leszek Koltunski
import org.distorted.library.effectqueue.EffectQueue;
27 7602a827 Leszek Koltunski
import org.distorted.library.main.InternalBuffer;
28 da681e7e Leszek Koltunski
import org.distorted.library.program.DistortedProgram;
29 a3a05347 Leszek Koltunski
import org.distorted.library.type.Static4D;
30 6c00149d Leszek Koltunski
31 f046b159 Leszek Koltunski
import java.nio.ByteBuffer;
32
import java.nio.ByteOrder;
33
import java.nio.FloatBuffer;
34 c90aca24 Leszek Koltunski
import java.util.ArrayList;
35 6a06a912 Leszek Koltunski
36
///////////////////////////////////////////////////////////////////////////////////////////////////
37 e0b6c593 Leszek Koltunski
/**
38 e5ba319d Leszek Koltunski
 * Abstract class which represents a Mesh, ie an array of vertices (rendered as a TRIANGLE_STRIP).
39 e0b6c593 Leszek Koltunski
 * <p>
40 da681e7e Leszek Koltunski
 * If you want to render to a particular shape, extend from here, construct a float array
41 7a5e538a Leszek Koltunski
 * containing per-vertex attributes, and call back setAttribs().
42 e0b6c593 Leszek Koltunski
 */
43 715e7726 Leszek Koltunski
public abstract class MeshBase
44 6a06a912 Leszek Koltunski
   {
45 36d65d88 Leszek Koltunski
   private static final int MAX_COMPONENTS= 100;
46 07206c71 Leszek Koltunski
   private static final int DEFAULT_ASSOCIATION = 0xffffffff;
47 bc208a9c Leszek Koltunski
48 227b9bca Leszek Koltunski
   // sizes of attributes of an individual vertex.
49
   private static final int POS_DATA_SIZE= 3; // vertex coordinates: x,y,z
50
   private static final int NOR_DATA_SIZE= 3; // normal vector: x,y,z
51
   private static final int INF_DATA_SIZE= 3; // 'inflate' vector: x,y,z
52 7a5e538a Leszek Koltunski
   private static final int TEX_DATA_SIZE= 2; // texture coordinates: s,t
53 36d65d88 Leszek Koltunski
   private static final int COM_DATA_SIZE= 1; // component number, a single float
54 6f2d931d Leszek Koltunski
55
   static final int POS_ATTRIB   = 0;
56
   static final int NOR_ATTRIB   = POS_DATA_SIZE;
57
   static final int INF_ATTRIB   = POS_DATA_SIZE + NOR_DATA_SIZE;
58 e54bfada Leszek Koltunski
   static final int TEX_ATTRIB   = 0;
59 36d65d88 Leszek Koltunski
   static final int COM_ATTRIB   = TEX_DATA_SIZE;
60 bc208a9c Leszek Koltunski
61 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)
62 36d65d88 Leszek Koltunski
   static final int VERT2_ATTRIBS= TEX_DATA_SIZE + COM_DATA_SIZE;                  // number of attributes of a vertex (the 'preapply invariant' part)
63 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
64 6a06a912 Leszek Koltunski
65 da681e7e Leszek Koltunski
   private static final int BYTES_PER_FLOAT = 4;
66 3ef3364d Leszek Koltunski
67 6f2d931d Leszek Koltunski
   private static final int OFFSET_POS = POS_ATTRIB*BYTES_PER_FLOAT;
68
   private static final int OFFSET_NOR = NOR_ATTRIB*BYTES_PER_FLOAT;
69
   private static final int OFFSET_INF = INF_ATTRIB*BYTES_PER_FLOAT;
70
   private static final int OFFSET_TEX = TEX_ATTRIB*BYTES_PER_FLOAT;
71 36d65d88 Leszek Koltunski
   private static final int OFFSET_COM = COM_ATTRIB*BYTES_PER_FLOAT;
72 bc208a9c Leszek Koltunski
73 227b9bca Leszek Koltunski
   private static final int TRAN_SIZE  = TRAN_ATTRIBS*BYTES_PER_FLOAT;
74 e54bfada Leszek Koltunski
   private static final int VERT1_SIZE = VERT1_ATTRIBS*BYTES_PER_FLOAT;
75
   private static final int VERT2_SIZE = VERT2_ATTRIBS*BYTES_PER_FLOAT;
76 a51fe521 Leszek Koltunski
77 e54bfada Leszek Koltunski
   private boolean mShowNormals;              // when rendering this mesh, draw normal vectors?
78
   private InternalBuffer mVBO1, mVBO2, mTFO; // main vertex buffer and transform feedback buffer
79 da681e7e Leszek Koltunski
   private int mNumVertices;
80 e54bfada Leszek Koltunski
   private float[] mVertAttribs1;             // packed: PosX,PosY,PosZ, NorX,NorY,NorZ, InfX,InfY,InfZ
81 36d65d88 Leszek Koltunski
   private float[] mVertAttribs2;             // packed: TexS,TexT, Component
82 7a5e538a Leszek Koltunski
   private float mInflate;
83 36d65d88 Leszek Koltunski
   private int[] mAssociation;
84
85 71d8aba1 Leszek Koltunski
   DeferredJobs.JobNode[] mJobNode;
86 07206c71 Leszek Koltunski
87 36d65d88 Leszek Koltunski
   private static int[] mComponentAssociationH = new int[EffectQueue.MAIN_VARIANTS];
88 3ef3364d Leszek Koltunski
89 7e53a35f Leszek Koltunski
   private static class Component
90 c90aca24 Leszek Koltunski
     {
91
     private int mEndIndex;
92 a3a05347 Leszek Koltunski
     private Static4D mTextureMap;
93 c90aca24 Leszek Koltunski
94 0d4aae88 Leszek Koltunski
     Component(int end)
95 c90aca24 Leszek Koltunski
       {
96 a3a05347 Leszek Koltunski
       mEndIndex  = end;
97
       mTextureMap= new Static4D(0,0,1,1);
98 c90aca24 Leszek Koltunski
       }
99
     Component(Component original)
100
       {
101
       mEndIndex = original.mEndIndex;
102 a3a05347 Leszek Koltunski
103
       float x = original.mTextureMap.get0();
104
       float y = original.mTextureMap.get1();
105
       float z = original.mTextureMap.get2();
106
       float w = original.mTextureMap.get3();
107
       mTextureMap = new Static4D(x,y,z,w);
108 0d4aae88 Leszek Koltunski
       }
109
110 a3a05347 Leszek Koltunski
     void setMap(Static4D map)
111 0d4aae88 Leszek Koltunski
       {
112 a3a05347 Leszek Koltunski
       mTextureMap.set(map.get0(),map.get1(),map.get2(),map.get3());
113 c90aca24 Leszek Koltunski
       }
114
     }
115
116
   private ArrayList<Component> mComponent;
117
118 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
119 3ef3364d Leszek Koltunski
120 0f10a0b6 Leszek Koltunski
   MeshBase()
121 3ef3364d Leszek Koltunski
     {
122 4f81e0c8 Leszek Koltunski
     mShowNormals= false;
123
     mInflate    = 0.0f;
124
     mComponent  = new ArrayList<>();
125 36d65d88 Leszek Koltunski
     mAssociation= new int[MAX_COMPONENTS];
126
127 71d8aba1 Leszek Koltunski
     mJobNode = new DeferredJobs.JobNode[1];
128
129 36d65d88 Leszek Koltunski
     for(int i=0; i<MAX_COMPONENTS; i++) mAssociation[i] = DEFAULT_ASSOCIATION;
130 4f81e0c8 Leszek Koltunski
131 e54bfada Leszek Koltunski
     mVBO1= new InternalBuffer(GLES30.GL_ARRAY_BUFFER             , GLES30.GL_STATIC_READ);
132
     mVBO2= new InternalBuffer(GLES30.GL_ARRAY_BUFFER             , GLES30.GL_STATIC_READ);
133 b7074bc6 Leszek Koltunski
     mTFO = new InternalBuffer(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, GLES30.GL_STATIC_READ);
134 c90aca24 Leszek Koltunski
     }
135
136
///////////////////////////////////////////////////////////////////////////////////////////////////
137
// copy constructor
138
139 4f81e0c8 Leszek Koltunski
   MeshBase(MeshBase original, boolean deep)
140 c90aca24 Leszek Koltunski
     {
141 1fad573e Leszek Koltunski
     mShowNormals= original.mShowNormals;
142
     mInflate    = original.mInflate;
143
     mNumVertices= original.mNumVertices;
144 42571056 Leszek Koltunski
145 36d65d88 Leszek Koltunski
     mAssociation= new int[MAX_COMPONENTS];
146
     System.arraycopy(original.mAssociation, 0, mAssociation, 0, MAX_COMPONENTS);
147
148 4f81e0c8 Leszek Koltunski
     if( deep )
149
       {
150 71d8aba1 Leszek Koltunski
       mJobNode = new DeferredJobs.JobNode[1];
151 1fad573e Leszek Koltunski
       if( original.mJobNode[0]==null ) copy(original);
152
       else mJobNode[0] = DeferredJobs.copy(this,original);
153 4f81e0c8 Leszek Koltunski
       }
154
     else
155
       {
156 71d8aba1 Leszek Koltunski
       mJobNode      = original.mJobNode;
157
       mVBO1         = original.mVBO1;
158
       mVertAttribs1 = original.mVertAttribs1;
159 1fad573e Leszek Koltunski
       shallowCopy(original);
160 4f81e0c8 Leszek Koltunski
       }
161 cbd502ec Leszek Koltunski
162 4f81e0c8 Leszek Koltunski
     mTFO = new InternalBuffer(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, GLES30.GL_STATIC_READ);
163 cbd502ec Leszek Koltunski
     mTFO.invalidate();
164 42571056 Leszek Koltunski
     }
165
166 36d65d88 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
167
168 1fad573e Leszek Koltunski
   void copy(MeshBase original)
169 36d65d88 Leszek Koltunski
     {
170 1fad573e Leszek Koltunski
     shallowCopy(original);
171
172
     mVBO1= new InternalBuffer(GLES30.GL_ARRAY_BUFFER, GLES30.GL_STATIC_READ);
173
     mVertAttribs1= new float[mNumVertices*VERT1_ATTRIBS];
174
     System.arraycopy(original.mVertAttribs1,0,mVertAttribs1,0,mNumVertices*VERT1_ATTRIBS);
175
     mVBO1.invalidate();
176 36d65d88 Leszek Koltunski
     }
177
178
///////////////////////////////////////////////////////////////////////////////////////////////////
179
180 1fad573e Leszek Koltunski
   private void shallowCopy(MeshBase original)
181 36d65d88 Leszek Koltunski
     {
182 1fad573e Leszek Koltunski
     int size = original.mComponent.size();
183
     mComponent = new ArrayList<>();
184 36d65d88 Leszek Koltunski
185 1fad573e Leszek Koltunski
     for(int i=0; i<size; i++)
186
       {
187
       Component comp = new Component(original.mComponent.get(i));
188
       mComponent.add(comp);
189
       }
190 f4c5a46e Leszek Koltunski
191 1fad573e Leszek Koltunski
     mVBO2= new InternalBuffer(GLES30.GL_ARRAY_BUFFER, GLES30.GL_STATIC_READ);
192
     mVertAttribs2= new float[mNumVertices*VERT2_ATTRIBS];
193
     System.arraycopy(original.mVertAttribs2,0,mVertAttribs2,0,mNumVertices*VERT2_ATTRIBS);
194
     mVBO2.invalidate();
195 f4c5a46e Leszek Koltunski
     }
196
197 a8dfedcc Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
198
199
   void merge()
200
     {
201
     int num = mComponent.size();
202
203
     if( num>1 )
204
       {
205
       mComponent.clear();
206
       mComponent.add(new Component(mNumVertices-1));
207
208
       for(int index=0; index<mNumVertices; index++)
209
         {
210
         mVertAttribs2[VERT2_ATTRIBS*index+COM_ATTRIB] = 0;
211
         }
212
213
       mVBO2.invalidate();
214
       }
215
     }
216
217 0d4aae88 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
218
219
   int numComponents()
220
     {
221
     return mComponent.size();
222
     }
223
224 42571056 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
225 6c00149d Leszek Koltunski
// when a derived class is done computing its mesh, it has to call this method.
226 42571056 Leszek Koltunski
227 e54bfada Leszek Koltunski
   void setAttribs(float[] vert1Attribs, float[] vert2Attribs)
228 42571056 Leszek Koltunski
     {
229 e54bfada Leszek Koltunski
     mNumVertices = vert1Attribs.length/VERT1_ATTRIBS;
230
     mVertAttribs1= vert1Attribs;
231
     mVertAttribs2= vert2Attribs;
232 da681e7e Leszek Koltunski
233 36d65d88 Leszek Koltunski
     mComponent.add(new Component(mNumVertices-1));
234 c90aca24 Leszek Koltunski
235 e54bfada Leszek Koltunski
     mVBO1.invalidate();
236
     mVBO2.invalidate();
237 42571056 Leszek Koltunski
     }
238
239
///////////////////////////////////////////////////////////////////////////////////////////////////
240
241 1fad573e Leszek Koltunski
   void joinAttribs(MeshBase[] meshes)
242 226144d0 leszek
     {
243 0d4aae88 Leszek Koltunski
     MeshBase mesh;
244 1fad573e Leszek Koltunski
     Component comp;
245 a8dfedcc Leszek Koltunski
     int numMeshes = meshes.length;
246 1fad573e Leszek Koltunski
     int origVertices = mNumVertices;
247
     int origComponents=0,numComponents,numVertices;
248 044b5494 Leszek Koltunski
249 0d4aae88 Leszek Koltunski
     if( origVertices>0 )
250
       {
251 1fad573e Leszek Koltunski
       origComponents = mComponent.size();
252
       mNumVertices+= ( mNumVertices%2==1 ? 2:1 );
253
       mComponent.get(origComponents-1).mEndIndex = mNumVertices-1;
254 0d4aae88 Leszek Koltunski
       }
255 044b5494 Leszek Koltunski
256 e7f85322 Leszek Koltunski
     for(int i=0; i<numMeshes; i++)
257 0d4aae88 Leszek Koltunski
       {
258
       mesh = meshes[i];
259 1fad573e Leszek Koltunski
       numComponents = mesh.mComponent.size();
260 36d65d88 Leszek Koltunski
       numVertices = mesh.mNumVertices;
261
262 1fad573e Leszek Koltunski
       int extraVerticesBefore = mNumVertices==0 ? 0:1;
263
       int extraVerticesAfter  = (i==numMeshes-1) ? 0 : (numVertices%2==1 ? 2:1);
264 36d65d88 Leszek Koltunski
265 1fad573e Leszek Koltunski
       for(int j=0; j<numComponents; j++)
266 0d4aae88 Leszek Koltunski
         {
267 1fad573e Leszek Koltunski
         comp = new Component(mesh.mComponent.get(j));
268
         comp.mEndIndex += (extraVerticesBefore+mNumVertices+extraVerticesAfter);
269
         mComponent.add(comp);
270 23b733db Leszek Koltunski
271 1fad573e Leszek Koltunski
         if( origComponents<MAX_COMPONENTS )
272 36d65d88 Leszek Koltunski
           {
273 1fad573e Leszek Koltunski
           mAssociation[origComponents] = mesh.mAssociation[j];
274
           origComponents++;
275 36d65d88 Leszek Koltunski
           }
276 e7f85322 Leszek Koltunski
         }
277 a8dfedcc Leszek Koltunski
278 1fad573e Leszek Koltunski
       mNumVertices += (extraVerticesBefore+numVertices+extraVerticesAfter);
279
       }
280 a8dfedcc Leszek Koltunski
281 1fad573e Leszek Koltunski
     float[] newAttribs1 = new float[VERT1_ATTRIBS*mNumVertices];
282 e54bfada Leszek Koltunski
     float[] newAttribs2 = new float[VERT2_ATTRIBS*mNumVertices];
283 1fad573e Leszek Koltunski
     numVertices = origVertices;
284 0d4aae88 Leszek Koltunski
285
     if( origVertices>0 )
286
       {
287 1fad573e Leszek Koltunski
       System.arraycopy(mVertAttribs1,                              0, newAttribs1,                         0, VERT1_ATTRIBS*numVertices);
288
       System.arraycopy(mVertAttribs1, VERT1_ATTRIBS*(origVertices-1), newAttribs1, VERT1_ATTRIBS*origVertices, VERT1_ATTRIBS    );
289 e54bfada Leszek Koltunski
       System.arraycopy(mVertAttribs2,                              0, newAttribs2,                         0, VERT2_ATTRIBS*numVertices);
290
       System.arraycopy(mVertAttribs2, VERT2_ATTRIBS*(origVertices-1), newAttribs2, VERT2_ATTRIBS*origVertices, VERT2_ATTRIBS    );
291 0d4aae88 Leszek Koltunski
       origVertices++;
292
293 e7f85322 Leszek Koltunski
       if( numVertices%2==1 )
294 0d4aae88 Leszek Koltunski
         {
295 1fad573e Leszek Koltunski
         System.arraycopy(mVertAttribs1, VERT1_ATTRIBS*(origVertices-1), newAttribs1, VERT1_ATTRIBS*origVertices, VERT1_ATTRIBS);
296 e54bfada Leszek Koltunski
         System.arraycopy(mVertAttribs2, VERT2_ATTRIBS*(origVertices-1), newAttribs2, VERT2_ATTRIBS*origVertices, VERT2_ATTRIBS);
297 0d4aae88 Leszek Koltunski
         origVertices++;
298
         }
299
       }
300
301 e7f85322 Leszek Koltunski
     for(int i=0; i<numMeshes; i++)
302 0d4aae88 Leszek Koltunski
       {
303
       mesh = meshes[i];
304 e7f85322 Leszek Koltunski
       numVertices = mesh.mNumVertices;
305 0d4aae88 Leszek Koltunski
306 22e60fba Leszek Koltunski
       if( origVertices>0 )
307
         {
308 1fad573e Leszek Koltunski
         System.arraycopy(mesh.mVertAttribs1, 0, newAttribs1, VERT1_ATTRIBS*origVertices, VERT1_ATTRIBS    );
309 e54bfada Leszek Koltunski
         System.arraycopy(mesh.mVertAttribs2, 0, newAttribs2, VERT2_ATTRIBS*origVertices, VERT2_ATTRIBS    );
310 22e60fba Leszek Koltunski
         origVertices++;
311
         }
312 1fad573e Leszek Koltunski
       System.arraycopy(mesh.mVertAttribs1, 0, newAttribs1, VERT1_ATTRIBS*origVertices, VERT1_ATTRIBS*numVertices);
313 e54bfada Leszek Koltunski
       System.arraycopy(mesh.mVertAttribs2, 0, newAttribs2, VERT2_ATTRIBS*origVertices, VERT2_ATTRIBS*numVertices);
314 e7f85322 Leszek Koltunski
       origVertices+=numVertices;
315 0d4aae88 Leszek Koltunski
316 e7f85322 Leszek Koltunski
       if( i<numMeshes-1 )
317 0d4aae88 Leszek Koltunski
         {
318 1fad573e Leszek Koltunski
         System.arraycopy(mesh.mVertAttribs1, VERT1_ATTRIBS*(numVertices-1), newAttribs1, VERT1_ATTRIBS*origVertices, VERT1_ATTRIBS);
319 e54bfada Leszek Koltunski
         System.arraycopy(mesh.mVertAttribs2, VERT2_ATTRIBS*(numVertices-1), newAttribs2, VERT2_ATTRIBS*origVertices, VERT2_ATTRIBS);
320 0d4aae88 Leszek Koltunski
         origVertices++;
321
322 e7f85322 Leszek Koltunski
         if( numVertices%2==1 )
323 0d4aae88 Leszek Koltunski
           {
324 1fad573e Leszek Koltunski
           System.arraycopy(mesh.mVertAttribs1, VERT1_ATTRIBS*(numVertices-1), newAttribs1, VERT1_ATTRIBS*origVertices, VERT1_ATTRIBS);
325 e54bfada Leszek Koltunski
           System.arraycopy(mesh.mVertAttribs2, VERT2_ATTRIBS*(numVertices-1), newAttribs2, VERT2_ATTRIBS*origVertices, VERT2_ATTRIBS);
326 0d4aae88 Leszek Koltunski
           origVertices++;
327
           }
328
         }
329
       }
330
331
     if( origVertices!=mNumVertices )
332
       {
333
       android.util.Log.e("mesh", "join: origVertices: "+origVertices+" numVertices: "+mNumVertices);
334
       }
335
336 36d65d88 Leszek Koltunski
     int endIndex, index=0, numComp = mComponent.size();
337
338
     for(int component=0; component<numComp; component++)
339
       {
340
       endIndex = mComponent.get(component).mEndIndex;
341
342
       for( ; index<=endIndex; index++) newAttribs2[VERT2_ATTRIBS*index+COM_ATTRIB] = component;
343
       }
344
345 1fad573e Leszek Koltunski
     mVertAttribs1 = newAttribs1;
346 e54bfada Leszek Koltunski
     mVertAttribs2 = newAttribs2;
347 1fad573e Leszek Koltunski
     mVBO1.invalidate();
348 e54bfada Leszek Koltunski
     mVBO2.invalidate();
349 23b733db Leszek Koltunski
     }
350
351 a8dfedcc Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
352
// called from MeshJoined
353
354
   void join(MeshBase[] meshes)
355
     {
356 1fad573e Leszek Koltunski
     boolean immediateJoin = true;
357 a8dfedcc Leszek Koltunski
358 1fad573e Leszek Koltunski
     for (MeshBase mesh : meshes)
359 a8dfedcc Leszek Koltunski
       {
360 1fad573e Leszek Koltunski
       if (mesh.mJobNode[0] != null)
361
         {
362
         immediateJoin = false;
363
         break;
364
         }
365 a8dfedcc Leszek Koltunski
       }
366
367 1fad573e Leszek Koltunski
     if( immediateJoin ) joinAttribs(meshes);
368
     else                mJobNode[0] = DeferredJobs.join(this,meshes);
369
     }
370 a8dfedcc Leszek Koltunski
371 1fad573e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
372 a8dfedcc Leszek Koltunski
373 1fad573e Leszek Koltunski
   void textureMap(Static4D[] maps)
374
     {
375
     int num_comp = mComponent.size();
376
     int num_maps = maps.length;
377
     int min = Math.min(num_comp, num_maps);
378
     int vertex = 0;
379
     Static4D newMap, oldMap;
380
     Component comp;
381
     float newW, newH, ratW, ratH, movX, movY;
382 a8dfedcc Leszek Koltunski
383 1fad573e Leszek Koltunski
     for(int i=0; i<min; i++)
384
       {
385
       newMap = maps[i];
386
       comp = mComponent.get(i);
387
388
       if( newMap!=null )
389 a8dfedcc Leszek Koltunski
         {
390 1fad573e Leszek Koltunski
         newW = newMap.get2();
391
         newH = newMap.get3();
392 a8dfedcc Leszek Koltunski
393 1fad573e Leszek Koltunski
         if( newW!=0.0f && newH!=0.0f )
394 a8dfedcc Leszek Koltunski
           {
395 1fad573e Leszek Koltunski
           oldMap = comp.mTextureMap;
396
           ratW = newW/oldMap.get2();
397
           ratH = newH/oldMap.get3();
398
           movX = newMap.get0() - ratW*oldMap.get0();
399
           movY = newMap.get1() - ratH*oldMap.get1();
400
401
           for( int index=vertex*VERT2_ATTRIBS+TEX_ATTRIB ; vertex<=comp.mEndIndex; vertex++, index+=VERT2_ATTRIBS)
402
             {
403
             mVertAttribs2[index  ] = ratW*mVertAttribs2[index  ] + movX;
404
             mVertAttribs2[index+1] = ratH*mVertAttribs2[index+1] + movY;
405
             }
406
           comp.setMap(newMap);
407 a8dfedcc Leszek Koltunski
           }
408
         }
409
410 1fad573e Leszek Koltunski
       vertex= comp.mEndIndex+1;
411 a8dfedcc Leszek Koltunski
       }
412
413 1fad573e Leszek Koltunski
     mVBO2.invalidate();
414
     }
415
416
///////////////////////////////////////////////////////////////////////////////////////////////////
417 a8dfedcc Leszek Koltunski
418 1fad573e Leszek Koltunski
   public static int getMaxComponents()
419
     {
420
     return MAX_COMPONENTS;
421
     }
422
423
///////////////////////////////////////////////////////////////////////////////////////////////////
424
425
   public static void getUniforms(int mProgramH, int variant)
426
     {
427
     mComponentAssociationH[variant] = GLES30.glGetUniformLocation( mProgramH, "vComponAssoc");
428 a8dfedcc Leszek Koltunski
     }
429
430 f046b159 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
431
/**
432
 * Not part of public API, do not document (public only because has to be used from the main package)
433
 *
434
 * @y.exclude
435
 */
436
   public void copyTransformToVertex()
437
     {
438 e54bfada Leszek Koltunski
     ByteBuffer buffer = (ByteBuffer)GLES30.glMapBufferRange( GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, 0,
439
                                                              TRAN_SIZE*mNumVertices, GLES30.GL_MAP_READ_BIT);
440 f046b159 Leszek Koltunski
     if( buffer!=null )
441
       {
442 e54bfada Leszek Koltunski
       FloatBuffer feedback = buffer.order(ByteOrder.nativeOrder()).asFloatBuffer();
443
       feedback.get(mVertAttribs1,0,VERT1_ATTRIBS*mNumVertices);
444
       mVBO1.update(mVertAttribs1);
445 f046b159 Leszek Koltunski
       }
446
     else
447
       {
448 b5be333a Leszek Koltunski
       int error = GLES30.glGetError();
449
       Log.e("mesh", "failed to map tf buffer, error="+error);
450 f046b159 Leszek Koltunski
       }
451
452 b5be333a Leszek Koltunski
     GLES30.glUnmapBuffer(GLES30.GL_TRANSFORM_FEEDBACK);
453 f046b159 Leszek Koltunski
     }
454
455 1fad573e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
456
/**
457
 * Not part of public API, do not document (public only because has to be used from the main package)
458
 *
459
 * @y.exclude
460
 */
461
   public void debug()
462
     {
463
     if( mJobNode[0]!=null )
464
       {
465
       mJobNode[0].print(0);
466
       }
467
     else
468
       {
469
       android.util.Log.e("mesh", "JobNode null");
470
       }
471
     }
472
473
///////////////////////////////////////////////////////////////////////////////////////////////////
474
/**
475
 * Not part of public API, do not document (public only because has to be used from the main package)
476
 *
477
 * @y.exclude
478
 */
479
   public void print()
480
     {
481
     StringBuffer sb = new StringBuffer();
482
483
     for(int i=0; i<mNumVertices; i++)
484
       {
485
       sb.append('(');
486
       sb.append(mVertAttribs1[VERT1_ATTRIBS*i+POS_ATTRIB  ]);
487
       sb.append(',');
488
       sb.append(mVertAttribs1[VERT1_ATTRIBS*i+POS_ATTRIB+1]);
489
       sb.append(',');
490
       sb.append(mVertAttribs1[VERT1_ATTRIBS*i+POS_ATTRIB+2]);
491
       sb.append(") ");
492
       }
493
494
     StringBuffer sb2 = new StringBuffer();
495
496
     for(int i=0; i<mNumVertices; i++)
497
       {
498
       sb2.append(mVertAttribs2[VERT2_ATTRIBS*i+COM_ATTRIB]);
499
       sb2.append(' ');
500
       }
501
502
     Log.d("mesh", sb.toString()+"\n"+sb2.toString());
503
     }
504
505 23b733db Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
506
/**
507 0d4aae88 Leszek Koltunski
 * Not part of public API, do not document (public only because has to be used from the main package)
508
 *
509
 * @y.exclude
510 23b733db Leszek Koltunski
 */
511 0d4aae88 Leszek Koltunski
   public int getTFO()
512 23b733db Leszek Koltunski
     {
513 b5be333a Leszek Koltunski
     return mTFO.createImmediately(mNumVertices*TRAN_SIZE, null);
514 23b733db Leszek Koltunski
     }
515
516
///////////////////////////////////////////////////////////////////////////////////////////////////
517
/**
518 0d4aae88 Leszek Koltunski
 * Not part of public API, do not document (public only because has to be used from the main package)
519
 *
520
 * @y.exclude
521 23b733db Leszek Koltunski
 */
522 0d4aae88 Leszek Koltunski
   public int getNumVertices()
523 23b733db Leszek Koltunski
     {
524 0d4aae88 Leszek Koltunski
     return mNumVertices;
525 23b733db Leszek Koltunski
     }
526
527 36d65d88 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
528
/**
529
 * Not part of public API, do not document (public only because has to be used from the main package)
530
 *
531
 * @y.exclude
532
 */
533
   public void send(int variant)
534
     {
535
     GLES30.glUniform1iv( mComponentAssociationH[variant], MAX_COMPONENTS, mAssociation, 0);
536
     }
537
538 044b5494 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
539 6c00149d Leszek Koltunski
/**
540
 * Not part of public API, do not document (public only because has to be used from the main package)
541
 *
542
 * @y.exclude
543
 */
544 da681e7e Leszek Koltunski
   public void bindVertexAttribs(DistortedProgram program)
545 6c00149d Leszek Koltunski
     {
546 71d8aba1 Leszek Koltunski
     if( mJobNode[0]!=null )
547 f046b159 Leszek Koltunski
       {
548 71d8aba1 Leszek Koltunski
       mJobNode[0].execute();  // this will set itself to null
549 f046b159 Leszek Koltunski
       }
550
551 e54bfada Leszek Koltunski
     int index1 = mVBO1.createImmediately(mNumVertices*VERT1_SIZE, mVertAttribs1);
552
     int index2 = mVBO2.createImmediately(mNumVertices*VERT2_SIZE, mVertAttribs2);
553 22e60fba Leszek Koltunski
554 e54bfada Leszek Koltunski
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, index1 );
555
     GLES30.glVertexAttribPointer(program.mAttribute[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, VERT1_SIZE, OFFSET_POS);
556
     GLES30.glVertexAttribPointer(program.mAttribute[1], NOR_DATA_SIZE, GLES30.GL_FLOAT, false, VERT1_SIZE, OFFSET_NOR);
557
     GLES30.glVertexAttribPointer(program.mAttribute[2], INF_DATA_SIZE, GLES30.GL_FLOAT, false, VERT1_SIZE, OFFSET_INF);
558
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, index2 );
559
     GLES30.glVertexAttribPointer(program.mAttribute[3], TEX_DATA_SIZE, GLES30.GL_FLOAT, false, VERT2_SIZE, OFFSET_TEX);
560 36d65d88 Leszek Koltunski
     GLES30.glVertexAttribPointer(program.mAttribute[4], COM_DATA_SIZE, GLES30.GL_FLOAT, false, VERT2_SIZE, OFFSET_COM);
561 b7074bc6 Leszek Koltunski
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, 0);
562 da681e7e Leszek Koltunski
     }
563
564
///////////////////////////////////////////////////////////////////////////////////////////////////
565
/**
566
 * Not part of public API, do not document (public only because has to be used from the main package)
567
 *
568
 * @y.exclude
569
 */
570
   public void bindTransformAttribs(DistortedProgram program)
571
     {
572 7e53a35f Leszek Koltunski
     int index = mTFO.createImmediately(mNumVertices*TRAN_SIZE, null);
573 22e60fba Leszek Koltunski
574 b7074bc6 Leszek Koltunski
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, index );
575
     GLES30.glVertexAttribPointer(program.mAttribute[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, 0, 0);
576
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, 0);
577 6c00149d Leszek Koltunski
     }
578
579 7a5e538a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
580
/**
581
 * Not part of public API, do not document (public only because has to be used from the main package)
582
 *
583
 * @y.exclude
584
 */
585
   public void setInflate(float inflate)
586
     {
587
     mInflate = inflate;
588
     }
589
590
///////////////////////////////////////////////////////////////////////////////////////////////////
591
/**
592
 * Not part of public API, do not document (public only because has to be used from the main package)
593
 *
594
 * @y.exclude
595
 */
596
   public float getInflate()
597
     {
598
     return mInflate;
599
     }
600
601 466450b5 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
602
// PUBLIC API
603 3fc9327a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
604
/**
605
 * When rendering this Mesh, do we want to render the Normal vectors as well?
606 420836fc leszek
 * <p>
607
 * Will work only on OpenGL ES >= 3.0 devices.
608 3fc9327a Leszek Koltunski
 *
609
 * @param show Controls if we render the Normal vectors or not.
610
 */
611
   public void setShowNormals(boolean show)
612
     {
613 b7074bc6 Leszek Koltunski
     mShowNormals = show;
614 3fc9327a Leszek Koltunski
     }
615 466450b5 Leszek Koltunski
616 6c00149d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
617
/**
618
 * When rendering this mesh, should we also draw the normal vectors?
619
 *
620
 * @return <i>true</i> if we do render normal vectors
621
 */
622
   public boolean getShowNormals()
623
     {
624
     return mShowNormals;
625
     }
626
627 a8dfedcc Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
628
/**
629
 * Merge all components of this Mesh into a single one.
630
 */
631
   public void mergeComponents()
632
     {
633 1fad573e Leszek Koltunski
     if( mJobNode[0]==null )
634 a8dfedcc Leszek Koltunski
       {
635
       merge();
636
       }
637
     else
638
       {
639
       mJobNode[0] = DeferredJobs.merge(this);
640
       }
641
     }
642
643 466450b5 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
644
/**
645
 * Release all internal resources.
646
 */
647
   public void markForDeletion()
648
     {
649 e54bfada Leszek Koltunski
     mVertAttribs1 = null;
650
     mVertAttribs2 = null;
651 466450b5 Leszek Koltunski
652 e54bfada Leszek Koltunski
     mVBO1.markForDeletion();
653
     mVBO2.markForDeletion();
654 466450b5 Leszek Koltunski
     mTFO.markForDeletion();
655
     }
656 fa8bc998 Leszek Koltunski
657
///////////////////////////////////////////////////////////////////////////////////////////////////
658
/**
659 f046b159 Leszek Koltunski
 * Apply a Vertex Effect to the vertex mesh.
660 fa8bc998 Leszek Koltunski
 * <p>
661 c90aca24 Leszek Koltunski
 * This is a static, permanent modification of the vertices contained in this Mesh. If the effects
662 f046b159 Leszek Koltunski
 * contain any Dynamics, the Dynamics will be evaluated at 0.
663 fa8bc998 Leszek Koltunski
 *
664 f046b159 Leszek Koltunski
 * We would call this several times building up a list of Effects to do. This list of effects gets
665
 * lazily executed only when the Mesh is used for rendering for the first time.
666 e172985c Leszek Koltunski
 *
667 f046b159 Leszek Koltunski
 * @param effect Vertex Effect to apply to the Mesh.
668 fa8bc998 Leszek Koltunski
 */
669 f046b159 Leszek Koltunski
   public void apply(VertexEffect effect)
670 fa8bc998 Leszek Koltunski
     {
671 71d8aba1 Leszek Koltunski
     mJobNode[0] = DeferredJobs.vertex(this,effect);
672 c90aca24 Leszek Koltunski
     }
673
674
///////////////////////////////////////////////////////////////////////////////////////////////////
675
/**
676 a3a05347 Leszek Koltunski
 * Sets texture maps for (some of) the components of this mesh.
677 c90aca24 Leszek Koltunski
 * <p>
678 a3a05347 Leszek Koltunski
 * Format: ( x of lower-left corner, y of lower-left corner, width, height ).
679
 * For example maps[0] = new Static4D( 0.0, 0.5, 0.5, 0.5 ) sets the 0th component texture map to the
680 0d4aae88 Leszek Koltunski
 * upper-left quadrant of the texture.
681 a3a05347 Leszek Koltunski
 * <p>
682
 * Probably the most common user case would be sending as many maps as there are components in this
683
 * Mesh. One can also send less, or more (the extraneous ones will be ignored) and set some of them
684
 * to null (those will be ignored as well). So if there are 5 components, and we want to set the map
685
 * of the 2nd and 4rd one, call this with
686
 * maps = new Static4D[4]
687
 * maps[0] = null
688
 * maps[1] = the map for the 2nd component
689
 * maps[2] = null
690
 * maps[3] = the map for the 4th component
691
 *
692
 * A map's width and height have to be non-zero (but can be negative!)
693 e172985c Leszek Koltunski
 *
694
 * @param maps List of texture maps to apply to the texture's components.
695 c90aca24 Leszek Koltunski
 */
696 a3a05347 Leszek Koltunski
   public void setTextureMap(Static4D[] maps)
697 c90aca24 Leszek Koltunski
     {
698 1fad573e Leszek Koltunski
     if( mJobNode[0]==null )
699 ea88d502 Leszek Koltunski
       {
700 1fad573e Leszek Koltunski
       textureMap(maps);
701
       }
702
     else
703
       {
704
       mJobNode[0] = DeferredJobs.textureMap(this,maps);
705 ea88d502 Leszek Koltunski
       }
706 fa8bc998 Leszek Koltunski
     }
707 9099e567 Leszek Koltunski
708 e172985c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
709
/**
710
 * Return the texture map of one of the components.
711
 *
712
 * @param component The component number whose texture map we want to retrieve.
713
 */
714
   public Static4D getTextureMap(int component)
715
     {
716
     return (component>=0 && component<mComponent.size()) ? mComponent.get(component).mTextureMap : null;
717
     }
718 cbd502ec Leszek Koltunski
719
///////////////////////////////////////////////////////////////////////////////////////////////////
720
/**
721 1e672c1d Leszek Koltunski
 * Set Effect association.
722 bc208a9c Leszek Koltunski
 *
723 1e672c1d Leszek Koltunski
 * This creates an association between a Component of this Mesh and a Vertex Effect.
724
 * One can set the association of an Effect and of a Component, and the Effect will only be active on
725 36d65d88 Leszek Koltunski
 * vertices of Components such that
726
 *
727
 * (effect assoc) & (component assoc) != 0 || (effect component) == (mesh component)
728
 *
729
 * (see main_vertex_shader)
730 bc208a9c Leszek Koltunski
 *
731
 * The point: this way we can configure the system so that each Vertex Effect acts only on a certain
732 1e672c1d Leszek Koltunski
 * subset of a Mesh, thus potentially significantly reducing the number of render calls.
733 cbd502ec Leszek Koltunski
 */
734 1e672c1d Leszek Koltunski
  public void setEffectAssociation(int component, int association)
735 bc208a9c Leszek Koltunski
    {
736 36d65d88 Leszek Koltunski
    if( component>=0 && component<MAX_COMPONENTS )
737 bc208a9c Leszek Koltunski
      {
738 36d65d88 Leszek Koltunski
      mAssociation[component] = association;
739 bc208a9c Leszek Koltunski
      }
740
    }
741
742
///////////////////////////////////////////////////////////////////////////////////////////////////
743
/**
744 4f81e0c8 Leszek Koltunski
 * Copy the Mesh.
745
 *
746
 * @param deep If to be a deep or shallow copy of mVertAttribs1, i.e. the array holding vertices,
747
 *             normals and inflates (the rest, in particular the mVertAttribs2 containing texture
748 36d65d88 Leszek Koltunski
 *             coordinates and effect associations, is always deep copied)
749 bc208a9c Leszek Koltunski
 */
750 4f81e0c8 Leszek Koltunski
   public abstract MeshBase copy(boolean deep);
751 bc208a9c Leszek Koltunski
   }