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