Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 1 | /* |
Jason Sams | e619de6 | 2012-05-08 18:40:58 -0700 | [diff] [blame] | 2 | * Copyright (C) 2008-2012 The Android Open Source Project |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.renderscript; |
| 18 | |
| 19 | import java.util.Vector; |
| 20 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 21 | /** |
Tim Murray | a908422 | 2013-04-05 22:06:43 +0000 | [diff] [blame] | 22 | * @hide |
Jason Sams | d4ca991 | 2012-05-08 19:02:07 -0700 | [diff] [blame] | 23 | * @deprecated in API 16 |
Robert Ly | 11518ac | 2011-02-09 13:57:06 -0800 | [diff] [blame] | 24 | * <p>This class is a container for geometric data displayed with |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 25 | * RenderScript. Internally, a mesh is a collection of allocations that |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 26 | * represent vertex data (positions, normals, texture |
Robert Ly | 11518ac | 2011-02-09 13:57:06 -0800 | [diff] [blame] | 27 | * 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 Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 32 | * vertex channel name matches an input in the vertex program, |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 33 | * RenderScript automatically connects the two together. |
Robert Ly | 11518ac | 2011-02-09 13:57:06 -0800 | [diff] [blame] | 34 | * </p> |
| 35 | * <p> |
| 36 | * Parts of the mesh can be rendered with either explicit |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 37 | * index sets or primitive types. |
Robert Ly | 11518ac | 2011-02-09 13:57:06 -0800 | [diff] [blame] | 38 | * </p> |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 39 | **/ |
| 40 | public class Mesh extends BaseObj { |
| 41 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 42 | /** |
Jason Sams | d4ca991 | 2012-05-08 19:02:07 -0700 | [diff] [blame] | 43 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 44 | * Describes the way mesh vertex data is interpreted when rendering |
| 45 | * |
| 46 | **/ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 47 | public enum Primitive { |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 48 | /** |
Jason Sams | d4ca991 | 2012-05-08 19:02:07 -0700 | [diff] [blame] | 49 | * @deprecated in API 16 |
Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 50 | * Vertex data will be rendered as a series of points |
| 51 | */ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 52 | POINT (0), |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 53 | /** |
Jason Sams | d4ca991 | 2012-05-08 19:02:07 -0700 | [diff] [blame] | 54 | * @deprecated in API 16 |
Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 55 | * Vertex pairs will be rendered as lines |
| 56 | */ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 57 | LINE (1), |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 58 | /** |
Jason Sams | d4ca991 | 2012-05-08 19:02:07 -0700 | [diff] [blame] | 59 | * @deprecated in API 16 |
Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 60 | * Vertex data will be rendered as a connected line strip |
| 61 | */ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 62 | LINE_STRIP (2), |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 63 | /** |
Jason Sams | d4ca991 | 2012-05-08 19:02:07 -0700 | [diff] [blame] | 64 | * @deprecated in API 16 |
Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 65 | * Vertices will be rendered as individual triangles |
| 66 | */ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 67 | TRIANGLE (3), |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 68 | /** |
Jason Sams | d4ca991 | 2012-05-08 19:02:07 -0700 | [diff] [blame] | 69 | * @deprecated in API 16 |
Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 70 | * 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 Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 74 | TRIANGLE_STRIP (4), |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 75 | /** |
Jason Sams | d4ca991 | 2012-05-08 19:02:07 -0700 | [diff] [blame] | 76 | * @deprecated in API 16 |
Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 77 | * Vertices will be rendered as a sequence of triangles that all |
| 78 | * share first vertex as the origin |
| 79 | */ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 80 | TRIANGLE_FAN (5); |
| 81 | |
| 82 | int mID; |
| 83 | Primitive(int id) { |
| 84 | mID = id; |
| 85 | } |
| 86 | } |
| 87 | |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 88 | Allocation[] mVertexBuffers; |
| 89 | Allocation[] mIndexBuffers; |
| 90 | Primitive[] mPrimitives; |
| 91 | |
Tim Murray | 460a049 | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 92 | Mesh(long id, RenderScript rs) { |
Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 93 | super(id, rs); |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 96 | /** |
Jason Sams | d4ca991 | 2012-05-08 19:02:07 -0700 | [diff] [blame] | 97 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 98 | * @return number of allocations containing vertex data |
| 99 | * |
| 100 | **/ |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 101 | public int getVertexAllocationCount() { |
| 102 | if(mVertexBuffers == null) { |
| 103 | return 0; |
| 104 | } |
| 105 | return mVertexBuffers.length; |
| 106 | } |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 107 | /** |
Jason Sams | d4ca991 | 2012-05-08 19:02:07 -0700 | [diff] [blame] | 108 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 109 | * @param slot index in the list of allocations to return |
| 110 | * @return vertex data allocation at the given index |
| 111 | * |
| 112 | **/ |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 113 | public Allocation getVertexAllocation(int slot) { |
| 114 | return mVertexBuffers[slot]; |
| 115 | } |
| 116 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 117 | /** |
Jason Sams | d4ca991 | 2012-05-08 19:02:07 -0700 | [diff] [blame] | 118 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 119 | * @return number of primitives or index sets in the mesh |
| 120 | * |
| 121 | **/ |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 122 | public int getPrimitiveCount() { |
| 123 | if(mIndexBuffers == null) { |
| 124 | return 0; |
| 125 | } |
| 126 | return mIndexBuffers.length; |
| 127 | } |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 128 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 129 | /** |
Jason Sams | d4ca991 | 2012-05-08 19:02:07 -0700 | [diff] [blame] | 130 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 131 | * @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 Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 136 | public Allocation getIndexSetAllocation(int slot) { |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 137 | return mIndexBuffers[slot]; |
| 138 | } |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 139 | /** |
Jason Sams | d4ca991 | 2012-05-08 19:02:07 -0700 | [diff] [blame] | 140 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 141 | * @param slot locaiton within the list of index set primitives |
| 142 | * @return index set primitive type |
| 143 | * |
| 144 | **/ |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 145 | public Primitive getPrimitive(int slot) { |
| 146 | return mPrimitives[slot]; |
| 147 | } |
| 148 | |
Alex Sakhartchouk | 80a4c2c | 2010-07-12 15:50:32 -0700 | [diff] [blame] | 149 | @Override |
| 150 | void updateFromNative() { |
Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 151 | super.updateFromNative(); |
Jason Sams | e07694b | 2012-04-03 15:36:36 -0700 | [diff] [blame] | 152 | int vtxCount = mRS.nMeshGetVertexBufferCount(getID(mRS)); |
| 153 | int idxCount = mRS.nMeshGetIndexCount(getID(mRS)); |
Alex Sakhartchouk | 80a4c2c | 2010-07-12 15:50:32 -0700 | [diff] [blame] | 154 | |
Ashok Bhat | 9807155 | 2014-02-12 09:54:43 +0000 | [diff] [blame] | 155 | long[] vtxIDs = new long[vtxCount]; |
| 156 | long[] idxIDs = new long[idxCount]; |
Alex Sakhartchouk | 80a4c2c | 2010-07-12 15:50:32 -0700 | [diff] [blame] | 157 | int[] primitives = new int[idxCount]; |
| 158 | |
Jason Sams | e07694b | 2012-04-03 15:36:36 -0700 | [diff] [blame] | 159 | mRS.nMeshGetVertices(getID(mRS), vtxIDs, vtxCount); |
| 160 | mRS.nMeshGetIndices(getID(mRS), idxIDs, primitives, idxCount); |
Alex Sakhartchouk | 80a4c2c | 2010-07-12 15:50:32 -0700 | [diff] [blame] | 161 | |
| 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 Sams | d4b23b5 | 2010-12-13 15:32:35 -0800 | [diff] [blame] | 168 | mVertexBuffers[i] = new Allocation(vtxIDs[i], mRS, null, Allocation.USAGE_SCRIPT); |
Alex Sakhartchouk | 80a4c2c | 2010-07-12 15:50:32 -0700 | [diff] [blame] | 169 | mVertexBuffers[i].updateFromNative(); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | for(int i = 0; i < idxCount; i ++) { |
| 174 | if(idxIDs[i] != 0) { |
Jason Sams | d4b23b5 | 2010-12-13 15:32:35 -0800 | [diff] [blame] | 175 | mIndexBuffers[i] = new Allocation(idxIDs[i], mRS, null, Allocation.USAGE_SCRIPT); |
Alex Sakhartchouk | 80a4c2c | 2010-07-12 15:50:32 -0700 | [diff] [blame] | 176 | mIndexBuffers[i].updateFromNative(); |
| 177 | } |
| 178 | mPrimitives[i] = Primitive.values()[primitives[i]]; |
| 179 | } |
| 180 | } |
| 181 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 182 | /** |
Jason Sams | d4ca991 | 2012-05-08 19:02:07 -0700 | [diff] [blame] | 183 | * @deprecated in API 16 |
Robert Ly | 11518ac | 2011-02-09 13:57:06 -0800 | [diff] [blame] | 184 | * Mesh builder object. It starts empty and requires you to |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 185 | * add the types necessary to create vertex and index |
Robert Ly | 11518ac | 2011-02-09 13:57:06 -0800 | [diff] [blame] | 186 | * allocations. |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 187 | * |
| 188 | */ |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 189 | public static class Builder { |
| 190 | RenderScript mRS; |
Jason Sams | d195240 | 2010-12-20 12:55:28 -0800 | [diff] [blame] | 191 | int mUsage; |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 192 | |
| 193 | class Entry { |
| 194 | Type t; |
| 195 | Element e; |
| 196 | int size; |
| 197 | Primitive prim; |
Jason Sams | d195240 | 2010-12-20 12:55:28 -0800 | [diff] [blame] | 198 | int usage; |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | int mVertexTypeCount; |
| 202 | Entry[] mVertexTypes; |
| 203 | Vector mIndexTypes; |
| 204 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 205 | /** |
Jason Sams | d4ca991 | 2012-05-08 19:02:07 -0700 | [diff] [blame] | 206 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 207 | * Creates builder object |
Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 208 | * @param rs Context to which the mesh will belong. |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 209 | * @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 Sams | d195240 | 2010-12-20 12:55:28 -0800 | [diff] [blame] | 213 | public Builder(RenderScript rs, int usage) { |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 214 | mRS = rs; |
Jason Sams | d195240 | 2010-12-20 12:55:28 -0800 | [diff] [blame] | 215 | mUsage = usage; |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 216 | mVertexTypeCount = 0; |
| 217 | mVertexTypes = new Entry[16]; |
| 218 | mIndexTypes = new Vector(); |
| 219 | } |
| 220 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 221 | /** |
Jason Sams | d4ca991 | 2012-05-08 19:02:07 -0700 | [diff] [blame] | 222 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 223 | * @return internal index of the last vertex buffer type added to |
| 224 | * builder |
| 225 | **/ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 226 | public int getCurrentVertexTypeIndex() { |
| 227 | return mVertexTypeCount - 1; |
| 228 | } |
| 229 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 230 | /** |
Jason Sams | d4ca991 | 2012-05-08 19:02:07 -0700 | [diff] [blame] | 231 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 232 | * @return internal index of the last index set added to the |
| 233 | * builder |
| 234 | **/ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 235 | public int getCurrentIndexSetIndex() { |
| 236 | return mIndexTypes.size() - 1; |
| 237 | } |
| 238 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 239 | /** |
Jason Sams | d4ca991 | 2012-05-08 19:02:07 -0700 | [diff] [blame] | 240 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 241 | * Adds a vertex data type to the builder object |
| 242 | * |
Stephen Hines | b11e3d2 | 2011-01-11 19:30:58 -0800 | [diff] [blame] | 243 | * @param t type of the vertex data allocation to be created |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 244 | * |
| 245 | * @return this |
| 246 | **/ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 247 | public Builder addVertexType(Type t) throws IllegalStateException { |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 248 | if (mVertexTypeCount >= mVertexTypes.length) { |
| 249 | throw new IllegalStateException("Max vertex types exceeded."); |
| 250 | } |
| 251 | |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 252 | mVertexTypes[mVertexTypeCount] = new Entry(); |
| 253 | mVertexTypes[mVertexTypeCount].t = t; |
| 254 | mVertexTypes[mVertexTypeCount].e = null; |
| 255 | mVertexTypeCount++; |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 256 | return this; |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 257 | } |
| 258 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 259 | /** |
Jason Sams | d4ca991 | 2012-05-08 19:02:07 -0700 | [diff] [blame] | 260 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 261 | * 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 Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 268 | public Builder addVertexType(Element e, int size) throws IllegalStateException { |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 269 | if (mVertexTypeCount >= mVertexTypes.length) { |
| 270 | throw new IllegalStateException("Max vertex types exceeded."); |
| 271 | } |
| 272 | |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 273 | mVertexTypes[mVertexTypeCount] = new Entry(); |
| 274 | mVertexTypes[mVertexTypeCount].t = null; |
| 275 | mVertexTypes[mVertexTypeCount].e = e; |
| 276 | mVertexTypes[mVertexTypeCount].size = size; |
| 277 | mVertexTypeCount++; |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 278 | return this; |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 279 | } |
| 280 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 281 | /** |
Jason Sams | d4ca991 | 2012-05-08 19:02:07 -0700 | [diff] [blame] | 282 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 283 | * 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 Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 290 | public Builder addIndexSetType(Type t, Primitive p) { |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 291 | 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 Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 297 | return this; |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 298 | } |
| 299 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 300 | /** |
Jason Sams | d4ca991 | 2012-05-08 19:02:07 -0700 | [diff] [blame] | 301 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 302 | * Adds an index set primitive type to the builder object |
| 303 | * |
| 304 | * @param p primitive type |
| 305 | * |
| 306 | * @return this |
| 307 | **/ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 308 | public Builder addIndexSetType(Primitive p) { |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 309 | 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 Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 315 | return this; |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 316 | } |
| 317 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 318 | /** |
Jason Sams | d4ca991 | 2012-05-08 19:02:07 -0700 | [diff] [blame] | 319 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 320 | * 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 Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 328 | public Builder addIndexSetType(Element e, int size, Primitive p) { |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 329 | 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 Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 335 | return this; |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | Type newType(Element e, int size) { |
| 339 | Type.Builder tb = new Type.Builder(mRS, e); |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 340 | tb.setX(size); |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 341 | return tb.create(); |
| 342 | } |
| 343 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 344 | /** |
Jason Sams | d4ca991 | 2012-05-08 19:02:07 -0700 | [diff] [blame] | 345 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 346 | * Create a Mesh object from the current state of the builder |
| 347 | * |
| 348 | **/ |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 349 | public Mesh create() { |
| 350 | mRS.validate(); |
Ashok Bhat | 9807155 | 2014-02-12 09:54:43 +0000 | [diff] [blame] | 351 | long[] vtx = new long[mVertexTypeCount]; |
| 352 | long[] idx = new long[mIndexTypes.size()]; |
Alex Sakhartchouk | 25999a0 | 2011-05-12 10:38:03 -0700 | [diff] [blame] | 353 | 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 Sams | ae5be38 | 2015-03-26 14:47:17 -0700 | [diff] [blame] | 366 | } else { |
| 367 | // Should never happen because the builder will always set one |
| 368 | throw new IllegalStateException("Builder corrupt, no valid element in entry."); |
Alex Sakhartchouk | 25999a0 | 2011-05-12 10:38:03 -0700 | [diff] [blame] | 369 | } |
| 370 | vertexBuffers[ct] = alloc; |
Ashok Bhat | 9807155 | 2014-02-12 09:54:43 +0000 | [diff] [blame] | 371 | vtx[ct] = alloc.getID(mRS); |
Alex Sakhartchouk | 25999a0 | 2011-05-12 10:38:03 -0700 | [diff] [blame] | 372 | } |
| 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 Sams | ae5be38 | 2015-03-26 14:47:17 -0700 | [diff] [blame] | 381 | } else { |
| 382 | // Should never happen because the builder will always set one |
| 383 | throw new IllegalStateException("Builder corrupt, no valid element in entry."); |
Alex Sakhartchouk | 25999a0 | 2011-05-12 10:38:03 -0700 | [diff] [blame] | 384 | } |
Tim Murray | 460a049 | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 385 | long allocID = (alloc == null) ? 0 : alloc.getID(mRS); |
Alex Sakhartchouk | 25999a0 | 2011-05-12 10:38:03 -0700 | [diff] [blame] | 386 | indexBuffers[ct] = alloc; |
| 387 | primitives[ct] = entry.prim; |
| 388 | |
Ashok Bhat | 9807155 | 2014-02-12 09:54:43 +0000 | [diff] [blame] | 389 | idx[ct] = allocID; |
Alex Sakhartchouk | 25999a0 | 2011-05-12 10:38:03 -0700 | [diff] [blame] | 390 | prim[ct] = entry.prim.mID; |
| 391 | } |
| 392 | |
Tim Murray | 460a049 | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 393 | long id = mRS.nMeshCreate(vtx, idx, prim); |
Alex Sakhartchouk | 25999a0 | 2011-05-12 10:38:03 -0700 | [diff] [blame] | 394 | Mesh newMesh = new Mesh(id, mRS); |
| 395 | newMesh.mVertexBuffers = vertexBuffers; |
| 396 | newMesh.mIndexBuffers = indexBuffers; |
| 397 | newMesh.mPrimitives = primitives; |
| 398 | |
| 399 | return newMesh; |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 400 | } |
| 401 | } |
| 402 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 403 | /** |
Jason Sams | e619de6 | 2012-05-08 18:40:58 -0700 | [diff] [blame] | 404 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 405 | * 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 Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 410 | 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 Sams | e619de6 | 2012-05-08 18:40:58 -0700 | [diff] [blame] | 423 | /** |
| 424 | * @deprecated in API 16 |
| 425 | **/ |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 426 | public AllocationBuilder(RenderScript rs) { |
| 427 | mRS = rs; |
| 428 | mVertexTypeCount = 0; |
| 429 | mVertexTypes = new Entry[16]; |
| 430 | mIndexTypes = new Vector(); |
| 431 | } |
| 432 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 433 | /** |
Jason Sams | e619de6 | 2012-05-08 18:40:58 -0700 | [diff] [blame] | 434 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 435 | * @return internal index of the last vertex buffer type added to |
| 436 | * builder |
| 437 | **/ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 438 | public int getCurrentVertexTypeIndex() { |
| 439 | return mVertexTypeCount - 1; |
| 440 | } |
| 441 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 442 | /** |
Jason Sams | e619de6 | 2012-05-08 18:40:58 -0700 | [diff] [blame] | 443 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 444 | * @return internal index of the last index set added to the |
| 445 | * builder |
| 446 | **/ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 447 | public int getCurrentIndexSetIndex() { |
| 448 | return mIndexTypes.size() - 1; |
| 449 | } |
| 450 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 451 | /** |
Jason Sams | e619de6 | 2012-05-08 18:40:58 -0700 | [diff] [blame] | 452 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 453 | * Adds an allocation containing vertex buffer data to the |
| 454 | * builder |
| 455 | * |
| 456 | * @param a vertex data allocation |
| 457 | * |
| 458 | * @return this |
| 459 | **/ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 460 | public AllocationBuilder addVertexAllocation(Allocation a) throws IllegalStateException { |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 461 | if (mVertexTypeCount >= mVertexTypes.length) { |
| 462 | throw new IllegalStateException("Max vertex types exceeded."); |
| 463 | } |
| 464 | |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 465 | mVertexTypes[mVertexTypeCount] = new Entry(); |
| 466 | mVertexTypes[mVertexTypeCount].a = a; |
| 467 | mVertexTypeCount++; |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 468 | return this; |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 469 | } |
| 470 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 471 | /** |
Jason Sams | e619de6 | 2012-05-08 18:40:58 -0700 | [diff] [blame] | 472 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 473 | * 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 Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 481 | public AllocationBuilder addIndexSetAllocation(Allocation a, Primitive p) { |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 482 | Entry indexType = new Entry(); |
| 483 | indexType.a = a; |
| 484 | indexType.prim = p; |
| 485 | mIndexTypes.addElement(indexType); |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 486 | return this; |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 487 | } |
| 488 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 489 | /** |
Jason Sams | e619de6 | 2012-05-08 18:40:58 -0700 | [diff] [blame] | 490 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 491 | * Adds an index set type to the builder |
| 492 | * |
| 493 | * @param p index set primitive type |
| 494 | * |
| 495 | * @return this |
| 496 | **/ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 497 | public AllocationBuilder addIndexSetType(Primitive p) { |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 498 | Entry indexType = new Entry(); |
| 499 | indexType.a = null; |
| 500 | indexType.prim = p; |
| 501 | mIndexTypes.addElement(indexType); |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 502 | return this; |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 503 | } |
| 504 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 505 | /** |
Jason Sams | e619de6 | 2012-05-08 18:40:58 -0700 | [diff] [blame] | 506 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 507 | * Create a Mesh object from the current state of the builder |
| 508 | * |
| 509 | **/ |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 510 | public Mesh create() { |
| 511 | mRS.validate(); |
Alex Sakhartchouk | 25999a0 | 2011-05-12 10:38:03 -0700 | [diff] [blame] | 512 | |
Ashok Bhat | 9807155 | 2014-02-12 09:54:43 +0000 | [diff] [blame] | 513 | long[] vtx = new long[mVertexTypeCount]; |
| 514 | long[] idx = new long[mIndexTypes.size()]; |
Alex Sakhartchouk | 25999a0 | 2011-05-12 10:38:03 -0700 | [diff] [blame] | 515 | 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 Bhat | 9807155 | 2014-02-12 09:54:43 +0000 | [diff] [blame] | 524 | vtx[ct] = entry.a.getID(mRS); |
Alex Sakhartchouk | 25999a0 | 2011-05-12 10:38:03 -0700 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | for(int ct = 0; ct < mIndexTypes.size(); ct ++) { |
| 528 | Entry entry = (Entry)mIndexTypes.elementAt(ct); |
Tim Murray | 460a049 | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 529 | long allocID = (entry.a == null) ? 0 : entry.a.getID(mRS); |
Alex Sakhartchouk | 25999a0 | 2011-05-12 10:38:03 -0700 | [diff] [blame] | 530 | indexBuffers[ct] = entry.a; |
| 531 | primitives[ct] = entry.prim; |
| 532 | |
Ashok Bhat | 9807155 | 2014-02-12 09:54:43 +0000 | [diff] [blame] | 533 | idx[ct] = allocID; |
Alex Sakhartchouk | 25999a0 | 2011-05-12 10:38:03 -0700 | [diff] [blame] | 534 | prim[ct] = entry.prim.mID; |
| 535 | } |
| 536 | |
Tim Murray | 460a049 | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 537 | long id = mRS.nMeshCreate(vtx, idx, prim); |
Alex Sakhartchouk | 25999a0 | 2011-05-12 10:38:03 -0700 | [diff] [blame] | 538 | Mesh newMesh = new Mesh(id, mRS); |
| 539 | newMesh.mVertexBuffers = vertexBuffers; |
| 540 | newMesh.mIndexBuffers = indexBuffers; |
| 541 | newMesh.mPrimitives = primitives; |
| 542 | |
| 543 | return newMesh; |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 544 | } |
| 545 | } |
| 546 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 547 | /** |
Jason Sams | e619de6 | 2012-05-08 18:40:58 -0700 | [diff] [blame] | 548 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 549 | * Builder that allows creation of a mesh object point by point |
| 550 | * and triangle by triangle |
| 551 | * |
| 552 | **/ |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 553 | public static class TriangleMeshBuilder { |
| 554 | float mVtxData[]; |
| 555 | int mVtxCount; |
Alex Sakhartchouk | e60149d | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 556 | int mMaxIndex; |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 557 | 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 Sams | e619de6 | 2012-05-08 18:40:58 -0700 | [diff] [blame] | 575 | /** |
| 576 | * @deprecated in API 16 |
| 577 | **/ |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 578 | public static final int COLOR = 0x0001; |
Jason Sams | e619de6 | 2012-05-08 18:40:58 -0700 | [diff] [blame] | 579 | /** |
| 580 | * @deprecated in API 16 |
| 581 | **/ |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 582 | public static final int NORMAL = 0x0002; |
Jason Sams | e619de6 | 2012-05-08 18:40:58 -0700 | [diff] [blame] | 583 | /** |
| 584 | * @deprecated in API 16 |
| 585 | **/ |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 586 | public static final int TEXTURE_0 = 0x0100; |
| 587 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 588 | /** |
Jason Sams | e619de6 | 2012-05-08 18:40:58 -0700 | [diff] [blame] | 589 | * @deprecated in API 16 |
Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 590 | * @param rs Context to which the mesh will belong. |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 591 | * @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 Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 598 | public TriangleMeshBuilder(RenderScript rs, int vtxSize, int flags) { |
| 599 | mRS = rs; |
| 600 | mVtxCount = 0; |
Alex Sakhartchouk | e60149d | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 601 | mMaxIndex = 0; |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 602 | 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 Sakhartchouk | e60149d | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 635 | makeSpace(4); |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 636 | mVtxData[mVtxCount++] = mNX; |
| 637 | mVtxData[mVtxCount++] = mNY; |
| 638 | mVtxData[mVtxCount++] = mNZ; |
Alex Sakhartchouk | e60149d | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 639 | mVtxData[mVtxCount++] = 0.0f; |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 640 | } |
Alex Sakhartchouk | e60149d | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 641 | mMaxIndex ++; |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 642 | } |
| 643 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 644 | /** |
Jason Sams | e619de6 | 2012-05-08 18:40:58 -0700 | [diff] [blame] | 645 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 646 | * 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 Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 654 | public TriangleMeshBuilder addVertex(float x, float y) { |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 655 | 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 Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 662 | return this; |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 663 | } |
| 664 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 665 | /** |
Jason Sams | e619de6 | 2012-05-08 18:40:58 -0700 | [diff] [blame] | 666 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 667 | * 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 Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 676 | public TriangleMeshBuilder addVertex(float x, float y, float z) { |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 677 | if (mVtxSize != 3) { |
| 678 | throw new IllegalStateException("add mistmatch with declared components."); |
| 679 | } |
Alex Sakhartchouk | e60149d | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 680 | makeSpace(4); |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 681 | mVtxData[mVtxCount++] = x; |
| 682 | mVtxData[mVtxCount++] = y; |
| 683 | mVtxData[mVtxCount++] = z; |
Alex Sakhartchouk | e60149d | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 684 | mVtxData[mVtxCount++] = 1.0f; |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 685 | latch(); |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 686 | return this; |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 687 | } |
| 688 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 689 | /** |
Jason Sams | e619de6 | 2012-05-08 18:40:58 -0700 | [diff] [blame] | 690 | * @deprecated in API 16 |
Robert Ly | f11ffc11 | 2012-02-22 10:59:12 -0800 | [diff] [blame] | 691 | * Sets the texture coordinate for the vertices that are added after this method call. |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 692 | * |
| 693 | * @param s texture coordinate s |
| 694 | * @param t texture coordinate t |
| 695 | * |
| 696 | * @return this |
| 697 | **/ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 698 | public TriangleMeshBuilder setTexture(float s, float t) { |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 699 | if ((mFlags & TEXTURE_0) == 0) { |
| 700 | throw new IllegalStateException("add mistmatch with declared components."); |
| 701 | } |
| 702 | mS0 = s; |
| 703 | mT0 = t; |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 704 | return this; |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 705 | } |
| 706 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 707 | /** |
Jason Sams | e619de6 | 2012-05-08 18:40:58 -0700 | [diff] [blame] | 708 | * @deprecated in API 16 |
Robert Ly | f11ffc11 | 2012-02-22 10:59:12 -0800 | [diff] [blame] | 709 | * Sets the normal vector for the vertices that are added after this method call. |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 710 | * |
| 711 | * @param x normal vector x |
| 712 | * @param y normal vector y |
| 713 | * @param z normal vector z |
| 714 | * |
| 715 | * @return this |
| 716 | **/ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 717 | public TriangleMeshBuilder setNormal(float x, float y, float z) { |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 718 | if ((mFlags & NORMAL) == 0) { |
| 719 | throw new IllegalStateException("add mistmatch with declared components."); |
| 720 | } |
| 721 | mNX = x; |
| 722 | mNY = y; |
| 723 | mNZ = z; |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 724 | return this; |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 725 | } |
| 726 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 727 | /** |
Jason Sams | e619de6 | 2012-05-08 18:40:58 -0700 | [diff] [blame] | 728 | * @deprecated in API 16 |
Robert Ly | f11ffc11 | 2012-02-22 10:59:12 -0800 | [diff] [blame] | 729 | * Sets the color for the vertices that are added after this method call. |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 730 | * |
| 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 Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 738 | public TriangleMeshBuilder setColor(float r, float g, float b, float a) { |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 739 | 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 Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 746 | return this; |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 747 | } |
| 748 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 749 | /** |
Jason Sams | e619de6 | 2012-05-08 18:40:58 -0700 | [diff] [blame] | 750 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 751 | * 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 Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 759 | public TriangleMeshBuilder addTriangle(int idx1, int idx2, int idx3) { |
Alex Sakhartchouk | e60149d | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 760 | if((idx1 >= mMaxIndex) || (idx1 < 0) || |
| 761 | (idx2 >= mMaxIndex) || (idx2 < 0) || |
| 762 | (idx3 >= mMaxIndex) || (idx3 < 0)) { |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 763 | 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 Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 773 | return this; |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 774 | } |
| 775 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 776 | /** |
Jason Sams | e619de6 | 2012-05-08 18:40:58 -0700 | [diff] [blame] | 777 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 778 | * 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 Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 791 | public Mesh create(boolean uploadToBufferObject) { |
| 792 | Element.Builder b = new Element.Builder(mRS); |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 793 | b.add(Element.createVector(mRS, |
| 794 | Element.DataType.FLOAT_32, |
| 795 | mVtxSize), "position"); |
| 796 | if ((mFlags & COLOR) != 0) { |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 797 | b.add(Element.F32_4(mRS), "color"); |
| 798 | } |
| 799 | if ((mFlags & TEXTURE_0) != 0) { |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 800 | b.add(Element.F32_2(mRS), "texture0"); |
| 801 | } |
| 802 | if ((mFlags & NORMAL) != 0) { |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 803 | b.add(Element.F32_3(mRS), "normal"); |
| 804 | } |
| 805 | mElement = b.create(); |
| 806 | |
Jason Sams | d195240 | 2010-12-20 12:55:28 -0800 | [diff] [blame] | 807 | int usage = Allocation.USAGE_SCRIPT; |
| 808 | if (uploadToBufferObject) { |
| 809 | usage |= Allocation.USAGE_GRAPHICS_VERTEX; |
| 810 | } |
| 811 | |
| 812 | Builder smb = new Builder(mRS, usage); |
Alex Sakhartchouk | e60149d | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 813 | smb.addVertexType(mElement, mMaxIndex); |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 814 | smb.addIndexSetType(Element.U16(mRS), mIndexCount, Primitive.TRIANGLE); |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 815 | |
| 816 | Mesh sm = smb.create(); |
| 817 | |
Alex Sakhartchouk | e60149d | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 818 | sm.getVertexAllocation(0).copy1DRangeFromUnchecked(0, mMaxIndex, mVtxData); |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 819 | if(uploadToBufferObject) { |
Andreas Gampe | 16720c1 | 2015-03-17 19:10:14 -0700 | [diff] [blame] | 820 | sm.getVertexAllocation(0).syncAll(Allocation.USAGE_SCRIPT); |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 821 | } |
| 822 | |
Jason Sams | b97b251 | 2011-01-16 15:04:08 -0800 | [diff] [blame] | 823 | sm.getIndexSetAllocation(0).copy1DRangeFromUnchecked(0, mIndexCount, mIndexData); |
Jason Sams | d195240 | 2010-12-20 12:55:28 -0800 | [diff] [blame] | 824 | if (uploadToBufferObject) { |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 825 | sm.getIndexSetAllocation(0).syncAll(Allocation.USAGE_SCRIPT); |
Jason Sams | d195240 | 2010-12-20 12:55:28 -0800 | [diff] [blame] | 826 | } |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 827 | |
| 828 | return sm; |
| 829 | } |
| 830 | } |
| 831 | } |
| 832 | |