blob: bb910cc6bb118956817cfa72cf754a71bbfdbddd [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
21import android.util.Config;
22import android.util.Log;
23
24/**
Robert Ly11518ac2011-02-09 13:57:06 -080025 * <p>This class is a container for geometric data displayed with
26 * Renderscript. Internally, a mesh is a collection of allocations that
Alex Sakhartchoukdf272022011-01-09 11:34:03 -080027 * represent vertex data (positions, normals, texture
Robert Ly11518ac2011-02-09 13:57:06 -080028 * coordinates) and index data such as triangles and lines. </p>
29 * <p>
30 * Vertex data could either be interleaved within one
31 * allocation that is provided separately, as multiple allocation
32 * objects, or done as a combination of both. When a
Alex Sakhartchoukdf272022011-01-09 11:34:03 -080033 * vertex channel name matches an input in the vertex program,
Robert Ly11518ac2011-02-09 13:57:06 -080034 * Renderscript automatically connects the two together.
35 * </p>
36 * <p>
37 * Parts of the mesh can be rendered with either explicit
Alex Sakhartchoukdf272022011-01-09 11:34:03 -080038 * index sets or primitive types.
Robert Ly11518ac2011-02-09 13:57:06 -080039 * </p>
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -070040 **/
41public class Mesh extends BaseObj {
42
Alex Sakhartchoukdf272022011-01-09 11:34:03 -080043 /**
44 * Describes the way mesh vertex data is interpreted when rendering
45 *
46 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080047 public enum Primitive {
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080048 /**
49 * Vertex data will be rendered as a series of points
50 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080051 POINT (0),
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080052 /**
53 * Vertex pairs will be rendered as lines
54 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080055 LINE (1),
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080056 /**
57 * Vertex data will be rendered as a connected line strip
58 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080059 LINE_STRIP (2),
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080060 /**
61 * Vertices will be rendered as individual triangles
62 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080063 TRIANGLE (3),
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080064 /**
65 * Vertices will be rendered as a connected triangle strip
66 * defined by the first three vertices with each additional
67 * triangle defined by a new vertex
68 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080069 TRIANGLE_STRIP (4),
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080070 /**
71 * Vertices will be rendered as a sequence of triangles that all
72 * share first vertex as the origin
73 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080074 TRIANGLE_FAN (5);
75
76 int mID;
77 Primitive(int id) {
78 mID = id;
79 }
80 }
81
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -070082 Allocation[] mVertexBuffers;
83 Allocation[] mIndexBuffers;
84 Primitive[] mPrimitives;
85
86 Mesh(int id, RenderScript rs) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -070087 super(id, rs);
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -070088 }
89
Alex Sakhartchoukdf272022011-01-09 11:34:03 -080090 /**
91 * @return number of allocations containing vertex data
92 *
93 **/
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -070094 public int getVertexAllocationCount() {
95 if(mVertexBuffers == null) {
96 return 0;
97 }
98 return mVertexBuffers.length;
99 }
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800100 /**
101 * @param slot index in the list of allocations to return
102 * @return vertex data allocation at the given index
103 *
104 **/
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700105 public Allocation getVertexAllocation(int slot) {
106 return mVertexBuffers[slot];
107 }
108
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800109 /**
110 * @return number of primitives or index sets in the mesh
111 *
112 **/
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700113 public int getPrimitiveCount() {
114 if(mIndexBuffers == null) {
115 return 0;
116 }
117 return mIndexBuffers.length;
118 }
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800119
120 /**
121 * @param slot locaton within the list of index set allocation
122 * @return allocation containing primtive index data or null if
123 * the index data is not specified explicitly
124 *
125 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800126 public Allocation getIndexSetAllocation(int slot) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700127 return mIndexBuffers[slot];
128 }
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800129 /**
130 * @param slot locaiton within the list of index set primitives
131 * @return index set primitive type
132 *
133 **/
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700134 public Primitive getPrimitive(int slot) {
135 return mPrimitives[slot];
136 }
137
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700138 @Override
139 void updateFromNative() {
Jason Sams06d69de2010-11-09 17:11:40 -0800140 super.updateFromNative();
141 int vtxCount = mRS.nMeshGetVertexBufferCount(getID());
142 int idxCount = mRS.nMeshGetIndexCount(getID());
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700143
144 int[] vtxIDs = new int[vtxCount];
145 int[] idxIDs = new int[idxCount];
146 int[] primitives = new int[idxCount];
147
Jason Sams06d69de2010-11-09 17:11:40 -0800148 mRS.nMeshGetVertices(getID(), vtxIDs, vtxCount);
149 mRS.nMeshGetIndices(getID(), idxIDs, primitives, idxCount);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700150
151 mVertexBuffers = new Allocation[vtxCount];
152 mIndexBuffers = new Allocation[idxCount];
153 mPrimitives = new Primitive[idxCount];
154
155 for(int i = 0; i < vtxCount; i ++) {
156 if(vtxIDs[i] != 0) {
Jason Samsd4b23b52010-12-13 15:32:35 -0800157 mVertexBuffers[i] = new Allocation(vtxIDs[i], mRS, null, Allocation.USAGE_SCRIPT);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700158 mVertexBuffers[i].updateFromNative();
159 }
160 }
161
162 for(int i = 0; i < idxCount; i ++) {
163 if(idxIDs[i] != 0) {
Jason Samsd4b23b52010-12-13 15:32:35 -0800164 mIndexBuffers[i] = new Allocation(idxIDs[i], mRS, null, Allocation.USAGE_SCRIPT);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700165 mIndexBuffers[i].updateFromNative();
166 }
167 mPrimitives[i] = Primitive.values()[primitives[i]];
168 }
169 }
170
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800171 /**
Robert Ly11518ac2011-02-09 13:57:06 -0800172 * Mesh builder object. It starts empty and requires you to
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800173 * add the types necessary to create vertex and index
Robert Ly11518ac2011-02-09 13:57:06 -0800174 * allocations.
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800175 *
176 */
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700177 public static class Builder {
178 RenderScript mRS;
Jason Samsd1952402010-12-20 12:55:28 -0800179 int mUsage;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700180
181 class Entry {
182 Type t;
183 Element e;
184 int size;
185 Primitive prim;
Jason Samsd1952402010-12-20 12:55:28 -0800186 int usage;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700187 }
188
189 int mVertexTypeCount;
190 Entry[] mVertexTypes;
191 Vector mIndexTypes;
192
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800193 /**
194 * Creates builder object
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800195 * @param rs Context to which the mesh will belong.
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800196 * @param usage specifies how the mesh allocations are to be
197 * handled, whether they need to be uploaded to a
198 * buffer on the gpu, maintain a cpu copy, etc
199 */
Jason Samsd1952402010-12-20 12:55:28 -0800200 public Builder(RenderScript rs, int usage) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700201 mRS = rs;
Jason Samsd1952402010-12-20 12:55:28 -0800202 mUsage = usage;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700203 mVertexTypeCount = 0;
204 mVertexTypes = new Entry[16];
205 mIndexTypes = new Vector();
206 }
207
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800208 /**
209 * @return internal index of the last vertex buffer type added to
210 * builder
211 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800212 public int getCurrentVertexTypeIndex() {
213 return mVertexTypeCount - 1;
214 }
215
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800216 /**
217 * @return internal index of the last index set added to the
218 * builder
219 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800220 public int getCurrentIndexSetIndex() {
221 return mIndexTypes.size() - 1;
222 }
223
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800224 /**
225 * Adds a vertex data type to the builder object
226 *
Stephen Hinesb11e3d22011-01-11 19:30:58 -0800227 * @param t type of the vertex data allocation to be created
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800228 *
229 * @return this
230 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800231 public Builder addVertexType(Type t) throws IllegalStateException {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700232 if (mVertexTypeCount >= mVertexTypes.length) {
233 throw new IllegalStateException("Max vertex types exceeded.");
234 }
235
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700236 mVertexTypes[mVertexTypeCount] = new Entry();
237 mVertexTypes[mVertexTypeCount].t = t;
238 mVertexTypes[mVertexTypeCount].e = null;
239 mVertexTypeCount++;
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800240 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700241 }
242
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800243 /**
244 * Adds a vertex data type to the builder object
245 *
246 * @param e element describing the vertex data layout
247 * @param size number of elements in the buffer
248 *
249 * @return this
250 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800251 public Builder addVertexType(Element e, int size) throws IllegalStateException {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700252 if (mVertexTypeCount >= mVertexTypes.length) {
253 throw new IllegalStateException("Max vertex types exceeded.");
254 }
255
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700256 mVertexTypes[mVertexTypeCount] = new Entry();
257 mVertexTypes[mVertexTypeCount].t = null;
258 mVertexTypes[mVertexTypeCount].e = e;
259 mVertexTypes[mVertexTypeCount].size = size;
260 mVertexTypeCount++;
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800261 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700262 }
263
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800264 /**
265 * Adds an index set data type to the builder object
266 *
267 * @param t type of the index set data, could be null
268 * @param p primitive type
269 *
270 * @return this
271 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800272 public Builder addIndexSetType(Type t, Primitive p) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700273 Entry indexType = new Entry();
274 indexType.t = t;
275 indexType.e = null;
276 indexType.size = 0;
277 indexType.prim = p;
278 mIndexTypes.addElement(indexType);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800279 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700280 }
281
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800282 /**
283 * Adds an index set primitive type to the builder object
284 *
285 * @param p primitive type
286 *
287 * @return this
288 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800289 public Builder addIndexSetType(Primitive p) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700290 Entry indexType = new Entry();
291 indexType.t = null;
292 indexType.e = null;
293 indexType.size = 0;
294 indexType.prim = p;
295 mIndexTypes.addElement(indexType);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800296 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700297 }
298
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800299 /**
300 * Adds an index set data type to the builder object
301 *
302 * @param e element describing the index set data layout
303 * @param size number of elements in the buffer
304 * @param p primitive type
305 *
306 * @return this
307 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800308 public Builder addIndexSetType(Element e, int size, Primitive p) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700309 Entry indexType = new Entry();
310 indexType.t = null;
311 indexType.e = e;
312 indexType.size = size;
313 indexType.prim = p;
314 mIndexTypes.addElement(indexType);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800315 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700316 }
317
318 Type newType(Element e, int size) {
319 Type.Builder tb = new Type.Builder(mRS, e);
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800320 tb.setX(size);
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700321 return tb.create();
322 }
323
324 static synchronized Mesh internalCreate(RenderScript rs, Builder b) {
325
326 int id = rs.nMeshCreate(b.mVertexTypeCount, b.mIndexTypes.size());
327 Mesh newMesh = new Mesh(id, rs);
328 newMesh.mIndexBuffers = new Allocation[b.mIndexTypes.size()];
329 newMesh.mPrimitives = new Primitive[b.mIndexTypes.size()];
330 newMesh.mVertexBuffers = new Allocation[b.mVertexTypeCount];
331
332 for(int ct = 0; ct < b.mIndexTypes.size(); ct ++) {
333 Allocation alloc = null;
334 Entry entry = (Entry)b.mIndexTypes.elementAt(ct);
335 if (entry.t != null) {
Jason Samsd1952402010-12-20 12:55:28 -0800336 alloc = Allocation.createTyped(rs, entry.t, b.mUsage);
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700337 }
338 else if(entry.e != null) {
Jason Samsd1952402010-12-20 12:55:28 -0800339 alloc = Allocation.createSized(rs, entry.e, entry.size, b.mUsage);
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700340 }
341 int allocID = (alloc == null) ? 0 : alloc.getID();
342 rs.nMeshBindIndex(id, allocID, entry.prim.mID, ct);
343 newMesh.mIndexBuffers[ct] = alloc;
344 newMesh.mPrimitives[ct] = entry.prim;
345 }
346
347 for(int ct = 0; ct < b.mVertexTypeCount; ct ++) {
348 Allocation alloc = null;
349 Entry entry = b.mVertexTypes[ct];
350 if (entry.t != null) {
Jason Samsd1952402010-12-20 12:55:28 -0800351 alloc = Allocation.createTyped(rs, entry.t, b.mUsage);
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700352 } else if(entry.e != null) {
Jason Samsd1952402010-12-20 12:55:28 -0800353 alloc = Allocation.createSized(rs, entry.e, entry.size, b.mUsage);
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700354 }
355 rs.nMeshBindVertex(id, alloc.getID(), ct);
356 newMesh.mVertexBuffers[ct] = alloc;
357 }
Alex Sakhartchouk9d71e212010-11-08 15:10:52 -0800358 rs.nMeshInitVertexAttribs(id);
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700359
360 return newMesh;
361 }
362
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800363 /**
364 * Create a Mesh object from the current state of the builder
365 *
366 **/
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700367 public Mesh create() {
368 mRS.validate();
369 Mesh sm = internalCreate(mRS, this);
370 return sm;
371 }
372 }
373
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800374 /**
375 * Mesh builder object. It starts empty and requires the user to
376 * add all the vertex and index allocations that comprise the
377 * mesh
378 *
379 */
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700380 public static class AllocationBuilder {
381 RenderScript mRS;
382
383 class Entry {
384 Allocation a;
385 Primitive prim;
386 }
387
388 int mVertexTypeCount;
389 Entry[] mVertexTypes;
390
391 Vector mIndexTypes;
392
393 public AllocationBuilder(RenderScript rs) {
394 mRS = rs;
395 mVertexTypeCount = 0;
396 mVertexTypes = new Entry[16];
397 mIndexTypes = new Vector();
398 }
399
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800400 /**
401 * @return internal index of the last vertex buffer type added to
402 * builder
403 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800404 public int getCurrentVertexTypeIndex() {
405 return mVertexTypeCount - 1;
406 }
407
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800408 /**
409 * @return internal index of the last index set added to the
410 * builder
411 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800412 public int getCurrentIndexSetIndex() {
413 return mIndexTypes.size() - 1;
414 }
415
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800416 /**
417 * Adds an allocation containing vertex buffer data to the
418 * builder
419 *
420 * @param a vertex data allocation
421 *
422 * @return this
423 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800424 public AllocationBuilder addVertexAllocation(Allocation a) throws IllegalStateException {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700425 if (mVertexTypeCount >= mVertexTypes.length) {
426 throw new IllegalStateException("Max vertex types exceeded.");
427 }
428
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700429 mVertexTypes[mVertexTypeCount] = new Entry();
430 mVertexTypes[mVertexTypeCount].a = a;
431 mVertexTypeCount++;
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800432 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700433 }
434
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800435 /**
436 * Adds an allocation containing index buffer data and index type
437 * to the builder
438 *
439 * @param a index set data allocation, could be null
440 * @param p index set primitive type
441 *
442 * @return this
443 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800444 public AllocationBuilder addIndexSetAllocation(Allocation a, Primitive p) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700445 Entry indexType = new Entry();
446 indexType.a = a;
447 indexType.prim = p;
448 mIndexTypes.addElement(indexType);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800449 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700450 }
451
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800452 /**
453 * Adds an index set type to the builder
454 *
455 * @param p index set primitive type
456 *
457 * @return this
458 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800459 public AllocationBuilder addIndexSetType(Primitive p) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700460 Entry indexType = new Entry();
461 indexType.a = null;
462 indexType.prim = p;
463 mIndexTypes.addElement(indexType);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800464 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700465 }
466
467 static synchronized Mesh internalCreate(RenderScript rs, AllocationBuilder b) {
468
469 int id = rs.nMeshCreate(b.mVertexTypeCount, b.mIndexTypes.size());
470 Mesh newMesh = new Mesh(id, rs);
471 newMesh.mIndexBuffers = new Allocation[b.mIndexTypes.size()];
472 newMesh.mPrimitives = new Primitive[b.mIndexTypes.size()];
473 newMesh.mVertexBuffers = new Allocation[b.mVertexTypeCount];
474
475 for(int ct = 0; ct < b.mIndexTypes.size(); ct ++) {
476 Entry entry = (Entry)b.mIndexTypes.elementAt(ct);
477 int allocID = (entry.a == null) ? 0 : entry.a.getID();
478 rs.nMeshBindIndex(id, allocID, entry.prim.mID, ct);
479 newMesh.mIndexBuffers[ct] = entry.a;
480 newMesh.mPrimitives[ct] = entry.prim;
481 }
482
483 for(int ct = 0; ct < b.mVertexTypeCount; ct ++) {
484 Entry entry = b.mVertexTypes[ct];
Jason Sams06d69de2010-11-09 17:11:40 -0800485 rs.nMeshBindVertex(id, entry.a.getID(), ct);
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700486 newMesh.mVertexBuffers[ct] = entry.a;
487 }
Alex Sakhartchouk9d71e212010-11-08 15:10:52 -0800488 rs.nMeshInitVertexAttribs(id);
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700489
490 return newMesh;
491 }
492
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800493 /**
494 * Create a Mesh object from the current state of the builder
495 *
496 **/
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700497 public Mesh create() {
498 mRS.validate();
499 Mesh sm = internalCreate(mRS, this);
500 return sm;
501 }
502 }
503
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800504 /**
505 * Builder that allows creation of a mesh object point by point
506 * and triangle by triangle
507 *
508 **/
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700509 public static class TriangleMeshBuilder {
510 float mVtxData[];
511 int mVtxCount;
512 short mIndexData[];
513 int mIndexCount;
514 RenderScript mRS;
515 Element mElement;
516
517 float mNX = 0;
518 float mNY = 0;
519 float mNZ = -1;
520 float mS0 = 0;
521 float mT0 = 0;
522 float mR = 1;
523 float mG = 1;
524 float mB = 1;
525 float mA = 1;
526
527 int mVtxSize;
528 int mFlags;
529
530 public static final int COLOR = 0x0001;
531 public static final int NORMAL = 0x0002;
532 public static final int TEXTURE_0 = 0x0100;
533
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800534 /**
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800535 * @param rs Context to which the mesh will belong.
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800536 * @param vtxSize specifies whether the vertex is a float2 or
537 * float3
538 * @param flags bitfield that is a combination of COLOR, NORMAL,
539 * and TEXTURE_0 that specifies what vertex data
540 * channels are present in the mesh
541 *
542 **/
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700543 public TriangleMeshBuilder(RenderScript rs, int vtxSize, int flags) {
544 mRS = rs;
545 mVtxCount = 0;
546 mIndexCount = 0;
547 mVtxData = new float[128];
548 mIndexData = new short[128];
549 mVtxSize = vtxSize;
550 mFlags = flags;
551
552 if (vtxSize < 2 || vtxSize > 3) {
553 throw new IllegalArgumentException("Vertex size out of range.");
554 }
555 }
556
557 private void makeSpace(int count) {
558 if ((mVtxCount + count) >= mVtxData.length) {
559 float t[] = new float[mVtxData.length * 2];
560 System.arraycopy(mVtxData, 0, t, 0, mVtxData.length);
561 mVtxData = t;
562 }
563 }
564
565 private void latch() {
566 if ((mFlags & COLOR) != 0) {
567 makeSpace(4);
568 mVtxData[mVtxCount++] = mR;
569 mVtxData[mVtxCount++] = mG;
570 mVtxData[mVtxCount++] = mB;
571 mVtxData[mVtxCount++] = mA;
572 }
573 if ((mFlags & TEXTURE_0) != 0) {
574 makeSpace(2);
575 mVtxData[mVtxCount++] = mS0;
576 mVtxData[mVtxCount++] = mT0;
577 }
578 if ((mFlags & NORMAL) != 0) {
579 makeSpace(3);
580 mVtxData[mVtxCount++] = mNX;
581 mVtxData[mVtxCount++] = mNY;
582 mVtxData[mVtxCount++] = mNZ;
583 }
584 }
585
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800586 /**
587 * Adds a float2 vertex to the mesh
588 *
589 * @param x position x
590 * @param y position y
591 *
592 * @return this
593 *
594 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800595 public TriangleMeshBuilder addVertex(float x, float y) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700596 if (mVtxSize != 2) {
597 throw new IllegalStateException("add mistmatch with declared components.");
598 }
599 makeSpace(2);
600 mVtxData[mVtxCount++] = x;
601 mVtxData[mVtxCount++] = y;
602 latch();
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800603 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700604 }
605
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800606 /**
607 * Adds a float3 vertex to the mesh
608 *
609 * @param x position x
610 * @param y position y
611 * @param z position z
612 *
613 * @return this
614 *
615 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800616 public TriangleMeshBuilder addVertex(float x, float y, float z) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700617 if (mVtxSize != 3) {
618 throw new IllegalStateException("add mistmatch with declared components.");
619 }
620 makeSpace(3);
621 mVtxData[mVtxCount++] = x;
622 mVtxData[mVtxCount++] = y;
623 mVtxData[mVtxCount++] = z;
624 latch();
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800625 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700626 }
627
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800628 /**
629 * Sets the texture coordinate for the last added vertex
630 *
631 * @param s texture coordinate s
632 * @param t texture coordinate t
633 *
634 * @return this
635 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800636 public TriangleMeshBuilder setTexture(float s, float t) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700637 if ((mFlags & TEXTURE_0) == 0) {
638 throw new IllegalStateException("add mistmatch with declared components.");
639 }
640 mS0 = s;
641 mT0 = t;
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800642 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700643 }
644
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800645 /**
646 * Sets the normal vector for the last added vertex
647 *
648 * @param x normal vector x
649 * @param y normal vector y
650 * @param z normal vector z
651 *
652 * @return this
653 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800654 public TriangleMeshBuilder setNormal(float x, float y, float z) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700655 if ((mFlags & NORMAL) == 0) {
656 throw new IllegalStateException("add mistmatch with declared components.");
657 }
658 mNX = x;
659 mNY = y;
660 mNZ = z;
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800661 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700662 }
663
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800664 /**
665 * Sets the color for the last added vertex
666 *
667 * @param r red component
668 * @param g green component
669 * @param b blue component
670 * @param a alpha component
671 *
672 * @return this
673 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800674 public TriangleMeshBuilder setColor(float r, float g, float b, float a) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700675 if ((mFlags & COLOR) == 0) {
676 throw new IllegalStateException("add mistmatch with declared components.");
677 }
678 mR = r;
679 mG = g;
680 mB = b;
681 mA = a;
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800682 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700683 }
684
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800685 /**
686 * Adds a new triangle to the mesh builder
687 *
688 * @param idx1 index of the first vertex in the triangle
689 * @param idx2 index of the second vertex in the triangle
690 * @param idx3 index of the third vertex in the triangle
691 *
692 * @return this
693 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800694 public TriangleMeshBuilder addTriangle(int idx1, int idx2, int idx3) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700695 if((idx1 >= mVtxCount) || (idx1 < 0) ||
696 (idx2 >= mVtxCount) || (idx2 < 0) ||
697 (idx3 >= mVtxCount) || (idx3 < 0)) {
698 throw new IllegalStateException("Index provided greater than vertex count.");
699 }
700 if ((mIndexCount + 3) >= mIndexData.length) {
701 short t[] = new short[mIndexData.length * 2];
702 System.arraycopy(mIndexData, 0, t, 0, mIndexData.length);
703 mIndexData = t;
704 }
705 mIndexData[mIndexCount++] = (short)idx1;
706 mIndexData[mIndexCount++] = (short)idx2;
707 mIndexData[mIndexCount++] = (short)idx3;
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800708 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700709 }
710
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800711 /**
712 * Creates the mesh object from the current state of the builder
713 *
714 * @param uploadToBufferObject specifies whether the vertex data
715 * is to be uploaded into the buffer
716 * object indicating that it's likely
717 * not going to be modified and
718 * rendered many times.
719 * Alternatively, it indicates the
720 * mesh data will be updated
721 * frequently and remain in script
722 * accessible memory
723 *
724 **/
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700725 public Mesh create(boolean uploadToBufferObject) {
726 Element.Builder b = new Element.Builder(mRS);
727 int floatCount = mVtxSize;
728 b.add(Element.createVector(mRS,
729 Element.DataType.FLOAT_32,
730 mVtxSize), "position");
731 if ((mFlags & COLOR) != 0) {
732 floatCount += 4;
733 b.add(Element.F32_4(mRS), "color");
734 }
735 if ((mFlags & TEXTURE_0) != 0) {
736 floatCount += 2;
737 b.add(Element.F32_2(mRS), "texture0");
738 }
739 if ((mFlags & NORMAL) != 0) {
740 floatCount += 3;
741 b.add(Element.F32_3(mRS), "normal");
742 }
743 mElement = b.create();
744
Jason Samsd1952402010-12-20 12:55:28 -0800745 int usage = Allocation.USAGE_SCRIPT;
746 if (uploadToBufferObject) {
747 usage |= Allocation.USAGE_GRAPHICS_VERTEX;
748 }
749
750 Builder smb = new Builder(mRS, usage);
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700751 smb.addVertexType(mElement, mVtxCount / floatCount);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800752 smb.addIndexSetType(Element.U16(mRS), mIndexCount, Primitive.TRIANGLE);
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700753
754 Mesh sm = smb.create();
755
Jason Samsb97b2512011-01-16 15:04:08 -0800756 sm.getVertexAllocation(0).copy1DRangeFromUnchecked(0, mVtxCount / floatCount, mVtxData);
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700757 if(uploadToBufferObject) {
Jason Samsd1952402010-12-20 12:55:28 -0800758 if (uploadToBufferObject) {
759 sm.getVertexAllocation(0).syncAll(Allocation.USAGE_SCRIPT);
760 }
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700761 }
762
Jason Samsb97b2512011-01-16 15:04:08 -0800763 sm.getIndexSetAllocation(0).copy1DRangeFromUnchecked(0, mIndexCount, mIndexData);
Jason Samsd1952402010-12-20 12:55:28 -0800764 if (uploadToBufferObject) {
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800765 sm.getIndexSetAllocation(0).syncAll(Allocation.USAGE_SCRIPT);
Jason Samsd1952402010-12-20 12:55:28 -0800766 }
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700767
768 return sm;
769 }
770 }
771}
772