Project

General

Profile

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

library / src / main / java / org / distorted / library / mesh / MeshBase.java @ 07206c71

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 07206c71 Leszek Koltunski
   DeferredJobs.JobNode mJobNode;
86
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
     for(int i=0; i<MAX_COMPONENTS; i++) mAssociation[i] = DEFAULT_ASSOCIATION;
128 4f81e0c8 Leszek Koltunski
129 e54bfada Leszek Koltunski
     mVBO1= new InternalBuffer(GLES30.GL_ARRAY_BUFFER             , GLES30.GL_STATIC_READ);
130
     mVBO2= new InternalBuffer(GLES30.GL_ARRAY_BUFFER             , GLES30.GL_STATIC_READ);
131 b7074bc6 Leszek Koltunski
     mTFO = new InternalBuffer(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, GLES30.GL_STATIC_READ);
132 c90aca24 Leszek Koltunski
     }
133
134
///////////////////////////////////////////////////////////////////////////////////////////////////
135
// copy constructor
136
137 4f81e0c8 Leszek Koltunski
   MeshBase(MeshBase original, boolean deep)
138 c90aca24 Leszek Koltunski
     {
139 f046b159 Leszek Koltunski
     mShowNormals     = original.mShowNormals;
140
     mInflate         = original.mInflate;
141 4f81e0c8 Leszek Koltunski
     mNumVertices     = original.mNumVertices;
142 c90aca24 Leszek Koltunski
143
     int size = original.mComponent.size();
144
     mComponent = new ArrayList<>();
145 bc208a9c Leszek Koltunski
146 c90aca24 Leszek Koltunski
     for(int i=0; i<size; i++)
147
       {
148
       Component comp = new Component(original.mComponent.get(i));
149
       mComponent.add(comp);
150
       }
151 42571056 Leszek Koltunski
152 36d65d88 Leszek Koltunski
     mAssociation= new int[MAX_COMPONENTS];
153
     System.arraycopy(original.mAssociation, 0, mAssociation, 0, MAX_COMPONENTS);
154
155 4f81e0c8 Leszek Koltunski
     if( deep )
156
       {
157 07206c71 Leszek Koltunski
       if( original.mJobNode!=null )
158
         {
159
         mJobNode = new DeferredJobs.JobNode(original.mJobNode);
160
         }
161 c90aca24 Leszek Koltunski
162 4f81e0c8 Leszek Koltunski
       mVBO1= new InternalBuffer(GLES30.GL_ARRAY_BUFFER, GLES30.GL_STATIC_READ);
163
       mVertAttribs1= new float[mNumVertices*VERT1_ATTRIBS];
164
       System.arraycopy(original.mVertAttribs1,0,mVertAttribs1,0,mNumVertices*VERT1_ATTRIBS);
165
       mVBO1.invalidate();
166
       }
167
     else
168
       {
169 07206c71 Leszek Koltunski
       mJobNode         = original.mJobNode;
170 4f81e0c8 Leszek Koltunski
       mVBO1            = original.mVBO1;
171
       mVertAttribs1    = original.mVertAttribs1;
172
       }
173 cbd502ec Leszek Koltunski
174 4f81e0c8 Leszek Koltunski
     mVBO2= new InternalBuffer(GLES30.GL_ARRAY_BUFFER, GLES30.GL_STATIC_READ);
175
     mVertAttribs2= new float[mNumVertices*VERT2_ATTRIBS];
176 e54bfada Leszek Koltunski
     System.arraycopy(original.mVertAttribs2,0,mVertAttribs2,0,mNumVertices*VERT2_ATTRIBS);
177
     mVBO2.invalidate();
178 4f81e0c8 Leszek Koltunski
179
     mTFO = new InternalBuffer(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, GLES30.GL_STATIC_READ);
180 cbd502ec Leszek Koltunski
     mTFO.invalidate();
181 42571056 Leszek Koltunski
     }
182
183 36d65d88 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
184
185
   public static int getMaxComponents()
186
     {
187
     return MAX_COMPONENTS;
188
     }
189
190
///////////////////////////////////////////////////////////////////////////////////////////////////
191
192
   public static void getUniforms(int mProgramH, int variant)
193
     {
194
     mComponentAssociationH[variant] = GLES30.glGetUniformLocation( mProgramH, "vComponAssoc");
195
     }
196
197 0d4aae88 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
198
199
   int numComponents()
200
     {
201
     return mComponent.size();
202
     }
203
204 42571056 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
205 6c00149d Leszek Koltunski
// when a derived class is done computing its mesh, it has to call this method.
206 42571056 Leszek Koltunski
207 e54bfada Leszek Koltunski
   void setAttribs(float[] vert1Attribs, float[] vert2Attribs)
208 42571056 Leszek Koltunski
     {
209 e54bfada Leszek Koltunski
     mNumVertices = vert1Attribs.length/VERT1_ATTRIBS;
210
     mVertAttribs1= vert1Attribs;
211
     mVertAttribs2= vert2Attribs;
212 da681e7e Leszek Koltunski
213 36d65d88 Leszek Koltunski
     mComponent.add(new Component(mNumVertices-1));
214 c90aca24 Leszek Koltunski
215 e54bfada Leszek Koltunski
     mVBO1.invalidate();
216
     mVBO2.invalidate();
217 42571056 Leszek Koltunski
     }
218
219
///////////////////////////////////////////////////////////////////////////////////////////////////
220 0d4aae88 Leszek Koltunski
// called from MeshJoined
221 42571056 Leszek Koltunski
222 0d4aae88 Leszek Koltunski
   void join(MeshBase[] meshes)
223 226144d0 leszek
     {
224 0d4aae88 Leszek Koltunski
     MeshBase mesh;
225
     Component comp;
226 36d65d88 Leszek Koltunski
     int origComponents=0, numComponents, numVertices, numMeshes = meshes.length;
227 0d4aae88 Leszek Koltunski
     int origVertices = mNumVertices;
228 42571056 Leszek Koltunski
229 0d4aae88 Leszek Koltunski
     // compute new numVertices; take care of Components
230 044b5494 Leszek Koltunski
231 0d4aae88 Leszek Koltunski
     if( origVertices>0 )
232
       {
233 36d65d88 Leszek Koltunski
       origComponents = mComponent.size();
234 0d4aae88 Leszek Koltunski
       mNumVertices+= ( mNumVertices%2==1 ? 2:1 );
235 36d65d88 Leszek Koltunski
       mComponent.get(origComponents-1).mEndIndex = mNumVertices-1;
236 0d4aae88 Leszek Koltunski
       }
237 044b5494 Leszek Koltunski
238 e7f85322 Leszek Koltunski
     for(int i=0; i<numMeshes; i++)
239 0d4aae88 Leszek Koltunski
       {
240
       mesh = meshes[i];
241 e7f85322 Leszek Koltunski
       numComponents = mesh.mComponent.size();
242 044b5494 Leszek Koltunski
243 36d65d88 Leszek Koltunski
       numVertices = mesh.mNumVertices;
244
245
       int extraVerticesBefore = mNumVertices==0 ? 0:1;
246
       int extraVerticesAfter  = (i==numMeshes-1) ? 0 : (numVertices%2==1 ? 2:1);
247
248 e7f85322 Leszek Koltunski
       for(int j=0; j<numComponents; j++)
249 0d4aae88 Leszek Koltunski
         {
250
         comp = new Component(mesh.mComponent.get(j));
251 36d65d88 Leszek Koltunski
         comp.mEndIndex += (extraVerticesBefore+mNumVertices+extraVerticesAfter);
252 0d4aae88 Leszek Koltunski
         mComponent.add(comp);
253 23b733db Leszek Koltunski
254 36d65d88 Leszek Koltunski
         if( origComponents<MAX_COMPONENTS )
255
           {
256
           mAssociation[origComponents] = mesh.mAssociation[j];
257
           origComponents++;
258
           }
259 e7f85322 Leszek Koltunski
         }
260 36d65d88 Leszek Koltunski
261
       mNumVertices += (extraVerticesBefore+numVertices+extraVerticesAfter);
262 0d4aae88 Leszek Koltunski
       }
263
264
     // allocate new attrib array
265 e54bfada Leszek Koltunski
     float[] newAttribs1 = new float[VERT1_ATTRIBS*mNumVertices];
266
     float[] newAttribs2 = new float[VERT2_ATTRIBS*mNumVertices];
267 e7f85322 Leszek Koltunski
     numVertices = origVertices;
268 0d4aae88 Leszek Koltunski
269
     if( origVertices>0 )
270
       {
271 e54bfada Leszek Koltunski
       System.arraycopy(mVertAttribs1,                              0, newAttribs1,                         0, VERT1_ATTRIBS*numVertices);
272
       System.arraycopy(mVertAttribs2,                              0, newAttribs2,                         0, VERT2_ATTRIBS*numVertices);
273
       System.arraycopy(mVertAttribs1, VERT1_ATTRIBS*(origVertices-1), newAttribs1, VERT1_ATTRIBS*origVertices, VERT1_ATTRIBS    );
274
       System.arraycopy(mVertAttribs2, VERT2_ATTRIBS*(origVertices-1), newAttribs2, VERT2_ATTRIBS*origVertices, VERT2_ATTRIBS    );
275 0d4aae88 Leszek Koltunski
       origVertices++;
276
277 e7f85322 Leszek Koltunski
       if( numVertices%2==1 )
278 0d4aae88 Leszek Koltunski
         {
279 e54bfada Leszek Koltunski
         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
       }
284
285 e7f85322 Leszek Koltunski
     for(int i=0; i<numMeshes; i++)
286 0d4aae88 Leszek Koltunski
       {
287
       mesh = meshes[i];
288 e7f85322 Leszek Koltunski
       numVertices = mesh.mNumVertices;
289 0d4aae88 Leszek Koltunski
290 22e60fba Leszek Koltunski
       if( origVertices>0 )
291
         {
292 e54bfada Leszek Koltunski
         System.arraycopy(mesh.mVertAttribs1, 0, newAttribs1, VERT1_ATTRIBS*origVertices, VERT1_ATTRIBS    );
293
         System.arraycopy(mesh.mVertAttribs2, 0, newAttribs2, VERT2_ATTRIBS*origVertices, VERT2_ATTRIBS    );
294 22e60fba Leszek Koltunski
         origVertices++;
295
         }
296 e54bfada Leszek Koltunski
       System.arraycopy(mesh.mVertAttribs1, 0, newAttribs1, VERT1_ATTRIBS*origVertices, VERT1_ATTRIBS*numVertices);
297
       System.arraycopy(mesh.mVertAttribs2, 0, newAttribs2, VERT2_ATTRIBS*origVertices, VERT2_ATTRIBS*numVertices);
298 e7f85322 Leszek Koltunski
       origVertices+=numVertices;
299 0d4aae88 Leszek Koltunski
300 e7f85322 Leszek Koltunski
       if( i<numMeshes-1 )
301 0d4aae88 Leszek Koltunski
         {
302 e54bfada Leszek Koltunski
         System.arraycopy(mesh.mVertAttribs1, VERT1_ATTRIBS*(numVertices-1), newAttribs1, VERT1_ATTRIBS*origVertices, VERT1_ATTRIBS);
303
         System.arraycopy(mesh.mVertAttribs2, VERT2_ATTRIBS*(numVertices-1), newAttribs2, VERT2_ATTRIBS*origVertices, VERT2_ATTRIBS);
304 0d4aae88 Leszek Koltunski
         origVertices++;
305
306 e7f85322 Leszek Koltunski
         if( numVertices%2==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
         }
313
       }
314
315
     if( origVertices!=mNumVertices )
316
       {
317
       android.util.Log.e("mesh", "join: origVertices: "+origVertices+" numVertices: "+mNumVertices);
318
       }
319
320 36d65d88 Leszek Koltunski
     int endIndex, index=0, numComp = mComponent.size();
321
322
     for(int component=0; component<numComp; component++)
323
       {
324
       endIndex = mComponent.get(component).mEndIndex;
325
326
       for( ; index<=endIndex; index++) newAttribs2[VERT2_ATTRIBS*index+COM_ATTRIB] = component;
327
       }
328
329 e54bfada Leszek Koltunski
     mVertAttribs1 = newAttribs1;
330
     mVertAttribs2 = newAttribs2;
331 0d4aae88 Leszek Koltunski
332 e54bfada Leszek Koltunski
     mVBO1.invalidate();
333
     mVBO2.invalidate();
334 23b733db Leszek Koltunski
     }
335
336 f046b159 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
337
/**
338
 * Not part of public API, do not document (public only because has to be used from the main package)
339
 *
340
 * @y.exclude
341
 */
342
   public void copyTransformToVertex()
343
     {
344 e54bfada Leszek Koltunski
     ByteBuffer buffer = (ByteBuffer)GLES30.glMapBufferRange( GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, 0,
345
                                                              TRAN_SIZE*mNumVertices, GLES30.GL_MAP_READ_BIT);
346 f046b159 Leszek Koltunski
     if( buffer!=null )
347
       {
348 e54bfada Leszek Koltunski
       FloatBuffer feedback = buffer.order(ByteOrder.nativeOrder()).asFloatBuffer();
349
       feedback.get(mVertAttribs1,0,VERT1_ATTRIBS*mNumVertices);
350
       mVBO1.update(mVertAttribs1);
351 f046b159 Leszek Koltunski
       }
352
     else
353
       {
354 b5be333a Leszek Koltunski
       int error = GLES30.glGetError();
355
       Log.e("mesh", "failed to map tf buffer, error="+error);
356 f046b159 Leszek Koltunski
       }
357
358 b5be333a Leszek Koltunski
     GLES30.glUnmapBuffer(GLES30.GL_TRANSFORM_FEEDBACK);
359 f046b159 Leszek Koltunski
     }
360
361 23b733db Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
362
/**
363 0d4aae88 Leszek Koltunski
 * Not part of public API, do not document (public only because has to be used from the main package)
364
 *
365
 * @y.exclude
366 23b733db Leszek Koltunski
 */
367 0d4aae88 Leszek Koltunski
   public int getTFO()
368 23b733db Leszek Koltunski
     {
369 b5be333a Leszek Koltunski
     return mTFO.createImmediately(mNumVertices*TRAN_SIZE, null);
370 23b733db Leszek Koltunski
     }
371
372
///////////////////////////////////////////////////////////////////////////////////////////////////
373
/**
374 0d4aae88 Leszek Koltunski
 * Not part of public API, do not document (public only because has to be used from the main package)
375
 *
376
 * @y.exclude
377 23b733db Leszek Koltunski
 */
378 0d4aae88 Leszek Koltunski
   public int getNumVertices()
379 23b733db Leszek Koltunski
     {
380 0d4aae88 Leszek Koltunski
     return mNumVertices;
381 23b733db Leszek Koltunski
     }
382
383 36d65d88 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
384
/**
385
 * Not part of public API, do not document (public only because has to be used from the main package)
386
 *
387
 * @y.exclude
388
 */
389
   public void send(int variant)
390
     {
391
     GLES30.glUniform1iv( mComponentAssociationH[variant], MAX_COMPONENTS, mAssociation, 0);
392
     }
393
394 044b5494 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
395 6c00149d Leszek Koltunski
/**
396
 * Not part of public API, do not document (public only because has to be used from the main package)
397
 *
398
 * @y.exclude
399
 */
400 da681e7e Leszek Koltunski
   public void bindVertexAttribs(DistortedProgram program)
401 6c00149d Leszek Koltunski
     {
402 07206c71 Leszek Koltunski
     if( mJobNode!=null )
403 f046b159 Leszek Koltunski
       {
404 07206c71 Leszek Koltunski
       mJobNode.execute();  // this will set itself to null
405 f046b159 Leszek Koltunski
       }
406
407 e54bfada Leszek Koltunski
     int index1 = mVBO1.createImmediately(mNumVertices*VERT1_SIZE, mVertAttribs1);
408
     int index2 = mVBO2.createImmediately(mNumVertices*VERT2_SIZE, mVertAttribs2);
409 22e60fba Leszek Koltunski
410 e54bfada Leszek Koltunski
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, index1 );
411
     GLES30.glVertexAttribPointer(program.mAttribute[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, VERT1_SIZE, OFFSET_POS);
412
     GLES30.glVertexAttribPointer(program.mAttribute[1], NOR_DATA_SIZE, GLES30.GL_FLOAT, false, VERT1_SIZE, OFFSET_NOR);
413
     GLES30.glVertexAttribPointer(program.mAttribute[2], INF_DATA_SIZE, GLES30.GL_FLOAT, false, VERT1_SIZE, OFFSET_INF);
414
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, index2 );
415
     GLES30.glVertexAttribPointer(program.mAttribute[3], TEX_DATA_SIZE, GLES30.GL_FLOAT, false, VERT2_SIZE, OFFSET_TEX);
416 36d65d88 Leszek Koltunski
     GLES30.glVertexAttribPointer(program.mAttribute[4], COM_DATA_SIZE, GLES30.GL_FLOAT, false, VERT2_SIZE, OFFSET_COM);
417 b7074bc6 Leszek Koltunski
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, 0);
418 da681e7e Leszek Koltunski
     }
419
420
///////////////////////////////////////////////////////////////////////////////////////////////////
421
/**
422
 * Not part of public API, do not document (public only because has to be used from the main package)
423
 *
424
 * @y.exclude
425
 */
426
   public void bindTransformAttribs(DistortedProgram program)
427
     {
428 7e53a35f Leszek Koltunski
     int index = mTFO.createImmediately(mNumVertices*TRAN_SIZE, null);
429 22e60fba Leszek Koltunski
430 b7074bc6 Leszek Koltunski
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, index );
431
     GLES30.glVertexAttribPointer(program.mAttribute[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, 0, 0);
432
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, 0);
433 6c00149d Leszek Koltunski
     }
434
435 7a5e538a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
436
/**
437
 * Not part of public API, do not document (public only because has to be used from the main package)
438
 *
439
 * @y.exclude
440
 */
441
   public void setInflate(float inflate)
442
     {
443
     mInflate = inflate;
444
     }
445
446
///////////////////////////////////////////////////////////////////////////////////////////////////
447
/**
448
 * Not part of public API, do not document (public only because has to be used from the main package)
449
 *
450
 * @y.exclude
451
 */
452
   public float getInflate()
453
     {
454
     return mInflate;
455
     }
456
457 466450b5 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
458
// PUBLIC API
459 3fc9327a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
460
/**
461
 * When rendering this Mesh, do we want to render the Normal vectors as well?
462 420836fc leszek
 * <p>
463
 * Will work only on OpenGL ES >= 3.0 devices.
464 3fc9327a Leszek Koltunski
 *
465
 * @param show Controls if we render the Normal vectors or not.
466
 */
467
   public void setShowNormals(boolean show)
468
     {
469 b7074bc6 Leszek Koltunski
     mShowNormals = show;
470 3fc9327a Leszek Koltunski
     }
471 466450b5 Leszek Koltunski
472 6c00149d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
473
/**
474
 * When rendering this mesh, should we also draw the normal vectors?
475
 *
476
 * @return <i>true</i> if we do render normal vectors
477
 */
478
   public boolean getShowNormals()
479
     {
480
     return mShowNormals;
481
     }
482
483 466450b5 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
484
/**
485
 * Release all internal resources.
486
 */
487
   public void markForDeletion()
488
     {
489 e54bfada Leszek Koltunski
     mVertAttribs1 = null;
490
     mVertAttribs2 = null;
491 466450b5 Leszek Koltunski
492 e54bfada Leszek Koltunski
     mVBO1.markForDeletion();
493
     mVBO2.markForDeletion();
494 466450b5 Leszek Koltunski
     mTFO.markForDeletion();
495
     }
496 fa8bc998 Leszek Koltunski
497
///////////////////////////////////////////////////////////////////////////////////////////////////
498
/**
499 f046b159 Leszek Koltunski
 * Apply a Vertex Effect to the vertex mesh.
500 fa8bc998 Leszek Koltunski
 * <p>
501 c90aca24 Leszek Koltunski
 * This is a static, permanent modification of the vertices contained in this Mesh. If the effects
502 f046b159 Leszek Koltunski
 * contain any Dynamics, the Dynamics will be evaluated at 0.
503 fa8bc998 Leszek Koltunski
 *
504 f046b159 Leszek Koltunski
 * We would call this several times building up a list of Effects to do. This list of effects gets
505
 * lazily executed only when the Mesh is used for rendering for the first time.
506 e172985c Leszek Koltunski
 *
507 f046b159 Leszek Koltunski
 * @param effect Vertex Effect to apply to the Mesh.
508 fa8bc998 Leszek Koltunski
 */
509 f046b159 Leszek Koltunski
   public void apply(VertexEffect effect)
510 fa8bc998 Leszek Koltunski
     {
511 07206c71 Leszek Koltunski
     mJobNode = DeferredJobs.vertex(this,effect);
512 c90aca24 Leszek Koltunski
     }
513
514
///////////////////////////////////////////////////////////////////////////////////////////////////
515
/**
516 a3a05347 Leszek Koltunski
 * Sets texture maps for (some of) the components of this mesh.
517 c90aca24 Leszek Koltunski
 * <p>
518 a3a05347 Leszek Koltunski
 * Format: ( x of lower-left corner, y of lower-left corner, width, height ).
519
 * For example maps[0] = new Static4D( 0.0, 0.5, 0.5, 0.5 ) sets the 0th component texture map to the
520 0d4aae88 Leszek Koltunski
 * upper-left quadrant of the texture.
521 a3a05347 Leszek Koltunski
 * <p>
522
 * Probably the most common user case would be sending as many maps as there are components in this
523
 * Mesh. One can also send less, or more (the extraneous ones will be ignored) and set some of them
524
 * to null (those will be ignored as well). So if there are 5 components, and we want to set the map
525
 * of the 2nd and 4rd one, call this with
526
 * maps = new Static4D[4]
527
 * maps[0] = null
528
 * maps[1] = the map for the 2nd component
529
 * maps[2] = null
530
 * maps[3] = the map for the 4th component
531
 *
532
 * A map's width and height have to be non-zero (but can be negative!)
533 e172985c Leszek Koltunski
 *
534
 * @param maps List of texture maps to apply to the texture's components.
535 c90aca24 Leszek Koltunski
 */
536 a3a05347 Leszek Koltunski
   public void setTextureMap(Static4D[] maps)
537 c90aca24 Leszek Koltunski
     {
538 0d4aae88 Leszek Koltunski
     int num_comp = mComponent.size();
539
     int num_maps = maps.length;
540 d917f059 Leszek Koltunski
     int min = Math.min(num_comp, num_maps);
541 de53cf3e Leszek Koltunski
     int vertex = 0;
542 a3a05347 Leszek Koltunski
     Static4D newMap, oldMap;
543 de53cf3e Leszek Koltunski
     Component comp;
544 a3a05347 Leszek Koltunski
     float newW, newH, ratW, ratH, movX, movY;
545 ea88d502 Leszek Koltunski
546 0d4aae88 Leszek Koltunski
     for(int i=0; i<min; i++)
547 ea88d502 Leszek Koltunski
       {
548 a3a05347 Leszek Koltunski
       newMap = maps[i];
549 7e53a35f Leszek Koltunski
       comp = mComponent.get(i);
550 a3a05347 Leszek Koltunski
551
       if( newMap!=null )
552 ea88d502 Leszek Koltunski
         {
553 a3a05347 Leszek Koltunski
         newW = newMap.get2();
554
         newH = newMap.get3();
555 de53cf3e Leszek Koltunski
556 a3a05347 Leszek Koltunski
         if( newW!=0.0f && newH!=0.0f )
557 de53cf3e Leszek Koltunski
           {
558 a3a05347 Leszek Koltunski
           oldMap = comp.mTextureMap;
559
           ratW = newW/oldMap.get2();
560
           ratH = newH/oldMap.get3();
561
           movX = newMap.get0() - ratW*oldMap.get0();
562
           movY = newMap.get1() - ratH*oldMap.get1();
563
564 e54bfada Leszek Koltunski
           for( int index=vertex*VERT2_ATTRIBS+TEX_ATTRIB ; vertex<=comp.mEndIndex; vertex++, index+=VERT2_ATTRIBS)
565 a3a05347 Leszek Koltunski
             {
566 e54bfada Leszek Koltunski
             mVertAttribs2[index  ] = ratW*mVertAttribs2[index  ] + movX;
567
             mVertAttribs2[index+1] = ratH*mVertAttribs2[index+1] + movY;
568 a3a05347 Leszek Koltunski
             }
569
           comp.setMap(newMap);
570 de53cf3e Leszek Koltunski
           }
571 ea88d502 Leszek Koltunski
         }
572 7e53a35f Leszek Koltunski
573
       vertex= comp.mEndIndex+1;
574 ea88d502 Leszek Koltunski
       }
575 c90aca24 Leszek Koltunski
576 e54bfada Leszek Koltunski
     mVBO2.invalidate();
577 fa8bc998 Leszek Koltunski
     }
578 9099e567 Leszek Koltunski
579 e172985c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
580
/**
581
 * Return the texture map of one of the components.
582
 *
583
 * @param component The component number whose texture map we want to retrieve.
584
 */
585
   public Static4D getTextureMap(int component)
586
     {
587
     return (component>=0 && component<mComponent.size()) ? mComponent.get(component).mTextureMap : null;
588
     }
589 cbd502ec Leszek Koltunski
590
///////////////////////////////////////////////////////////////////////////////////////////////////
591
/**
592 1e672c1d Leszek Koltunski
 * Set Effect association.
593 bc208a9c Leszek Koltunski
 *
594 1e672c1d Leszek Koltunski
 * This creates an association between a Component of this Mesh and a Vertex Effect.
595
 * One can set the association of an Effect and of a Component, and the Effect will only be active on
596 36d65d88 Leszek Koltunski
 * vertices of Components such that
597
 *
598
 * (effect assoc) & (component assoc) != 0 || (effect component) == (mesh component)
599
 *
600
 * (see main_vertex_shader)
601 bc208a9c Leszek Koltunski
 *
602
 * The point: this way we can configure the system so that each Vertex Effect acts only on a certain
603 1e672c1d Leszek Koltunski
 * subset of a Mesh, thus potentially significantly reducing the number of render calls.
604 cbd502ec Leszek Koltunski
 */
605 1e672c1d Leszek Koltunski
  public void setEffectAssociation(int component, int association)
606 bc208a9c Leszek Koltunski
    {
607 36d65d88 Leszek Koltunski
    if( component>=0 && component<MAX_COMPONENTS )
608 bc208a9c Leszek Koltunski
      {
609 36d65d88 Leszek Koltunski
      mAssociation[component] = association;
610 bc208a9c Leszek Koltunski
      }
611
    }
612
613
///////////////////////////////////////////////////////////////////////////////////////////////////
614
/**
615 4f81e0c8 Leszek Koltunski
 * Copy the Mesh.
616
 *
617
 * @param deep If to be a deep or shallow copy of mVertAttribs1, i.e. the array holding vertices,
618
 *             normals and inflates (the rest, in particular the mVertAttribs2 containing texture
619 36d65d88 Leszek Koltunski
 *             coordinates and effect associations, is always deep copied)
620 bc208a9c Leszek Koltunski
 */
621 4f81e0c8 Leszek Koltunski
   public abstract MeshBase copy(boolean deep);
622 bc208a9c Leszek Koltunski
   }