Project

General

Profile

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

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

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