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