blob: 13c8e1c91052ca81d65e88d576699b7a17fd27e3 [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
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070021/**
Tim Murraya9084222013-04-05 22:06:43 +000022 * @hide
Jason Samsd4ca9912012-05-08 19:02:07 -070023 * @deprecated in API 16
Robert Ly11518ac2011-02-09 13:57:06 -080024 * <p>This class is a container for geometric data displayed with
Tim Murrayc11e25c2013-04-09 11:01:01 -070025 * 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,
Tim Murrayc11e25c2013-04-09 11:01:01 -070033 * RenderScript automatically connects the two together.
Robert Ly11518ac2011-02-09 13:57:06 -080034 * </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
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070042 /**
Jason Samsd4ca9912012-05-08 19:02:07 -070043 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -080044 * Describes the way mesh vertex data is interpreted when rendering
45 *
46 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080047 public enum Primitive {
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070048 /**
Jason Samsd4ca9912012-05-08 19:02:07 -070049 * @deprecated in API 16
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080050 * Vertex data will be rendered as a series of points
51 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080052 POINT (0),
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070053 /**
Jason Samsd4ca9912012-05-08 19:02:07 -070054 * @deprecated in API 16
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080055 * Vertex pairs will be rendered as lines
56 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080057 LINE (1),
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070058 /**
Jason Samsd4ca9912012-05-08 19:02:07 -070059 * @deprecated in API 16
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080060 * Vertex data will be rendered as a connected line strip
61 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080062 LINE_STRIP (2),
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070063 /**
Jason Samsd4ca9912012-05-08 19:02:07 -070064 * @deprecated in API 16
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080065 * Vertices will be rendered as individual triangles
66 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080067 TRIANGLE (3),
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070068 /**
Jason Samsd4ca9912012-05-08 19:02:07 -070069 * @deprecated in API 16
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080070 * Vertices will be rendered as a connected triangle strip
71 * defined by the first three vertices with each additional
72 * triangle defined by a new vertex
73 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080074 TRIANGLE_STRIP (4),
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070075 /**
Jason Samsd4ca9912012-05-08 19:02:07 -070076 * @deprecated in API 16
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080077 * Vertices will be rendered as a sequence of triangles that all
78 * share first vertex as the origin
79 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080080 TRIANGLE_FAN (5);
81
82 int mID;
83 Primitive(int id) {
84 mID = id;
85 }
86 }
87
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -070088 Allocation[] mVertexBuffers;
89 Allocation[] mIndexBuffers;
90 Primitive[] mPrimitives;
91
Tim Murray460a0492013-11-19 12:45:54 -080092 Mesh(long id, RenderScript rs) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -070093 super(id, rs);
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -070094 }
95
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070096 /**
Jason Samsd4ca9912012-05-08 19:02:07 -070097 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -080098 * @return number of allocations containing vertex data
99 *
100 **/
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700101 public int getVertexAllocationCount() {
102 if(mVertexBuffers == null) {
103 return 0;
104 }
105 return mVertexBuffers.length;
106 }
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700107 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700108 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800109 * @param slot index in the list of allocations to return
110 * @return vertex data allocation at the given index
111 *
112 **/
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700113 public Allocation getVertexAllocation(int slot) {
114 return mVertexBuffers[slot];
115 }
116
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700117 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700118 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800119 * @return number of primitives or index sets in the mesh
120 *
121 **/
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700122 public int getPrimitiveCount() {
123 if(mIndexBuffers == null) {
124 return 0;
125 }
126 return mIndexBuffers.length;
127 }
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800128
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700129 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700130 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800131 * @param slot locaton within the list of index set allocation
132 * @return allocation containing primtive index data or null if
133 * the index data is not specified explicitly
134 *
135 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800136 public Allocation getIndexSetAllocation(int slot) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700137 return mIndexBuffers[slot];
138 }
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700139 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700140 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800141 * @param slot locaiton within the list of index set primitives
142 * @return index set primitive type
143 *
144 **/
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700145 public Primitive getPrimitive(int slot) {
146 return mPrimitives[slot];
147 }
148
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700149 @Override
150 void updateFromNative() {
Jason Sams06d69de2010-11-09 17:11:40 -0800151 super.updateFromNative();
Jason Samse07694b2012-04-03 15:36:36 -0700152 int vtxCount = mRS.nMeshGetVertexBufferCount(getID(mRS));
153 int idxCount = mRS.nMeshGetIndexCount(getID(mRS));
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700154
Ashok Bhat98071552014-02-12 09:54:43 +0000155 long[] vtxIDs = new long[vtxCount];
156 long[] idxIDs = new long[idxCount];
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700157 int[] primitives = new int[idxCount];
158
Jason Samse07694b2012-04-03 15:36:36 -0700159 mRS.nMeshGetVertices(getID(mRS), vtxIDs, vtxCount);
160 mRS.nMeshGetIndices(getID(mRS), idxIDs, primitives, idxCount);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700161
162 mVertexBuffers = new Allocation[vtxCount];
163 mIndexBuffers = new Allocation[idxCount];
164 mPrimitives = new Primitive[idxCount];
165
166 for(int i = 0; i < vtxCount; i ++) {
167 if(vtxIDs[i] != 0) {
Jason Samsd4b23b52010-12-13 15:32:35 -0800168 mVertexBuffers[i] = new Allocation(vtxIDs[i], mRS, null, Allocation.USAGE_SCRIPT);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700169 mVertexBuffers[i].updateFromNative();
170 }
171 }
172
173 for(int i = 0; i < idxCount; i ++) {
174 if(idxIDs[i] != 0) {
Jason Samsd4b23b52010-12-13 15:32:35 -0800175 mIndexBuffers[i] = new Allocation(idxIDs[i], mRS, null, Allocation.USAGE_SCRIPT);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700176 mIndexBuffers[i].updateFromNative();
177 }
178 mPrimitives[i] = Primitive.values()[primitives[i]];
179 }
180 }
181
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700182 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700183 * @deprecated in API 16
Robert Ly11518ac2011-02-09 13:57:06 -0800184 * Mesh builder object. It starts empty and requires you to
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800185 * add the types necessary to create vertex and index
Robert Ly11518ac2011-02-09 13:57:06 -0800186 * allocations.
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800187 *
188 */
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700189 public static class Builder {
190 RenderScript mRS;
Jason Samsd1952402010-12-20 12:55:28 -0800191 int mUsage;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700192
193 class Entry {
194 Type t;
195 Element e;
196 int size;
197 Primitive prim;
Jason Samsd1952402010-12-20 12:55:28 -0800198 int usage;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700199 }
200
201 int mVertexTypeCount;
202 Entry[] mVertexTypes;
203 Vector mIndexTypes;
204
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700205 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700206 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800207 * Creates builder object
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800208 * @param rs Context to which the mesh will belong.
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800209 * @param usage specifies how the mesh allocations are to be
210 * handled, whether they need to be uploaded to a
211 * buffer on the gpu, maintain a cpu copy, etc
212 */
Jason Samsd1952402010-12-20 12:55:28 -0800213 public Builder(RenderScript rs, int usage) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700214 mRS = rs;
Jason Samsd1952402010-12-20 12:55:28 -0800215 mUsage = usage;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700216 mVertexTypeCount = 0;
217 mVertexTypes = new Entry[16];
218 mIndexTypes = new Vector();
219 }
220
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700221 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700222 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800223 * @return internal index of the last vertex buffer type added to
224 * builder
225 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800226 public int getCurrentVertexTypeIndex() {
227 return mVertexTypeCount - 1;
228 }
229
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700230 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700231 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800232 * @return internal index of the last index set added to the
233 * builder
234 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800235 public int getCurrentIndexSetIndex() {
236 return mIndexTypes.size() - 1;
237 }
238
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700239 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700240 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800241 * Adds a vertex data type to the builder object
242 *
Stephen Hinesb11e3d22011-01-11 19:30:58 -0800243 * @param t type of the vertex data allocation to be created
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800244 *
245 * @return this
246 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800247 public Builder addVertexType(Type t) throws IllegalStateException {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700248 if (mVertexTypeCount >= mVertexTypes.length) {
249 throw new IllegalStateException("Max vertex types exceeded.");
250 }
251
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700252 mVertexTypes[mVertexTypeCount] = new Entry();
253 mVertexTypes[mVertexTypeCount].t = t;
254 mVertexTypes[mVertexTypeCount].e = null;
255 mVertexTypeCount++;
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800256 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700257 }
258
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700259 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700260 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800261 * Adds a vertex data type to the builder object
262 *
263 * @param e element describing the vertex data layout
264 * @param size number of elements in the buffer
265 *
266 * @return this
267 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800268 public Builder addVertexType(Element e, int size) throws IllegalStateException {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700269 if (mVertexTypeCount >= mVertexTypes.length) {
270 throw new IllegalStateException("Max vertex types exceeded.");
271 }
272
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700273 mVertexTypes[mVertexTypeCount] = new Entry();
274 mVertexTypes[mVertexTypeCount].t = null;
275 mVertexTypes[mVertexTypeCount].e = e;
276 mVertexTypes[mVertexTypeCount].size = size;
277 mVertexTypeCount++;
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800278 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700279 }
280
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700281 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700282 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800283 * Adds an index set data type to the builder object
284 *
285 * @param t type of the index set data, could be null
286 * @param p primitive type
287 *
288 * @return this
289 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800290 public Builder addIndexSetType(Type t, Primitive p) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700291 Entry indexType = new Entry();
292 indexType.t = t;
293 indexType.e = null;
294 indexType.size = 0;
295 indexType.prim = p;
296 mIndexTypes.addElement(indexType);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800297 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700298 }
299
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700300 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700301 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800302 * Adds an index set primitive type to the builder object
303 *
304 * @param p primitive type
305 *
306 * @return this
307 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800308 public Builder addIndexSetType(Primitive p) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700309 Entry indexType = new Entry();
310 indexType.t = null;
311 indexType.e = null;
312 indexType.size = 0;
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
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700318 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700319 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800320 * Adds an index set data type to the builder object
321 *
322 * @param e element describing the index set data layout
323 * @param size number of elements in the buffer
324 * @param p primitive type
325 *
326 * @return this
327 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800328 public Builder addIndexSetType(Element e, int size, Primitive p) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700329 Entry indexType = new Entry();
330 indexType.t = null;
331 indexType.e = e;
332 indexType.size = size;
333 indexType.prim = p;
334 mIndexTypes.addElement(indexType);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800335 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700336 }
337
338 Type newType(Element e, int size) {
339 Type.Builder tb = new Type.Builder(mRS, e);
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800340 tb.setX(size);
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700341 return tb.create();
342 }
343
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700344 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700345 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800346 * Create a Mesh object from the current state of the builder
347 *
348 **/
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700349 public Mesh create() {
350 mRS.validate();
Ashok Bhat98071552014-02-12 09:54:43 +0000351 long[] vtx = new long[mVertexTypeCount];
352 long[] idx = new long[mIndexTypes.size()];
Alex Sakhartchouk25999a02011-05-12 10:38:03 -0700353 int[] prim = new int[mIndexTypes.size()];
354
355 Allocation[] vertexBuffers = new Allocation[mVertexTypeCount];
356 Allocation[] indexBuffers = new Allocation[mIndexTypes.size()];
357 Primitive[] primitives = new Primitive[mIndexTypes.size()];
358
359 for(int ct = 0; ct < mVertexTypeCount; ct ++) {
360 Allocation alloc = null;
361 Entry entry = mVertexTypes[ct];
362 if (entry.t != null) {
363 alloc = Allocation.createTyped(mRS, entry.t, mUsage);
364 } else if(entry.e != null) {
365 alloc = Allocation.createSized(mRS, entry.e, entry.size, mUsage);
Jason Samsae5be382015-03-26 14:47:17 -0700366 } else {
367 // Should never happen because the builder will always set one
368 throw new IllegalStateException("Builder corrupt, no valid element in entry.");
Alex Sakhartchouk25999a02011-05-12 10:38:03 -0700369 }
370 vertexBuffers[ct] = alloc;
Ashok Bhat98071552014-02-12 09:54:43 +0000371 vtx[ct] = alloc.getID(mRS);
Alex Sakhartchouk25999a02011-05-12 10:38:03 -0700372 }
373
374 for(int ct = 0; ct < mIndexTypes.size(); ct ++) {
375 Allocation alloc = null;
376 Entry entry = (Entry)mIndexTypes.elementAt(ct);
377 if (entry.t != null) {
378 alloc = Allocation.createTyped(mRS, entry.t, mUsage);
379 } else if(entry.e != null) {
380 alloc = Allocation.createSized(mRS, entry.e, entry.size, mUsage);
Jason Samsae5be382015-03-26 14:47:17 -0700381 } else {
382 // Should never happen because the builder will always set one
383 throw new IllegalStateException("Builder corrupt, no valid element in entry.");
Alex Sakhartchouk25999a02011-05-12 10:38:03 -0700384 }
Tim Murray460a0492013-11-19 12:45:54 -0800385 long allocID = (alloc == null) ? 0 : alloc.getID(mRS);
Alex Sakhartchouk25999a02011-05-12 10:38:03 -0700386 indexBuffers[ct] = alloc;
387 primitives[ct] = entry.prim;
388
Ashok Bhat98071552014-02-12 09:54:43 +0000389 idx[ct] = allocID;
Alex Sakhartchouk25999a02011-05-12 10:38:03 -0700390 prim[ct] = entry.prim.mID;
391 }
392
Tim Murray460a0492013-11-19 12:45:54 -0800393 long id = mRS.nMeshCreate(vtx, idx, prim);
Alex Sakhartchouk25999a02011-05-12 10:38:03 -0700394 Mesh newMesh = new Mesh(id, mRS);
395 newMesh.mVertexBuffers = vertexBuffers;
396 newMesh.mIndexBuffers = indexBuffers;
397 newMesh.mPrimitives = primitives;
398
399 return newMesh;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700400 }
401 }
402
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700403 /**
Jason Samse619de62012-05-08 18:40:58 -0700404 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800405 * Mesh builder object. It starts empty and requires the user to
406 * add all the vertex and index allocations that comprise the
407 * mesh
408 *
409 */
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700410 public static class AllocationBuilder {
411 RenderScript mRS;
412
413 class Entry {
414 Allocation a;
415 Primitive prim;
416 }
417
418 int mVertexTypeCount;
419 Entry[] mVertexTypes;
420
421 Vector mIndexTypes;
422
Jason Samse619de62012-05-08 18:40:58 -0700423 /**
424 * @deprecated in API 16
425 **/
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700426 public AllocationBuilder(RenderScript rs) {
427 mRS = rs;
428 mVertexTypeCount = 0;
429 mVertexTypes = new Entry[16];
430 mIndexTypes = new Vector();
431 }
432
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700433 /**
Jason Samse619de62012-05-08 18:40:58 -0700434 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800435 * @return internal index of the last vertex buffer type added to
436 * builder
437 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800438 public int getCurrentVertexTypeIndex() {
439 return mVertexTypeCount - 1;
440 }
441
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700442 /**
Jason Samse619de62012-05-08 18:40:58 -0700443 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800444 * @return internal index of the last index set added to the
445 * builder
446 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800447 public int getCurrentIndexSetIndex() {
448 return mIndexTypes.size() - 1;
449 }
450
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700451 /**
Jason Samse619de62012-05-08 18:40:58 -0700452 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800453 * Adds an allocation containing vertex buffer data to the
454 * builder
455 *
456 * @param a vertex data allocation
457 *
458 * @return this
459 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800460 public AllocationBuilder addVertexAllocation(Allocation a) throws IllegalStateException {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700461 if (mVertexTypeCount >= mVertexTypes.length) {
462 throw new IllegalStateException("Max vertex types exceeded.");
463 }
464
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700465 mVertexTypes[mVertexTypeCount] = new Entry();
466 mVertexTypes[mVertexTypeCount].a = a;
467 mVertexTypeCount++;
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800468 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700469 }
470
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700471 /**
Jason Samse619de62012-05-08 18:40:58 -0700472 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800473 * Adds an allocation containing index buffer data and index type
474 * to the builder
475 *
476 * @param a index set data allocation, could be null
477 * @param p index set primitive type
478 *
479 * @return this
480 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800481 public AllocationBuilder addIndexSetAllocation(Allocation a, Primitive p) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700482 Entry indexType = new Entry();
483 indexType.a = a;
484 indexType.prim = p;
485 mIndexTypes.addElement(indexType);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800486 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700487 }
488
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700489 /**
Jason Samse619de62012-05-08 18:40:58 -0700490 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800491 * Adds an index set type to the builder
492 *
493 * @param p index set primitive type
494 *
495 * @return this
496 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800497 public AllocationBuilder addIndexSetType(Primitive p) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700498 Entry indexType = new Entry();
499 indexType.a = null;
500 indexType.prim = p;
501 mIndexTypes.addElement(indexType);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800502 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700503 }
504
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700505 /**
Jason Samse619de62012-05-08 18:40:58 -0700506 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800507 * Create a Mesh object from the current state of the builder
508 *
509 **/
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700510 public Mesh create() {
511 mRS.validate();
Alex Sakhartchouk25999a02011-05-12 10:38:03 -0700512
Ashok Bhat98071552014-02-12 09:54:43 +0000513 long[] vtx = new long[mVertexTypeCount];
514 long[] idx = new long[mIndexTypes.size()];
Alex Sakhartchouk25999a02011-05-12 10:38:03 -0700515 int[] prim = new int[mIndexTypes.size()];
516
517 Allocation[] indexBuffers = new Allocation[mIndexTypes.size()];
518 Primitive[] primitives = new Primitive[mIndexTypes.size()];
519 Allocation[] vertexBuffers = new Allocation[mVertexTypeCount];
520
521 for(int ct = 0; ct < mVertexTypeCount; ct ++) {
522 Entry entry = mVertexTypes[ct];
523 vertexBuffers[ct] = entry.a;
Ashok Bhat98071552014-02-12 09:54:43 +0000524 vtx[ct] = entry.a.getID(mRS);
Alex Sakhartchouk25999a02011-05-12 10:38:03 -0700525 }
526
527 for(int ct = 0; ct < mIndexTypes.size(); ct ++) {
528 Entry entry = (Entry)mIndexTypes.elementAt(ct);
Tim Murray460a0492013-11-19 12:45:54 -0800529 long allocID = (entry.a == null) ? 0 : entry.a.getID(mRS);
Alex Sakhartchouk25999a02011-05-12 10:38:03 -0700530 indexBuffers[ct] = entry.a;
531 primitives[ct] = entry.prim;
532
Ashok Bhat98071552014-02-12 09:54:43 +0000533 idx[ct] = allocID;
Alex Sakhartchouk25999a02011-05-12 10:38:03 -0700534 prim[ct] = entry.prim.mID;
535 }
536
Tim Murray460a0492013-11-19 12:45:54 -0800537 long id = mRS.nMeshCreate(vtx, idx, prim);
Alex Sakhartchouk25999a02011-05-12 10:38:03 -0700538 Mesh newMesh = new Mesh(id, mRS);
539 newMesh.mVertexBuffers = vertexBuffers;
540 newMesh.mIndexBuffers = indexBuffers;
541 newMesh.mPrimitives = primitives;
542
543 return newMesh;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700544 }
545 }
546
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700547 /**
Jason Samse619de62012-05-08 18:40:58 -0700548 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800549 * Builder that allows creation of a mesh object point by point
550 * and triangle by triangle
551 *
552 **/
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700553 public static class TriangleMeshBuilder {
554 float mVtxData[];
555 int mVtxCount;
Alex Sakhartchouke60149d2011-11-15 15:15:21 -0800556 int mMaxIndex;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700557 short mIndexData[];
558 int mIndexCount;
559 RenderScript mRS;
560 Element mElement;
561
562 float mNX = 0;
563 float mNY = 0;
564 float mNZ = -1;
565 float mS0 = 0;
566 float mT0 = 0;
567 float mR = 1;
568 float mG = 1;
569 float mB = 1;
570 float mA = 1;
571
572 int mVtxSize;
573 int mFlags;
574
Jason Samse619de62012-05-08 18:40:58 -0700575 /**
576 * @deprecated in API 16
577 **/
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700578 public static final int COLOR = 0x0001;
Jason Samse619de62012-05-08 18:40:58 -0700579 /**
580 * @deprecated in API 16
581 **/
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700582 public static final int NORMAL = 0x0002;
Jason Samse619de62012-05-08 18:40:58 -0700583 /**
584 * @deprecated in API 16
585 **/
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700586 public static final int TEXTURE_0 = 0x0100;
587
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700588 /**
Jason Samse619de62012-05-08 18:40:58 -0700589 * @deprecated in API 16
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800590 * @param rs Context to which the mesh will belong.
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800591 * @param vtxSize specifies whether the vertex is a float2 or
592 * float3
593 * @param flags bitfield that is a combination of COLOR, NORMAL,
594 * and TEXTURE_0 that specifies what vertex data
595 * channels are present in the mesh
596 *
597 **/
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700598 public TriangleMeshBuilder(RenderScript rs, int vtxSize, int flags) {
599 mRS = rs;
600 mVtxCount = 0;
Alex Sakhartchouke60149d2011-11-15 15:15:21 -0800601 mMaxIndex = 0;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700602 mIndexCount = 0;
603 mVtxData = new float[128];
604 mIndexData = new short[128];
605 mVtxSize = vtxSize;
606 mFlags = flags;
607
608 if (vtxSize < 2 || vtxSize > 3) {
609 throw new IllegalArgumentException("Vertex size out of range.");
610 }
611 }
612
613 private void makeSpace(int count) {
614 if ((mVtxCount + count) >= mVtxData.length) {
615 float t[] = new float[mVtxData.length * 2];
616 System.arraycopy(mVtxData, 0, t, 0, mVtxData.length);
617 mVtxData = t;
618 }
619 }
620
621 private void latch() {
622 if ((mFlags & COLOR) != 0) {
623 makeSpace(4);
624 mVtxData[mVtxCount++] = mR;
625 mVtxData[mVtxCount++] = mG;
626 mVtxData[mVtxCount++] = mB;
627 mVtxData[mVtxCount++] = mA;
628 }
629 if ((mFlags & TEXTURE_0) != 0) {
630 makeSpace(2);
631 mVtxData[mVtxCount++] = mS0;
632 mVtxData[mVtxCount++] = mT0;
633 }
634 if ((mFlags & NORMAL) != 0) {
Alex Sakhartchouke60149d2011-11-15 15:15:21 -0800635 makeSpace(4);
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700636 mVtxData[mVtxCount++] = mNX;
637 mVtxData[mVtxCount++] = mNY;
638 mVtxData[mVtxCount++] = mNZ;
Alex Sakhartchouke60149d2011-11-15 15:15:21 -0800639 mVtxData[mVtxCount++] = 0.0f;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700640 }
Alex Sakhartchouke60149d2011-11-15 15:15:21 -0800641 mMaxIndex ++;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700642 }
643
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700644 /**
Jason Samse619de62012-05-08 18:40:58 -0700645 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800646 * Adds a float2 vertex to the mesh
647 *
648 * @param x position x
649 * @param y position y
650 *
651 * @return this
652 *
653 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800654 public TriangleMeshBuilder addVertex(float x, float y) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700655 if (mVtxSize != 2) {
656 throw new IllegalStateException("add mistmatch with declared components.");
657 }
658 makeSpace(2);
659 mVtxData[mVtxCount++] = x;
660 mVtxData[mVtxCount++] = y;
661 latch();
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800662 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700663 }
664
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700665 /**
Jason Samse619de62012-05-08 18:40:58 -0700666 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800667 * Adds a float3 vertex to the mesh
668 *
669 * @param x position x
670 * @param y position y
671 * @param z position z
672 *
673 * @return this
674 *
675 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800676 public TriangleMeshBuilder addVertex(float x, float y, float z) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700677 if (mVtxSize != 3) {
678 throw new IllegalStateException("add mistmatch with declared components.");
679 }
Alex Sakhartchouke60149d2011-11-15 15:15:21 -0800680 makeSpace(4);
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700681 mVtxData[mVtxCount++] = x;
682 mVtxData[mVtxCount++] = y;
683 mVtxData[mVtxCount++] = z;
Alex Sakhartchouke60149d2011-11-15 15:15:21 -0800684 mVtxData[mVtxCount++] = 1.0f;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700685 latch();
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800686 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700687 }
688
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700689 /**
Jason Samse619de62012-05-08 18:40:58 -0700690 * @deprecated in API 16
Robert Lyf11ffc112012-02-22 10:59:12 -0800691 * Sets the texture coordinate for the vertices that are added after this method call.
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800692 *
693 * @param s texture coordinate s
694 * @param t texture coordinate t
695 *
696 * @return this
697 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800698 public TriangleMeshBuilder setTexture(float s, float t) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700699 if ((mFlags & TEXTURE_0) == 0) {
700 throw new IllegalStateException("add mistmatch with declared components.");
701 }
702 mS0 = s;
703 mT0 = t;
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800704 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700705 }
706
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700707 /**
Jason Samse619de62012-05-08 18:40:58 -0700708 * @deprecated in API 16
Robert Lyf11ffc112012-02-22 10:59:12 -0800709 * Sets the normal vector for the vertices that are added after this method call.
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800710 *
711 * @param x normal vector x
712 * @param y normal vector y
713 * @param z normal vector z
714 *
715 * @return this
716 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800717 public TriangleMeshBuilder setNormal(float x, float y, float z) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700718 if ((mFlags & NORMAL) == 0) {
719 throw new IllegalStateException("add mistmatch with declared components.");
720 }
721 mNX = x;
722 mNY = y;
723 mNZ = z;
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800724 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700725 }
726
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700727 /**
Jason Samse619de62012-05-08 18:40:58 -0700728 * @deprecated in API 16
Robert Lyf11ffc112012-02-22 10:59:12 -0800729 * Sets the color for the vertices that are added after this method call.
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800730 *
731 * @param r red component
732 * @param g green component
733 * @param b blue component
734 * @param a alpha component
735 *
736 * @return this
737 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800738 public TriangleMeshBuilder setColor(float r, float g, float b, float a) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700739 if ((mFlags & COLOR) == 0) {
740 throw new IllegalStateException("add mistmatch with declared components.");
741 }
742 mR = r;
743 mG = g;
744 mB = b;
745 mA = a;
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800746 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700747 }
748
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700749 /**
Jason Samse619de62012-05-08 18:40:58 -0700750 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800751 * Adds a new triangle to the mesh builder
752 *
753 * @param idx1 index of the first vertex in the triangle
754 * @param idx2 index of the second vertex in the triangle
755 * @param idx3 index of the third vertex in the triangle
756 *
757 * @return this
758 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800759 public TriangleMeshBuilder addTriangle(int idx1, int idx2, int idx3) {
Alex Sakhartchouke60149d2011-11-15 15:15:21 -0800760 if((idx1 >= mMaxIndex) || (idx1 < 0) ||
761 (idx2 >= mMaxIndex) || (idx2 < 0) ||
762 (idx3 >= mMaxIndex) || (idx3 < 0)) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700763 throw new IllegalStateException("Index provided greater than vertex count.");
764 }
765 if ((mIndexCount + 3) >= mIndexData.length) {
766 short t[] = new short[mIndexData.length * 2];
767 System.arraycopy(mIndexData, 0, t, 0, mIndexData.length);
768 mIndexData = t;
769 }
770 mIndexData[mIndexCount++] = (short)idx1;
771 mIndexData[mIndexCount++] = (short)idx2;
772 mIndexData[mIndexCount++] = (short)idx3;
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800773 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700774 }
775
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700776 /**
Jason Samse619de62012-05-08 18:40:58 -0700777 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800778 * Creates the mesh object from the current state of the builder
779 *
780 * @param uploadToBufferObject specifies whether the vertex data
781 * is to be uploaded into the buffer
782 * object indicating that it's likely
783 * not going to be modified and
784 * rendered many times.
785 * Alternatively, it indicates the
786 * mesh data will be updated
787 * frequently and remain in script
788 * accessible memory
789 *
790 **/
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700791 public Mesh create(boolean uploadToBufferObject) {
792 Element.Builder b = new Element.Builder(mRS);
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700793 b.add(Element.createVector(mRS,
794 Element.DataType.FLOAT_32,
795 mVtxSize), "position");
796 if ((mFlags & COLOR) != 0) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700797 b.add(Element.F32_4(mRS), "color");
798 }
799 if ((mFlags & TEXTURE_0) != 0) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700800 b.add(Element.F32_2(mRS), "texture0");
801 }
802 if ((mFlags & NORMAL) != 0) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700803 b.add(Element.F32_3(mRS), "normal");
804 }
805 mElement = b.create();
806
Jason Samsd1952402010-12-20 12:55:28 -0800807 int usage = Allocation.USAGE_SCRIPT;
808 if (uploadToBufferObject) {
809 usage |= Allocation.USAGE_GRAPHICS_VERTEX;
810 }
811
812 Builder smb = new Builder(mRS, usage);
Alex Sakhartchouke60149d2011-11-15 15:15:21 -0800813 smb.addVertexType(mElement, mMaxIndex);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800814 smb.addIndexSetType(Element.U16(mRS), mIndexCount, Primitive.TRIANGLE);
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700815
816 Mesh sm = smb.create();
817
Alex Sakhartchouke60149d2011-11-15 15:15:21 -0800818 sm.getVertexAllocation(0).copy1DRangeFromUnchecked(0, mMaxIndex, mVtxData);
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700819 if(uploadToBufferObject) {
Andreas Gampe16720c12015-03-17 19:10:14 -0700820 sm.getVertexAllocation(0).syncAll(Allocation.USAGE_SCRIPT);
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700821 }
822
Jason Samsb97b2512011-01-16 15:04:08 -0800823 sm.getIndexSetAllocation(0).copy1DRangeFromUnchecked(0, mIndexCount, mIndexData);
Jason Samsd1952402010-12-20 12:55:28 -0800824 if (uploadToBufferObject) {
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800825 sm.getIndexSetAllocation(0).syncAll(Allocation.USAGE_SCRIPT);
Jason Samsd1952402010-12-20 12:55:28 -0800826 }
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700827
828 return sm;
829 }
830 }
831}
832