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