blob: 9ce3fb256f6facebb5aeeb6f3bd577bae106518b [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/**
Tim Murraya9084222013-04-05 22:06:43 +000024 * @hide
Jason Samsd4ca9912012-05-08 19:02:07 -070025 * @deprecated in API 16
Robert Ly11518ac2011-02-09 13:57:06 -080026 * <p>This class is a container for geometric data displayed with
Tim Murrayc11e25c2013-04-09 11:01:01 -070027 * RenderScript. Internally, a mesh is a collection of allocations that
Alex Sakhartchoukdf272022011-01-09 11:34:03 -080028 * represent vertex data (positions, normals, texture
Robert Ly11518ac2011-02-09 13:57:06 -080029 * coordinates) and index data such as triangles and lines. </p>
30 * <p>
31 * Vertex data could either be interleaved within one
32 * allocation that is provided separately, as multiple allocation
33 * objects, or done as a combination of both. When a
Alex Sakhartchoukdf272022011-01-09 11:34:03 -080034 * vertex channel name matches an input in the vertex program,
Tim Murrayc11e25c2013-04-09 11:01:01 -070035 * RenderScript automatically connects the two together.
Robert Ly11518ac2011-02-09 13:57:06 -080036 * </p>
37 * <p>
38 * Parts of the mesh can be rendered with either explicit
Alex Sakhartchoukdf272022011-01-09 11:34:03 -080039 * index sets or primitive types.
Robert Ly11518ac2011-02-09 13:57:06 -080040 * </p>
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -070041 **/
42public class Mesh extends BaseObj {
43
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070044 /**
Jason Samsd4ca9912012-05-08 19:02:07 -070045 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -080046 * Describes the way mesh vertex data is interpreted when rendering
47 *
48 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080049 public enum Primitive {
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070050 /**
Jason Samsd4ca9912012-05-08 19:02:07 -070051 * @deprecated in API 16
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080052 * Vertex data will be rendered as a series of points
53 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080054 POINT (0),
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070055 /**
Jason Samsd4ca9912012-05-08 19:02:07 -070056 * @deprecated in API 16
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080057 * Vertex pairs will be rendered as lines
58 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080059 LINE (1),
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070060 /**
Jason Samsd4ca9912012-05-08 19:02:07 -070061 * @deprecated in API 16
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080062 * Vertex data will be rendered as a connected line strip
63 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080064 LINE_STRIP (2),
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070065 /**
Jason Samsd4ca9912012-05-08 19:02:07 -070066 * @deprecated in API 16
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080067 * Vertices will be rendered as individual triangles
68 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080069 TRIANGLE (3),
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070070 /**
Jason Samsd4ca9912012-05-08 19:02:07 -070071 * @deprecated in API 16
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080072 * Vertices will be rendered as a connected triangle strip
73 * defined by the first three vertices with each additional
74 * triangle defined by a new vertex
75 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080076 TRIANGLE_STRIP (4),
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070077 /**
Jason Samsd4ca9912012-05-08 19:02:07 -070078 * @deprecated in API 16
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080079 * Vertices will be rendered as a sequence of triangles that all
80 * share first vertex as the origin
81 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080082 TRIANGLE_FAN (5);
83
84 int mID;
85 Primitive(int id) {
86 mID = id;
87 }
88 }
89
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -070090 Allocation[] mVertexBuffers;
91 Allocation[] mIndexBuffers;
92 Primitive[] mPrimitives;
93
Tim Murray460a0492013-11-19 12:45:54 -080094 Mesh(long id, RenderScript rs) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -070095 super(id, rs);
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -070096 }
97
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070098 /**
Jason Samsd4ca9912012-05-08 19:02:07 -070099 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800100 * @return number of allocations containing vertex data
101 *
102 **/
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700103 public int getVertexAllocationCount() {
104 if(mVertexBuffers == null) {
105 return 0;
106 }
107 return mVertexBuffers.length;
108 }
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700109 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700110 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800111 * @param slot index in the list of allocations to return
112 * @return vertex data allocation at the given index
113 *
114 **/
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700115 public Allocation getVertexAllocation(int slot) {
116 return mVertexBuffers[slot];
117 }
118
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700119 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700120 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800121 * @return number of primitives or index sets in the mesh
122 *
123 **/
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700124 public int getPrimitiveCount() {
125 if(mIndexBuffers == null) {
126 return 0;
127 }
128 return mIndexBuffers.length;
129 }
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800130
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700131 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700132 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800133 * @param slot locaton within the list of index set allocation
134 * @return allocation containing primtive index data or null if
135 * the index data is not specified explicitly
136 *
137 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800138 public Allocation getIndexSetAllocation(int slot) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700139 return mIndexBuffers[slot];
140 }
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700141 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700142 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800143 * @param slot locaiton within the list of index set primitives
144 * @return index set primitive type
145 *
146 **/
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700147 public Primitive getPrimitive(int slot) {
148 return mPrimitives[slot];
149 }
150
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700151 @Override
152 void updateFromNative() {
Jason Sams06d69de2010-11-09 17:11:40 -0800153 super.updateFromNative();
Jason Samse07694b2012-04-03 15:36:36 -0700154 int vtxCount = mRS.nMeshGetVertexBufferCount(getID(mRS));
155 int idxCount = mRS.nMeshGetIndexCount(getID(mRS));
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700156
157 int[] vtxIDs = new int[vtxCount];
158 int[] idxIDs = new int[idxCount];
159 int[] primitives = new int[idxCount];
160
Jason Samse07694b2012-04-03 15:36:36 -0700161 mRS.nMeshGetVertices(getID(mRS), vtxIDs, vtxCount);
162 mRS.nMeshGetIndices(getID(mRS), idxIDs, primitives, idxCount);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700163
164 mVertexBuffers = new Allocation[vtxCount];
165 mIndexBuffers = new Allocation[idxCount];
166 mPrimitives = new Primitive[idxCount];
167
168 for(int i = 0; i < vtxCount; i ++) {
169 if(vtxIDs[i] != 0) {
Jason Samsd4b23b52010-12-13 15:32:35 -0800170 mVertexBuffers[i] = new Allocation(vtxIDs[i], mRS, null, Allocation.USAGE_SCRIPT);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700171 mVertexBuffers[i].updateFromNative();
172 }
173 }
174
175 for(int i = 0; i < idxCount; i ++) {
176 if(idxIDs[i] != 0) {
Jason Samsd4b23b52010-12-13 15:32:35 -0800177 mIndexBuffers[i] = new Allocation(idxIDs[i], mRS, null, Allocation.USAGE_SCRIPT);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700178 mIndexBuffers[i].updateFromNative();
179 }
180 mPrimitives[i] = Primitive.values()[primitives[i]];
181 }
182 }
183
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700184 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700185 * @deprecated in API 16
Robert Ly11518ac2011-02-09 13:57:06 -0800186 * Mesh builder object. It starts empty and requires you to
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800187 * add the types necessary to create vertex and index
Robert Ly11518ac2011-02-09 13:57:06 -0800188 * allocations.
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800189 *
190 */
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700191 public static class Builder {
192 RenderScript mRS;
Jason Samsd1952402010-12-20 12:55:28 -0800193 int mUsage;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700194
195 class Entry {
196 Type t;
197 Element e;
198 int size;
199 Primitive prim;
Jason Samsd1952402010-12-20 12:55:28 -0800200 int usage;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700201 }
202
203 int mVertexTypeCount;
204 Entry[] mVertexTypes;
205 Vector mIndexTypes;
206
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700207 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700208 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800209 * Creates builder object
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800210 * @param rs Context to which the mesh will belong.
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800211 * @param usage specifies how the mesh allocations are to be
212 * handled, whether they need to be uploaded to a
213 * buffer on the gpu, maintain a cpu copy, etc
214 */
Jason Samsd1952402010-12-20 12:55:28 -0800215 public Builder(RenderScript rs, int usage) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700216 mRS = rs;
Jason Samsd1952402010-12-20 12:55:28 -0800217 mUsage = usage;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700218 mVertexTypeCount = 0;
219 mVertexTypes = new Entry[16];
220 mIndexTypes = new Vector();
221 }
222
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700223 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700224 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800225 * @return internal index of the last vertex buffer type added to
226 * builder
227 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800228 public int getCurrentVertexTypeIndex() {
229 return mVertexTypeCount - 1;
230 }
231
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700232 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700233 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800234 * @return internal index of the last index set added to the
235 * builder
236 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800237 public int getCurrentIndexSetIndex() {
238 return mIndexTypes.size() - 1;
239 }
240
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700241 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700242 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800243 * Adds a vertex data type to the builder object
244 *
Stephen Hinesb11e3d22011-01-11 19:30:58 -0800245 * @param t type of the vertex data allocation to be created
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800246 *
247 * @return this
248 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800249 public Builder addVertexType(Type t) throws IllegalStateException {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700250 if (mVertexTypeCount >= mVertexTypes.length) {
251 throw new IllegalStateException("Max vertex types exceeded.");
252 }
253
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700254 mVertexTypes[mVertexTypeCount] = new Entry();
255 mVertexTypes[mVertexTypeCount].t = t;
256 mVertexTypes[mVertexTypeCount].e = null;
257 mVertexTypeCount++;
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800258 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700259 }
260
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700261 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700262 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800263 * Adds a vertex data type to the builder object
264 *
265 * @param e element describing the vertex data layout
266 * @param size number of elements in the buffer
267 *
268 * @return this
269 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800270 public Builder addVertexType(Element e, int size) throws IllegalStateException {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700271 if (mVertexTypeCount >= mVertexTypes.length) {
272 throw new IllegalStateException("Max vertex types exceeded.");
273 }
274
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700275 mVertexTypes[mVertexTypeCount] = new Entry();
276 mVertexTypes[mVertexTypeCount].t = null;
277 mVertexTypes[mVertexTypeCount].e = e;
278 mVertexTypes[mVertexTypeCount].size = size;
279 mVertexTypeCount++;
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800280 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700281 }
282
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700283 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700284 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800285 * Adds an index set data type to the builder object
286 *
287 * @param t type of the index set data, could be null
288 * @param p primitive type
289 *
290 * @return this
291 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800292 public Builder addIndexSetType(Type t, Primitive p) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700293 Entry indexType = new Entry();
294 indexType.t = t;
295 indexType.e = null;
296 indexType.size = 0;
297 indexType.prim = p;
298 mIndexTypes.addElement(indexType);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800299 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700300 }
301
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700302 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700303 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800304 * Adds an index set primitive type to the builder object
305 *
306 * @param p primitive type
307 *
308 * @return this
309 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800310 public Builder addIndexSetType(Primitive p) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700311 Entry indexType = new Entry();
312 indexType.t = null;
313 indexType.e = null;
314 indexType.size = 0;
315 indexType.prim = p;
316 mIndexTypes.addElement(indexType);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800317 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700318 }
319
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700320 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700321 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800322 * Adds an index set data type to the builder object
323 *
324 * @param e element describing the index set data layout
325 * @param size number of elements in the buffer
326 * @param p primitive type
327 *
328 * @return this
329 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800330 public Builder addIndexSetType(Element e, int size, Primitive p) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700331 Entry indexType = new Entry();
332 indexType.t = null;
333 indexType.e = e;
334 indexType.size = size;
335 indexType.prim = p;
336 mIndexTypes.addElement(indexType);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800337 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700338 }
339
340 Type newType(Element e, int size) {
341 Type.Builder tb = new Type.Builder(mRS, e);
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800342 tb.setX(size);
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700343 return tb.create();
344 }
345
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700346 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700347 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800348 * Create a Mesh object from the current state of the builder
349 *
350 **/
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700351 public Mesh create() {
352 mRS.validate();
Alex Sakhartchouk25999a02011-05-12 10:38:03 -0700353 int[] vtx = new int[mVertexTypeCount];
354 int[] idx = new int[mIndexTypes.size()];
355 int[] prim = new int[mIndexTypes.size()];
356
357 Allocation[] vertexBuffers = new Allocation[mVertexTypeCount];
358 Allocation[] indexBuffers = new Allocation[mIndexTypes.size()];
359 Primitive[] primitives = new Primitive[mIndexTypes.size()];
360
361 for(int ct = 0; ct < mVertexTypeCount; ct ++) {
362 Allocation alloc = null;
363 Entry entry = mVertexTypes[ct];
364 if (entry.t != null) {
365 alloc = Allocation.createTyped(mRS, entry.t, mUsage);
366 } else if(entry.e != null) {
367 alloc = Allocation.createSized(mRS, entry.e, entry.size, mUsage);
368 }
369 vertexBuffers[ct] = alloc;
Tim Murray460a0492013-11-19 12:45:54 -0800370 vtx[ct] = (int)alloc.getID(mRS);
Alex Sakhartchouk25999a02011-05-12 10:38:03 -0700371 }
372
373 for(int ct = 0; ct < mIndexTypes.size(); ct ++) {
374 Allocation alloc = null;
375 Entry entry = (Entry)mIndexTypes.elementAt(ct);
376 if (entry.t != null) {
377 alloc = Allocation.createTyped(mRS, entry.t, mUsage);
378 } else if(entry.e != null) {
379 alloc = Allocation.createSized(mRS, entry.e, entry.size, mUsage);
380 }
Tim Murray460a0492013-11-19 12:45:54 -0800381 long allocID = (alloc == null) ? 0 : alloc.getID(mRS);
Alex Sakhartchouk25999a02011-05-12 10:38:03 -0700382 indexBuffers[ct] = alloc;
383 primitives[ct] = entry.prim;
384
Tim Murray460a0492013-11-19 12:45:54 -0800385 idx[ct] = (int)allocID;
Alex Sakhartchouk25999a02011-05-12 10:38:03 -0700386 prim[ct] = entry.prim.mID;
387 }
388
Tim Murray460a0492013-11-19 12:45:54 -0800389 long id = mRS.nMeshCreate(vtx, idx, prim);
Alex Sakhartchouk25999a02011-05-12 10:38:03 -0700390 Mesh newMesh = new Mesh(id, mRS);
391 newMesh.mVertexBuffers = vertexBuffers;
392 newMesh.mIndexBuffers = indexBuffers;
393 newMesh.mPrimitives = primitives;
394
395 return newMesh;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700396 }
397 }
398
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700399 /**
Jason Samse619de62012-05-08 18:40:58 -0700400 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800401 * Mesh builder object. It starts empty and requires the user to
402 * add all the vertex and index allocations that comprise the
403 * mesh
404 *
405 */
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700406 public static class AllocationBuilder {
407 RenderScript mRS;
408
409 class Entry {
410 Allocation a;
411 Primitive prim;
412 }
413
414 int mVertexTypeCount;
415 Entry[] mVertexTypes;
416
417 Vector mIndexTypes;
418
Jason Samse619de62012-05-08 18:40:58 -0700419 /**
420 * @deprecated in API 16
421 **/
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700422 public AllocationBuilder(RenderScript rs) {
423 mRS = rs;
424 mVertexTypeCount = 0;
425 mVertexTypes = new Entry[16];
426 mIndexTypes = new Vector();
427 }
428
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700429 /**
Jason Samse619de62012-05-08 18:40:58 -0700430 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800431 * @return internal index of the last vertex buffer type added to
432 * builder
433 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800434 public int getCurrentVertexTypeIndex() {
435 return mVertexTypeCount - 1;
436 }
437
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700438 /**
Jason Samse619de62012-05-08 18:40:58 -0700439 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800440 * @return internal index of the last index set added to the
441 * builder
442 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800443 public int getCurrentIndexSetIndex() {
444 return mIndexTypes.size() - 1;
445 }
446
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700447 /**
Jason Samse619de62012-05-08 18:40:58 -0700448 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800449 * Adds an allocation containing vertex buffer data to the
450 * builder
451 *
452 * @param a vertex data allocation
453 *
454 * @return this
455 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800456 public AllocationBuilder addVertexAllocation(Allocation a) throws IllegalStateException {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700457 if (mVertexTypeCount >= mVertexTypes.length) {
458 throw new IllegalStateException("Max vertex types exceeded.");
459 }
460
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700461 mVertexTypes[mVertexTypeCount] = new Entry();
462 mVertexTypes[mVertexTypeCount].a = a;
463 mVertexTypeCount++;
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800464 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700465 }
466
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700467 /**
Jason Samse619de62012-05-08 18:40:58 -0700468 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800469 * Adds an allocation containing index buffer data and index type
470 * to the builder
471 *
472 * @param a index set data allocation, could be null
473 * @param p index set primitive type
474 *
475 * @return this
476 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800477 public AllocationBuilder addIndexSetAllocation(Allocation a, Primitive p) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700478 Entry indexType = new Entry();
479 indexType.a = a;
480 indexType.prim = p;
481 mIndexTypes.addElement(indexType);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800482 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700483 }
484
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700485 /**
Jason Samse619de62012-05-08 18:40:58 -0700486 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800487 * Adds an index set type to the builder
488 *
489 * @param p index set primitive type
490 *
491 * @return this
492 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800493 public AllocationBuilder addIndexSetType(Primitive p) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700494 Entry indexType = new Entry();
495 indexType.a = null;
496 indexType.prim = p;
497 mIndexTypes.addElement(indexType);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800498 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700499 }
500
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700501 /**
Jason Samse619de62012-05-08 18:40:58 -0700502 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800503 * Create a Mesh object from the current state of the builder
504 *
505 **/
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700506 public Mesh create() {
507 mRS.validate();
Alex Sakhartchouk25999a02011-05-12 10:38:03 -0700508
509 int[] vtx = new int[mVertexTypeCount];
510 int[] idx = new int[mIndexTypes.size()];
511 int[] prim = new int[mIndexTypes.size()];
512
513 Allocation[] indexBuffers = new Allocation[mIndexTypes.size()];
514 Primitive[] primitives = new Primitive[mIndexTypes.size()];
515 Allocation[] vertexBuffers = new Allocation[mVertexTypeCount];
516
517 for(int ct = 0; ct < mVertexTypeCount; ct ++) {
518 Entry entry = mVertexTypes[ct];
519 vertexBuffers[ct] = entry.a;
Tim Murray460a0492013-11-19 12:45:54 -0800520 vtx[ct] = (int)entry.a.getID(mRS);
Alex Sakhartchouk25999a02011-05-12 10:38:03 -0700521 }
522
523 for(int ct = 0; ct < mIndexTypes.size(); ct ++) {
524 Entry entry = (Entry)mIndexTypes.elementAt(ct);
Tim Murray460a0492013-11-19 12:45:54 -0800525 long allocID = (entry.a == null) ? 0 : entry.a.getID(mRS);
Alex Sakhartchouk25999a02011-05-12 10:38:03 -0700526 indexBuffers[ct] = entry.a;
527 primitives[ct] = entry.prim;
528
Tim Murray460a0492013-11-19 12:45:54 -0800529 idx[ct] = (int)allocID;
Alex Sakhartchouk25999a02011-05-12 10:38:03 -0700530 prim[ct] = entry.prim.mID;
531 }
532
Tim Murray460a0492013-11-19 12:45:54 -0800533 long id = mRS.nMeshCreate(vtx, idx, prim);
Alex Sakhartchouk25999a02011-05-12 10:38:03 -0700534 Mesh newMesh = new Mesh(id, mRS);
535 newMesh.mVertexBuffers = vertexBuffers;
536 newMesh.mIndexBuffers = indexBuffers;
537 newMesh.mPrimitives = primitives;
538
539 return newMesh;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700540 }
541 }
542
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700543 /**
Jason Samse619de62012-05-08 18:40:58 -0700544 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800545 * Builder that allows creation of a mesh object point by point
546 * and triangle by triangle
547 *
548 **/
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700549 public static class TriangleMeshBuilder {
550 float mVtxData[];
551 int mVtxCount;
Alex Sakhartchouke60149d2011-11-15 15:15:21 -0800552 int mMaxIndex;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700553 short mIndexData[];
554 int mIndexCount;
555 RenderScript mRS;
556 Element mElement;
557
558 float mNX = 0;
559 float mNY = 0;
560 float mNZ = -1;
561 float mS0 = 0;
562 float mT0 = 0;
563 float mR = 1;
564 float mG = 1;
565 float mB = 1;
566 float mA = 1;
567
568 int mVtxSize;
569 int mFlags;
570
Jason Samse619de62012-05-08 18:40:58 -0700571 /**
572 * @deprecated in API 16
573 **/
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700574 public static final int COLOR = 0x0001;
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 NORMAL = 0x0002;
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 TEXTURE_0 = 0x0100;
583
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700584 /**
Jason Samse619de62012-05-08 18:40:58 -0700585 * @deprecated in API 16
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800586 * @param rs Context to which the mesh will belong.
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800587 * @param vtxSize specifies whether the vertex is a float2 or
588 * float3
589 * @param flags bitfield that is a combination of COLOR, NORMAL,
590 * and TEXTURE_0 that specifies what vertex data
591 * channels are present in the mesh
592 *
593 **/
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700594 public TriangleMeshBuilder(RenderScript rs, int vtxSize, int flags) {
595 mRS = rs;
596 mVtxCount = 0;
Alex Sakhartchouke60149d2011-11-15 15:15:21 -0800597 mMaxIndex = 0;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700598 mIndexCount = 0;
599 mVtxData = new float[128];
600 mIndexData = new short[128];
601 mVtxSize = vtxSize;
602 mFlags = flags;
603
604 if (vtxSize < 2 || vtxSize > 3) {
605 throw new IllegalArgumentException("Vertex size out of range.");
606 }
607 }
608
609 private void makeSpace(int count) {
610 if ((mVtxCount + count) >= mVtxData.length) {
611 float t[] = new float[mVtxData.length * 2];
612 System.arraycopy(mVtxData, 0, t, 0, mVtxData.length);
613 mVtxData = t;
614 }
615 }
616
617 private void latch() {
618 if ((mFlags & COLOR) != 0) {
619 makeSpace(4);
620 mVtxData[mVtxCount++] = mR;
621 mVtxData[mVtxCount++] = mG;
622 mVtxData[mVtxCount++] = mB;
623 mVtxData[mVtxCount++] = mA;
624 }
625 if ((mFlags & TEXTURE_0) != 0) {
626 makeSpace(2);
627 mVtxData[mVtxCount++] = mS0;
628 mVtxData[mVtxCount++] = mT0;
629 }
630 if ((mFlags & NORMAL) != 0) {
Alex Sakhartchouke60149d2011-11-15 15:15:21 -0800631 makeSpace(4);
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700632 mVtxData[mVtxCount++] = mNX;
633 mVtxData[mVtxCount++] = mNY;
634 mVtxData[mVtxCount++] = mNZ;
Alex Sakhartchouke60149d2011-11-15 15:15:21 -0800635 mVtxData[mVtxCount++] = 0.0f;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700636 }
Alex Sakhartchouke60149d2011-11-15 15:15:21 -0800637 mMaxIndex ++;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700638 }
639
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700640 /**
Jason Samse619de62012-05-08 18:40:58 -0700641 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800642 * Adds a float2 vertex to the mesh
643 *
644 * @param x position x
645 * @param y position y
646 *
647 * @return this
648 *
649 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800650 public TriangleMeshBuilder addVertex(float x, float y) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700651 if (mVtxSize != 2) {
652 throw new IllegalStateException("add mistmatch with declared components.");
653 }
654 makeSpace(2);
655 mVtxData[mVtxCount++] = x;
656 mVtxData[mVtxCount++] = y;
657 latch();
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800658 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700659 }
660
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700661 /**
Jason Samse619de62012-05-08 18:40:58 -0700662 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800663 * Adds a float3 vertex to the mesh
664 *
665 * @param x position x
666 * @param y position y
667 * @param z position z
668 *
669 * @return this
670 *
671 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800672 public TriangleMeshBuilder addVertex(float x, float y, float z) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700673 if (mVtxSize != 3) {
674 throw new IllegalStateException("add mistmatch with declared components.");
675 }
Alex Sakhartchouke60149d2011-11-15 15:15:21 -0800676 makeSpace(4);
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700677 mVtxData[mVtxCount++] = x;
678 mVtxData[mVtxCount++] = y;
679 mVtxData[mVtxCount++] = z;
Alex Sakhartchouke60149d2011-11-15 15:15:21 -0800680 mVtxData[mVtxCount++] = 1.0f;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700681 latch();
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800682 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700683 }
684
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700685 /**
Jason Samse619de62012-05-08 18:40:58 -0700686 * @deprecated in API 16
Robert Lyf11ffc112012-02-22 10:59:12 -0800687 * Sets the texture coordinate for the vertices that are added after this method call.
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800688 *
689 * @param s texture coordinate s
690 * @param t texture coordinate t
691 *
692 * @return this
693 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800694 public TriangleMeshBuilder setTexture(float s, float t) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700695 if ((mFlags & TEXTURE_0) == 0) {
696 throw new IllegalStateException("add mistmatch with declared components.");
697 }
698 mS0 = s;
699 mT0 = t;
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800700 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700701 }
702
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700703 /**
Jason Samse619de62012-05-08 18:40:58 -0700704 * @deprecated in API 16
Robert Lyf11ffc112012-02-22 10:59:12 -0800705 * Sets the normal vector for the vertices that are added after this method call.
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800706 *
707 * @param x normal vector x
708 * @param y normal vector y
709 * @param z normal vector z
710 *
711 * @return this
712 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800713 public TriangleMeshBuilder setNormal(float x, float y, float z) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700714 if ((mFlags & NORMAL) == 0) {
715 throw new IllegalStateException("add mistmatch with declared components.");
716 }
717 mNX = x;
718 mNY = y;
719 mNZ = z;
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800720 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700721 }
722
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700723 /**
Jason Samse619de62012-05-08 18:40:58 -0700724 * @deprecated in API 16
Robert Lyf11ffc112012-02-22 10:59:12 -0800725 * Sets the color for the vertices that are added after this method call.
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800726 *
727 * @param r red component
728 * @param g green component
729 * @param b blue component
730 * @param a alpha component
731 *
732 * @return this
733 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800734 public TriangleMeshBuilder setColor(float r, float g, float b, float a) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700735 if ((mFlags & COLOR) == 0) {
736 throw new IllegalStateException("add mistmatch with declared components.");
737 }
738 mR = r;
739 mG = g;
740 mB = b;
741 mA = a;
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800742 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700743 }
744
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700745 /**
Jason Samse619de62012-05-08 18:40:58 -0700746 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800747 * Adds a new triangle to the mesh builder
748 *
749 * @param idx1 index of the first vertex in the triangle
750 * @param idx2 index of the second vertex in the triangle
751 * @param idx3 index of the third vertex in the triangle
752 *
753 * @return this
754 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800755 public TriangleMeshBuilder addTriangle(int idx1, int idx2, int idx3) {
Alex Sakhartchouke60149d2011-11-15 15:15:21 -0800756 if((idx1 >= mMaxIndex) || (idx1 < 0) ||
757 (idx2 >= mMaxIndex) || (idx2 < 0) ||
758 (idx3 >= mMaxIndex) || (idx3 < 0)) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700759 throw new IllegalStateException("Index provided greater than vertex count.");
760 }
761 if ((mIndexCount + 3) >= mIndexData.length) {
762 short t[] = new short[mIndexData.length * 2];
763 System.arraycopy(mIndexData, 0, t, 0, mIndexData.length);
764 mIndexData = t;
765 }
766 mIndexData[mIndexCount++] = (short)idx1;
767 mIndexData[mIndexCount++] = (short)idx2;
768 mIndexData[mIndexCount++] = (short)idx3;
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800769 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700770 }
771
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700772 /**
Jason Samse619de62012-05-08 18:40:58 -0700773 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800774 * Creates the mesh object from the current state of the builder
775 *
776 * @param uploadToBufferObject specifies whether the vertex data
777 * is to be uploaded into the buffer
778 * object indicating that it's likely
779 * not going to be modified and
780 * rendered many times.
781 * Alternatively, it indicates the
782 * mesh data will be updated
783 * frequently and remain in script
784 * accessible memory
785 *
786 **/
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700787 public Mesh create(boolean uploadToBufferObject) {
788 Element.Builder b = new Element.Builder(mRS);
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700789 b.add(Element.createVector(mRS,
790 Element.DataType.FLOAT_32,
791 mVtxSize), "position");
792 if ((mFlags & COLOR) != 0) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700793 b.add(Element.F32_4(mRS), "color");
794 }
795 if ((mFlags & TEXTURE_0) != 0) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700796 b.add(Element.F32_2(mRS), "texture0");
797 }
798 if ((mFlags & NORMAL) != 0) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700799 b.add(Element.F32_3(mRS), "normal");
800 }
801 mElement = b.create();
802
Jason Samsd1952402010-12-20 12:55:28 -0800803 int usage = Allocation.USAGE_SCRIPT;
804 if (uploadToBufferObject) {
805 usage |= Allocation.USAGE_GRAPHICS_VERTEX;
806 }
807
808 Builder smb = new Builder(mRS, usage);
Alex Sakhartchouke60149d2011-11-15 15:15:21 -0800809 smb.addVertexType(mElement, mMaxIndex);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800810 smb.addIndexSetType(Element.U16(mRS), mIndexCount, Primitive.TRIANGLE);
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700811
812 Mesh sm = smb.create();
813
Alex Sakhartchouke60149d2011-11-15 15:15:21 -0800814 sm.getVertexAllocation(0).copy1DRangeFromUnchecked(0, mMaxIndex, mVtxData);
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700815 if(uploadToBufferObject) {
Jason Samsd1952402010-12-20 12:55:28 -0800816 if (uploadToBufferObject) {
817 sm.getVertexAllocation(0).syncAll(Allocation.USAGE_SCRIPT);
818 }
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700819 }
820
Jason Samsb97b2512011-01-16 15:04:08 -0800821 sm.getIndexSetAllocation(0).copy1DRangeFromUnchecked(0, mIndexCount, mIndexData);
Jason Samsd1952402010-12-20 12:55:28 -0800822 if (uploadToBufferObject) {
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800823 sm.getIndexSetAllocation(0).syncAll(Allocation.USAGE_SCRIPT);
Jason Samsd1952402010-12-20 12:55:28 -0800824 }
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700825
826 return sm;
827 }
828 }
829}
830