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); |
Yang Ni | eb4dd08 | 2016-03-24 09:40:32 -0700 | [diff] [blame] | 94 | guard.open("destroy"); |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 97 | /** |
Jason Sams | d4ca991 | 2012-05-08 19:02:07 -0700 | [diff] [blame] | 98 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 99 | * @return number of allocations containing vertex data |
| 100 | * |
| 101 | **/ |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 102 | public int getVertexAllocationCount() { |
| 103 | if(mVertexBuffers == null) { |
| 104 | return 0; |
| 105 | } |
| 106 | return mVertexBuffers.length; |
| 107 | } |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 108 | /** |
Jason Sams | d4ca991 | 2012-05-08 19:02:07 -0700 | [diff] [blame] | 109 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 110 | * @param slot index in the list of allocations to return |
| 111 | * @return vertex data allocation at the given index |
| 112 | * |
| 113 | **/ |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 114 | public Allocation getVertexAllocation(int slot) { |
| 115 | return mVertexBuffers[slot]; |
| 116 | } |
| 117 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 118 | /** |
Jason Sams | d4ca991 | 2012-05-08 19:02:07 -0700 | [diff] [blame] | 119 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 120 | * @return number of primitives or index sets in the mesh |
| 121 | * |
| 122 | **/ |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 123 | public int getPrimitiveCount() { |
| 124 | if(mIndexBuffers == null) { |
| 125 | return 0; |
| 126 | } |
| 127 | return mIndexBuffers.length; |
| 128 | } |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 129 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 130 | /** |
Jason Sams | d4ca991 | 2012-05-08 19:02:07 -0700 | [diff] [blame] | 131 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 132 | * @param slot locaton within the list of index set allocation |
| 133 | * @return allocation containing primtive index data or null if |
| 134 | * the index data is not specified explicitly |
| 135 | * |
| 136 | **/ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 137 | public Allocation getIndexSetAllocation(int slot) { |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 138 | return mIndexBuffers[slot]; |
| 139 | } |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 140 | /** |
Jason Sams | d4ca991 | 2012-05-08 19:02:07 -0700 | [diff] [blame] | 141 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 142 | * @param slot locaiton within the list of index set primitives |
| 143 | * @return index set primitive type |
| 144 | * |
| 145 | **/ |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 146 | public Primitive getPrimitive(int slot) { |
| 147 | return mPrimitives[slot]; |
| 148 | } |
| 149 | |
Alex Sakhartchouk | 80a4c2c | 2010-07-12 15:50:32 -0700 | [diff] [blame] | 150 | @Override |
| 151 | void updateFromNative() { |
Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 152 | super.updateFromNative(); |
Jason Sams | e07694b | 2012-04-03 15:36:36 -0700 | [diff] [blame] | 153 | int vtxCount = mRS.nMeshGetVertexBufferCount(getID(mRS)); |
| 154 | int idxCount = mRS.nMeshGetIndexCount(getID(mRS)); |
Alex Sakhartchouk | 80a4c2c | 2010-07-12 15:50:32 -0700 | [diff] [blame] | 155 | |
Ashok Bhat | 9807155 | 2014-02-12 09:54:43 +0000 | [diff] [blame] | 156 | long[] vtxIDs = new long[vtxCount]; |
| 157 | long[] idxIDs = new long[idxCount]; |
Alex Sakhartchouk | 80a4c2c | 2010-07-12 15:50:32 -0700 | [diff] [blame] | 158 | int[] primitives = new int[idxCount]; |
| 159 | |
Jason Sams | e07694b | 2012-04-03 15:36:36 -0700 | [diff] [blame] | 160 | mRS.nMeshGetVertices(getID(mRS), vtxIDs, vtxCount); |
| 161 | mRS.nMeshGetIndices(getID(mRS), idxIDs, primitives, idxCount); |
Alex Sakhartchouk | 80a4c2c | 2010-07-12 15:50:32 -0700 | [diff] [blame] | 162 | |
| 163 | mVertexBuffers = new Allocation[vtxCount]; |
| 164 | mIndexBuffers = new Allocation[idxCount]; |
| 165 | mPrimitives = new Primitive[idxCount]; |
| 166 | |
| 167 | for(int i = 0; i < vtxCount; i ++) { |
| 168 | if(vtxIDs[i] != 0) { |
Jason Sams | d4b23b5 | 2010-12-13 15:32:35 -0800 | [diff] [blame] | 169 | mVertexBuffers[i] = new Allocation(vtxIDs[i], mRS, null, Allocation.USAGE_SCRIPT); |
Alex Sakhartchouk | 80a4c2c | 2010-07-12 15:50:32 -0700 | [diff] [blame] | 170 | mVertexBuffers[i].updateFromNative(); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | for(int i = 0; i < idxCount; i ++) { |
| 175 | if(idxIDs[i] != 0) { |
Jason Sams | d4b23b5 | 2010-12-13 15:32:35 -0800 | [diff] [blame] | 176 | mIndexBuffers[i] = new Allocation(idxIDs[i], mRS, null, Allocation.USAGE_SCRIPT); |
Alex Sakhartchouk | 80a4c2c | 2010-07-12 15:50:32 -0700 | [diff] [blame] | 177 | mIndexBuffers[i].updateFromNative(); |
| 178 | } |
| 179 | mPrimitives[i] = Primitive.values()[primitives[i]]; |
| 180 | } |
| 181 | } |
| 182 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 183 | /** |
Jason Sams | d4ca991 | 2012-05-08 19:02:07 -0700 | [diff] [blame] | 184 | * @deprecated in API 16 |
Robert Ly | 11518ac | 2011-02-09 13:57:06 -0800 | [diff] [blame] | 185 | * Mesh builder object. It starts empty and requires you to |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 186 | * add the types necessary to create vertex and index |
Robert Ly | 11518ac | 2011-02-09 13:57:06 -0800 | [diff] [blame] | 187 | * allocations. |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 188 | * |
| 189 | */ |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 190 | public static class Builder { |
| 191 | RenderScript mRS; |
Jason Sams | d195240 | 2010-12-20 12:55:28 -0800 | [diff] [blame] | 192 | int mUsage; |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 193 | |
| 194 | class Entry { |
| 195 | Type t; |
| 196 | Element e; |
| 197 | int size; |
| 198 | Primitive prim; |
Jason Sams | d195240 | 2010-12-20 12:55:28 -0800 | [diff] [blame] | 199 | int usage; |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | int mVertexTypeCount; |
| 203 | Entry[] mVertexTypes; |
| 204 | Vector mIndexTypes; |
| 205 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 206 | /** |
Jason Sams | d4ca991 | 2012-05-08 19:02:07 -0700 | [diff] [blame] | 207 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 208 | * Creates builder object |
Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 209 | * @param rs Context to which the mesh will belong. |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 210 | * @param usage specifies how the mesh allocations are to be |
| 211 | * handled, whether they need to be uploaded to a |
| 212 | * buffer on the gpu, maintain a cpu copy, etc |
| 213 | */ |
Jason Sams | d195240 | 2010-12-20 12:55:28 -0800 | [diff] [blame] | 214 | public Builder(RenderScript rs, int usage) { |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 215 | mRS = rs; |
Jason Sams | d195240 | 2010-12-20 12:55:28 -0800 | [diff] [blame] | 216 | mUsage = usage; |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 217 | mVertexTypeCount = 0; |
| 218 | mVertexTypes = new Entry[16]; |
| 219 | mIndexTypes = new Vector(); |
| 220 | } |
| 221 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 222 | /** |
Jason Sams | d4ca991 | 2012-05-08 19:02:07 -0700 | [diff] [blame] | 223 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 224 | * @return internal index of the last vertex buffer type added to |
| 225 | * builder |
| 226 | **/ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 227 | public int getCurrentVertexTypeIndex() { |
| 228 | return mVertexTypeCount - 1; |
| 229 | } |
| 230 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 231 | /** |
Jason Sams | d4ca991 | 2012-05-08 19:02:07 -0700 | [diff] [blame] | 232 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 233 | * @return internal index of the last index set added to the |
| 234 | * builder |
| 235 | **/ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 236 | public int getCurrentIndexSetIndex() { |
| 237 | return mIndexTypes.size() - 1; |
| 238 | } |
| 239 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 240 | /** |
Jason Sams | d4ca991 | 2012-05-08 19:02:07 -0700 | [diff] [blame] | 241 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 242 | * Adds a vertex data type to the builder object |
| 243 | * |
Stephen Hines | b11e3d2 | 2011-01-11 19:30:58 -0800 | [diff] [blame] | 244 | * @param t type of the vertex data allocation to be created |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 245 | * |
| 246 | * @return this |
| 247 | **/ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 248 | public Builder addVertexType(Type t) throws IllegalStateException { |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 249 | if (mVertexTypeCount >= mVertexTypes.length) { |
| 250 | throw new IllegalStateException("Max vertex types exceeded."); |
| 251 | } |
| 252 | |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 253 | mVertexTypes[mVertexTypeCount] = new Entry(); |
| 254 | mVertexTypes[mVertexTypeCount].t = t; |
| 255 | mVertexTypes[mVertexTypeCount].e = null; |
| 256 | mVertexTypeCount++; |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 257 | return this; |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 258 | } |
| 259 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 260 | /** |
Jason Sams | d4ca991 | 2012-05-08 19:02:07 -0700 | [diff] [blame] | 261 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 262 | * Adds a vertex data type to the builder object |
| 263 | * |
| 264 | * @param e element describing the vertex data layout |
| 265 | * @param size number of elements in the buffer |
| 266 | * |
| 267 | * @return this |
| 268 | **/ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 269 | public Builder addVertexType(Element e, int size) throws IllegalStateException { |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 270 | if (mVertexTypeCount >= mVertexTypes.length) { |
| 271 | throw new IllegalStateException("Max vertex types exceeded."); |
| 272 | } |
| 273 | |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 274 | mVertexTypes[mVertexTypeCount] = new Entry(); |
| 275 | mVertexTypes[mVertexTypeCount].t = null; |
| 276 | mVertexTypes[mVertexTypeCount].e = e; |
| 277 | mVertexTypes[mVertexTypeCount].size = size; |
| 278 | mVertexTypeCount++; |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 279 | return this; |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 280 | } |
| 281 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 282 | /** |
Jason Sams | d4ca991 | 2012-05-08 19:02:07 -0700 | [diff] [blame] | 283 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 284 | * Adds an index set data type to the builder object |
| 285 | * |
| 286 | * @param t type of the index set data, could be null |
| 287 | * @param p primitive type |
| 288 | * |
| 289 | * @return this |
| 290 | **/ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 291 | public Builder addIndexSetType(Type t, Primitive p) { |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 292 | Entry indexType = new Entry(); |
| 293 | indexType.t = t; |
| 294 | indexType.e = null; |
| 295 | indexType.size = 0; |
| 296 | indexType.prim = p; |
| 297 | mIndexTypes.addElement(indexType); |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 298 | return this; |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 299 | } |
| 300 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 301 | /** |
Jason Sams | d4ca991 | 2012-05-08 19:02:07 -0700 | [diff] [blame] | 302 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 303 | * Adds an index set primitive type to the builder object |
| 304 | * |
| 305 | * @param p primitive type |
| 306 | * |
| 307 | * @return this |
| 308 | **/ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 309 | public Builder addIndexSetType(Primitive p) { |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 310 | Entry indexType = new Entry(); |
| 311 | indexType.t = null; |
| 312 | indexType.e = null; |
| 313 | indexType.size = 0; |
| 314 | indexType.prim = p; |
| 315 | mIndexTypes.addElement(indexType); |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 316 | return this; |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 317 | } |
| 318 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 319 | /** |
Jason Sams | d4ca991 | 2012-05-08 19:02:07 -0700 | [diff] [blame] | 320 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 321 | * Adds an index set data type to the builder object |
| 322 | * |
| 323 | * @param e element describing the index set data layout |
| 324 | * @param size number of elements in the buffer |
| 325 | * @param p primitive type |
| 326 | * |
| 327 | * @return this |
| 328 | **/ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 329 | public Builder addIndexSetType(Element e, int size, Primitive p) { |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 330 | Entry indexType = new Entry(); |
| 331 | indexType.t = null; |
| 332 | indexType.e = e; |
| 333 | indexType.size = size; |
| 334 | indexType.prim = p; |
| 335 | mIndexTypes.addElement(indexType); |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 336 | return this; |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | Type newType(Element e, int size) { |
| 340 | Type.Builder tb = new Type.Builder(mRS, e); |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 341 | tb.setX(size); |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 342 | return tb.create(); |
| 343 | } |
| 344 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 345 | /** |
Jason Sams | d4ca991 | 2012-05-08 19:02:07 -0700 | [diff] [blame] | 346 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 347 | * Create a Mesh object from the current state of the builder |
| 348 | * |
| 349 | **/ |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 350 | public Mesh create() { |
| 351 | mRS.validate(); |
Ashok Bhat | 9807155 | 2014-02-12 09:54:43 +0000 | [diff] [blame] | 352 | long[] vtx = new long[mVertexTypeCount]; |
| 353 | long[] idx = new long[mIndexTypes.size()]; |
Alex Sakhartchouk | 25999a0 | 2011-05-12 10:38:03 -0700 | [diff] [blame] | 354 | int[] prim = new int[mIndexTypes.size()]; |
| 355 | |
| 356 | Allocation[] vertexBuffers = new Allocation[mVertexTypeCount]; |
| 357 | Allocation[] indexBuffers = new Allocation[mIndexTypes.size()]; |
| 358 | Primitive[] primitives = new Primitive[mIndexTypes.size()]; |
| 359 | |
| 360 | for(int ct = 0; ct < mVertexTypeCount; ct ++) { |
| 361 | Allocation alloc = null; |
| 362 | Entry entry = mVertexTypes[ct]; |
| 363 | if (entry.t != null) { |
| 364 | alloc = Allocation.createTyped(mRS, entry.t, mUsage); |
| 365 | } else if(entry.e != null) { |
| 366 | alloc = Allocation.createSized(mRS, entry.e, entry.size, mUsage); |
Jason Sams | ae5be38 | 2015-03-26 14:47:17 -0700 | [diff] [blame] | 367 | } else { |
| 368 | // Should never happen because the builder will always set one |
| 369 | throw new IllegalStateException("Builder corrupt, no valid element in entry."); |
Alex Sakhartchouk | 25999a0 | 2011-05-12 10:38:03 -0700 | [diff] [blame] | 370 | } |
| 371 | vertexBuffers[ct] = alloc; |
Ashok Bhat | 9807155 | 2014-02-12 09:54:43 +0000 | [diff] [blame] | 372 | vtx[ct] = alloc.getID(mRS); |
Alex Sakhartchouk | 25999a0 | 2011-05-12 10:38:03 -0700 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | for(int ct = 0; ct < mIndexTypes.size(); ct ++) { |
| 376 | Allocation alloc = null; |
| 377 | Entry entry = (Entry)mIndexTypes.elementAt(ct); |
| 378 | if (entry.t != null) { |
| 379 | alloc = Allocation.createTyped(mRS, entry.t, mUsage); |
| 380 | } else if(entry.e != null) { |
| 381 | alloc = Allocation.createSized(mRS, entry.e, entry.size, mUsage); |
Jason Sams | ae5be38 | 2015-03-26 14:47:17 -0700 | [diff] [blame] | 382 | } else { |
| 383 | // Should never happen because the builder will always set one |
| 384 | throw new IllegalStateException("Builder corrupt, no valid element in entry."); |
Alex Sakhartchouk | 25999a0 | 2011-05-12 10:38:03 -0700 | [diff] [blame] | 385 | } |
Tim Murray | 460a049 | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 386 | long allocID = (alloc == null) ? 0 : alloc.getID(mRS); |
Alex Sakhartchouk | 25999a0 | 2011-05-12 10:38:03 -0700 | [diff] [blame] | 387 | indexBuffers[ct] = alloc; |
| 388 | primitives[ct] = entry.prim; |
| 389 | |
Ashok Bhat | 9807155 | 2014-02-12 09:54:43 +0000 | [diff] [blame] | 390 | idx[ct] = allocID; |
Alex Sakhartchouk | 25999a0 | 2011-05-12 10:38:03 -0700 | [diff] [blame] | 391 | prim[ct] = entry.prim.mID; |
| 392 | } |
| 393 | |
Tim Murray | 460a049 | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 394 | long id = mRS.nMeshCreate(vtx, idx, prim); |
Alex Sakhartchouk | 25999a0 | 2011-05-12 10:38:03 -0700 | [diff] [blame] | 395 | Mesh newMesh = new Mesh(id, mRS); |
| 396 | newMesh.mVertexBuffers = vertexBuffers; |
| 397 | newMesh.mIndexBuffers = indexBuffers; |
| 398 | newMesh.mPrimitives = primitives; |
| 399 | |
| 400 | return newMesh; |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 401 | } |
| 402 | } |
| 403 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 404 | /** |
Jason Sams | e619de6 | 2012-05-08 18:40:58 -0700 | [diff] [blame] | 405 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 406 | * Mesh builder object. It starts empty and requires the user to |
| 407 | * add all the vertex and index allocations that comprise the |
| 408 | * mesh |
| 409 | * |
| 410 | */ |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 411 | public static class AllocationBuilder { |
| 412 | RenderScript mRS; |
| 413 | |
| 414 | class Entry { |
| 415 | Allocation a; |
| 416 | Primitive prim; |
| 417 | } |
| 418 | |
| 419 | int mVertexTypeCount; |
| 420 | Entry[] mVertexTypes; |
| 421 | |
| 422 | Vector mIndexTypes; |
| 423 | |
Jason Sams | e619de6 | 2012-05-08 18:40:58 -0700 | [diff] [blame] | 424 | /** |
| 425 | * @deprecated in API 16 |
| 426 | **/ |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 427 | public AllocationBuilder(RenderScript rs) { |
| 428 | mRS = rs; |
| 429 | mVertexTypeCount = 0; |
| 430 | mVertexTypes = new Entry[16]; |
| 431 | mIndexTypes = new Vector(); |
| 432 | } |
| 433 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 434 | /** |
Jason Sams | e619de6 | 2012-05-08 18:40:58 -0700 | [diff] [blame] | 435 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 436 | * @return internal index of the last vertex buffer type added to |
| 437 | * builder |
| 438 | **/ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 439 | public int getCurrentVertexTypeIndex() { |
| 440 | return mVertexTypeCount - 1; |
| 441 | } |
| 442 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 443 | /** |
Jason Sams | e619de6 | 2012-05-08 18:40:58 -0700 | [diff] [blame] | 444 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 445 | * @return internal index of the last index set added to the |
| 446 | * builder |
| 447 | **/ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 448 | public int getCurrentIndexSetIndex() { |
| 449 | return mIndexTypes.size() - 1; |
| 450 | } |
| 451 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 452 | /** |
Jason Sams | e619de6 | 2012-05-08 18:40:58 -0700 | [diff] [blame] | 453 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 454 | * Adds an allocation containing vertex buffer data to the |
| 455 | * builder |
| 456 | * |
| 457 | * @param a vertex data allocation |
| 458 | * |
| 459 | * @return this |
| 460 | **/ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 461 | public AllocationBuilder addVertexAllocation(Allocation a) throws IllegalStateException { |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 462 | if (mVertexTypeCount >= mVertexTypes.length) { |
| 463 | throw new IllegalStateException("Max vertex types exceeded."); |
| 464 | } |
| 465 | |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 466 | mVertexTypes[mVertexTypeCount] = new Entry(); |
| 467 | mVertexTypes[mVertexTypeCount].a = a; |
| 468 | mVertexTypeCount++; |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 469 | return this; |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 470 | } |
| 471 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 472 | /** |
Jason Sams | e619de6 | 2012-05-08 18:40:58 -0700 | [diff] [blame] | 473 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 474 | * Adds an allocation containing index buffer data and index type |
| 475 | * to the builder |
| 476 | * |
| 477 | * @param a index set data allocation, could be null |
| 478 | * @param p index set primitive type |
| 479 | * |
| 480 | * @return this |
| 481 | **/ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 482 | public AllocationBuilder addIndexSetAllocation(Allocation a, Primitive p) { |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 483 | Entry indexType = new Entry(); |
| 484 | indexType.a = a; |
| 485 | indexType.prim = p; |
| 486 | mIndexTypes.addElement(indexType); |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 487 | return this; |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 488 | } |
| 489 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 490 | /** |
Jason Sams | e619de6 | 2012-05-08 18:40:58 -0700 | [diff] [blame] | 491 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 492 | * Adds an index set type to the builder |
| 493 | * |
| 494 | * @param p index set primitive type |
| 495 | * |
| 496 | * @return this |
| 497 | **/ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 498 | public AllocationBuilder addIndexSetType(Primitive p) { |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 499 | Entry indexType = new Entry(); |
| 500 | indexType.a = null; |
| 501 | indexType.prim = p; |
| 502 | mIndexTypes.addElement(indexType); |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 503 | return this; |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 504 | } |
| 505 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 506 | /** |
Jason Sams | e619de6 | 2012-05-08 18:40:58 -0700 | [diff] [blame] | 507 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 508 | * Create a Mesh object from the current state of the builder |
| 509 | * |
| 510 | **/ |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 511 | public Mesh create() { |
| 512 | mRS.validate(); |
Alex Sakhartchouk | 25999a0 | 2011-05-12 10:38:03 -0700 | [diff] [blame] | 513 | |
Ashok Bhat | 9807155 | 2014-02-12 09:54:43 +0000 | [diff] [blame] | 514 | long[] vtx = new long[mVertexTypeCount]; |
| 515 | long[] idx = new long[mIndexTypes.size()]; |
Alex Sakhartchouk | 25999a0 | 2011-05-12 10:38:03 -0700 | [diff] [blame] | 516 | int[] prim = new int[mIndexTypes.size()]; |
| 517 | |
| 518 | Allocation[] indexBuffers = new Allocation[mIndexTypes.size()]; |
| 519 | Primitive[] primitives = new Primitive[mIndexTypes.size()]; |
| 520 | Allocation[] vertexBuffers = new Allocation[mVertexTypeCount]; |
| 521 | |
| 522 | for(int ct = 0; ct < mVertexTypeCount; ct ++) { |
| 523 | Entry entry = mVertexTypes[ct]; |
| 524 | vertexBuffers[ct] = entry.a; |
Ashok Bhat | 9807155 | 2014-02-12 09:54:43 +0000 | [diff] [blame] | 525 | vtx[ct] = entry.a.getID(mRS); |
Alex Sakhartchouk | 25999a0 | 2011-05-12 10:38:03 -0700 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | for(int ct = 0; ct < mIndexTypes.size(); ct ++) { |
| 529 | Entry entry = (Entry)mIndexTypes.elementAt(ct); |
Tim Murray | 460a049 | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 530 | long allocID = (entry.a == null) ? 0 : entry.a.getID(mRS); |
Alex Sakhartchouk | 25999a0 | 2011-05-12 10:38:03 -0700 | [diff] [blame] | 531 | indexBuffers[ct] = entry.a; |
| 532 | primitives[ct] = entry.prim; |
| 533 | |
Ashok Bhat | 9807155 | 2014-02-12 09:54:43 +0000 | [diff] [blame] | 534 | idx[ct] = allocID; |
Alex Sakhartchouk | 25999a0 | 2011-05-12 10:38:03 -0700 | [diff] [blame] | 535 | prim[ct] = entry.prim.mID; |
| 536 | } |
| 537 | |
Tim Murray | 460a049 | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 538 | long id = mRS.nMeshCreate(vtx, idx, prim); |
Alex Sakhartchouk | 25999a0 | 2011-05-12 10:38:03 -0700 | [diff] [blame] | 539 | Mesh newMesh = new Mesh(id, mRS); |
| 540 | newMesh.mVertexBuffers = vertexBuffers; |
| 541 | newMesh.mIndexBuffers = indexBuffers; |
| 542 | newMesh.mPrimitives = primitives; |
| 543 | |
| 544 | return newMesh; |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 545 | } |
| 546 | } |
| 547 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 548 | /** |
Jason Sams | e619de6 | 2012-05-08 18:40:58 -0700 | [diff] [blame] | 549 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 550 | * Builder that allows creation of a mesh object point by point |
| 551 | * and triangle by triangle |
| 552 | * |
| 553 | **/ |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 554 | public static class TriangleMeshBuilder { |
| 555 | float mVtxData[]; |
| 556 | int mVtxCount; |
Alex Sakhartchouk | e60149d | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 557 | int mMaxIndex; |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 558 | short mIndexData[]; |
| 559 | int mIndexCount; |
| 560 | RenderScript mRS; |
| 561 | Element mElement; |
| 562 | |
| 563 | float mNX = 0; |
| 564 | float mNY = 0; |
| 565 | float mNZ = -1; |
| 566 | float mS0 = 0; |
| 567 | float mT0 = 0; |
| 568 | float mR = 1; |
| 569 | float mG = 1; |
| 570 | float mB = 1; |
| 571 | float mA = 1; |
| 572 | |
| 573 | int mVtxSize; |
| 574 | int mFlags; |
| 575 | |
Jason Sams | e619de6 | 2012-05-08 18:40:58 -0700 | [diff] [blame] | 576 | /** |
| 577 | * @deprecated in API 16 |
| 578 | **/ |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 579 | public static final int COLOR = 0x0001; |
Jason Sams | e619de6 | 2012-05-08 18:40:58 -0700 | [diff] [blame] | 580 | /** |
| 581 | * @deprecated in API 16 |
| 582 | **/ |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 583 | public static final int NORMAL = 0x0002; |
Jason Sams | e619de6 | 2012-05-08 18:40:58 -0700 | [diff] [blame] | 584 | /** |
| 585 | * @deprecated in API 16 |
| 586 | **/ |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 587 | public static final int TEXTURE_0 = 0x0100; |
| 588 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 589 | /** |
Jason Sams | e619de6 | 2012-05-08 18:40:58 -0700 | [diff] [blame] | 590 | * @deprecated in API 16 |
Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 591 | * @param rs Context to which the mesh will belong. |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 592 | * @param vtxSize specifies whether the vertex is a float2 or |
| 593 | * float3 |
| 594 | * @param flags bitfield that is a combination of COLOR, NORMAL, |
| 595 | * and TEXTURE_0 that specifies what vertex data |
| 596 | * channels are present in the mesh |
| 597 | * |
| 598 | **/ |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 599 | public TriangleMeshBuilder(RenderScript rs, int vtxSize, int flags) { |
| 600 | mRS = rs; |
| 601 | mVtxCount = 0; |
Alex Sakhartchouk | e60149d | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 602 | mMaxIndex = 0; |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 603 | mIndexCount = 0; |
| 604 | mVtxData = new float[128]; |
| 605 | mIndexData = new short[128]; |
| 606 | mVtxSize = vtxSize; |
| 607 | mFlags = flags; |
| 608 | |
| 609 | if (vtxSize < 2 || vtxSize > 3) { |
| 610 | throw new IllegalArgumentException("Vertex size out of range."); |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | private void makeSpace(int count) { |
| 615 | if ((mVtxCount + count) >= mVtxData.length) { |
| 616 | float t[] = new float[mVtxData.length * 2]; |
| 617 | System.arraycopy(mVtxData, 0, t, 0, mVtxData.length); |
| 618 | mVtxData = t; |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | private void latch() { |
| 623 | if ((mFlags & COLOR) != 0) { |
| 624 | makeSpace(4); |
| 625 | mVtxData[mVtxCount++] = mR; |
| 626 | mVtxData[mVtxCount++] = mG; |
| 627 | mVtxData[mVtxCount++] = mB; |
| 628 | mVtxData[mVtxCount++] = mA; |
| 629 | } |
| 630 | if ((mFlags & TEXTURE_0) != 0) { |
| 631 | makeSpace(2); |
| 632 | mVtxData[mVtxCount++] = mS0; |
| 633 | mVtxData[mVtxCount++] = mT0; |
| 634 | } |
| 635 | if ((mFlags & NORMAL) != 0) { |
Alex Sakhartchouk | e60149d | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 636 | makeSpace(4); |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 637 | mVtxData[mVtxCount++] = mNX; |
| 638 | mVtxData[mVtxCount++] = mNY; |
| 639 | mVtxData[mVtxCount++] = mNZ; |
Alex Sakhartchouk | e60149d | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 640 | mVtxData[mVtxCount++] = 0.0f; |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 641 | } |
Alex Sakhartchouk | e60149d | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 642 | mMaxIndex ++; |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 643 | } |
| 644 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 645 | /** |
Jason Sams | e619de6 | 2012-05-08 18:40:58 -0700 | [diff] [blame] | 646 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 647 | * Adds a float2 vertex to the mesh |
| 648 | * |
| 649 | * @param x position x |
| 650 | * @param y position y |
| 651 | * |
| 652 | * @return this |
| 653 | * |
| 654 | **/ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 655 | public TriangleMeshBuilder addVertex(float x, float y) { |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 656 | if (mVtxSize != 2) { |
| 657 | throw new IllegalStateException("add mistmatch with declared components."); |
| 658 | } |
| 659 | makeSpace(2); |
| 660 | mVtxData[mVtxCount++] = x; |
| 661 | mVtxData[mVtxCount++] = y; |
| 662 | latch(); |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 663 | return this; |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 664 | } |
| 665 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 666 | /** |
Jason Sams | e619de6 | 2012-05-08 18:40:58 -0700 | [diff] [blame] | 667 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 668 | * Adds a float3 vertex to the mesh |
| 669 | * |
| 670 | * @param x position x |
| 671 | * @param y position y |
| 672 | * @param z position z |
| 673 | * |
| 674 | * @return this |
| 675 | * |
| 676 | **/ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 677 | public TriangleMeshBuilder addVertex(float x, float y, float z) { |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 678 | if (mVtxSize != 3) { |
| 679 | throw new IllegalStateException("add mistmatch with declared components."); |
| 680 | } |
Alex Sakhartchouk | e60149d | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 681 | makeSpace(4); |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 682 | mVtxData[mVtxCount++] = x; |
| 683 | mVtxData[mVtxCount++] = y; |
| 684 | mVtxData[mVtxCount++] = z; |
Alex Sakhartchouk | e60149d | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 685 | mVtxData[mVtxCount++] = 1.0f; |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 686 | latch(); |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 687 | return this; |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 688 | } |
| 689 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 690 | /** |
Jason Sams | e619de6 | 2012-05-08 18:40:58 -0700 | [diff] [blame] | 691 | * @deprecated in API 16 |
Robert Ly | f11ffc11 | 2012-02-22 10:59:12 -0800 | [diff] [blame] | 692 | * 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] | 693 | * |
| 694 | * @param s texture coordinate s |
| 695 | * @param t texture coordinate t |
| 696 | * |
| 697 | * @return this |
| 698 | **/ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 699 | public TriangleMeshBuilder setTexture(float s, float t) { |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 700 | if ((mFlags & TEXTURE_0) == 0) { |
| 701 | throw new IllegalStateException("add mistmatch with declared components."); |
| 702 | } |
| 703 | mS0 = s; |
| 704 | mT0 = t; |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 705 | return this; |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 706 | } |
| 707 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 708 | /** |
Jason Sams | e619de6 | 2012-05-08 18:40:58 -0700 | [diff] [blame] | 709 | * @deprecated in API 16 |
Robert Ly | f11ffc11 | 2012-02-22 10:59:12 -0800 | [diff] [blame] | 710 | * 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] | 711 | * |
| 712 | * @param x normal vector x |
| 713 | * @param y normal vector y |
| 714 | * @param z normal vector z |
| 715 | * |
| 716 | * @return this |
| 717 | **/ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 718 | public TriangleMeshBuilder setNormal(float x, float y, float z) { |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 719 | if ((mFlags & NORMAL) == 0) { |
| 720 | throw new IllegalStateException("add mistmatch with declared components."); |
| 721 | } |
| 722 | mNX = x; |
| 723 | mNY = y; |
| 724 | mNZ = z; |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 725 | return this; |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 726 | } |
| 727 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 728 | /** |
Jason Sams | e619de6 | 2012-05-08 18:40:58 -0700 | [diff] [blame] | 729 | * @deprecated in API 16 |
Robert Ly | f11ffc11 | 2012-02-22 10:59:12 -0800 | [diff] [blame] | 730 | * 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] | 731 | * |
| 732 | * @param r red component |
| 733 | * @param g green component |
| 734 | * @param b blue component |
| 735 | * @param a alpha component |
| 736 | * |
| 737 | * @return this |
| 738 | **/ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 739 | public TriangleMeshBuilder setColor(float r, float g, float b, float a) { |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 740 | if ((mFlags & COLOR) == 0) { |
| 741 | throw new IllegalStateException("add mistmatch with declared components."); |
| 742 | } |
| 743 | mR = r; |
| 744 | mG = g; |
| 745 | mB = b; |
| 746 | mA = a; |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 747 | return this; |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 748 | } |
| 749 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 750 | /** |
Jason Sams | e619de6 | 2012-05-08 18:40:58 -0700 | [diff] [blame] | 751 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 752 | * Adds a new triangle to the mesh builder |
| 753 | * |
| 754 | * @param idx1 index of the first vertex in the triangle |
| 755 | * @param idx2 index of the second vertex in the triangle |
| 756 | * @param idx3 index of the third vertex in the triangle |
| 757 | * |
| 758 | * @return this |
| 759 | **/ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 760 | public TriangleMeshBuilder addTriangle(int idx1, int idx2, int idx3) { |
Alex Sakhartchouk | e60149d | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 761 | if((idx1 >= mMaxIndex) || (idx1 < 0) || |
| 762 | (idx2 >= mMaxIndex) || (idx2 < 0) || |
| 763 | (idx3 >= mMaxIndex) || (idx3 < 0)) { |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 764 | throw new IllegalStateException("Index provided greater than vertex count."); |
| 765 | } |
| 766 | if ((mIndexCount + 3) >= mIndexData.length) { |
| 767 | short t[] = new short[mIndexData.length * 2]; |
| 768 | System.arraycopy(mIndexData, 0, t, 0, mIndexData.length); |
| 769 | mIndexData = t; |
| 770 | } |
| 771 | mIndexData[mIndexCount++] = (short)idx1; |
| 772 | mIndexData[mIndexCount++] = (short)idx2; |
| 773 | mIndexData[mIndexCount++] = (short)idx3; |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 774 | return this; |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 775 | } |
| 776 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 777 | /** |
Jason Sams | e619de6 | 2012-05-08 18:40:58 -0700 | [diff] [blame] | 778 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 779 | * Creates the mesh object from the current state of the builder |
| 780 | * |
| 781 | * @param uploadToBufferObject specifies whether the vertex data |
| 782 | * is to be uploaded into the buffer |
| 783 | * object indicating that it's likely |
| 784 | * not going to be modified and |
| 785 | * rendered many times. |
| 786 | * Alternatively, it indicates the |
| 787 | * mesh data will be updated |
| 788 | * frequently and remain in script |
| 789 | * accessible memory |
| 790 | * |
| 791 | **/ |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 792 | public Mesh create(boolean uploadToBufferObject) { |
| 793 | Element.Builder b = new Element.Builder(mRS); |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 794 | b.add(Element.createVector(mRS, |
| 795 | Element.DataType.FLOAT_32, |
| 796 | mVtxSize), "position"); |
| 797 | if ((mFlags & COLOR) != 0) { |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 798 | b.add(Element.F32_4(mRS), "color"); |
| 799 | } |
| 800 | if ((mFlags & TEXTURE_0) != 0) { |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 801 | b.add(Element.F32_2(mRS), "texture0"); |
| 802 | } |
| 803 | if ((mFlags & NORMAL) != 0) { |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 804 | b.add(Element.F32_3(mRS), "normal"); |
| 805 | } |
| 806 | mElement = b.create(); |
| 807 | |
Jason Sams | d195240 | 2010-12-20 12:55:28 -0800 | [diff] [blame] | 808 | int usage = Allocation.USAGE_SCRIPT; |
| 809 | if (uploadToBufferObject) { |
| 810 | usage |= Allocation.USAGE_GRAPHICS_VERTEX; |
| 811 | } |
| 812 | |
| 813 | Builder smb = new Builder(mRS, usage); |
Alex Sakhartchouk | e60149d | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 814 | smb.addVertexType(mElement, mMaxIndex); |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 815 | smb.addIndexSetType(Element.U16(mRS), mIndexCount, Primitive.TRIANGLE); |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 816 | |
| 817 | Mesh sm = smb.create(); |
| 818 | |
Alex Sakhartchouk | e60149d | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 819 | sm.getVertexAllocation(0).copy1DRangeFromUnchecked(0, mMaxIndex, mVtxData); |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 820 | if(uploadToBufferObject) { |
Andreas Gampe | 16720c1 | 2015-03-17 19:10:14 -0700 | [diff] [blame] | 821 | sm.getVertexAllocation(0).syncAll(Allocation.USAGE_SCRIPT); |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 822 | } |
| 823 | |
Jason Sams | b97b251 | 2011-01-16 15:04:08 -0800 | [diff] [blame] | 824 | sm.getIndexSetAllocation(0).copy1DRangeFromUnchecked(0, mIndexCount, mIndexData); |
Jason Sams | d195240 | 2010-12-20 12:55:28 -0800 | [diff] [blame] | 825 | if (uploadToBufferObject) { |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 826 | sm.getIndexSetAllocation(0).syncAll(Allocation.USAGE_SCRIPT); |
Jason Sams | d195240 | 2010-12-20 12:55:28 -0800 | [diff] [blame] | 827 | } |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 828 | |
| 829 | return sm; |
| 830 | } |
| 831 | } |
| 832 | } |
| 833 | |