Project

General

Profile

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

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

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 c90aca24 Leszek Koltunski
import android.opengl.Matrix;
24 6c00149d Leszek Koltunski
25 fa8bc998 Leszek Koltunski
import org.distorted.library.effect.MatrixEffect;
26 7602a827 Leszek Koltunski
import org.distorted.library.main.InternalBuffer;
27 da681e7e Leszek Koltunski
import org.distorted.library.program.DistortedProgram;
28 a3a05347 Leszek Koltunski
import org.distorted.library.type.Static4D;
29 6c00149d Leszek Koltunski
30 c90aca24 Leszek Koltunski
import java.util.ArrayList;
31 6a06a912 Leszek Koltunski
32
///////////////////////////////////////////////////////////////////////////////////////////////////
33 e0b6c593 Leszek Koltunski
/**
34 e5ba319d Leszek Koltunski
 * Abstract class which represents a Mesh, ie an array of vertices (rendered as a TRIANGLE_STRIP).
35 e0b6c593 Leszek Koltunski
 * <p>
36 da681e7e Leszek Koltunski
 * If you want to render to a particular shape, extend from here, construct a float array
37 7a5e538a Leszek Koltunski
 * containing per-vertex attributes, and call back setAttribs().
38 e0b6c593 Leszek Koltunski
 */
39 715e7726 Leszek Koltunski
public abstract class MeshBase
40 6a06a912 Leszek Koltunski
   {
41 227b9bca Leszek Koltunski
   // sizes of attributes of an individual vertex.
42
   private static final int POS_DATA_SIZE= 3; // vertex coordinates: x,y,z
43
   private static final int NOR_DATA_SIZE= 3; // normal vector: x,y,z
44
   private static final int INF_DATA_SIZE= 3; // 'inflate' vector: x,y,z
45 7a5e538a Leszek Koltunski
   private static final int TEX_DATA_SIZE= 2; // texture coordinates: s,t
46 6f2d931d Leszek Koltunski
47
   static final int POS_ATTRIB   = 0;
48
   static final int NOR_ATTRIB   = POS_DATA_SIZE;
49
   static final int INF_ATTRIB   = POS_DATA_SIZE + NOR_DATA_SIZE;
50
   static final int TEX_ATTRIB   = POS_DATA_SIZE + NOR_DATA_SIZE + INF_DATA_SIZE;
51 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
52
   static final int TRAN_ATTRIBS = POS_DATA_SIZE + POS_DATA_SIZE;                                  // number of attributes of a transform feedback vertex
53 6a06a912 Leszek Koltunski
54 da681e7e Leszek Koltunski
   private static final int BYTES_PER_FLOAT = 4;
55 3ef3364d Leszek Koltunski
56 6f2d931d Leszek Koltunski
   private static final int OFFSET_POS = POS_ATTRIB*BYTES_PER_FLOAT;
57
   private static final int OFFSET_NOR = NOR_ATTRIB*BYTES_PER_FLOAT;
58
   private static final int OFFSET_INF = INF_ATTRIB*BYTES_PER_FLOAT;
59
   private static final int OFFSET_TEX = TEX_ATTRIB*BYTES_PER_FLOAT;
60 227b9bca Leszek Koltunski
   private static final int TRAN_SIZE  = TRAN_ATTRIBS*BYTES_PER_FLOAT;
61
   private static final int VERT_SIZE  = VERT_ATTRIBS*BYTES_PER_FLOAT;
62 a51fe521 Leszek Koltunski
63 fa8bc998 Leszek Koltunski
   private boolean mShowNormals;      // when rendering this mesh, draw normal vectors?
64
   private InternalBuffer mVBO, mTFO; // main vertex buffer and transform feedback buffer
65 da681e7e Leszek Koltunski
   private int mNumVertices;
66 fa8bc998 Leszek Koltunski
   private float[] mVertAttribs;      // packed: PosX,PosY,PosZ, NorX,NorY,NorZ, InfX,InfY,InfZ, TexS,TexT
67 7a5e538a Leszek Koltunski
   private float mInflate;
68 1b059065 Leszek Koltunski
   private float mBoundingX, mBoundingY, mBoundingZ;
69 23b733db Leszek Koltunski
   private float mStretchX, mStretchY, mStretchZ;
70 3ef3364d Leszek Koltunski
71 7e53a35f Leszek Koltunski
   private static class Component
72 c90aca24 Leszek Koltunski
     {
73
     private int mEndIndex;
74 a3a05347 Leszek Koltunski
     private Static4D mTextureMap;
75 c90aca24 Leszek Koltunski
76 0d4aae88 Leszek Koltunski
     Component(int end)
77 c90aca24 Leszek Koltunski
       {
78 a3a05347 Leszek Koltunski
       mEndIndex  = end;
79
       mTextureMap= new Static4D(0,0,1,1);
80 c90aca24 Leszek Koltunski
       }
81
     Component(Component original)
82
       {
83
       mEndIndex = original.mEndIndex;
84 a3a05347 Leszek Koltunski
85
       float x = original.mTextureMap.get0();
86
       float y = original.mTextureMap.get1();
87
       float z = original.mTextureMap.get2();
88
       float w = original.mTextureMap.get3();
89
       mTextureMap = new Static4D(x,y,z,w);
90 0d4aae88 Leszek Koltunski
       }
91
92 a3a05347 Leszek Koltunski
     void setMap(Static4D map)
93 0d4aae88 Leszek Koltunski
       {
94 a3a05347 Leszek Koltunski
       mTextureMap.set(map.get0(),map.get1(),map.get2(),map.get3());
95 c90aca24 Leszek Koltunski
       }
96
     }
97
98
   private ArrayList<Component> mComponent;
99
100 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
101 3ef3364d Leszek Koltunski
102 1b059065 Leszek Koltunski
   MeshBase(float bx, float by, float bz)
103 3ef3364d Leszek Koltunski
     {
104 1b059065 Leszek Koltunski
     mBoundingX = bx/2;
105
     mBoundingY = by/2;
106
     mBoundingZ = bz/2;
107 044b5494 Leszek Koltunski
108 23b733db Leszek Koltunski
     mStretchX = 1.0f;
109
     mStretchY = 1.0f;
110
     mStretchZ = 1.0f;
111
112 3fc9327a Leszek Koltunski
     mShowNormals = false;
113 c90aca24 Leszek Koltunski
     mInflate     = 0.0f;
114 0d4aae88 Leszek Koltunski
     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 1b059065 Leszek Koltunski
     mBoundingX = original.mBoundingX;
126
     mBoundingY = original.mBoundingY;
127
     mBoundingZ = original.mBoundingZ;
128 044b5494 Leszek Koltunski
129 23b733db Leszek Koltunski
     mStretchX = original.mStretchX;
130
     mStretchY = original.mStretchY;
131
     mStretchZ = original.mStretchZ;
132
133 c90aca24 Leszek Koltunski
     mShowNormals = original.mShowNormals;
134
     mInflate     = original.mInflate;
135
136
     int size = original.mComponent.size();
137
     mComponent = new ArrayList<>();
138
     for(int i=0; i<size; i++)
139
       {
140
       Component comp = new Component(original.mComponent.get(i));
141
       mComponent.add(comp);
142
       }
143 42571056 Leszek Koltunski
144 b7074bc6 Leszek Koltunski
     mVBO = new InternalBuffer(GLES30.GL_ARRAY_BUFFER             , GLES30.GL_STATIC_READ);
145
     mTFO = new InternalBuffer(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, GLES30.GL_STATIC_READ);
146 c90aca24 Leszek Koltunski
147
     System.arraycopy(original.mVertAttribs,0,mVertAttribs,0,original.mNumVertices*VERT_ATTRIBS);
148
     setAttribs(mVertAttribs);
149 42571056 Leszek Koltunski
     }
150
151 0d4aae88 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
152
153
   int numComponents()
154
     {
155
     return mComponent.size();
156
     }
157
158
///////////////////////////////////////////////////////////////////////////////////////////////////
159
160
   void setBounding(float bx, float by, float bz)
161
     {
162
     mBoundingX = bx/2;
163
     mBoundingY = by/2;
164
     mBoundingZ = bz/2;
165
     }
166
167 42571056 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
168 6c00149d Leszek Koltunski
// when a derived class is done computing its mesh, it has to call this method.
169 42571056 Leszek Koltunski
170 227b9bca Leszek Koltunski
   void setAttribs(float[] vertexAttribs)
171 42571056 Leszek Koltunski
     {
172 227b9bca Leszek Koltunski
     mNumVertices = vertexAttribs.length/VERT_ATTRIBS;
173 fa8bc998 Leszek Koltunski
     mVertAttribs = vertexAttribs;
174 da681e7e Leszek Koltunski
175 0d4aae88 Leszek Koltunski
     mComponent.add(new Component(mNumVertices));
176 c90aca24 Leszek Koltunski
177 22e60fba Leszek Koltunski
     mVBO.invalidate();
178
     mTFO.invalidate();
179 42571056 Leszek Koltunski
     }
180
181
///////////////////////////////////////////////////////////////////////////////////////////////////
182 0d4aae88 Leszek Koltunski
// called from MeshJoined
183 42571056 Leszek Koltunski
184 0d4aae88 Leszek Koltunski
   void join(MeshBase[] meshes)
185 226144d0 leszek
     {
186 0d4aae88 Leszek Koltunski
     MeshBase mesh;
187
     Component comp;
188 e7f85322 Leszek Koltunski
     int numComponents, numVertices, numMeshes = meshes.length;
189 0d4aae88 Leszek Koltunski
     int origVertices = mNumVertices;
190 42571056 Leszek Koltunski
191 0d4aae88 Leszek Koltunski
     // compute new numVertices; take care of Components
192 044b5494 Leszek Koltunski
193 0d4aae88 Leszek Koltunski
     if( origVertices>0 )
194
       {
195 e7f85322 Leszek Koltunski
       numComponents = mComponent.size();
196 0d4aae88 Leszek Koltunski
       mNumVertices+= ( mNumVertices%2==1 ? 2:1 );
197 e7f85322 Leszek Koltunski
       mComponent.get(numComponents-1).mEndIndex = mNumVertices;
198 0d4aae88 Leszek Koltunski
       }
199 044b5494 Leszek Koltunski
200 e7f85322 Leszek Koltunski
     for(int i=0; i<numMeshes; i++)
201 0d4aae88 Leszek Koltunski
       {
202
       mesh = meshes[i];
203 e7f85322 Leszek Koltunski
       numComponents = mesh.mComponent.size();
204 044b5494 Leszek Koltunski
205 e7f85322 Leszek Koltunski
       for(int j=0; j<numComponents; j++)
206 0d4aae88 Leszek Koltunski
         {
207
         comp = new Component(mesh.mComponent.get(j));
208
         comp.mEndIndex += mNumVertices;
209
         mComponent.add(comp);
210
         }
211 23b733db Leszek Koltunski
212 e7f85322 Leszek Koltunski
       numVertices = mesh.mNumVertices;
213 22e60fba Leszek Koltunski
214 e7f85322 Leszek Koltunski
       if( mNumVertices==0 )
215
         {
216
         if( numMeshes>1 )
217
            mNumVertices += (numVertices%2==1 ? numVertices+2 : numVertices+1);
218
         else
219
            mNumVertices +=  numVertices;
220
         }
221
       else if( i==numMeshes-1 ) mNumVertices += (numVertices+1);
222
       else                      mNumVertices += (numVertices%2==1 ? numVertices+3 : numVertices+2);
223 0d4aae88 Leszek Koltunski
       }
224
225
     // allocate new attrib array
226
     float[] newAttribs = new float[VERT_ATTRIBS*mNumVertices];
227 e7f85322 Leszek Koltunski
     numVertices = origVertices;
228 0d4aae88 Leszek Koltunski
229
     if( origVertices>0 )
230
       {
231 e7f85322 Leszek Koltunski
       System.arraycopy(mVertAttribs,                             0, newAttribs,                         0, VERT_ATTRIBS*numVertices);
232 0d4aae88 Leszek Koltunski
       System.arraycopy(mVertAttribs, VERT_ATTRIBS*(origVertices-1), newAttribs, VERT_ATTRIBS*origVertices, VERT_ATTRIBS    );
233
       origVertices++;
234
235 e7f85322 Leszek Koltunski
       if( numVertices%2==1 )
236 0d4aae88 Leszek Koltunski
         {
237
         System.arraycopy(mVertAttribs, VERT_ATTRIBS*(origVertices-1), newAttribs, VERT_ATTRIBS*origVertices, VERT_ATTRIBS);
238
         origVertices++;
239
         }
240
       }
241
242 e7f85322 Leszek Koltunski
     for(int i=0; i<numMeshes; i++)
243 0d4aae88 Leszek Koltunski
       {
244
       mesh = meshes[i];
245 e7f85322 Leszek Koltunski
       numVertices = mesh.mNumVertices;
246 0d4aae88 Leszek Koltunski
247 22e60fba Leszek Koltunski
       if( origVertices>0 )
248
         {
249
         System.arraycopy(mesh.mVertAttribs, 0, newAttribs, VERT_ATTRIBS*origVertices, VERT_ATTRIBS    );
250
         origVertices++;
251
         }
252 e7f85322 Leszek Koltunski
       System.arraycopy(mesh.mVertAttribs, 0, newAttribs, VERT_ATTRIBS*origVertices, VERT_ATTRIBS*numVertices);
253
       origVertices+=numVertices;
254 0d4aae88 Leszek Koltunski
255 e7f85322 Leszek Koltunski
       if( i<numMeshes-1 )
256 0d4aae88 Leszek Koltunski
         {
257 e7f85322 Leszek Koltunski
         System.arraycopy(mesh.mVertAttribs, VERT_ATTRIBS*(numVertices-1), newAttribs, VERT_ATTRIBS*origVertices, VERT_ATTRIBS);
258 0d4aae88 Leszek Koltunski
         origVertices++;
259
260 e7f85322 Leszek Koltunski
         if( numVertices%2==1 )
261 0d4aae88 Leszek Koltunski
           {
262 e7f85322 Leszek Koltunski
           System.arraycopy(mesh.mVertAttribs, VERT_ATTRIBS*(numVertices-1), newAttribs, VERT_ATTRIBS*origVertices, VERT_ATTRIBS);
263 0d4aae88 Leszek Koltunski
           origVertices++;
264
           }
265
         }
266
       }
267
268
     if( origVertices!=mNumVertices )
269
       {
270
       android.util.Log.e("mesh", "join: origVertices: "+origVertices+" numVertices: "+mNumVertices);
271
       }
272
273
     mVertAttribs = newAttribs;
274
275 22e60fba Leszek Koltunski
     mVBO.invalidate();
276 23b733db Leszek Koltunski
     }
277
278
///////////////////////////////////////////////////////////////////////////////////////////////////
279
/**
280 0d4aae88 Leszek Koltunski
 * Not part of public API, do not document (public only because has to be used from the main package)
281
 *
282
 * @y.exclude
283 23b733db Leszek Koltunski
 */
284 0d4aae88 Leszek Koltunski
   public int getTFO()
285 23b733db Leszek Koltunski
     {
286 7e53a35f Leszek Koltunski
     return mTFO.getIndex();
287 23b733db Leszek Koltunski
     }
288
289
///////////////////////////////////////////////////////////////////////////////////////////////////
290
/**
291 0d4aae88 Leszek Koltunski
 * Not part of public API, do not document (public only because has to be used from the main package)
292
 *
293
 * @y.exclude
294 23b733db Leszek Koltunski
 */
295 0d4aae88 Leszek Koltunski
   public int getNumVertices()
296 23b733db Leszek Koltunski
     {
297 0d4aae88 Leszek Koltunski
     return mNumVertices;
298 23b733db Leszek Koltunski
     }
299
300 044b5494 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
301 6c00149d Leszek Koltunski
/**
302
 * Not part of public API, do not document (public only because has to be used from the main package)
303
 *
304
 * @y.exclude
305
 */
306 da681e7e Leszek Koltunski
   public void bindVertexAttribs(DistortedProgram program)
307 6c00149d Leszek Koltunski
     {
308 7e53a35f Leszek Koltunski
     int index = mVBO.createImmediately(mNumVertices*VERT_SIZE, mVertAttribs);
309 22e60fba Leszek Koltunski
310 b7074bc6 Leszek Koltunski
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, index );
311
     GLES30.glVertexAttribPointer(program.mAttribute[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, VERT_SIZE, OFFSET_POS);
312
     GLES30.glVertexAttribPointer(program.mAttribute[1], NOR_DATA_SIZE, GLES30.GL_FLOAT, false, VERT_SIZE, OFFSET_NOR);
313
     GLES30.glVertexAttribPointer(program.mAttribute[2], INF_DATA_SIZE, GLES30.GL_FLOAT, false, VERT_SIZE, OFFSET_INF);
314
     GLES30.glVertexAttribPointer(program.mAttribute[3], TEX_DATA_SIZE, GLES30.GL_FLOAT, false, VERT_SIZE, OFFSET_TEX);
315
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, 0);
316 da681e7e Leszek Koltunski
     }
317
318
///////////////////////////////////////////////////////////////////////////////////////////////////
319
/**
320
 * Not part of public API, do not document (public only because has to be used from the main package)
321
 *
322
 * @y.exclude
323
 */
324
   public void bindTransformAttribs(DistortedProgram program)
325
     {
326 7e53a35f Leszek Koltunski
     int index = mTFO.createImmediately(mNumVertices*TRAN_SIZE, null);
327 22e60fba Leszek Koltunski
328 b7074bc6 Leszek Koltunski
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, index );
329
     GLES30.glVertexAttribPointer(program.mAttribute[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, 0, 0);
330
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, 0);
331 6c00149d Leszek Koltunski
     }
332
333 7a5e538a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
334
/**
335
 * Not part of public API, do not document (public only because has to be used from the main package)
336
 *
337
 * @y.exclude
338
 */
339
   public void setInflate(float inflate)
340
     {
341
     mInflate = inflate;
342
     }
343
344
///////////////////////////////////////////////////////////////////////////////////////////////////
345
/**
346
 * Not part of public API, do not document (public only because has to be used from the main package)
347
 *
348
 * @y.exclude
349
 */
350
   public float getInflate()
351
     {
352
     return mInflate;
353
     }
354
355 466450b5 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
356
// PUBLIC API
357 3fc9327a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
358
/**
359
 * When rendering this Mesh, do we want to render the Normal vectors as well?
360 420836fc leszek
 * <p>
361
 * Will work only on OpenGL ES >= 3.0 devices.
362 3fc9327a Leszek Koltunski
 *
363
 * @param show Controls if we render the Normal vectors or not.
364
 */
365
   public void setShowNormals(boolean show)
366
     {
367 b7074bc6 Leszek Koltunski
     mShowNormals = show;
368 3fc9327a Leszek Koltunski
     }
369 466450b5 Leszek Koltunski
370 6c00149d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
371
/**
372
 * When rendering this mesh, should we also draw the normal vectors?
373
 *
374
 * @return <i>true</i> if we do render normal vectors
375
 */
376
   public boolean getShowNormals()
377
     {
378
     return mShowNormals;
379
     }
380
381 466450b5 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
382
/**
383
 * Release all internal resources.
384
 */
385
   public void markForDeletion()
386
     {
387 fa8bc998 Leszek Koltunski
     mVertAttribs = null;
388 466450b5 Leszek Koltunski
389
     mVBO.markForDeletion();
390
     mTFO.markForDeletion();
391
     }
392 fa8bc998 Leszek Koltunski
393
///////////////////////////////////////////////////////////////////////////////////////////////////
394
/**
395 c90aca24 Leszek Koltunski
 * Apply all Effects to the vertex mesh. Overwrite the mesh in place.
396 fa8bc998 Leszek Koltunski
 * <p>
397 c90aca24 Leszek Koltunski
 * This is a static, permanent modification of the vertices contained in this Mesh. If the effects
398
 * contain any Dynamics, they will be evaluated at 0.
399 fa8bc998 Leszek Koltunski
 *
400
 * Please note that calling this once with the complete list of Effects will be much faster than
401
 * calling it repeatedly with one Effect at a time, as we have to reallocate the array of vertices
402
 * each time.
403 e172985c Leszek Koltunski
 *
404
 * @param effects List of Matrix Effects to apply to the Mesh.
405 fa8bc998 Leszek Koltunski
 */
406
   public void apply(MatrixEffect[] effects)
407
     {
408 c90aca24 Leszek Koltunski
     float[][] matrix = new float[effects.length][16];
409
     float[] tmp;
410 0d4aae88 Leszek Koltunski
     float[] array = new float[7];
411 c90aca24 Leszek Koltunski
     float x,y,z;
412
     int numEffects = 0;
413
414
     for(MatrixEffect eff: effects)
415 fa8bc998 Leszek Koltunski
       {
416 c90aca24 Leszek Koltunski
       if( eff!=null )
417 fa8bc998 Leszek Koltunski
         {
418 c90aca24 Leszek Koltunski
         Matrix.setIdentityM(matrix[numEffects],0);
419
         eff.compute(array,0,0,0);
420
         eff.apply(matrix[numEffects], array, 0);
421
         numEffects++;
422 fa8bc998 Leszek Koltunski
         }
423
       }
424
425 7594a5d2 Leszek Koltunski
     for(int index=0; index<mNumVertices*VERT_ATTRIBS; index+=VERT_ATTRIBS )
426 c90aca24 Leszek Koltunski
       {
427
       for(int mat=0; mat<numEffects; mat++)
428
         {
429
         tmp = matrix[mat];
430
431
         x = mVertAttribs[index+POS_ATTRIB  ];
432
         y = mVertAttribs[index+POS_ATTRIB+1];
433
         z = mVertAttribs[index+POS_ATTRIB+2];
434
435
         mVertAttribs[index+POS_ATTRIB  ] = tmp[0]*x + tmp[4]*y + tmp[ 8]*z + tmp[12];
436
         mVertAttribs[index+POS_ATTRIB+1] = tmp[1]*x + tmp[5]*y + tmp[ 9]*z + tmp[13];
437
         mVertAttribs[index+POS_ATTRIB+2] = tmp[2]*x + tmp[6]*y + tmp[10]*z + tmp[14];
438
439
         x = mVertAttribs[index+NOR_ATTRIB  ];
440
         y = mVertAttribs[index+NOR_ATTRIB+1];
441
         z = mVertAttribs[index+NOR_ATTRIB+2];
442
443
         mVertAttribs[index+NOR_ATTRIB  ] = tmp[0]*x + tmp[4]*y + tmp[ 8]*z;
444
         mVertAttribs[index+NOR_ATTRIB+1] = tmp[1]*x + tmp[5]*y + tmp[ 9]*z;
445
         mVertAttribs[index+NOR_ATTRIB+2] = tmp[2]*x + tmp[6]*y + tmp[10]*z;
446
447
         x = mVertAttribs[index+INF_ATTRIB  ];
448
         y = mVertAttribs[index+INF_ATTRIB+1];
449
         z = mVertAttribs[index+INF_ATTRIB+2];
450
451
         mVertAttribs[index+INF_ATTRIB  ] = tmp[0]*x + tmp[4]*y + tmp[ 8]*z;
452
         mVertAttribs[index+INF_ATTRIB+1] = tmp[1]*x + tmp[5]*y + tmp[ 9]*z;
453
         mVertAttribs[index+INF_ATTRIB+2] = tmp[2]*x + tmp[6]*y + tmp[10]*z;
454
         }
455
       }
456
457 7e53a35f Leszek Koltunski
     mVBO.invalidate();
458 c90aca24 Leszek Koltunski
     }
459
460
///////////////////////////////////////////////////////////////////////////////////////////////////
461
/**
462 a3a05347 Leszek Koltunski
 * Sets texture maps for (some of) the components of this mesh.
463 c90aca24 Leszek Koltunski
 * <p>
464 a3a05347 Leszek Koltunski
 * Format: ( x of lower-left corner, y of lower-left corner, width, height ).
465
 * For example maps[0] = new Static4D( 0.0, 0.5, 0.5, 0.5 ) sets the 0th component texture map to the
466 0d4aae88 Leszek Koltunski
 * upper-left quadrant of the texture.
467 a3a05347 Leszek Koltunski
 * <p>
468
 * Probably the most common user case would be sending as many maps as there are components in this
469
 * Mesh. One can also send less, or more (the extraneous ones will be ignored) and set some of them
470
 * to null (those will be ignored as well). So if there are 5 components, and we want to set the map
471
 * of the 2nd and 4rd one, call this with
472
 * maps = new Static4D[4]
473
 * maps[0] = null
474
 * maps[1] = the map for the 2nd component
475
 * maps[2] = null
476
 * maps[3] = the map for the 4th component
477
 *
478
 * A map's width and height have to be non-zero (but can be negative!)
479 e172985c Leszek Koltunski
 *
480
 * @param maps List of texture maps to apply to the texture's components.
481 c90aca24 Leszek Koltunski
 */
482 a3a05347 Leszek Koltunski
   public void setTextureMap(Static4D[] maps)
483 c90aca24 Leszek Koltunski
     {
484 0d4aae88 Leszek Koltunski
     int num_comp = mComponent.size();
485
     int num_maps = maps.length;
486 d917f059 Leszek Koltunski
     int min = Math.min(num_comp, num_maps);
487 de53cf3e Leszek Koltunski
     int vertex = 0;
488 a3a05347 Leszek Koltunski
     Static4D newMap, oldMap;
489 de53cf3e Leszek Koltunski
     Component comp;
490 a3a05347 Leszek Koltunski
     float newW, newH, ratW, ratH, movX, movY;
491 ea88d502 Leszek Koltunski
492 0d4aae88 Leszek Koltunski
     for(int i=0; i<min; i++)
493 ea88d502 Leszek Koltunski
       {
494 a3a05347 Leszek Koltunski
       newMap = maps[i];
495 7e53a35f Leszek Koltunski
       comp = mComponent.get(i);
496 a3a05347 Leszek Koltunski
497
       if( newMap!=null )
498 ea88d502 Leszek Koltunski
         {
499 a3a05347 Leszek Koltunski
         newW = newMap.get2();
500
         newH = newMap.get3();
501 de53cf3e Leszek Koltunski
502 a3a05347 Leszek Koltunski
         if( newW!=0.0f && newH!=0.0f )
503 de53cf3e Leszek Koltunski
           {
504 a3a05347 Leszek Koltunski
           oldMap = comp.mTextureMap;
505
           ratW = newW/oldMap.get2();
506
           ratH = newH/oldMap.get3();
507
           movX = newMap.get0() - ratW*oldMap.get0();
508
           movY = newMap.get1() - ratH*oldMap.get1();
509
510 7e53a35f Leszek Koltunski
           for( int index=vertex*VERT_ATTRIBS+TEX_ATTRIB ; vertex<=comp.mEndIndex; vertex++, index+=VERT_ATTRIBS)
511 a3a05347 Leszek Koltunski
             {
512
             mVertAttribs[index  ] = ratW*mVertAttribs[index  ] + movX;
513
             mVertAttribs[index+1] = ratH*mVertAttribs[index+1] + movY;
514
             }
515
           comp.setMap(newMap);
516 de53cf3e Leszek Koltunski
           }
517 ea88d502 Leszek Koltunski
         }
518 7e53a35f Leszek Koltunski
519
       vertex= comp.mEndIndex+1;
520 ea88d502 Leszek Koltunski
       }
521 c90aca24 Leszek Koltunski
522 7e53a35f Leszek Koltunski
     mVBO.invalidate();
523 fa8bc998 Leszek Koltunski
     }
524 9099e567 Leszek Koltunski
525 e172985c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
526
/**
527
 * Return the texture map of one of the components.
528
 *
529
 * @param component The component number whose texture map we want to retrieve.
530
 */
531
   public Static4D getTextureMap(int component)
532
     {
533
     return (component>=0 && component<mComponent.size()) ? mComponent.get(component).mTextureMap : null;
534
     }
535
536 9099e567 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
537
/**
538 0d4aae88 Leszek Koltunski
 * Each mesh has its 'bounding box' - return half of its X-length.
539 9099e567 Leszek Koltunski
 * <p>
540 0d4aae88 Leszek Koltunski
 * In case of all 'simple' Meshes, the bounding box is always 1x1x1 (Sphere, Cubes) or 1x1x0
541
 * (Rectangles, Triangles, Quad - i.e. all 'flat' Meshes). But this can be something else in case of
542
 * MeshComponent.
543 9099e567 Leszek Koltunski
 */
544 0d4aae88 Leszek Koltunski
   public float getBoundingX()
545
    {
546
    return mBoundingX*mStretchX;
547
    }
548
549
///////////////////////////////////////////////////////////////////////////////////////////////////
550
/**
551
 * Each mesh has its 'bounding box' - return half of its Y-length.
552
 */
553
   public float getBoundingY()
554
    {
555
    return mBoundingY*mStretchY;
556
    }
557
558
///////////////////////////////////////////////////////////////////////////////////////////////////
559
/**
560
 * Each mesh has its 'bounding box' - return half of its Z-length.
561
 */
562
   public float getBoundingZ()
563
    {
564
    return mBoundingZ*mStretchZ;
565
    }
566
567
///////////////////////////////////////////////////////////////////////////////////////////////////
568
/**
569
 * Sometimes we want to display a Mesh on a rectangular screen. Then we need to stretch it by
570
 * different factors in x and y (or z) directions. If we also wanted do display some vertex effects
571
 * done on this mesh, let's say a bulge done by a Distort effect, and wanted the bulge to be round,
572
 * (i.e the same in x and y directions) then doing so without this method would be impossible.
573
 *
574
 * This sets 'stretch' factors in each 3 dimensions. All vertices of this Mesh will be premultiplied
575
 * by those factors in the very first line of the Vertex Shader, before any Effects are done on it.
576
 * Using this we can thus pre-stretch the mesh to aspect ratio equal to the surface we eventually
577
 * want to display the Mesh on, and this way we can achieve a round Distort bulge!
578
 *
579
 * This could also be used to pre-stretch a Rectangles Mesh to a size equal (in pixels) to the bitmap
580
 * this mesh is textured with - and this lets us work with all Effects in natural, pixel units.
581
 *
582
 * @param sx stretch factor in x.
583
 * @param sy stretch factor in y.
584
 * @param sz stretch factor in z.
585
 */
586
   public void setStretch(float sx, float sy, float sz)
587 9099e567 Leszek Koltunski
     {
588 0d4aae88 Leszek Koltunski
     mStretchX = sx;
589
     mStretchY = sy;
590
     mStretchZ = sz;
591
     }
592 9099e567 Leszek Koltunski
593 0d4aae88 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
594
/**
595
 * Returns the x-factor set by setStretch().
596
 */
597
   public float getStretchX()
598
     {
599
     return mStretchX;
600
     }
601 9099e567 Leszek Koltunski
602 0d4aae88 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
603
/**
604
 * Returns the y-factor set by setStretch().
605
 */
606
   public float getStretchY()
607
     {
608
     return mStretchY;
609
     }
610
611
///////////////////////////////////////////////////////////////////////////////////////////////////
612
/**
613
 * Returns the z-factor set by setStretch().
614
 */
615
   public float getStretchZ()
616
     {
617
     return mStretchZ;
618 9099e567 Leszek Koltunski
     }
619 6a06a912 Leszek Koltunski
   }
620 226144d0 leszek
621 3fc9327a Leszek Koltunski