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