Project

General

Profile

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

library / src / main / java / org / distorted / library / mesh / MeshBase.java @ 36d65d88

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