Project

General

Profile

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

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

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 227b9bca Leszek Koltunski
   // sizes of attributes of an individual vertex.
47
   private static final int POS_DATA_SIZE= 3; // vertex coordinates: x,y,z
48
   private static final int NOR_DATA_SIZE= 3; // normal vector: x,y,z
49
   private static final int INF_DATA_SIZE= 3; // 'inflate' vector: x,y,z
50 7a5e538a Leszek Koltunski
   private static final int TEX_DATA_SIZE= 2; // texture coordinates: s,t
51 6f2d931d Leszek Koltunski
52
   static final int POS_ATTRIB   = 0;
53
   static final int NOR_ATTRIB   = POS_DATA_SIZE;
54
   static final int INF_ATTRIB   = POS_DATA_SIZE + NOR_DATA_SIZE;
55
   static final int TEX_ATTRIB   = POS_DATA_SIZE + NOR_DATA_SIZE + INF_DATA_SIZE;
56 227b9bca Leszek Koltunski
   static final int VERT_ATTRIBS = POS_DATA_SIZE + NOR_DATA_SIZE + INF_DATA_SIZE + TEX_DATA_SIZE;  // number of attributes of a 'normal' vertex
57
   static final int TRAN_ATTRIBS = POS_DATA_SIZE + POS_DATA_SIZE;                                  // number of attributes of a transform feedback vertex
58 6a06a912 Leszek Koltunski
59 da681e7e Leszek Koltunski
   private static final int BYTES_PER_FLOAT = 4;
60 3ef3364d Leszek Koltunski
61 6f2d931d Leszek Koltunski
   private static final int OFFSET_POS = POS_ATTRIB*BYTES_PER_FLOAT;
62
   private static final int OFFSET_NOR = NOR_ATTRIB*BYTES_PER_FLOAT;
63
   private static final int OFFSET_INF = INF_ATTRIB*BYTES_PER_FLOAT;
64
   private static final int OFFSET_TEX = TEX_ATTRIB*BYTES_PER_FLOAT;
65 227b9bca Leszek Koltunski
   private static final int TRAN_SIZE  = TRAN_ATTRIBS*BYTES_PER_FLOAT;
66
   private static final int VERT_SIZE  = VERT_ATTRIBS*BYTES_PER_FLOAT;
67 a51fe521 Leszek Koltunski
68 fa8bc998 Leszek Koltunski
   private boolean mShowNormals;      // when rendering this mesh, draw normal vectors?
69
   private InternalBuffer mVBO, mTFO; // main vertex buffer and transform feedback buffer
70 da681e7e Leszek Koltunski
   private int mNumVertices;
71 fa8bc998 Leszek Koltunski
   private float[] mVertAttribs;      // packed: PosX,PosY,PosZ, NorX,NorY,NorZ, InfX,InfY,InfZ, TexS,TexT
72 7a5e538a Leszek Koltunski
   private float mInflate;
73 f046b159 Leszek Koltunski
   private boolean mNeedsAdjustment;
74 3ef3364d Leszek Koltunski
75 7e53a35f Leszek Koltunski
   private static class Component
76 c90aca24 Leszek Koltunski
     {
77
     private int mEndIndex;
78 a3a05347 Leszek Koltunski
     private Static4D mTextureMap;
79 f046b159 Leszek Koltunski
     private EffectQueueVertex mQueue;
80 c90aca24 Leszek Koltunski
81 0d4aae88 Leszek Koltunski
     Component(int end)
82 c90aca24 Leszek Koltunski
       {
83 a3a05347 Leszek Koltunski
       mEndIndex  = end;
84
       mTextureMap= new Static4D(0,0,1,1);
85 f046b159 Leszek Koltunski
       mQueue     = new EffectQueueVertex();
86 c90aca24 Leszek Koltunski
       }
87
     Component(Component original)
88
       {
89
       mEndIndex = original.mEndIndex;
90 a3a05347 Leszek Koltunski
91
       float x = original.mTextureMap.get0();
92
       float y = original.mTextureMap.get1();
93
       float z = original.mTextureMap.get2();
94
       float w = original.mTextureMap.get3();
95
       mTextureMap = new Static4D(x,y,z,w);
96 f046b159 Leszek Koltunski
       mQueue = new EffectQueueVertex(original.mQueue);
97 0d4aae88 Leszek Koltunski
       }
98
99 a3a05347 Leszek Koltunski
     void setMap(Static4D map)
100 0d4aae88 Leszek Koltunski
       {
101 a3a05347 Leszek Koltunski
       mTextureMap.set(map.get0(),map.get1(),map.get2(),map.get3());
102 c90aca24 Leszek Koltunski
       }
103
     }
104
105
   private ArrayList<Component> mComponent;
106
107 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
108 3ef3364d Leszek Koltunski
109 0f10a0b6 Leszek Koltunski
   MeshBase()
110 3ef3364d Leszek Koltunski
     {
111 f046b159 Leszek Koltunski
     mShowNormals     = false;
112
     mInflate         = 0.0f;
113
     mNeedsAdjustment = false;
114
     mComponent       = new ArrayList<>();
115 c90aca24 Leszek Koltunski
116 b7074bc6 Leszek Koltunski
     mVBO = new InternalBuffer(GLES30.GL_ARRAY_BUFFER             , GLES30.GL_STATIC_READ);
117
     mTFO = new InternalBuffer(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, GLES30.GL_STATIC_READ);
118 c90aca24 Leszek Koltunski
     }
119
120
///////////////////////////////////////////////////////////////////////////////////////////////////
121
// copy constructor
122
123
   MeshBase(MeshBase original)
124
     {
125 f046b159 Leszek Koltunski
     mShowNormals     = original.mShowNormals;
126
     mInflate         = original.mInflate;
127
     mNeedsAdjustment = original.mNeedsAdjustment;
128 c90aca24 Leszek Koltunski
129
     int size = original.mComponent.size();
130
     mComponent = new ArrayList<>();
131
     for(int i=0; i<size; i++)
132
       {
133
       Component comp = new Component(original.mComponent.get(i));
134
       mComponent.add(comp);
135
       }
136 42571056 Leszek Koltunski
137 b7074bc6 Leszek Koltunski
     mVBO = new InternalBuffer(GLES30.GL_ARRAY_BUFFER             , GLES30.GL_STATIC_READ);
138
     mTFO = new InternalBuffer(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, GLES30.GL_STATIC_READ);
139 c90aca24 Leszek Koltunski
140
     System.arraycopy(original.mVertAttribs,0,mVertAttribs,0,original.mNumVertices*VERT_ATTRIBS);
141
     setAttribs(mVertAttribs);
142 42571056 Leszek Koltunski
     }
143
144 0d4aae88 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
145
146
   int numComponents()
147
     {
148
     return mComponent.size();
149
     }
150
151 42571056 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
152 6c00149d Leszek Koltunski
// when a derived class is done computing its mesh, it has to call this method.
153 42571056 Leszek Koltunski
154 227b9bca Leszek Koltunski
   void setAttribs(float[] vertexAttribs)
155 42571056 Leszek Koltunski
     {
156 227b9bca Leszek Koltunski
     mNumVertices = vertexAttribs.length/VERT_ATTRIBS;
157 fa8bc998 Leszek Koltunski
     mVertAttribs = vertexAttribs;
158 da681e7e Leszek Koltunski
159 0d4aae88 Leszek Koltunski
     mComponent.add(new Component(mNumVertices));
160 c90aca24 Leszek Koltunski
161 22e60fba Leszek Koltunski
     mVBO.invalidate();
162
     mTFO.invalidate();
163 42571056 Leszek Koltunski
     }
164
165
///////////////////////////////////////////////////////////////////////////////////////////////////
166 0d4aae88 Leszek Koltunski
// called from MeshJoined
167 42571056 Leszek Koltunski
168 0d4aae88 Leszek Koltunski
   void join(MeshBase[] meshes)
169 226144d0 leszek
     {
170 0d4aae88 Leszek Koltunski
     MeshBase mesh;
171
     Component comp;
172 e7f85322 Leszek Koltunski
     int numComponents, numVertices, numMeshes = meshes.length;
173 0d4aae88 Leszek Koltunski
     int origVertices = mNumVertices;
174 42571056 Leszek Koltunski
175 0d4aae88 Leszek Koltunski
     // compute new numVertices; take care of Components
176 044b5494 Leszek Koltunski
177 0d4aae88 Leszek Koltunski
     if( origVertices>0 )
178
       {
179 e7f85322 Leszek Koltunski
       numComponents = mComponent.size();
180 0d4aae88 Leszek Koltunski
       mNumVertices+= ( mNumVertices%2==1 ? 2:1 );
181 e7f85322 Leszek Koltunski
       mComponent.get(numComponents-1).mEndIndex = mNumVertices;
182 0d4aae88 Leszek Koltunski
       }
183 044b5494 Leszek Koltunski
184 e7f85322 Leszek Koltunski
     for(int i=0; i<numMeshes; i++)
185 0d4aae88 Leszek Koltunski
       {
186
       mesh = meshes[i];
187 e7f85322 Leszek Koltunski
       numComponents = mesh.mComponent.size();
188 044b5494 Leszek Koltunski
189 e7f85322 Leszek Koltunski
       for(int j=0; j<numComponents; j++)
190 0d4aae88 Leszek Koltunski
         {
191
         comp = new Component(mesh.mComponent.get(j));
192
         comp.mEndIndex += mNumVertices;
193
         mComponent.add(comp);
194
         }
195 23b733db Leszek Koltunski
196 e7f85322 Leszek Koltunski
       numVertices = mesh.mNumVertices;
197 22e60fba Leszek Koltunski
198 e7f85322 Leszek Koltunski
       if( mNumVertices==0 )
199
         {
200
         if( numMeshes>1 )
201
            mNumVertices += (numVertices%2==1 ? numVertices+2 : numVertices+1);
202
         else
203
            mNumVertices +=  numVertices;
204
         }
205
       else if( i==numMeshes-1 ) mNumVertices += (numVertices+1);
206
       else                      mNumVertices += (numVertices%2==1 ? numVertices+3 : numVertices+2);
207 0d4aae88 Leszek Koltunski
       }
208
209
     // allocate new attrib array
210
     float[] newAttribs = new float[VERT_ATTRIBS*mNumVertices];
211 e7f85322 Leszek Koltunski
     numVertices = origVertices;
212 0d4aae88 Leszek Koltunski
213
     if( origVertices>0 )
214
       {
215 e7f85322 Leszek Koltunski
       System.arraycopy(mVertAttribs,                             0, newAttribs,                         0, VERT_ATTRIBS*numVertices);
216 0d4aae88 Leszek Koltunski
       System.arraycopy(mVertAttribs, VERT_ATTRIBS*(origVertices-1), newAttribs, VERT_ATTRIBS*origVertices, VERT_ATTRIBS    );
217
       origVertices++;
218
219 e7f85322 Leszek Koltunski
       if( numVertices%2==1 )
220 0d4aae88 Leszek Koltunski
         {
221
         System.arraycopy(mVertAttribs, VERT_ATTRIBS*(origVertices-1), newAttribs, VERT_ATTRIBS*origVertices, VERT_ATTRIBS);
222
         origVertices++;
223
         }
224
       }
225
226 e7f85322 Leszek Koltunski
     for(int i=0; i<numMeshes; i++)
227 0d4aae88 Leszek Koltunski
       {
228
       mesh = meshes[i];
229 e7f85322 Leszek Koltunski
       numVertices = mesh.mNumVertices;
230 0d4aae88 Leszek Koltunski
231 22e60fba Leszek Koltunski
       if( origVertices>0 )
232
         {
233
         System.arraycopy(mesh.mVertAttribs, 0, newAttribs, VERT_ATTRIBS*origVertices, VERT_ATTRIBS    );
234
         origVertices++;
235
         }
236 e7f85322 Leszek Koltunski
       System.arraycopy(mesh.mVertAttribs, 0, newAttribs, VERT_ATTRIBS*origVertices, VERT_ATTRIBS*numVertices);
237
       origVertices+=numVertices;
238 0d4aae88 Leszek Koltunski
239 e7f85322 Leszek Koltunski
       if( i<numMeshes-1 )
240 0d4aae88 Leszek Koltunski
         {
241 e7f85322 Leszek Koltunski
         System.arraycopy(mesh.mVertAttribs, VERT_ATTRIBS*(numVertices-1), newAttribs, VERT_ATTRIBS*origVertices, VERT_ATTRIBS);
242 0d4aae88 Leszek Koltunski
         origVertices++;
243
244 e7f85322 Leszek Koltunski
         if( numVertices%2==1 )
245 0d4aae88 Leszek Koltunski
           {
246 e7f85322 Leszek Koltunski
           System.arraycopy(mesh.mVertAttribs, VERT_ATTRIBS*(numVertices-1), newAttribs, VERT_ATTRIBS*origVertices, VERT_ATTRIBS);
247 0d4aae88 Leszek Koltunski
           origVertices++;
248
           }
249
         }
250
       }
251
252
     if( origVertices!=mNumVertices )
253
       {
254
       android.util.Log.e("mesh", "join: origVertices: "+origVertices+" numVertices: "+mNumVertices);
255
       }
256
257
     mVertAttribs = newAttribs;
258
259 22e60fba Leszek Koltunski
     mVBO.invalidate();
260 23b733db Leszek Koltunski
     }
261
262 f046b159 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
263
/**
264
 * Not part of public API, do not document (public only because has to be used from the main package)
265
 *
266
 * @y.exclude
267
 */
268
   public void copyTransformToVertex()
269
     {
270
     float posX, posY, posZ, norX, norY, norZ;
271
     FloatBuffer feedback=null;
272
     ByteBuffer buffer = (ByteBuffer)GLES30.glMapBufferRange( GLES30.GL_TRANSFORM_FEEDBACK, 0, 6*4*mNumVertices,
273
                                                                GLES30.GL_MAP_READ_BIT);
274
     if( buffer!=null )
275
       {
276
       feedback = buffer.order(ByteOrder.nativeOrder()).asFloatBuffer();
277
       }
278
     else
279
       {
280
       Log.e("mesh", "print: failed to map tf buffer");
281
       }
282
283
     if( feedback!=null )
284
       {
285
       for(int vertex=0; vertex<mNumVertices; vertex++)
286
         {
287
         posX = feedback.get(6*vertex  );
288
         posY = feedback.get(6*vertex+1);
289
         posZ = feedback.get(6*vertex+2);
290
         norX = feedback.get(6*vertex+3) - posX;
291
         norY = feedback.get(6*vertex+4) - posY;
292
         norZ = feedback.get(6*vertex+5) - posZ;
293
294
         mVertAttribs[vertex*VERT_ATTRIBS + POS_ATTRIB     ] = posX;
295
         mVertAttribs[vertex*VERT_ATTRIBS + POS_ATTRIB + 1 ] = posY;
296
         mVertAttribs[vertex*VERT_ATTRIBS + POS_ATTRIB + 2 ] = posZ;
297
298
         mVertAttribs[vertex*VERT_ATTRIBS + NOR_ATTRIB     ] = norX;
299
         mVertAttribs[vertex*VERT_ATTRIBS + NOR_ATTRIB + 1 ] = norY;
300
         mVertAttribs[vertex*VERT_ATTRIBS + NOR_ATTRIB + 2 ] = norZ;
301
         }
302
       }
303
     }
304
305
///////////////////////////////////////////////////////////////////////////////////////////////////
306
/**
307
 * Not part of public API, do not document (public only because has to be used from the main package)
308
 *
309
 * @y.exclude
310
 */
311
   public void computeQueue()
312
     {
313
     int numComp = mComponent.size();
314
315
     for(int i=0; i<numComp; i++)
316
       {
317
       mComponent.get(i).mQueue.compute(1);
318
       }
319
     }
320
321
///////////////////////////////////////////////////////////////////////////////////////////////////
322
/**
323
 * Not part of public API, do not document (public only because has to be used from the main package)
324
 *
325
 * @y.exclude
326
 */
327
   public void sendQueue()
328
     {
329
     int numComp = mComponent.size();
330
331
     for(int i=0; i<numComp; i++)
332
       {
333
       mComponent.get(i).mQueue.send(0.0f,4);
334
       }
335
     }
336
337 23b733db Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
338
/**
339 0d4aae88 Leszek Koltunski
 * Not part of public API, do not document (public only because has to be used from the main package)
340
 *
341
 * @y.exclude
342 23b733db Leszek Koltunski
 */
343 0d4aae88 Leszek Koltunski
   public int getTFO()
344 23b733db Leszek Koltunski
     {
345 7e53a35f Leszek Koltunski
     return mTFO.getIndex();
346 23b733db Leszek Koltunski
     }
347
348
///////////////////////////////////////////////////////////////////////////////////////////////////
349
/**
350 0d4aae88 Leszek Koltunski
 * Not part of public API, do not document (public only because has to be used from the main package)
351
 *
352
 * @y.exclude
353 23b733db Leszek Koltunski
 */
354 0d4aae88 Leszek Koltunski
   public int getNumVertices()
355 23b733db Leszek Koltunski
     {
356 0d4aae88 Leszek Koltunski
     return mNumVertices;
357 23b733db Leszek Koltunski
     }
358
359 044b5494 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
360 6c00149d Leszek Koltunski
/**
361
 * Not part of public API, do not document (public only because has to be used from the main package)
362
 *
363
 * @y.exclude
364
 */
365 da681e7e Leszek Koltunski
   public void bindVertexAttribs(DistortedProgram program)
366 6c00149d Leszek Koltunski
     {
367 f046b159 Leszek Koltunski
     if( mNeedsAdjustment )
368
       {
369
       mNeedsAdjustment = false;
370
       DistortedLibrary.adjustVertices(this);
371
       }
372
373 7e53a35f Leszek Koltunski
     int index = mVBO.createImmediately(mNumVertices*VERT_SIZE, mVertAttribs);
374 22e60fba Leszek Koltunski
375 b7074bc6 Leszek Koltunski
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, index );
376
     GLES30.glVertexAttribPointer(program.mAttribute[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, VERT_SIZE, OFFSET_POS);
377
     GLES30.glVertexAttribPointer(program.mAttribute[1], NOR_DATA_SIZE, GLES30.GL_FLOAT, false, VERT_SIZE, OFFSET_NOR);
378
     GLES30.glVertexAttribPointer(program.mAttribute[2], INF_DATA_SIZE, GLES30.GL_FLOAT, false, VERT_SIZE, OFFSET_INF);
379
     GLES30.glVertexAttribPointer(program.mAttribute[3], TEX_DATA_SIZE, GLES30.GL_FLOAT, false, VERT_SIZE, OFFSET_TEX);
380
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, 0);
381 da681e7e Leszek Koltunski
     }
382
383
///////////////////////////////////////////////////////////////////////////////////////////////////
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 bindTransformAttribs(DistortedProgram program)
390
     {
391 7e53a35f Leszek Koltunski
     int index = mTFO.createImmediately(mNumVertices*TRAN_SIZE, null);
392 22e60fba Leszek Koltunski
393 b7074bc6 Leszek Koltunski
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, index );
394
     GLES30.glVertexAttribPointer(program.mAttribute[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, 0, 0);
395
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, 0);
396 6c00149d Leszek Koltunski
     }
397
398 7a5e538a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
399
/**
400
 * Not part of public API, do not document (public only because has to be used from the main package)
401
 *
402
 * @y.exclude
403
 */
404
   public void setInflate(float inflate)
405
     {
406
     mInflate = inflate;
407
     }
408
409
///////////////////////////////////////////////////////////////////////////////////////////////////
410
/**
411
 * Not part of public API, do not document (public only because has to be used from the main package)
412
 *
413
 * @y.exclude
414
 */
415
   public float getInflate()
416
     {
417
     return mInflate;
418
     }
419
420 466450b5 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
421
// PUBLIC API
422 3fc9327a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
423
/**
424
 * When rendering this Mesh, do we want to render the Normal vectors as well?
425 420836fc leszek
 * <p>
426
 * Will work only on OpenGL ES >= 3.0 devices.
427 3fc9327a Leszek Koltunski
 *
428
 * @param show Controls if we render the Normal vectors or not.
429
 */
430
   public void setShowNormals(boolean show)
431
     {
432 b7074bc6 Leszek Koltunski
     mShowNormals = show;
433 3fc9327a Leszek Koltunski
     }
434 466450b5 Leszek Koltunski
435 6c00149d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
436
/**
437
 * When rendering this mesh, should we also draw the normal vectors?
438
 *
439
 * @return <i>true</i> if we do render normal vectors
440
 */
441
   public boolean getShowNormals()
442
     {
443
     return mShowNormals;
444
     }
445
446 466450b5 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
447
/**
448
 * Release all internal resources.
449
 */
450
   public void markForDeletion()
451
     {
452 fa8bc998 Leszek Koltunski
     mVertAttribs = null;
453 466450b5 Leszek Koltunski
454
     mVBO.markForDeletion();
455
     mTFO.markForDeletion();
456
     }
457 fa8bc998 Leszek Koltunski
458
///////////////////////////////////////////////////////////////////////////////////////////////////
459
/**
460 f046b159 Leszek Koltunski
 * Apply a Vertex Effect to the vertex mesh.
461 fa8bc998 Leszek Koltunski
 * <p>
462 c90aca24 Leszek Koltunski
 * This is a static, permanent modification of the vertices contained in this Mesh. If the effects
463 f046b159 Leszek Koltunski
 * contain any Dynamics, the Dynamics will be evaluated at 0.
464 fa8bc998 Leszek Koltunski
 *
465 f046b159 Leszek Koltunski
 * We would call this several times building up a list of Effects to do. This list of effects gets
466
 * lazily executed only when the Mesh is used for rendering for the first time.
467 e172985c Leszek Koltunski
 *
468 f046b159 Leszek Koltunski
 * @param effect Vertex Effect to apply to the Mesh.
469 fa8bc998 Leszek Koltunski
 */
470 f046b159 Leszek Koltunski
   public void apply(VertexEffect effect)
471 fa8bc998 Leszek Koltunski
     {
472 f046b159 Leszek Koltunski
     int numComp = mComponent.size();
473 c90aca24 Leszek Koltunski
474 f046b159 Leszek Koltunski
     for(int i=0; i<numComp; i++)
475 fa8bc998 Leszek Koltunski
       {
476 f046b159 Leszek Koltunski
       mComponent.get(i).mQueue.add(effect);
477 fa8bc998 Leszek Koltunski
       }
478
479 f046b159 Leszek Koltunski
     mNeedsAdjustment = true;
480 c90aca24 Leszek Koltunski
     }
481
482
///////////////////////////////////////////////////////////////////////////////////////////////////
483
/**
484 a3a05347 Leszek Koltunski
 * Sets texture maps for (some of) the components of this mesh.
485 c90aca24 Leszek Koltunski
 * <p>
486 a3a05347 Leszek Koltunski
 * Format: ( x of lower-left corner, y of lower-left corner, width, height ).
487
 * For example maps[0] = new Static4D( 0.0, 0.5, 0.5, 0.5 ) sets the 0th component texture map to the
488 0d4aae88 Leszek Koltunski
 * upper-left quadrant of the texture.
489 a3a05347 Leszek Koltunski
 * <p>
490
 * Probably the most common user case would be sending as many maps as there are components in this
491
 * Mesh. One can also send less, or more (the extraneous ones will be ignored) and set some of them
492
 * to null (those will be ignored as well). So if there are 5 components, and we want to set the map
493
 * of the 2nd and 4rd one, call this with
494
 * maps = new Static4D[4]
495
 * maps[0] = null
496
 * maps[1] = the map for the 2nd component
497
 * maps[2] = null
498
 * maps[3] = the map for the 4th component
499
 *
500
 * A map's width and height have to be non-zero (but can be negative!)
501 e172985c Leszek Koltunski
 *
502
 * @param maps List of texture maps to apply to the texture's components.
503 c90aca24 Leszek Koltunski
 */
504 a3a05347 Leszek Koltunski
   public void setTextureMap(Static4D[] maps)
505 c90aca24 Leszek Koltunski
     {
506 0d4aae88 Leszek Koltunski
     int num_comp = mComponent.size();
507
     int num_maps = maps.length;
508 d917f059 Leszek Koltunski
     int min = Math.min(num_comp, num_maps);
509 de53cf3e Leszek Koltunski
     int vertex = 0;
510 a3a05347 Leszek Koltunski
     Static4D newMap, oldMap;
511 de53cf3e Leszek Koltunski
     Component comp;
512 a3a05347 Leszek Koltunski
     float newW, newH, ratW, ratH, movX, movY;
513 ea88d502 Leszek Koltunski
514 0d4aae88 Leszek Koltunski
     for(int i=0; i<min; i++)
515 ea88d502 Leszek Koltunski
       {
516 a3a05347 Leszek Koltunski
       newMap = maps[i];
517 7e53a35f Leszek Koltunski
       comp = mComponent.get(i);
518 a3a05347 Leszek Koltunski
519
       if( newMap!=null )
520 ea88d502 Leszek Koltunski
         {
521 a3a05347 Leszek Koltunski
         newW = newMap.get2();
522
         newH = newMap.get3();
523 de53cf3e Leszek Koltunski
524 a3a05347 Leszek Koltunski
         if( newW!=0.0f && newH!=0.0f )
525 de53cf3e Leszek Koltunski
           {
526 a3a05347 Leszek Koltunski
           oldMap = comp.mTextureMap;
527
           ratW = newW/oldMap.get2();
528
           ratH = newH/oldMap.get3();
529
           movX = newMap.get0() - ratW*oldMap.get0();
530
           movY = newMap.get1() - ratH*oldMap.get1();
531
532 7e53a35f Leszek Koltunski
           for( int index=vertex*VERT_ATTRIBS+TEX_ATTRIB ; vertex<=comp.mEndIndex; vertex++, index+=VERT_ATTRIBS)
533 a3a05347 Leszek Koltunski
             {
534
             mVertAttribs[index  ] = ratW*mVertAttribs[index  ] + movX;
535
             mVertAttribs[index+1] = ratH*mVertAttribs[index+1] + movY;
536
             }
537
           comp.setMap(newMap);
538 de53cf3e Leszek Koltunski
           }
539 ea88d502 Leszek Koltunski
         }
540 7e53a35f Leszek Koltunski
541
       vertex= comp.mEndIndex+1;
542 ea88d502 Leszek Koltunski
       }
543 c90aca24 Leszek Koltunski
544 7e53a35f Leszek Koltunski
     mVBO.invalidate();
545 fa8bc998 Leszek Koltunski
     }
546 9099e567 Leszek Koltunski
547 e172985c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
548
/**
549
 * Return the texture map of one of the components.
550
 *
551
 * @param component The component number whose texture map we want to retrieve.
552
 */
553
   public Static4D getTextureMap(int component)
554
     {
555
     return (component>=0 && component<mComponent.size()) ? mComponent.get(component).mTextureMap : null;
556
     }
557 6a06a912 Leszek Koltunski
   }
558 226144d0 leszek
559 3fc9327a Leszek Koltunski