Project

General

Profile

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

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

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