blob: 277480924d65babe7a7abe72d1fc72b2fac0c169 [file] [log] [blame]
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.renderscript;
18
19import java.util.Vector;
20
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -070021import android.util.Log;
22
23/**
Robert Ly11518ac2011-02-09 13:57:06 -080024 * <p>This class is a container for geometric data displayed with
25 * Renderscript. Internally, a mesh is a collection of allocations that
Alex Sakhartchoukdf272022011-01-09 11:34:03 -080026 * represent vertex data (positions, normals, texture
Robert Ly11518ac2011-02-09 13:57:06 -080027 * coordinates) and index data such as triangles and lines. </p>
28 * <p>
29 * Vertex data could either be interleaved within one
30 * allocation that is provided separately, as multiple allocation
31 * objects, or done as a combination of both. When a
Alex Sakhartchoukdf272022011-01-09 11:34:03 -080032 * vertex channel name matches an input in the vertex program,
Robert Ly11518ac2011-02-09 13:57:06 -080033 * Renderscript automatically connects the two together.
34 * </p>
35 * <p>
36 * Parts of the mesh can be rendered with either explicit
Alex Sakhartchoukdf272022011-01-09 11:34:03 -080037 * index sets or primitive types.
Robert Ly11518ac2011-02-09 13:57:06 -080038 * </p>
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -070039 **/
40public class Mesh extends BaseObj {
41
Alex Sakhartchoukdf272022011-01-09 11:34:03 -080042 /**
43 * Describes the way mesh vertex data is interpreted when rendering
44 *
45 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080046 public enum Primitive {
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080047 /**
48 * Vertex data will be rendered as a series of points
49 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080050 POINT (0),
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080051 /**
52 * Vertex pairs will be rendered as lines
53 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080054 LINE (1),
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080055 /**
56 * Vertex data will be rendered as a connected line strip
57 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080058 LINE_STRIP (2),
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080059 /**
60 * Vertices will be rendered as individual triangles
61 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080062 TRIANGLE (3),
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080063 /**
64 * Vertices will be rendered as a connected triangle strip
65 * defined by the first three vertices with each additional
66 * triangle defined by a new vertex
67 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080068 TRIANGLE_STRIP (4),
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080069 /**
70 * Vertices will be rendered as a sequence of triangles that all
71 * share first vertex as the origin
72 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080073 TRIANGLE_FAN (5);
74
75 int mID;
76 Primitive(int id) {
77 mID = id;
78 }
79 }
80
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -070081 Allocation[] mVertexBuffers;
82 Allocation[] mIndexBuffers;
83 Primitive[] mPrimitives;
84
85 Mesh(int id, RenderScript rs) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -070086 super(id, rs);
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -070087 }
88
Alex Sakhartchoukdf272022011-01-09 11:34:03 -080089 /**
90 * @return number of allocations containing vertex data
91 *
92 **/
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -070093 public int getVertexAllocationCount() {
94 if(mVertexBuffers == null) {
95 return 0;
96 }
97 return mVertexBuffers.length;
98 }
Alex Sakhartchoukdf272022011-01-09 11:34:03 -080099 /**
100 * @param slot index in the list of allocations to return
101 * @return vertex data allocation at the given index
102 *
103 **/
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700104 public Allocation getVertexAllocation(int slot) {
105 return mVertexBuffers[slot];
106 }
107
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800108 /**
109 * @return number of primitives or index sets in the mesh
110 *
111 **/
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700112 public int getPrimitiveCount() {
113 if(mIndexBuffers == null) {
114 return 0;
115 }
116 return mIndexBuffers.length;
117 }
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800118
119 /**
120 * @param slot locaton within the list of index set allocation
121 * @return allocation containing primtive index data or null if
122 * the index data is not specified explicitly
123 *
124 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800125 public Allocation getIndexSetAllocation(int slot) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700126 return mIndexBuffers[slot];
127 }
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800128 /**
129 * @param slot locaiton within the list of index set primitives
130 * @return index set primitive type
131 *
132 **/
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700133 public Primitive getPrimitive(int slot) {
134 return mPrimitives[slot];
135 }
136
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700137 @Override
138 void updateFromNative() {
Jason Sams06d69de2010-11-09 17:11:40 -0800139 super.updateFromNative();
140 int vtxCount = mRS.nMeshGetVertexBufferCount(getID());
141 int idxCount = mRS.nMeshGetIndexCount(getID());
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700142
143 int[] vtxIDs = new int[vtxCount];
144 int[] idxIDs = new int[idxCount];
145 int[] primitives = new int[idxCount];
146
Jason Sams06d69de2010-11-09 17:11:40 -0800147 mRS.nMeshGetVertices(getID(), vtxIDs, vtxCount);
148 mRS.nMeshGetIndices(getID(), idxIDs, primitives, idxCount);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700149
150 mVertexBuffers = new Allocation[vtxCount];
151 mIndexBuffers = new Allocation[idxCount];
152 mPrimitives = new Primitive[idxCount];
153
154 for(int i = 0; i < vtxCount; i ++) {
155 if(vtxIDs[i] != 0) {
Jason Samsd4b23b52010-12-13 15:32:35 -0800156 mVertexBuffers[i] = new Allocation(vtxIDs[i], mRS, null, Allocation.USAGE_SCRIPT);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700157 mVertexBuffers[i].updateFromNative();
158 }
159 }
160
161 for(int i = 0; i < idxCount; i ++) {
162 if(idxIDs[i] != 0) {
Jason Samsd4b23b52010-12-13 15:32:35 -0800163 mIndexBuffers[i] = new Allocation(idxIDs[i], mRS, null, Allocation.USAGE_SCRIPT);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700164 mIndexBuffers[i].updateFromNative();
165 }
166 mPrimitives[i] = Primitive.values()[primitives[i]];
167 }
168 }
169
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800170 /**
Robert Ly11518ac2011-02-09 13:57:06 -0800171 * Mesh builder object. It starts empty and requires you to
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800172 * add the types necessary to create vertex and index
Robert Ly11518ac2011-02-09 13:57:06 -0800173 * allocations.
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800174 *
175 */
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700176 public static class Builder {
177 RenderScript mRS;
Jason Samsd1952402010-12-20 12:55:28 -0800178 int mUsage;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700179
180 class Entry {
181 Type t;
182 Element e;
183 int size;
184 Primitive prim;
Jason Samsd1952402010-12-20 12:55:28 -0800185 int usage;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700186 }
187
188 int mVertexTypeCount;
189 Entry[] mVertexTypes;
190 Vector mIndexTypes;
191
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800192 /**
193 * Creates builder object
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800194 * @param rs Context to which the mesh will belong.
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800195 * @param usage specifies how the mesh allocations are to be
196 * handled, whether they need to be uploaded to a
197 * buffer on the gpu, maintain a cpu copy, etc
198 */
Jason Samsd1952402010-12-20 12:55:28 -0800199 public Builder(RenderScript rs, int usage) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700200 mRS = rs;
Jason Samsd1952402010-12-20 12:55:28 -0800201 mUsage = usage;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700202 mVertexTypeCount = 0;
203 mVertexTypes = new Entry[16];
204 mIndexTypes = new Vector();
205 }
206
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800207 /**
208 * @return internal index of the last vertex buffer type added to
209 * builder
210 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800211 public int getCurrentVertexTypeIndex() {
212 return mVertexTypeCount - 1;
213 }
214
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800215 /**
216 * @return internal index of the last index set added to the
217 * builder
218 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800219 public int getCurrentIndexSetIndex() {
220 return mIndexTypes.size() - 1;
221 }
222
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800223 /**
224 * Adds a vertex data type to the builder object
225 *
Stephen Hinesb11e3d22011-01-11 19:30:58 -0800226 * @param t type of the vertex data allocation to be created
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800227 *
228 * @return this
229 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800230 public Builder addVertexType(Type t) throws IllegalStateException {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700231 if (mVertexTypeCount >= mVertexTypes.length) {
232 throw new IllegalStateException("Max vertex types exceeded.");
233 }
234
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700235 mVertexTypes[mVertexTypeCount] = new Entry();
236 mVertexTypes[mVertexTypeCount].t = t;
237 mVertexTypes[mVertexTypeCount].e = null;
238 mVertexTypeCount++;
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800239 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700240 }
241
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800242 /**
243 * Adds a vertex data type to the builder object
244 *
245 * @param e element describing the vertex data layout
246 * @param size number of elements in the buffer
247 *
248 * @return this
249 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800250 public Builder addVertexType(Element e, int size) throws IllegalStateException {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700251 if (mVertexTypeCount >= mVertexTypes.length) {
252 throw new IllegalStateException("Max vertex types exceeded.");
253 }
254
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700255 mVertexTypes[mVertexTypeCount] = new Entry();
256 mVertexTypes[mVertexTypeCount].t = null;
257 mVertexTypes[mVertexTypeCount].e = e;
258 mVertexTypes[mVertexTypeCount].size = size;
259 mVertexTypeCount++;
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800260 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700261 }
262
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800263 /**
264 * Adds an index set data type to the builder object
265 *
266 * @param t type of the index set data, could be null
267 * @param p primitive type
268 *
269 * @return this
270 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800271 public Builder addIndexSetType(Type t, Primitive p) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700272 Entry indexType = new Entry();
273 indexType.t = t;
274 indexType.e = null;
275 indexType.size = 0;
276 indexType.prim = p;
277 mIndexTypes.addElement(indexType);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800278 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700279 }
280
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800281 /**
282 * Adds an index set primitive type to the builder object
283 *
284 * @param p primitive type
285 *
286 * @return this
287 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800288 public Builder addIndexSetType(Primitive p) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700289 Entry indexType = new Entry();
290 indexType.t = null;
291 indexType.e = null;
292 indexType.size = 0;
293 indexType.prim = p;
294 mIndexTypes.addElement(indexType);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800295 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700296 }
297
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800298 /**
299 * Adds an index set data type to the builder object
300 *
301 * @param e element describing the index set data layout
302 * @param size number of elements in the buffer
303 * @param p primitive type
304 *
305 * @return this
306 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800307 public Builder addIndexSetType(Element e, int size, Primitive p) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700308 Entry indexType = new Entry();
309 indexType.t = null;
310 indexType.e = e;
311 indexType.size = size;
312 indexType.prim = p;
313 mIndexTypes.addElement(indexType);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800314 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700315 }
316
317 Type newType(Element e, int size) {
318 Type.Builder tb = new Type.Builder(mRS, e);
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800319 tb.setX(size);
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700320 return tb.create();
321 }
322
323 static synchronized Mesh internalCreate(RenderScript rs, Builder b) {
324
325 int id = rs.nMeshCreate(b.mVertexTypeCount, b.mIndexTypes.size());
326 Mesh newMesh = new Mesh(id, rs);
327 newMesh.mIndexBuffers = new Allocation[b.mIndexTypes.size()];
328 newMesh.mPrimitives = new Primitive[b.mIndexTypes.size()];
329 newMesh.mVertexBuffers = new Allocation[b.mVertexTypeCount];
330
331 for(int ct = 0; ct < b.mIndexTypes.size(); ct ++) {
332 Allocation alloc = null;
333 Entry entry = (Entry)b.mIndexTypes.elementAt(ct);
334 if (entry.t != null) {
Jason Samsd1952402010-12-20 12:55:28 -0800335 alloc = Allocation.createTyped(rs, entry.t, b.mUsage);
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700336 }
337 else if(entry.e != null) {
Jason Samsd1952402010-12-20 12:55:28 -0800338 alloc = Allocation.createSized(rs, entry.e, entry.size, b.mUsage);
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700339 }
340 int allocID = (alloc == null) ? 0 : alloc.getID();
341 rs.nMeshBindIndex(id, allocID, entry.prim.mID, ct);
342 newMesh.mIndexBuffers[ct] = alloc;
343 newMesh.mPrimitives[ct] = entry.prim;
344 }
345
346 for(int ct = 0; ct < b.mVertexTypeCount; ct ++) {
347 Allocation alloc = null;
348 Entry entry = b.mVertexTypes[ct];
349 if (entry.t != null) {
Jason Samsd1952402010-12-20 12:55:28 -0800350 alloc = Allocation.createTyped(rs, entry.t, b.mUsage);
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700351 } else if(entry.e != null) {
Jason Samsd1952402010-12-20 12:55:28 -0800352 alloc = Allocation.createSized(rs, entry.e, entry.size, b.mUsage);
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700353 }
354 rs.nMeshBindVertex(id, alloc.getID(), ct);
355 newMesh.mVertexBuffers[ct] = alloc;
356 }
Alex Sakhartchouk9d71e212010-11-08 15:10:52 -0800357 rs.nMeshInitVertexAttribs(id);
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700358
359 return newMesh;
360 }
361
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800362 /**
363 * Create a Mesh object from the current state of the builder
364 *
365 **/
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700366 public Mesh create() {
367 mRS.validate();
368 Mesh sm = internalCreate(mRS, this);
369 return sm;
370 }
371 }
372
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800373 /**
374 * Mesh builder object. It starts empty and requires the user to
375 * add all the vertex and index allocations that comprise the
376 * mesh
377 *
378 */
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700379 public static class AllocationBuilder {
380 RenderScript mRS;
381
382 class Entry {
383 Allocation a;
384 Primitive prim;
385 }
386
387 int mVertexTypeCount;
388 Entry[] mVertexTypes;
389
390 Vector mIndexTypes;
391
392 public AllocationBuilder(RenderScript rs) {
393 mRS = rs;
394 mVertexTypeCount = 0;
395 mVertexTypes = new Entry[16];
396 mIndexTypes = new Vector();
397 }
398
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800399 /**
400 * @return internal index of the last vertex buffer type added to
401 * builder
402 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800403 public int getCurrentVertexTypeIndex() {
404 return mVertexTypeCount - 1;
405 }
406
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800407 /**
408 * @return internal index of the last index set added to the
409 * builder
410 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800411 public int getCurrentIndexSetIndex() {
412 return mIndexTypes.size() - 1;
413 }
414
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800415 /**
416 * Adds an allocation containing vertex buffer data to the
417 * builder
418 *
419 * @param a vertex data allocation
420 *
421 * @return this
422 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800423 public AllocationBuilder addVertexAllocation(Allocation a) throws IllegalStateException {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700424 if (mVertexTypeCount >= mVertexTypes.length) {
425 throw new IllegalStateException("Max vertex types exceeded.");
426 }
427
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700428 mVertexTypes[mVertexTypeCount] = new Entry();
429 mVertexTypes[mVertexTypeCount].a = a;
430 mVertexTypeCount++;
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800431 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700432 }
433
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800434 /**
435 * Adds an allocation containing index buffer data and index type
436 * to the builder
437 *
438 * @param a index set data allocation, could be null
439 * @param p index set primitive type
440 *
441 * @return this
442 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800443 public AllocationBuilder addIndexSetAllocation(Allocation a, Primitive p) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700444 Entry indexType = new Entry();
445 indexType.a = a;
446 indexType.prim = p;
447 mIndexTypes.addElement(indexType);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800448 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700449 }
450
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800451 /**
452 * Adds an index set type to the builder
453 *
454 * @param p index set primitive type
455 *
456 * @return this
457 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800458 public AllocationBuilder addIndexSetType(Primitive p) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700459 Entry indexType = new Entry();
460 indexType.a = null;
461 indexType.prim = p;
462 mIndexTypes.addElement(indexType);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800463 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700464 }
465
466 static synchronized Mesh internalCreate(RenderScript rs, AllocationBuilder b) {
467
468 int id = rs.nMeshCreate(b.mVertexTypeCount, b.mIndexTypes.size());
469 Mesh newMesh = new Mesh(id, rs);
470 newMesh.mIndexBuffers = new Allocation[b.mIndexTypes.size()];
471 newMesh.mPrimitives = new Primitive[b.mIndexTypes.size()];
472 newMesh.mVertexBuffers = new Allocation[b.mVertexTypeCount];
473
474 for(int ct = 0; ct < b.mIndexTypes.size(); ct ++) {
475 Entry entry = (Entry)b.mIndexTypes.elementAt(ct);
476 int allocID = (entry.a == null) ? 0 : entry.a.getID();
477 rs.nMeshBindIndex(id, allocID, entry.prim.mID, ct);
478 newMesh.mIndexBuffers[ct] = entry.a;
479 newMesh.mPrimitives[ct] = entry.prim;
480 }
481
482 for(int ct = 0; ct < b.mVertexTypeCount; ct ++) {
483 Entry entry = b.mVertexTypes[ct];
Jason Sams06d69de2010-11-09 17:11:40 -0800484 rs.nMeshBindVertex(id, entry.a.getID(), ct);
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700485 newMesh.mVertexBuffers[ct] = entry.a;
486 }
Alex Sakhartchouk9d71e212010-11-08 15:10:52 -0800487 rs.nMeshInitVertexAttribs(id);
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700488
489 return newMesh;
490 }
491
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800492 /**
493 * Create a Mesh object from the current state of the builder
494 *
495 **/
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700496 public Mesh create() {
497 mRS.validate();
498 Mesh sm = internalCreate(mRS, this);
499 return sm;
500 }
501 }
502
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800503 /**
504 * Builder that allows creation of a mesh object point by point
505 * and triangle by triangle
506 *
507 **/
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700508 public static class TriangleMeshBuilder {
509 float mVtxData[];
510 int mVtxCount;
511 short mIndexData[];
512 int mIndexCount;
513 RenderScript mRS;
514 Element mElement;
515
516 float mNX = 0;
517 float mNY = 0;
518 float mNZ = -1;
519 float mS0 = 0;
520 float mT0 = 0;
521 float mR = 1;
522 float mG = 1;
523 float mB = 1;
524 float mA = 1;
525
526 int mVtxSize;
527 int mFlags;
528
529 public static final int COLOR = 0x0001;
530 public static final int NORMAL = 0x0002;
531 public static final int TEXTURE_0 = 0x0100;
532
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800533 /**
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800534 * @param rs Context to which the mesh will belong.
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800535 * @param vtxSize specifies whether the vertex is a float2 or
536 * float3
537 * @param flags bitfield that is a combination of COLOR, NORMAL,
538 * and TEXTURE_0 that specifies what vertex data
539 * channels are present in the mesh
540 *
541 **/
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700542 public TriangleMeshBuilder(RenderScript rs, int vtxSize, int flags) {
543 mRS = rs;
544 mVtxCount = 0;
545 mIndexCount = 0;
546 mVtxData = new float[128];
547 mIndexData = new short[128];
548 mVtxSize = vtxSize;
549 mFlags = flags;
550
551 if (vtxSize < 2 || vtxSize > 3) {
552 throw new IllegalArgumentException("Vertex size out of range.");
553 }
554 }
555
556 private void makeSpace(int count) {
557 if ((mVtxCount + count) >= mVtxData.length) {
558 float t[] = new float[mVtxData.length * 2];
559 System.arraycopy(mVtxData, 0, t, 0, mVtxData.length);
560 mVtxData = t;
561 }
562 }
563
564 private void latch() {
565 if ((mFlags & COLOR) != 0) {
566 makeSpace(4);
567 mVtxData[mVtxCount++] = mR;
568 mVtxData[mVtxCount++] = mG;
569 mVtxData[mVtxCount++] = mB;
570 mVtxData[mVtxCount++] = mA;
571 }
572 if ((mFlags & TEXTURE_0) != 0) {
573 makeSpace(2);
574 mVtxData[mVtxCount++] = mS0;
575 mVtxData[mVtxCount++] = mT0;
576 }
577 if ((mFlags & NORMAL) != 0) {
578 makeSpace(3);
579 mVtxData[mVtxCount++] = mNX;
580 mVtxData[mVtxCount++] = mNY;
581 mVtxData[mVtxCount++] = mNZ;
582 }
583 }
584
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800585 /**
586 * Adds a float2 vertex to the mesh
587 *
588 * @param x position x
589 * @param y position y
590 *
591 * @return this
592 *
593 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800594 public TriangleMeshBuilder addVertex(float x, float y) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700595 if (mVtxSize != 2) {
596 throw new IllegalStateException("add mistmatch with declared components.");
597 }
598 makeSpace(2);
599 mVtxData[mVtxCount++] = x;
600 mVtxData[mVtxCount++] = y;
601 latch();
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800602 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700603 }
604
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800605 /**
606 * Adds a float3 vertex to the mesh
607 *
608 * @param x position x
609 * @param y position y
610 * @param z position z
611 *
612 * @return this
613 *
614 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800615 public TriangleMeshBuilder addVertex(float x, float y, float z) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700616 if (mVtxSize != 3) {
617 throw new IllegalStateException("add mistmatch with declared components.");
618 }
619 makeSpace(3);
620 mVtxData[mVtxCount++] = x;
621 mVtxData[mVtxCount++] = y;
622 mVtxData[mVtxCount++] = z;
623 latch();
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800624 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700625 }
626
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800627 /**
628 * Sets the texture coordinate for the last added vertex
629 *
630 * @param s texture coordinate s
631 * @param t texture coordinate t
632 *
633 * @return this
634 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800635 public TriangleMeshBuilder setTexture(float s, float t) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700636 if ((mFlags & TEXTURE_0) == 0) {
637 throw new IllegalStateException("add mistmatch with declared components.");
638 }
639 mS0 = s;
640 mT0 = t;
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800641 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700642 }
643
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800644 /**
645 * Sets the normal vector for the last added vertex
646 *
647 * @param x normal vector x
648 * @param y normal vector y
649 * @param z normal vector z
650 *
651 * @return this
652 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800653 public TriangleMeshBuilder setNormal(float x, float y, float z) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700654 if ((mFlags & NORMAL) == 0) {
655 throw new IllegalStateException("add mistmatch with declared components.");
656 }
657 mNX = x;
658 mNY = y;
659 mNZ = z;
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800660 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700661 }
662
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800663 /**
664 * Sets the color for the last added vertex
665 *
666 * @param r red component
667 * @param g green component
668 * @param b blue component
669 * @param a alpha component
670 *
671 * @return this
672 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800673 public TriangleMeshBuilder setColor(float r, float g, float b, float a) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700674 if ((mFlags & COLOR) == 0) {
675 throw new IllegalStateException("add mistmatch with declared components.");
676 }
677 mR = r;
678 mG = g;
679 mB = b;
680 mA = a;
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800681 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700682 }
683
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800684 /**
685 * Adds a new triangle to the mesh builder
686 *
687 * @param idx1 index of the first vertex in the triangle
688 * @param idx2 index of the second vertex in the triangle
689 * @param idx3 index of the third vertex in the triangle
690 *
691 * @return this
692 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800693 public TriangleMeshBuilder addTriangle(int idx1, int idx2, int idx3) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700694 if((idx1 >= mVtxCount) || (idx1 < 0) ||
695 (idx2 >= mVtxCount) || (idx2 < 0) ||
696 (idx3 >= mVtxCount) || (idx3 < 0)) {
697 throw new IllegalStateException("Index provided greater than vertex count.");
698 }
699 if ((mIndexCount + 3) >= mIndexData.length) {
700 short t[] = new short[mIndexData.length * 2];
701 System.arraycopy(mIndexData, 0, t, 0, mIndexData.length);
702 mIndexData = t;
703 }
704 mIndexData[mIndexCount++] = (short)idx1;
705 mIndexData[mIndexCount++] = (short)idx2;
706 mIndexData[mIndexCount++] = (short)idx3;
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800707 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700708 }
709
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800710 /**
711 * Creates the mesh object from the current state of the builder
712 *
713 * @param uploadToBufferObject specifies whether the vertex data
714 * is to be uploaded into the buffer
715 * object indicating that it's likely
716 * not going to be modified and
717 * rendered many times.
718 * Alternatively, it indicates the
719 * mesh data will be updated
720 * frequently and remain in script
721 * accessible memory
722 *
723 **/
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700724 public Mesh create(boolean uploadToBufferObject) {
725 Element.Builder b = new Element.Builder(mRS);
726 int floatCount = mVtxSize;
727 b.add(Element.createVector(mRS,
728 Element.DataType.FLOAT_32,
729 mVtxSize), "position");
730 if ((mFlags & COLOR) != 0) {
731 floatCount += 4;
732 b.add(Element.F32_4(mRS), "color");
733 }
734 if ((mFlags & TEXTURE_0) != 0) {
735 floatCount += 2;
736 b.add(Element.F32_2(mRS), "texture0");
737 }
738 if ((mFlags & NORMAL) != 0) {
739 floatCount += 3;
740 b.add(Element.F32_3(mRS), "normal");
741 }
742 mElement = b.create();
743
Jason Samsd1952402010-12-20 12:55:28 -0800744 int usage = Allocation.USAGE_SCRIPT;
745 if (uploadToBufferObject) {
746 usage |= Allocation.USAGE_GRAPHICS_VERTEX;
747 }
748
749 Builder smb = new Builder(mRS, usage);
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700750 smb.addVertexType(mElement, mVtxCount / floatCount);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800751 smb.addIndexSetType(Element.U16(mRS), mIndexCount, Primitive.TRIANGLE);
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700752
753 Mesh sm = smb.create();
754
Jason Samsb97b2512011-01-16 15:04:08 -0800755 sm.getVertexAllocation(0).copy1DRangeFromUnchecked(0, mVtxCount / floatCount, mVtxData);
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700756 if(uploadToBufferObject) {
Jason Samsd1952402010-12-20 12:55:28 -0800757 if (uploadToBufferObject) {
758 sm.getVertexAllocation(0).syncAll(Allocation.USAGE_SCRIPT);
759 }
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700760 }
761
Jason Samsb97b2512011-01-16 15:04:08 -0800762 sm.getIndexSetAllocation(0).copy1DRangeFromUnchecked(0, mIndexCount, mIndexData);
Jason Samsd1952402010-12-20 12:55:28 -0800763 if (uploadToBufferObject) {
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800764 sm.getIndexSetAllocation(0).syncAll(Allocation.USAGE_SCRIPT);
Jason Samsd1952402010-12-20 12:55:28 -0800765 }
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700766
767 return sm;
768 }
769 }
770}
771