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