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 | |
| 155 | int[] vtxIDs = new int[vtxCount]; |
| 156 | int[] idxIDs = new int[idxCount]; |
| 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(); |
Alex Sakhartchouk | 25999a0 | 2011-05-12 10:38:03 -0700 | [diff] [blame] | 351 | int[] vtx = new int[mVertexTypeCount]; |
| 352 | int[] idx = new int[mIndexTypes.size()]; |
| 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); |
| 366 | } |
| 367 | vertexBuffers[ct] = alloc; |
Tim Murray | 460a049 | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 368 | vtx[ct] = (int)alloc.getID(mRS); |
Alex Sakhartchouk | 25999a0 | 2011-05-12 10:38:03 -0700 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | for(int ct = 0; ct < mIndexTypes.size(); ct ++) { |
| 372 | Allocation alloc = null; |
| 373 | Entry entry = (Entry)mIndexTypes.elementAt(ct); |
| 374 | if (entry.t != null) { |
| 375 | alloc = Allocation.createTyped(mRS, entry.t, mUsage); |
| 376 | } else if(entry.e != null) { |
| 377 | alloc = Allocation.createSized(mRS, entry.e, entry.size, mUsage); |
| 378 | } |
Tim Murray | 460a049 | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 379 | long allocID = (alloc == null) ? 0 : alloc.getID(mRS); |
Alex Sakhartchouk | 25999a0 | 2011-05-12 10:38:03 -0700 | [diff] [blame] | 380 | indexBuffers[ct] = alloc; |
| 381 | primitives[ct] = entry.prim; |
| 382 | |
Tim Murray | 460a049 | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 383 | idx[ct] = (int)allocID; |
Alex Sakhartchouk | 25999a0 | 2011-05-12 10:38:03 -0700 | [diff] [blame] | 384 | prim[ct] = entry.prim.mID; |
| 385 | } |
| 386 | |
Tim Murray | 460a049 | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 387 | long id = mRS.nMeshCreate(vtx, idx, prim); |
Alex Sakhartchouk | 25999a0 | 2011-05-12 10:38:03 -0700 | [diff] [blame] | 388 | Mesh newMesh = new Mesh(id, mRS); |
| 389 | newMesh.mVertexBuffers = vertexBuffers; |
| 390 | newMesh.mIndexBuffers = indexBuffers; |
| 391 | newMesh.mPrimitives = primitives; |
| 392 | |
| 393 | return newMesh; |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 394 | } |
| 395 | } |
| 396 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 397 | /** |
Jason Sams | e619de6 | 2012-05-08 18:40:58 -0700 | [diff] [blame] | 398 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 399 | * Mesh builder object. It starts empty and requires the user to |
| 400 | * add all the vertex and index allocations that comprise the |
| 401 | * mesh |
| 402 | * |
| 403 | */ |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 404 | public static class AllocationBuilder { |
| 405 | RenderScript mRS; |
| 406 | |
| 407 | class Entry { |
| 408 | Allocation a; |
| 409 | Primitive prim; |
| 410 | } |
| 411 | |
| 412 | int mVertexTypeCount; |
| 413 | Entry[] mVertexTypes; |
| 414 | |
| 415 | Vector mIndexTypes; |
| 416 | |
Jason Sams | e619de6 | 2012-05-08 18:40:58 -0700 | [diff] [blame] | 417 | /** |
| 418 | * @deprecated in API 16 |
| 419 | **/ |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 420 | public AllocationBuilder(RenderScript rs) { |
| 421 | mRS = rs; |
| 422 | mVertexTypeCount = 0; |
| 423 | mVertexTypes = new Entry[16]; |
| 424 | mIndexTypes = new Vector(); |
| 425 | } |
| 426 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 427 | /** |
Jason Sams | e619de6 | 2012-05-08 18:40:58 -0700 | [diff] [blame] | 428 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 429 | * @return internal index of the last vertex buffer type added to |
| 430 | * builder |
| 431 | **/ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 432 | public int getCurrentVertexTypeIndex() { |
| 433 | return mVertexTypeCount - 1; |
| 434 | } |
| 435 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 436 | /** |
Jason Sams | e619de6 | 2012-05-08 18:40:58 -0700 | [diff] [blame] | 437 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 438 | * @return internal index of the last index set added to the |
| 439 | * builder |
| 440 | **/ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 441 | public int getCurrentIndexSetIndex() { |
| 442 | return mIndexTypes.size() - 1; |
| 443 | } |
| 444 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 445 | /** |
Jason Sams | e619de6 | 2012-05-08 18:40:58 -0700 | [diff] [blame] | 446 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 447 | * Adds an allocation containing vertex buffer data to the |
| 448 | * builder |
| 449 | * |
| 450 | * @param a vertex data allocation |
| 451 | * |
| 452 | * @return this |
| 453 | **/ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 454 | public AllocationBuilder addVertexAllocation(Allocation a) throws IllegalStateException { |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 455 | if (mVertexTypeCount >= mVertexTypes.length) { |
| 456 | throw new IllegalStateException("Max vertex types exceeded."); |
| 457 | } |
| 458 | |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 459 | mVertexTypes[mVertexTypeCount] = new Entry(); |
| 460 | mVertexTypes[mVertexTypeCount].a = a; |
| 461 | mVertexTypeCount++; |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 462 | return this; |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 463 | } |
| 464 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 465 | /** |
Jason Sams | e619de6 | 2012-05-08 18:40:58 -0700 | [diff] [blame] | 466 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 467 | * Adds an allocation containing index buffer data and index type |
| 468 | * to the builder |
| 469 | * |
| 470 | * @param a index set data allocation, could be null |
| 471 | * @param p index set primitive type |
| 472 | * |
| 473 | * @return this |
| 474 | **/ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 475 | public AllocationBuilder addIndexSetAllocation(Allocation a, Primitive p) { |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 476 | Entry indexType = new Entry(); |
| 477 | indexType.a = a; |
| 478 | indexType.prim = p; |
| 479 | mIndexTypes.addElement(indexType); |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 480 | return this; |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 481 | } |
| 482 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 483 | /** |
Jason Sams | e619de6 | 2012-05-08 18:40:58 -0700 | [diff] [blame] | 484 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 485 | * Adds an index set type to the builder |
| 486 | * |
| 487 | * @param p index set primitive type |
| 488 | * |
| 489 | * @return this |
| 490 | **/ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 491 | public AllocationBuilder addIndexSetType(Primitive p) { |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 492 | Entry indexType = new Entry(); |
| 493 | indexType.a = null; |
| 494 | indexType.prim = p; |
| 495 | mIndexTypes.addElement(indexType); |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 496 | return this; |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 497 | } |
| 498 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 499 | /** |
Jason Sams | e619de6 | 2012-05-08 18:40:58 -0700 | [diff] [blame] | 500 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 501 | * Create a Mesh object from the current state of the builder |
| 502 | * |
| 503 | **/ |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 504 | public Mesh create() { |
| 505 | mRS.validate(); |
Alex Sakhartchouk | 25999a0 | 2011-05-12 10:38:03 -0700 | [diff] [blame] | 506 | |
| 507 | int[] vtx = new int[mVertexTypeCount]; |
| 508 | int[] idx = new int[mIndexTypes.size()]; |
| 509 | int[] prim = new int[mIndexTypes.size()]; |
| 510 | |
| 511 | Allocation[] indexBuffers = new Allocation[mIndexTypes.size()]; |
| 512 | Primitive[] primitives = new Primitive[mIndexTypes.size()]; |
| 513 | Allocation[] vertexBuffers = new Allocation[mVertexTypeCount]; |
| 514 | |
| 515 | for(int ct = 0; ct < mVertexTypeCount; ct ++) { |
| 516 | Entry entry = mVertexTypes[ct]; |
| 517 | vertexBuffers[ct] = entry.a; |
Tim Murray | 460a049 | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 518 | vtx[ct] = (int)entry.a.getID(mRS); |
Alex Sakhartchouk | 25999a0 | 2011-05-12 10:38:03 -0700 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | for(int ct = 0; ct < mIndexTypes.size(); ct ++) { |
| 522 | Entry entry = (Entry)mIndexTypes.elementAt(ct); |
Tim Murray | 460a049 | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 523 | long allocID = (entry.a == null) ? 0 : entry.a.getID(mRS); |
Alex Sakhartchouk | 25999a0 | 2011-05-12 10:38:03 -0700 | [diff] [blame] | 524 | indexBuffers[ct] = entry.a; |
| 525 | primitives[ct] = entry.prim; |
| 526 | |
Tim Murray | 460a049 | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 527 | idx[ct] = (int)allocID; |
Alex Sakhartchouk | 25999a0 | 2011-05-12 10:38:03 -0700 | [diff] [blame] | 528 | prim[ct] = entry.prim.mID; |
| 529 | } |
| 530 | |
Tim Murray | 460a049 | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 531 | long id = mRS.nMeshCreate(vtx, idx, prim); |
Alex Sakhartchouk | 25999a0 | 2011-05-12 10:38:03 -0700 | [diff] [blame] | 532 | Mesh newMesh = new Mesh(id, mRS); |
| 533 | newMesh.mVertexBuffers = vertexBuffers; |
| 534 | newMesh.mIndexBuffers = indexBuffers; |
| 535 | newMesh.mPrimitives = primitives; |
| 536 | |
| 537 | return newMesh; |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 538 | } |
| 539 | } |
| 540 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 541 | /** |
Jason Sams | e619de6 | 2012-05-08 18:40:58 -0700 | [diff] [blame] | 542 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 543 | * Builder that allows creation of a mesh object point by point |
| 544 | * and triangle by triangle |
| 545 | * |
| 546 | **/ |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 547 | public static class TriangleMeshBuilder { |
| 548 | float mVtxData[]; |
| 549 | int mVtxCount; |
Alex Sakhartchouk | e60149d | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 550 | int mMaxIndex; |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 551 | short mIndexData[]; |
| 552 | int mIndexCount; |
| 553 | RenderScript mRS; |
| 554 | Element mElement; |
| 555 | |
| 556 | float mNX = 0; |
| 557 | float mNY = 0; |
| 558 | float mNZ = -1; |
| 559 | float mS0 = 0; |
| 560 | float mT0 = 0; |
| 561 | float mR = 1; |
| 562 | float mG = 1; |
| 563 | float mB = 1; |
| 564 | float mA = 1; |
| 565 | |
| 566 | int mVtxSize; |
| 567 | int mFlags; |
| 568 | |
Jason Sams | e619de6 | 2012-05-08 18:40:58 -0700 | [diff] [blame] | 569 | /** |
| 570 | * @deprecated in API 16 |
| 571 | **/ |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 572 | public static final int COLOR = 0x0001; |
Jason Sams | e619de6 | 2012-05-08 18:40:58 -0700 | [diff] [blame] | 573 | /** |
| 574 | * @deprecated in API 16 |
| 575 | **/ |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 576 | public static final int NORMAL = 0x0002; |
Jason Sams | e619de6 | 2012-05-08 18:40:58 -0700 | [diff] [blame] | 577 | /** |
| 578 | * @deprecated in API 16 |
| 579 | **/ |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 580 | public static final int TEXTURE_0 = 0x0100; |
| 581 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 582 | /** |
Jason Sams | e619de6 | 2012-05-08 18:40:58 -0700 | [diff] [blame] | 583 | * @deprecated in API 16 |
Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 584 | * @param rs Context to which the mesh will belong. |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 585 | * @param vtxSize specifies whether the vertex is a float2 or |
| 586 | * float3 |
| 587 | * @param flags bitfield that is a combination of COLOR, NORMAL, |
| 588 | * and TEXTURE_0 that specifies what vertex data |
| 589 | * channels are present in the mesh |
| 590 | * |
| 591 | **/ |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 592 | public TriangleMeshBuilder(RenderScript rs, int vtxSize, int flags) { |
| 593 | mRS = rs; |
| 594 | mVtxCount = 0; |
Alex Sakhartchouk | e60149d | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 595 | mMaxIndex = 0; |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 596 | mIndexCount = 0; |
| 597 | mVtxData = new float[128]; |
| 598 | mIndexData = new short[128]; |
| 599 | mVtxSize = vtxSize; |
| 600 | mFlags = flags; |
| 601 | |
| 602 | if (vtxSize < 2 || vtxSize > 3) { |
| 603 | throw new IllegalArgumentException("Vertex size out of range."); |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | private void makeSpace(int count) { |
| 608 | if ((mVtxCount + count) >= mVtxData.length) { |
| 609 | float t[] = new float[mVtxData.length * 2]; |
| 610 | System.arraycopy(mVtxData, 0, t, 0, mVtxData.length); |
| 611 | mVtxData = t; |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | private void latch() { |
| 616 | if ((mFlags & COLOR) != 0) { |
| 617 | makeSpace(4); |
| 618 | mVtxData[mVtxCount++] = mR; |
| 619 | mVtxData[mVtxCount++] = mG; |
| 620 | mVtxData[mVtxCount++] = mB; |
| 621 | mVtxData[mVtxCount++] = mA; |
| 622 | } |
| 623 | if ((mFlags & TEXTURE_0) != 0) { |
| 624 | makeSpace(2); |
| 625 | mVtxData[mVtxCount++] = mS0; |
| 626 | mVtxData[mVtxCount++] = mT0; |
| 627 | } |
| 628 | if ((mFlags & NORMAL) != 0) { |
Alex Sakhartchouk | e60149d | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 629 | makeSpace(4); |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 630 | mVtxData[mVtxCount++] = mNX; |
| 631 | mVtxData[mVtxCount++] = mNY; |
| 632 | mVtxData[mVtxCount++] = mNZ; |
Alex Sakhartchouk | e60149d | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 633 | mVtxData[mVtxCount++] = 0.0f; |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 634 | } |
Alex Sakhartchouk | e60149d | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 635 | mMaxIndex ++; |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 636 | } |
| 637 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 638 | /** |
Jason Sams | e619de6 | 2012-05-08 18:40:58 -0700 | [diff] [blame] | 639 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 640 | * Adds a float2 vertex to the mesh |
| 641 | * |
| 642 | * @param x position x |
| 643 | * @param y position y |
| 644 | * |
| 645 | * @return this |
| 646 | * |
| 647 | **/ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 648 | public TriangleMeshBuilder addVertex(float x, float y) { |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 649 | if (mVtxSize != 2) { |
| 650 | throw new IllegalStateException("add mistmatch with declared components."); |
| 651 | } |
| 652 | makeSpace(2); |
| 653 | mVtxData[mVtxCount++] = x; |
| 654 | mVtxData[mVtxCount++] = y; |
| 655 | latch(); |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 656 | return this; |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 657 | } |
| 658 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 659 | /** |
Jason Sams | e619de6 | 2012-05-08 18:40:58 -0700 | [diff] [blame] | 660 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 661 | * Adds a float3 vertex to the mesh |
| 662 | * |
| 663 | * @param x position x |
| 664 | * @param y position y |
| 665 | * @param z position z |
| 666 | * |
| 667 | * @return this |
| 668 | * |
| 669 | **/ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 670 | public TriangleMeshBuilder addVertex(float x, float y, float z) { |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 671 | if (mVtxSize != 3) { |
| 672 | throw new IllegalStateException("add mistmatch with declared components."); |
| 673 | } |
Alex Sakhartchouk | e60149d | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 674 | makeSpace(4); |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 675 | mVtxData[mVtxCount++] = x; |
| 676 | mVtxData[mVtxCount++] = y; |
| 677 | mVtxData[mVtxCount++] = z; |
Alex Sakhartchouk | e60149d | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 678 | mVtxData[mVtxCount++] = 1.0f; |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 679 | latch(); |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 680 | return this; |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 681 | } |
| 682 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 683 | /** |
Jason Sams | e619de6 | 2012-05-08 18:40:58 -0700 | [diff] [blame] | 684 | * @deprecated in API 16 |
Robert Ly | f11ffc11 | 2012-02-22 10:59:12 -0800 | [diff] [blame] | 685 | * 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] | 686 | * |
| 687 | * @param s texture coordinate s |
| 688 | * @param t texture coordinate t |
| 689 | * |
| 690 | * @return this |
| 691 | **/ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 692 | public TriangleMeshBuilder setTexture(float s, float t) { |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 693 | if ((mFlags & TEXTURE_0) == 0) { |
| 694 | throw new IllegalStateException("add mistmatch with declared components."); |
| 695 | } |
| 696 | mS0 = s; |
| 697 | mT0 = t; |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 698 | return this; |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 699 | } |
| 700 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 701 | /** |
Jason Sams | e619de6 | 2012-05-08 18:40:58 -0700 | [diff] [blame] | 702 | * @deprecated in API 16 |
Robert Ly | f11ffc11 | 2012-02-22 10:59:12 -0800 | [diff] [blame] | 703 | * 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] | 704 | * |
| 705 | * @param x normal vector x |
| 706 | * @param y normal vector y |
| 707 | * @param z normal vector z |
| 708 | * |
| 709 | * @return this |
| 710 | **/ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 711 | public TriangleMeshBuilder setNormal(float x, float y, float z) { |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 712 | if ((mFlags & NORMAL) == 0) { |
| 713 | throw new IllegalStateException("add mistmatch with declared components."); |
| 714 | } |
| 715 | mNX = x; |
| 716 | mNY = y; |
| 717 | mNZ = z; |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 718 | return this; |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 719 | } |
| 720 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 721 | /** |
Jason Sams | e619de6 | 2012-05-08 18:40:58 -0700 | [diff] [blame] | 722 | * @deprecated in API 16 |
Robert Ly | f11ffc11 | 2012-02-22 10:59:12 -0800 | [diff] [blame] | 723 | * 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] | 724 | * |
| 725 | * @param r red component |
| 726 | * @param g green component |
| 727 | * @param b blue component |
| 728 | * @param a alpha component |
| 729 | * |
| 730 | * @return this |
| 731 | **/ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 732 | public TriangleMeshBuilder setColor(float r, float g, float b, float a) { |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 733 | if ((mFlags & COLOR) == 0) { |
| 734 | throw new IllegalStateException("add mistmatch with declared components."); |
| 735 | } |
| 736 | mR = r; |
| 737 | mG = g; |
| 738 | mB = b; |
| 739 | mA = a; |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 740 | return this; |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 741 | } |
| 742 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 743 | /** |
Jason Sams | e619de6 | 2012-05-08 18:40:58 -0700 | [diff] [blame] | 744 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 745 | * Adds a new triangle to the mesh builder |
| 746 | * |
| 747 | * @param idx1 index of the first vertex in the triangle |
| 748 | * @param idx2 index of the second vertex in the triangle |
| 749 | * @param idx3 index of the third vertex in the triangle |
| 750 | * |
| 751 | * @return this |
| 752 | **/ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 753 | public TriangleMeshBuilder addTriangle(int idx1, int idx2, int idx3) { |
Alex Sakhartchouk | e60149d | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 754 | if((idx1 >= mMaxIndex) || (idx1 < 0) || |
| 755 | (idx2 >= mMaxIndex) || (idx2 < 0) || |
| 756 | (idx3 >= mMaxIndex) || (idx3 < 0)) { |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 757 | throw new IllegalStateException("Index provided greater than vertex count."); |
| 758 | } |
| 759 | if ((mIndexCount + 3) >= mIndexData.length) { |
| 760 | short t[] = new short[mIndexData.length * 2]; |
| 761 | System.arraycopy(mIndexData, 0, t, 0, mIndexData.length); |
| 762 | mIndexData = t; |
| 763 | } |
| 764 | mIndexData[mIndexCount++] = (short)idx1; |
| 765 | mIndexData[mIndexCount++] = (short)idx2; |
| 766 | mIndexData[mIndexCount++] = (short)idx3; |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 767 | return this; |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 768 | } |
| 769 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 770 | /** |
Jason Sams | e619de6 | 2012-05-08 18:40:58 -0700 | [diff] [blame] | 771 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 772 | * Creates the mesh object from the current state of the builder |
| 773 | * |
| 774 | * @param uploadToBufferObject specifies whether the vertex data |
| 775 | * is to be uploaded into the buffer |
| 776 | * object indicating that it's likely |
| 777 | * not going to be modified and |
| 778 | * rendered many times. |
| 779 | * Alternatively, it indicates the |
| 780 | * mesh data will be updated |
| 781 | * frequently and remain in script |
| 782 | * accessible memory |
| 783 | * |
| 784 | **/ |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 785 | public Mesh create(boolean uploadToBufferObject) { |
| 786 | Element.Builder b = new Element.Builder(mRS); |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 787 | b.add(Element.createVector(mRS, |
| 788 | Element.DataType.FLOAT_32, |
| 789 | mVtxSize), "position"); |
| 790 | if ((mFlags & COLOR) != 0) { |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 791 | b.add(Element.F32_4(mRS), "color"); |
| 792 | } |
| 793 | if ((mFlags & TEXTURE_0) != 0) { |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 794 | b.add(Element.F32_2(mRS), "texture0"); |
| 795 | } |
| 796 | if ((mFlags & NORMAL) != 0) { |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 797 | b.add(Element.F32_3(mRS), "normal"); |
| 798 | } |
| 799 | mElement = b.create(); |
| 800 | |
Jason Sams | d195240 | 2010-12-20 12:55:28 -0800 | [diff] [blame] | 801 | int usage = Allocation.USAGE_SCRIPT; |
| 802 | if (uploadToBufferObject) { |
| 803 | usage |= Allocation.USAGE_GRAPHICS_VERTEX; |
| 804 | } |
| 805 | |
| 806 | Builder smb = new Builder(mRS, usage); |
Alex Sakhartchouk | e60149d | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 807 | smb.addVertexType(mElement, mMaxIndex); |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 808 | smb.addIndexSetType(Element.U16(mRS), mIndexCount, Primitive.TRIANGLE); |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 809 | |
| 810 | Mesh sm = smb.create(); |
| 811 | |
Alex Sakhartchouk | e60149d | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 812 | sm.getVertexAllocation(0).copy1DRangeFromUnchecked(0, mMaxIndex, mVtxData); |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 813 | if(uploadToBufferObject) { |
Jason Sams | d195240 | 2010-12-20 12:55:28 -0800 | [diff] [blame] | 814 | if (uploadToBufferObject) { |
| 815 | sm.getVertexAllocation(0).syncAll(Allocation.USAGE_SCRIPT); |
| 816 | } |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 817 | } |
| 818 | |
Jason Sams | b97b251 | 2011-01-16 15:04:08 -0800 | [diff] [blame] | 819 | sm.getIndexSetAllocation(0).copy1DRangeFromUnchecked(0, mIndexCount, mIndexData); |
Jason Sams | d195240 | 2010-12-20 12:55:28 -0800 | [diff] [blame] | 820 | if (uploadToBufferObject) { |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 821 | sm.getIndexSetAllocation(0).syncAll(Allocation.USAGE_SCRIPT); |
Jason Sams | d195240 | 2010-12-20 12:55:28 -0800 | [diff] [blame] | 822 | } |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 823 | |
| 824 | return sm; |
| 825 | } |
| 826 | } |
| 827 | } |
| 828 | |