blob: 72105131735a6f6703cc727616b447dad3c7de20 [file] [log] [blame]
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -07001/*
Jason Samse619de62012-05-08 18:40:58 -07002 * Copyright (C) 2008-2012 The Android Open Source Project
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -07003 *
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
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070023/**
Jason Samsd4ca9912012-05-08 19:02:07 -070024 * @deprecated in API 16
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
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070043 /**
Jason Samsd4ca9912012-05-08 19:02:07 -070044 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -080045 * Describes the way mesh vertex data is interpreted when rendering
46 *
47 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080048 public enum Primitive {
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070049 /**
Jason Samsd4ca9912012-05-08 19:02:07 -070050 * @deprecated in API 16
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080051 * Vertex data will be rendered as a series of points
52 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080053 POINT (0),
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070054 /**
Jason Samsd4ca9912012-05-08 19:02:07 -070055 * @deprecated in API 16
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080056 * Vertex pairs will be rendered as lines
57 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080058 LINE (1),
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070059 /**
Jason Samsd4ca9912012-05-08 19:02:07 -070060 * @deprecated in API 16
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080061 * Vertex data will be rendered as a connected line strip
62 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080063 LINE_STRIP (2),
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070064 /**
Jason Samsd4ca9912012-05-08 19:02:07 -070065 * @deprecated in API 16
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080066 * Vertices will be rendered as individual triangles
67 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080068 TRIANGLE (3),
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070069 /**
Jason Samsd4ca9912012-05-08 19:02:07 -070070 * @deprecated in API 16
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080071 * Vertices will be rendered as a connected triangle strip
72 * defined by the first three vertices with each additional
73 * triangle defined by a new vertex
74 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080075 TRIANGLE_STRIP (4),
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070076 /**
Jason Samsd4ca9912012-05-08 19:02:07 -070077 * @deprecated in API 16
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080078 * Vertices will be rendered as a sequence of triangles that all
79 * share first vertex as the origin
80 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080081 TRIANGLE_FAN (5);
82
83 int mID;
84 Primitive(int id) {
85 mID = id;
86 }
87 }
88
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -070089 Allocation[] mVertexBuffers;
90 Allocation[] mIndexBuffers;
91 Primitive[] mPrimitives;
92
93 Mesh(int id, RenderScript rs) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -070094 super(id, rs);
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -070095 }
96
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070097 /**
Jason Samsd4ca9912012-05-08 19:02:07 -070098 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -080099 * @return number of allocations containing vertex data
100 *
101 **/
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700102 public int getVertexAllocationCount() {
103 if(mVertexBuffers == null) {
104 return 0;
105 }
106 return mVertexBuffers.length;
107 }
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700108 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700109 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800110 * @param slot index in the list of allocations to return
111 * @return vertex data allocation at the given index
112 *
113 **/
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700114 public Allocation getVertexAllocation(int slot) {
115 return mVertexBuffers[slot];
116 }
117
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700118 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700119 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800120 * @return number of primitives or index sets in the mesh
121 *
122 **/
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700123 public int getPrimitiveCount() {
124 if(mIndexBuffers == null) {
125 return 0;
126 }
127 return mIndexBuffers.length;
128 }
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800129
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700130 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700131 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800132 * @param slot locaton within the list of index set allocation
133 * @return allocation containing primtive index data or null if
134 * the index data is not specified explicitly
135 *
136 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800137 public Allocation getIndexSetAllocation(int slot) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700138 return mIndexBuffers[slot];
139 }
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700140 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700141 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800142 * @param slot locaiton within the list of index set primitives
143 * @return index set primitive type
144 *
145 **/
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700146 public Primitive getPrimitive(int slot) {
147 return mPrimitives[slot];
148 }
149
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700150 @Override
151 void updateFromNative() {
Jason Sams06d69de2010-11-09 17:11:40 -0800152 super.updateFromNative();
Jason Samse07694b2012-04-03 15:36:36 -0700153 int vtxCount = mRS.nMeshGetVertexBufferCount(getID(mRS));
154 int idxCount = mRS.nMeshGetIndexCount(getID(mRS));
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700155
156 int[] vtxIDs = new int[vtxCount];
157 int[] idxIDs = new int[idxCount];
158 int[] primitives = new int[idxCount];
159
Jason Samse07694b2012-04-03 15:36:36 -0700160 mRS.nMeshGetVertices(getID(mRS), vtxIDs, vtxCount);
161 mRS.nMeshGetIndices(getID(mRS), idxIDs, primitives, idxCount);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700162
163 mVertexBuffers = new Allocation[vtxCount];
164 mIndexBuffers = new Allocation[idxCount];
165 mPrimitives = new Primitive[idxCount];
166
167 for(int i = 0; i < vtxCount; i ++) {
168 if(vtxIDs[i] != 0) {
Jason Samsd4b23b52010-12-13 15:32:35 -0800169 mVertexBuffers[i] = new Allocation(vtxIDs[i], mRS, null, Allocation.USAGE_SCRIPT);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700170 mVertexBuffers[i].updateFromNative();
171 }
172 }
173
174 for(int i = 0; i < idxCount; i ++) {
175 if(idxIDs[i] != 0) {
Jason Samsd4b23b52010-12-13 15:32:35 -0800176 mIndexBuffers[i] = new Allocation(idxIDs[i], mRS, null, Allocation.USAGE_SCRIPT);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700177 mIndexBuffers[i].updateFromNative();
178 }
179 mPrimitives[i] = Primitive.values()[primitives[i]];
180 }
181 }
182
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700183 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700184 * @deprecated in API 16
Robert Ly11518ac2011-02-09 13:57:06 -0800185 * Mesh builder object. It starts empty and requires you to
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800186 * add the types necessary to create vertex and index
Robert Ly11518ac2011-02-09 13:57:06 -0800187 * allocations.
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800188 *
189 */
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700190 public static class Builder {
191 RenderScript mRS;
Jason Samsd1952402010-12-20 12:55:28 -0800192 int mUsage;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700193
194 class Entry {
195 Type t;
196 Element e;
197 int size;
198 Primitive prim;
Jason Samsd1952402010-12-20 12:55:28 -0800199 int usage;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700200 }
201
202 int mVertexTypeCount;
203 Entry[] mVertexTypes;
204 Vector mIndexTypes;
205
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700206 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700207 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800208 * Creates builder object
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800209 * @param rs Context to which the mesh will belong.
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800210 * @param usage specifies how the mesh allocations are to be
211 * handled, whether they need to be uploaded to a
212 * buffer on the gpu, maintain a cpu copy, etc
213 */
Jason Samsd1952402010-12-20 12:55:28 -0800214 public Builder(RenderScript rs, int usage) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700215 mRS = rs;
Jason Samsd1952402010-12-20 12:55:28 -0800216 mUsage = usage;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700217 mVertexTypeCount = 0;
218 mVertexTypes = new Entry[16];
219 mIndexTypes = new Vector();
220 }
221
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700222 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700223 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800224 * @return internal index of the last vertex buffer type added to
225 * builder
226 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800227 public int getCurrentVertexTypeIndex() {
228 return mVertexTypeCount - 1;
229 }
230
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700231 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700232 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800233 * @return internal index of the last index set added to the
234 * builder
235 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800236 public int getCurrentIndexSetIndex() {
237 return mIndexTypes.size() - 1;
238 }
239
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700240 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700241 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800242 * Adds a vertex data type to the builder object
243 *
Stephen Hinesb11e3d22011-01-11 19:30:58 -0800244 * @param t type of the vertex data allocation to be created
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800245 *
246 * @return this
247 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800248 public Builder addVertexType(Type t) throws IllegalStateException {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700249 if (mVertexTypeCount >= mVertexTypes.length) {
250 throw new IllegalStateException("Max vertex types exceeded.");
251 }
252
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700253 mVertexTypes[mVertexTypeCount] = new Entry();
254 mVertexTypes[mVertexTypeCount].t = t;
255 mVertexTypes[mVertexTypeCount].e = null;
256 mVertexTypeCount++;
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800257 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700258 }
259
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700260 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700261 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800262 * Adds a vertex data type to the builder object
263 *
264 * @param e element describing the vertex data layout
265 * @param size number of elements in the buffer
266 *
267 * @return this
268 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800269 public Builder addVertexType(Element e, int size) throws IllegalStateException {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700270 if (mVertexTypeCount >= mVertexTypes.length) {
271 throw new IllegalStateException("Max vertex types exceeded.");
272 }
273
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700274 mVertexTypes[mVertexTypeCount] = new Entry();
275 mVertexTypes[mVertexTypeCount].t = null;
276 mVertexTypes[mVertexTypeCount].e = e;
277 mVertexTypes[mVertexTypeCount].size = size;
278 mVertexTypeCount++;
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800279 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700280 }
281
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700282 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700283 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800284 * Adds an index set data type to the builder object
285 *
286 * @param t type of the index set data, could be null
287 * @param p primitive type
288 *
289 * @return this
290 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800291 public Builder addIndexSetType(Type t, Primitive p) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700292 Entry indexType = new Entry();
293 indexType.t = t;
294 indexType.e = null;
295 indexType.size = 0;
296 indexType.prim = p;
297 mIndexTypes.addElement(indexType);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800298 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700299 }
300
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700301 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700302 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800303 * Adds an index set primitive type to the builder object
304 *
305 * @param p primitive type
306 *
307 * @return this
308 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800309 public Builder addIndexSetType(Primitive p) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700310 Entry indexType = new Entry();
311 indexType.t = null;
312 indexType.e = null;
313 indexType.size = 0;
314 indexType.prim = p;
315 mIndexTypes.addElement(indexType);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800316 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700317 }
318
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700319 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700320 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800321 * Adds an index set data type to the builder object
322 *
323 * @param e element describing the index set data layout
324 * @param size number of elements in the buffer
325 * @param p primitive type
326 *
327 * @return this
328 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800329 public Builder addIndexSetType(Element e, int size, Primitive p) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700330 Entry indexType = new Entry();
331 indexType.t = null;
332 indexType.e = e;
333 indexType.size = size;
334 indexType.prim = p;
335 mIndexTypes.addElement(indexType);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800336 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700337 }
338
339 Type newType(Element e, int size) {
340 Type.Builder tb = new Type.Builder(mRS, e);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800341 tb.setX(size);
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700342 return tb.create();
343 }
344
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700345 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700346 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800347 * Create a Mesh object from the current state of the builder
348 *
349 **/
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700350 public Mesh create() {
351 mRS.validate();
Alex Sakhartchouk25999a02011-05-12 10:38:03 -0700352 int[] vtx = new int[mVertexTypeCount];
353 int[] idx = new int[mIndexTypes.size()];
354 int[] prim = new int[mIndexTypes.size()];
355
356 Allocation[] vertexBuffers = new Allocation[mVertexTypeCount];
357 Allocation[] indexBuffers = new Allocation[mIndexTypes.size()];
358 Primitive[] primitives = new Primitive[mIndexTypes.size()];
359
360 for(int ct = 0; ct < mVertexTypeCount; ct ++) {
361 Allocation alloc = null;
362 Entry entry = mVertexTypes[ct];
363 if (entry.t != null) {
364 alloc = Allocation.createTyped(mRS, entry.t, mUsage);
365 } else if(entry.e != null) {
366 alloc = Allocation.createSized(mRS, entry.e, entry.size, mUsage);
367 }
368 vertexBuffers[ct] = alloc;
Jason Samse07694b2012-04-03 15:36:36 -0700369 vtx[ct] = alloc.getID(mRS);
Alex Sakhartchouk25999a02011-05-12 10:38:03 -0700370 }
371
372 for(int ct = 0; ct < mIndexTypes.size(); ct ++) {
373 Allocation alloc = null;
374 Entry entry = (Entry)mIndexTypes.elementAt(ct);
375 if (entry.t != null) {
376 alloc = Allocation.createTyped(mRS, entry.t, mUsage);
377 } else if(entry.e != null) {
378 alloc = Allocation.createSized(mRS, entry.e, entry.size, mUsage);
379 }
Jason Samse07694b2012-04-03 15:36:36 -0700380 int allocID = (alloc == null) ? 0 : alloc.getID(mRS);
Alex Sakhartchouk25999a02011-05-12 10:38:03 -0700381 indexBuffers[ct] = alloc;
382 primitives[ct] = entry.prim;
383
384 idx[ct] = allocID;
385 prim[ct] = entry.prim.mID;
386 }
387
388 int id = mRS.nMeshCreate(vtx, idx, prim);
389 Mesh newMesh = new Mesh(id, mRS);
390 newMesh.mVertexBuffers = vertexBuffers;
391 newMesh.mIndexBuffers = indexBuffers;
392 newMesh.mPrimitives = primitives;
393
394 return newMesh;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700395 }
396 }
397
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700398 /**
Jason Samse619de62012-05-08 18:40:58 -0700399 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800400 * Mesh builder object. It starts empty and requires the user to
401 * add all the vertex and index allocations that comprise the
402 * mesh
403 *
404 */
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700405 public static class AllocationBuilder {
406 RenderScript mRS;
407
408 class Entry {
409 Allocation a;
410 Primitive prim;
411 }
412
413 int mVertexTypeCount;
414 Entry[] mVertexTypes;
415
416 Vector mIndexTypes;
417
Jason Samse619de62012-05-08 18:40:58 -0700418 /**
419 * @deprecated in API 16
420 **/
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700421 public AllocationBuilder(RenderScript rs) {
422 mRS = rs;
423 mVertexTypeCount = 0;
424 mVertexTypes = new Entry[16];
425 mIndexTypes = new Vector();
426 }
427
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700428 /**
Jason Samse619de62012-05-08 18:40:58 -0700429 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800430 * @return internal index of the last vertex buffer type added to
431 * builder
432 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800433 public int getCurrentVertexTypeIndex() {
434 return mVertexTypeCount - 1;
435 }
436
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700437 /**
Jason Samse619de62012-05-08 18:40:58 -0700438 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800439 * @return internal index of the last index set added to the
440 * builder
441 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800442 public int getCurrentIndexSetIndex() {
443 return mIndexTypes.size() - 1;
444 }
445
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700446 /**
Jason Samse619de62012-05-08 18:40:58 -0700447 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800448 * Adds an allocation containing vertex buffer data to the
449 * builder
450 *
451 * @param a vertex data allocation
452 *
453 * @return this
454 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800455 public AllocationBuilder addVertexAllocation(Allocation a) throws IllegalStateException {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700456 if (mVertexTypeCount >= mVertexTypes.length) {
457 throw new IllegalStateException("Max vertex types exceeded.");
458 }
459
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700460 mVertexTypes[mVertexTypeCount] = new Entry();
461 mVertexTypes[mVertexTypeCount].a = a;
462 mVertexTypeCount++;
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800463 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700464 }
465
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700466 /**
Jason Samse619de62012-05-08 18:40:58 -0700467 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800468 * Adds an allocation containing index buffer data and index type
469 * to the builder
470 *
471 * @param a index set data allocation, could be null
472 * @param p index set primitive type
473 *
474 * @return this
475 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800476 public AllocationBuilder addIndexSetAllocation(Allocation a, Primitive p) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700477 Entry indexType = new Entry();
478 indexType.a = a;
479 indexType.prim = p;
480 mIndexTypes.addElement(indexType);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800481 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700482 }
483
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700484 /**
Jason Samse619de62012-05-08 18:40:58 -0700485 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800486 * Adds an index set type to the builder
487 *
488 * @param p index set primitive type
489 *
490 * @return this
491 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800492 public AllocationBuilder addIndexSetType(Primitive p) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700493 Entry indexType = new Entry();
494 indexType.a = null;
495 indexType.prim = p;
496 mIndexTypes.addElement(indexType);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800497 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700498 }
499
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700500 /**
Jason Samse619de62012-05-08 18:40:58 -0700501 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800502 * Create a Mesh object from the current state of the builder
503 *
504 **/
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700505 public Mesh create() {
506 mRS.validate();
Alex Sakhartchouk25999a02011-05-12 10:38:03 -0700507
508 int[] vtx = new int[mVertexTypeCount];
509 int[] idx = new int[mIndexTypes.size()];
510 int[] prim = new int[mIndexTypes.size()];
511
512 Allocation[] indexBuffers = new Allocation[mIndexTypes.size()];
513 Primitive[] primitives = new Primitive[mIndexTypes.size()];
514 Allocation[] vertexBuffers = new Allocation[mVertexTypeCount];
515
516 for(int ct = 0; ct < mVertexTypeCount; ct ++) {
517 Entry entry = mVertexTypes[ct];
518 vertexBuffers[ct] = entry.a;
Jason Samse07694b2012-04-03 15:36:36 -0700519 vtx[ct] = entry.a.getID(mRS);
Alex Sakhartchouk25999a02011-05-12 10:38:03 -0700520 }
521
522 for(int ct = 0; ct < mIndexTypes.size(); ct ++) {
523 Entry entry = (Entry)mIndexTypes.elementAt(ct);
Jason Samse07694b2012-04-03 15:36:36 -0700524 int allocID = (entry.a == null) ? 0 : entry.a.getID(mRS);
Alex Sakhartchouk25999a02011-05-12 10:38:03 -0700525 indexBuffers[ct] = entry.a;
526 primitives[ct] = entry.prim;
527
528 idx[ct] = allocID;
529 prim[ct] = entry.prim.mID;
530 }
531
532 int id = mRS.nMeshCreate(vtx, idx, prim);
533 Mesh newMesh = new Mesh(id, mRS);
534 newMesh.mVertexBuffers = vertexBuffers;
535 newMesh.mIndexBuffers = indexBuffers;
536 newMesh.mPrimitives = primitives;
537
538 return newMesh;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700539 }
540 }
541
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700542 /**
Jason Samse619de62012-05-08 18:40:58 -0700543 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800544 * Builder that allows creation of a mesh object point by point
545 * and triangle by triangle
546 *
547 **/
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700548 public static class TriangleMeshBuilder {
549 float mVtxData[];
550 int mVtxCount;
Alex Sakhartchouke60149d2011-11-15 15:15:21 -0800551 int mMaxIndex;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700552 short mIndexData[];
553 int mIndexCount;
554 RenderScript mRS;
555 Element mElement;
556
557 float mNX = 0;
558 float mNY = 0;
559 float mNZ = -1;
560 float mS0 = 0;
561 float mT0 = 0;
562 float mR = 1;
563 float mG = 1;
564 float mB = 1;
565 float mA = 1;
566
567 int mVtxSize;
568 int mFlags;
569
Jason Samse619de62012-05-08 18:40:58 -0700570 /**
571 * @deprecated in API 16
572 **/
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700573 public static final int COLOR = 0x0001;
Jason Samse619de62012-05-08 18:40:58 -0700574 /**
575 * @deprecated in API 16
576 **/
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700577 public static final int NORMAL = 0x0002;
Jason Samse619de62012-05-08 18:40:58 -0700578 /**
579 * @deprecated in API 16
580 **/
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700581 public static final int TEXTURE_0 = 0x0100;
582
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700583 /**
Jason Samse619de62012-05-08 18:40:58 -0700584 * @deprecated in API 16
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800585 * @param rs Context to which the mesh will belong.
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800586 * @param vtxSize specifies whether the vertex is a float2 or
587 * float3
588 * @param flags bitfield that is a combination of COLOR, NORMAL,
589 * and TEXTURE_0 that specifies what vertex data
590 * channels are present in the mesh
591 *
592 **/
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700593 public TriangleMeshBuilder(RenderScript rs, int vtxSize, int flags) {
594 mRS = rs;
595 mVtxCount = 0;
Alex Sakhartchouke60149d2011-11-15 15:15:21 -0800596 mMaxIndex = 0;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700597 mIndexCount = 0;
598 mVtxData = new float[128];
599 mIndexData = new short[128];
600 mVtxSize = vtxSize;
601 mFlags = flags;
602
603 if (vtxSize < 2 || vtxSize > 3) {
604 throw new IllegalArgumentException("Vertex size out of range.");
605 }
606 }
607
608 private void makeSpace(int count) {
609 if ((mVtxCount + count) >= mVtxData.length) {
610 float t[] = new float[mVtxData.length * 2];
611 System.arraycopy(mVtxData, 0, t, 0, mVtxData.length);
612 mVtxData = t;
613 }
614 }
615
616 private void latch() {
617 if ((mFlags & COLOR) != 0) {
618 makeSpace(4);
619 mVtxData[mVtxCount++] = mR;
620 mVtxData[mVtxCount++] = mG;
621 mVtxData[mVtxCount++] = mB;
622 mVtxData[mVtxCount++] = mA;
623 }
624 if ((mFlags & TEXTURE_0) != 0) {
625 makeSpace(2);
626 mVtxData[mVtxCount++] = mS0;
627 mVtxData[mVtxCount++] = mT0;
628 }
629 if ((mFlags & NORMAL) != 0) {
Alex Sakhartchouke60149d2011-11-15 15:15:21 -0800630 makeSpace(4);
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700631 mVtxData[mVtxCount++] = mNX;
632 mVtxData[mVtxCount++] = mNY;
633 mVtxData[mVtxCount++] = mNZ;
Alex Sakhartchouke60149d2011-11-15 15:15:21 -0800634 mVtxData[mVtxCount++] = 0.0f;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700635 }
Alex Sakhartchouke60149d2011-11-15 15:15:21 -0800636 mMaxIndex ++;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700637 }
638
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700639 /**
Jason Samse619de62012-05-08 18:40:58 -0700640 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800641 * Adds a float2 vertex to the mesh
642 *
643 * @param x position x
644 * @param y position y
645 *
646 * @return this
647 *
648 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800649 public TriangleMeshBuilder addVertex(float x, float y) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700650 if (mVtxSize != 2) {
651 throw new IllegalStateException("add mistmatch with declared components.");
652 }
653 makeSpace(2);
654 mVtxData[mVtxCount++] = x;
655 mVtxData[mVtxCount++] = y;
656 latch();
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800657 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700658 }
659
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700660 /**
Jason Samse619de62012-05-08 18:40:58 -0700661 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800662 * Adds a float3 vertex to the mesh
663 *
664 * @param x position x
665 * @param y position y
666 * @param z position z
667 *
668 * @return this
669 *
670 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800671 public TriangleMeshBuilder addVertex(float x, float y, float z) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700672 if (mVtxSize != 3) {
673 throw new IllegalStateException("add mistmatch with declared components.");
674 }
Alex Sakhartchouke60149d2011-11-15 15:15:21 -0800675 makeSpace(4);
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700676 mVtxData[mVtxCount++] = x;
677 mVtxData[mVtxCount++] = y;
678 mVtxData[mVtxCount++] = z;
Alex Sakhartchouke60149d2011-11-15 15:15:21 -0800679 mVtxData[mVtxCount++] = 1.0f;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700680 latch();
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800681 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700682 }
683
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700684 /**
Jason Samse619de62012-05-08 18:40:58 -0700685 * @deprecated in API 16
Robert Lyf11ffc112012-02-22 10:59:12 -0800686 * Sets the texture coordinate for the vertices that are added after this method call.
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800687 *
688 * @param s texture coordinate s
689 * @param t texture coordinate t
690 *
691 * @return this
692 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800693 public TriangleMeshBuilder setTexture(float s, float t) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700694 if ((mFlags & TEXTURE_0) == 0) {
695 throw new IllegalStateException("add mistmatch with declared components.");
696 }
697 mS0 = s;
698 mT0 = t;
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800699 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700700 }
701
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700702 /**
Jason Samse619de62012-05-08 18:40:58 -0700703 * @deprecated in API 16
Robert Lyf11ffc112012-02-22 10:59:12 -0800704 * Sets the normal vector for the vertices that are added after this method call.
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800705 *
706 * @param x normal vector x
707 * @param y normal vector y
708 * @param z normal vector z
709 *
710 * @return this
711 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800712 public TriangleMeshBuilder setNormal(float x, float y, float z) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700713 if ((mFlags & NORMAL) == 0) {
714 throw new IllegalStateException("add mistmatch with declared components.");
715 }
716 mNX = x;
717 mNY = y;
718 mNZ = z;
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800719 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700720 }
721
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700722 /**
Jason Samse619de62012-05-08 18:40:58 -0700723 * @deprecated in API 16
Robert Lyf11ffc112012-02-22 10:59:12 -0800724 * Sets the color for the vertices that are added after this method call.
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800725 *
726 * @param r red component
727 * @param g green component
728 * @param b blue component
729 * @param a alpha component
730 *
731 * @return this
732 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800733 public TriangleMeshBuilder setColor(float r, float g, float b, float a) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700734 if ((mFlags & COLOR) == 0) {
735 throw new IllegalStateException("add mistmatch with declared components.");
736 }
737 mR = r;
738 mG = g;
739 mB = b;
740 mA = a;
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800741 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700742 }
743
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700744 /**
Jason Samse619de62012-05-08 18:40:58 -0700745 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800746 * Adds a new triangle to the mesh builder
747 *
748 * @param idx1 index of the first vertex in the triangle
749 * @param idx2 index of the second vertex in the triangle
750 * @param idx3 index of the third vertex in the triangle
751 *
752 * @return this
753 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800754 public TriangleMeshBuilder addTriangle(int idx1, int idx2, int idx3) {
Alex Sakhartchouke60149d2011-11-15 15:15:21 -0800755 if((idx1 >= mMaxIndex) || (idx1 < 0) ||
756 (idx2 >= mMaxIndex) || (idx2 < 0) ||
757 (idx3 >= mMaxIndex) || (idx3 < 0)) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700758 throw new IllegalStateException("Index provided greater than vertex count.");
759 }
760 if ((mIndexCount + 3) >= mIndexData.length) {
761 short t[] = new short[mIndexData.length * 2];
762 System.arraycopy(mIndexData, 0, t, 0, mIndexData.length);
763 mIndexData = t;
764 }
765 mIndexData[mIndexCount++] = (short)idx1;
766 mIndexData[mIndexCount++] = (short)idx2;
767 mIndexData[mIndexCount++] = (short)idx3;
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800768 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700769 }
770
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700771 /**
Jason Samse619de62012-05-08 18:40:58 -0700772 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800773 * Creates the mesh object from the current state of the builder
774 *
775 * @param uploadToBufferObject specifies whether the vertex data
776 * is to be uploaded into the buffer
777 * object indicating that it's likely
778 * not going to be modified and
779 * rendered many times.
780 * Alternatively, it indicates the
781 * mesh data will be updated
782 * frequently and remain in script
783 * accessible memory
784 *
785 **/
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700786 public Mesh create(boolean uploadToBufferObject) {
787 Element.Builder b = new Element.Builder(mRS);
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700788 b.add(Element.createVector(mRS,
789 Element.DataType.FLOAT_32,
790 mVtxSize), "position");
791 if ((mFlags & COLOR) != 0) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700792 b.add(Element.F32_4(mRS), "color");
793 }
794 if ((mFlags & TEXTURE_0) != 0) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700795 b.add(Element.F32_2(mRS), "texture0");
796 }
797 if ((mFlags & NORMAL) != 0) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700798 b.add(Element.F32_3(mRS), "normal");
799 }
800 mElement = b.create();
801
Jason Samsd1952402010-12-20 12:55:28 -0800802 int usage = Allocation.USAGE_SCRIPT;
803 if (uploadToBufferObject) {
804 usage |= Allocation.USAGE_GRAPHICS_VERTEX;
805 }
806
807 Builder smb = new Builder(mRS, usage);
Alex Sakhartchouke60149d2011-11-15 15:15:21 -0800808 smb.addVertexType(mElement, mMaxIndex);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800809 smb.addIndexSetType(Element.U16(mRS), mIndexCount, Primitive.TRIANGLE);
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700810
811 Mesh sm = smb.create();
812
Alex Sakhartchouke60149d2011-11-15 15:15:21 -0800813 sm.getVertexAllocation(0).copy1DRangeFromUnchecked(0, mMaxIndex, mVtxData);
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700814 if(uploadToBufferObject) {
Jason Samsd1952402010-12-20 12:55:28 -0800815 if (uploadToBufferObject) {
816 sm.getVertexAllocation(0).syncAll(Allocation.USAGE_SCRIPT);
817 }
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700818 }
819
Jason Samsb97b2512011-01-16 15:04:08 -0800820 sm.getIndexSetAllocation(0).copy1DRangeFromUnchecked(0, mIndexCount, mIndexData);
Jason Samsd1952402010-12-20 12:55:28 -0800821 if (uploadToBufferObject) {
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800822 sm.getIndexSetAllocation(0).syncAll(Allocation.USAGE_SCRIPT);
Jason Samsd1952402010-12-20 12:55:28 -0800823 }
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700824
825 return sm;
826 }
827 }
828}
829