1 /******************************************************************************* 2 * Copyright 2011 See AUTHORS file. 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 com.badlogic.gdx.graphics.g3d.utils; 18 19 import com.badlogic.gdx.graphics.Color; 20 import com.badlogic.gdx.graphics.GL20; 21 import com.badlogic.gdx.graphics.Mesh; 22 import com.badlogic.gdx.graphics.VertexAttributes; 23 import com.badlogic.gdx.graphics.VertexAttributes.Usage; 24 import com.badlogic.gdx.graphics.g2d.TextureRegion; 25 import com.badlogic.gdx.graphics.g3d.model.MeshPart; 26 import com.badlogic.gdx.math.Matrix4; 27 import com.badlogic.gdx.math.Vector2; 28 import com.badlogic.gdx.math.Vector3; 29 import com.badlogic.gdx.utils.Pool.Poolable; 30 31 public interface MeshPartBuilder { 32 /** @return The {@link MeshPart} currently building. */ 33 public MeshPart getMeshPart (); 34 35 /** @return The primitive type used for building, e.g. {@link GL20#GL_TRIANGLES} or {@link GL20#GL_LINES}. */ 36 public int getPrimitiveType (); 37 38 /** @return The {@link VertexAttributes} available for building. */ 39 public VertexAttributes getAttributes (); 40 41 /** Set the color used to tint the vertex color, defaults to white. Only applicable for {@link Usage#ColorPacked} or 42 * {@link Usage#ColorUnpacked}. */ 43 public void setColor (final Color color); 44 45 /** Set the color used to tint the vertex color, defaults to white. Only applicable for {@link Usage#ColorPacked} or 46 * {@link Usage#ColorUnpacked}. */ 47 public void setColor (float r, float g, float b, float a); 48 49 /** Set range of texture coordinates used (default is 0,0,1,1). */ 50 public void setUVRange (float u1, float v1, float u2, float v2); 51 52 /** Set range of texture coordinates from the specified TextureRegion. */ 53 public void setUVRange (TextureRegion r); 54 55 /** Get the current vertex transformation matrix. */ 56 public Matrix4 getVertexTransform (Matrix4 out); 57 58 /** Set the current vertex transformation matrix and enables vertex transformation. */ 59 public void setVertexTransform (Matrix4 transform); 60 61 /** Indicates whether vertex transformation is enabled. */ 62 public boolean isVertexTransformationEnabled (); 63 64 /** Sets whether vertex transformation is enabled. */ 65 public void setVertexTransformationEnabled (boolean enabled); 66 67 /** Increases the size of the backing vertices array to accommodate the specified number of additional vertices. Useful before 68 * adding many vertices to avoid multiple backing array resizes. 69 * @param numVertices The number of vertices you are about to add */ 70 public void ensureVertices (int numVertices); 71 72 /** Increases the size of the backing indices array to accommodate the specified number of additional indices. Useful before 73 * adding many indices to avoid multiple backing array resizes. 74 * @param numIndices The number of indices you are about to add */ 75 public void ensureIndices (int numIndices); 76 77 /** Increases the size of the backing vertices and indices arrays to accommodate the specified number of additional vertices and 78 * indices. Useful before adding many vertices and indices to avoid multiple backing array resizes. 79 * @param numVertices The number of vertices you are about to add 80 * @param numIndices The number of indices you are about to add */ 81 public void ensureCapacity (int numVertices, int numIndices); 82 83 /** Increases the size of the backing indices array to accommodate the specified number of additional triangles. Useful before 84 * adding many triangles using {@link #triangle(short, short, short)} to avoid multiple backing array resizes. The actual 85 * number of indices accounted for depends on the primitive type (see {@link #getPrimitiveType()}). 86 * @param numTriangles The number of triangles you are about to add */ 87 public void ensureTriangleIndices (int numTriangles); 88 89 /** Increases the size of the backing indices array to accommodate the specified number of additional rectangles. Useful before 90 * adding many rectangles using {@link #rect(short, short, short, short)} to avoid multiple backing array resizes. 91 * @param numRectangles The number of rectangles you are about to add */ 92 public void ensureRectangleIndices (int numRectangles); 93 94 /** Add one or more vertices, returns the index of the last vertex added. The length of values must a power of the vertex size. */ 95 public short vertex (final float... values); 96 97 /** Add a vertex, returns the index. Null values are allowed. Use {@link #getAttributes} to check which values are available. */ 98 public short vertex (Vector3 pos, Vector3 nor, Color col, Vector2 uv); 99 100 /** Add a vertex, returns the index. Use {@link #getAttributes} to check which values are available. */ 101 public short vertex (final VertexInfo info); 102 103 /** @return The index of the last added vertex. */ 104 public short lastIndex (); 105 106 /** Add an index, MeshPartBuilder expects all meshes to be indexed. */ 107 public void index (final short value); 108 109 /** Add multiple indices, MeshPartBuilder expects all meshes to be indexed. */ 110 public void index (short value1, short value2); 111 112 /** Add multiple indices, MeshPartBuilder expects all meshes to be indexed. */ 113 public void index (short value1, short value2, short value3); 114 115 /** Add multiple indices, MeshPartBuilder expects all meshes to be indexed. */ 116 public void index (short value1, short value2, short value3, short value4); 117 118 /** Add multiple indices, MeshPartBuilder expects all meshes to be indexed. */ 119 public void index (short value1, short value2, short value3, short value4, short value5, short value6); 120 121 /** Add multiple indices, MeshPartBuilder expects all meshes to be indexed. */ 122 public void index (short value1, short value2, short value3, short value4, short value5, short value6, short value7, 123 short value8); 124 125 /** Add a line by indices. Requires GL_LINES primitive type. */ 126 public void line (short index1, short index2); 127 128 /** Add a line. Requires GL_LINES primitive type. */ 129 public void line (VertexInfo p1, VertexInfo p2); 130 131 /** Add a line. Requires GL_LINES primitive type. */ 132 public void line (Vector3 p1, Vector3 p2); 133 134 /** Add a line. Requires GL_LINES primitive type. */ 135 public void line (float x1, float y1, float z1, float x2, float y2, float z2); 136 137 /** Add a line. Requires GL_LINES primitive type. */ 138 public void line (Vector3 p1, Color c1, Vector3 p2, Color c2); 139 140 /** Add a triangle by indices. Requires GL_POINTS, GL_LINES or GL_TRIANGLES primitive type. */ 141 public void triangle (short index1, short index2, short index3); 142 143 /** Add a triangle. Requires GL_POINTS, GL_LINES or GL_TRIANGLES primitive type. */ 144 public void triangle (VertexInfo p1, VertexInfo p2, VertexInfo p3); 145 146 /** Add a triangle. Requires GL_POINTS, GL_LINES or GL_TRIANGLES primitive type. */ 147 public void triangle (Vector3 p1, Vector3 p2, Vector3 p3); 148 149 /** Add a triangle. Requires GL_POINTS, GL_LINES or GL_TRIANGLES primitive type. */ 150 public void triangle (Vector3 p1, Color c1, Vector3 p2, Color c2, Vector3 p3, Color c3); 151 152 /** Add a rectangle by indices. Requires GL_POINTS, GL_LINES or GL_TRIANGLES primitive type. */ 153 public void rect (short corner00, short corner10, short corner11, short corner01); 154 155 /** Add a rectangle. Requires GL_POINTS, GL_LINES or GL_TRIANGLES primitive type. */ 156 public void rect (VertexInfo corner00, VertexInfo corner10, VertexInfo corner11, VertexInfo corner01); 157 158 /** Add a rectangle. Requires GL_POINTS, GL_LINES or GL_TRIANGLES primitive type. */ 159 public void rect (Vector3 corner00, Vector3 corner10, Vector3 corner11, Vector3 corner01, Vector3 normal); 160 161 /** Add a rectangle Requires GL_POINTS, GL_LINES or GL_TRIANGLES primitive type. */ 162 public void rect (float x00, float y00, float z00, float x10, float y10, float z10, float x11, float y11, float z11, 163 float x01, float y01, float z01, float normalX, float normalY, float normalZ); 164 165 /** Copies a mesh to the mesh (part) currently being build. 166 * @param mesh The mesh to copy, must have the same vertex attributes and must be indexed. */ 167 public void addMesh (Mesh mesh); 168 169 /** Copies a MeshPart to the mesh (part) currently being build. 170 * @param meshpart The MeshPart to copy, must have the same vertex attributes, primitive type and must be indexed. */ 171 public void addMesh (MeshPart meshpart); 172 173 /** Copies a (part of a) mesh to the mesh (part) currently being build. 174 * @param mesh The mesh to (partly) copy, must have the same vertex attributes and must be indexed. 175 * @param indexOffset The zero-based offset of the first index of the part of the mesh to copy. 176 * @param numIndices The number of indices of the part of the mesh to copy. */ 177 public void addMesh (Mesh mesh, int indexOffset, int numIndices); 178 179 /** Copies a mesh to the mesh (part) currently being build. The entire vertices array is added, even if some of the vertices are 180 * not indexed by the indices array. If you want to add only the vertices that are actually indexed, then use the 181 * {@link #addMesh(float[], short[], int, int)} method instead. 182 * @param vertices The vertices to copy, must be in the same vertex layout as the mesh being build. 183 * @param indices Array containing the indices to copy, each index should be valid in the vertices array. */ 184 public void addMesh (float[] vertices, short[] indices); 185 186 /** Copies a (part of a) mesh to the mesh (part) currently being build. 187 * @param vertices The vertices to (partly) copy, must be in the same vertex layout as the mesh being build. 188 * @param indices Array containing the indices to (partly) copy, each index should be valid in the vertices array. 189 * @param indexOffset The zero-based offset of the first index of the part of indices array to copy. 190 * @param numIndices The number of indices of the part of the indices array to copy. */ 191 public void addMesh (float[] vertices, short[] indices, int indexOffset, int numIndices); 192 193 /** Class that contains all vertex information the builder can use. 194 * @author Xoppa */ 195 public static class VertexInfo implements Poolable { 196 public final Vector3 position = new Vector3(); 197 public boolean hasPosition; 198 public final Vector3 normal = new Vector3(0, 1, 0); 199 public boolean hasNormal; 200 public final Color color = new Color(1, 1, 1, 1); 201 public boolean hasColor; 202 public final Vector2 uv = new Vector2(); 203 public boolean hasUV; 204 205 @Override 206 public void reset () { 207 position.set(0, 0, 0); 208 normal.set(0, 1, 0); 209 color.set(1, 1, 1, 1); 210 uv.set(0, 0); 211 } 212 213 public VertexInfo set (Vector3 pos, Vector3 nor, Color col, Vector2 uv) { 214 reset(); 215 if ((hasPosition = pos != null) == true) position.set(pos); 216 if ((hasNormal = nor != null) == true) normal.set(nor); 217 if ((hasColor = col != null) == true) color.set(col); 218 if ((hasUV = uv != null) == true) this.uv.set(uv); 219 return this; 220 } 221 222 public VertexInfo set (final VertexInfo other) { 223 if (other == null) return set(null, null, null, null); 224 hasPosition = other.hasPosition; 225 position.set(other.position); 226 hasNormal = other.hasNormal; 227 normal.set(other.normal); 228 hasColor = other.hasColor; 229 color.set(other.color); 230 hasUV = other.hasUV; 231 uv.set(other.uv); 232 return this; 233 } 234 235 public VertexInfo setPos (float x, float y, float z) { 236 position.set(x, y, z); 237 hasPosition = true; 238 return this; 239 } 240 241 public VertexInfo setPos (Vector3 pos) { 242 if ((hasPosition = pos != null) == true) position.set(pos); 243 return this; 244 } 245 246 public VertexInfo setNor (float x, float y, float z) { 247 normal.set(x, y, z); 248 hasNormal = true; 249 return this; 250 } 251 252 public VertexInfo setNor (Vector3 nor) { 253 if ((hasNormal = nor != null) == true) normal.set(nor); 254 return this; 255 } 256 257 public VertexInfo setCol (float r, float g, float b, float a) { 258 color.set(r, g, b, a); 259 hasColor = true; 260 return this; 261 } 262 263 public VertexInfo setCol (Color col) { 264 if ((hasColor = col != null) == true) color.set(col); 265 return this; 266 } 267 268 public VertexInfo setUV (float u, float v) { 269 uv.set(u, v); 270 hasUV = true; 271 return this; 272 } 273 274 public VertexInfo setUV (Vector2 uv) { 275 if ((hasUV = uv != null) == true) this.uv.set(uv); 276 return this; 277 } 278 279 public VertexInfo lerp (final VertexInfo target, float alpha) { 280 if (hasPosition && target.hasPosition) position.lerp(target.position, alpha); 281 if (hasNormal && target.hasNormal) normal.lerp(target.normal, alpha); 282 if (hasColor && target.hasColor) color.lerp(target.color, alpha); 283 if (hasUV && target.hasUV) uv.lerp(target.uv, alpha); 284 return this; 285 } 286 } 287 288 // TODO: The following methods are deprecated and will be removed in a future release 289 290 291 /** @deprecated use PatchShapeBuilder.build instead. */ 292 @Deprecated 293 public void patch (VertexInfo corner00, VertexInfo corner10, VertexInfo corner11, VertexInfo corner01, int divisionsU, 294 int divisionsV); 295 296 /** @deprecated use PatchShapeBuilder.build instead. */ 297 @Deprecated 298 public void patch (Vector3 corner00, Vector3 corner10, Vector3 corner11, Vector3 corner01, Vector3 normal, int divisionsU, 299 int divisionsV); 300 301 /** @deprecated use PatchShapeBuilder.build instead. */ 302 @Deprecated 303 public void patch (float x00, float y00, float z00, float x10, float y10, float z10, float x11, float y11, float z11, 304 float x01, float y01, float z01, float normalX, float normalY, float normalZ, int divisionsU, int divisionsV); 305 306 /** @deprecated use BoxShapeBuilder.build instead. */ 307 @Deprecated 308 public void box (VertexInfo corner000, VertexInfo corner010, VertexInfo corner100, VertexInfo corner110, VertexInfo corner001, 309 VertexInfo corner011, VertexInfo corner101, VertexInfo corner111); 310 311 /** @deprecated use BoxShapeBuilder.build instead. */ 312 @Deprecated 313 public void box (Vector3 corner000, Vector3 corner010, Vector3 corner100, Vector3 corner110, Vector3 corner001, 314 Vector3 corner011, Vector3 corner101, Vector3 corner111); 315 316 /** @deprecated use BoxShapeBuilder.build instead. */ 317 @Deprecated 318 public void box (Matrix4 transform); 319 320 /** @deprecated use BoxShapeBuilder.build instead. */ 321 @Deprecated 322 public void box (float width, float height, float depth); 323 324 /** @deprecated use BoxShapeBuilder.build instead. */ 325 @Deprecated 326 public void box (float x, float y, float z, float width, float height, float depth); 327 328 /** @deprecated Use EllipseShapeBuilder.build instead. */ 329 @Deprecated 330 public void circle (float radius, int divisions, float centerX, float centerY, float centerZ, float normalX, float normalY, 331 float normalZ); 332 333 /** @deprecated Use EllipseShapeBuilder.build instead. */ 334 @Deprecated 335 public void circle (float radius, int divisions, final Vector3 center, final Vector3 normal); 336 337 /** @deprecated Use EllipseShapeBuilder.build instead. */ 338 @Deprecated 339 public void circle (float radius, int divisions, final Vector3 center, final Vector3 normal, final Vector3 tangent, 340 final Vector3 binormal); 341 342 /** @deprecated Use EllipseShapeBuilder.build instead. */ 343 @Deprecated 344 public void circle (float radius, int divisions, float centerX, float centerY, float centerZ, float normalX, float normalY, 345 float normalZ, float tangentX, float tangentY, float tangentZ, float binormalX, float binormalY, float binormalZ); 346 347 /** @deprecated Use EllipseShapeBuilder.build instead. */ 348 @Deprecated 349 public void circle (float radius, int divisions, float centerX, float centerY, float centerZ, float normalX, float normalY, 350 float normalZ, float angleFrom, float angleTo); 351 352 /** @deprecated Use EllipseShapeBuilder.build instead. */ 353 @Deprecated 354 public void circle (float radius, int divisions, final Vector3 center, final Vector3 normal, float angleFrom, float angleTo); 355 356 /** @deprecated Use EllipseShapeBuilder.build instead. */ 357 @Deprecated 358 public void circle (float radius, int divisions, final Vector3 center, final Vector3 normal, final Vector3 tangent, 359 final Vector3 binormal, float angleFrom, float angleTo); 360 361 /** @deprecated Use EllipseShapeBuilder.build instead. */ 362 @Deprecated 363 public void circle (float radius, int divisions, float centerX, float centerY, float centerZ, float normalX, float normalY, 364 float normalZ, float tangentX, float tangentY, float tangentZ, float binormalX, float binormalY, float binormalZ, 365 float angleFrom, float angleTo); 366 367 /** @deprecated Use EllipseShapeBuilder.build instead. */ 368 @Deprecated 369 public void ellipse (float width, float height, int divisions, float centerX, float centerY, float centerZ, float normalX, 370 float normalY, float normalZ); 371 372 /** @deprecated Use EllipseShapeBuilder.build instead. */ 373 @Deprecated 374 public void ellipse (float width, float height, int divisions, final Vector3 center, final Vector3 normal); 375 376 /** @deprecated Use EllipseShapeBuilder.build instead. */ 377 @Deprecated 378 public void ellipse (float width, float height, int divisions, final Vector3 center, final Vector3 normal, 379 final Vector3 tangent, final Vector3 binormal); 380 381 /** @deprecated Use EllipseShapeBuilder.build instead. */ 382 @Deprecated 383 public void ellipse (float width, float height, int divisions, float centerX, float centerY, float centerZ, float normalX, 384 float normalY, float normalZ, float tangentX, float tangentY, float tangentZ, float binormalX, float binormalY, 385 float binormalZ); 386 387 /** @deprecated Use EllipseShapeBuilder.build instead. */ 388 @Deprecated 389 public void ellipse (float width, float height, int divisions, float centerX, float centerY, float centerZ, float normalX, 390 float normalY, float normalZ, float angleFrom, float angleTo); 391 392 /** @deprecated Use EllipseShapeBuilder.build instead. */ 393 @Deprecated 394 public void ellipse (float width, float height, int divisions, final Vector3 center, final Vector3 normal, float angleFrom, 395 float angleTo); 396 397 /** @deprecated Use EllipseShapeBuilder.build instead. */ 398 @Deprecated 399 public void ellipse (float width, float height, int divisions, final Vector3 center, final Vector3 normal, 400 final Vector3 tangent, final Vector3 binormal, float angleFrom, float angleTo); 401 402 /** @deprecated Use EllipseShapeBuilder.build instead. */ 403 @Deprecated 404 public void ellipse (float width, float height, int divisions, float centerX, float centerY, float centerZ, float normalX, 405 float normalY, float normalZ, float tangentX, float tangentY, float tangentZ, float binormalX, float binormalY, 406 float binormalZ, float angleFrom, float angleTo); 407 408 /** @deprecated Use EllipseShapeBuilder.build instead. */ 409 @Deprecated 410 public void ellipse (float width, float height, float innerWidth, float innerHeight, int divisions, float centerX, 411 float centerY, float centerZ, float normalX, float normalY, float normalZ, float tangentX, float tangentY, float tangentZ, 412 float binormalX, float binormalY, float binormalZ, float angleFrom, float angleTo); 413 414 /** @deprecated Use EllipseShapeBuilder.build instead. */ 415 @Deprecated 416 public void ellipse (float width, float height, float innerWidth, float innerHeight, int divisions, float centerX, 417 float centerY, float centerZ, float normalX, float normalY, float normalZ, float angleFrom, float angleTo); 418 419 /** @deprecated Use EllipseShapeBuilder.build instead. */ 420 @Deprecated 421 public void ellipse (float width, float height, float innerWidth, float innerHeight, int divisions, float centerX, 422 float centerY, float centerZ, float normalX, float normalY, float normalZ); 423 424 /** @deprecated Use EllipseShapeBuilder.build instead. */ 425 @Deprecated 426 public void ellipse (float width, float height, float innerWidth, float innerHeight, int divisions, Vector3 center, 427 Vector3 normal); 428 429 /** @deprecated Use CylinderShapeBuilder.build instead. */ 430 @Deprecated 431 public void cylinder (float width, float height, float depth, int divisions); 432 433 /** @deprecated Use CylinderShapeBuilder.build instead. */ 434 @Deprecated 435 public void cylinder (float width, float height, float depth, int divisions, float angleFrom, float angleTo); 436 437 /** @deprecated Use CylinderShapeBuilder.build instead. */ 438 @Deprecated 439 public void cylinder (float width, float height, float depth, int divisions, float angleFrom, float angleTo, boolean close); 440 441 /** @deprecated Use ConeShapeBuilder.build instead. */ 442 @Deprecated 443 public void cone (float width, float height, float depth, int divisions); 444 445 /** @deprecated Use ConeShapeBuilder.build instead. */ 446 @Deprecated 447 public void cone (float width, float height, float depth, int divisions, float angleFrom, float angleTo); 448 449 /** @deprecated Use SphereShapeBuilder.build instead. */ 450 @Deprecated 451 public void sphere (float width, float height, float depth, int divisionsU, int divisionsV); 452 453 /** @deprecated Use SphereShapeBuilder.build instead. */ 454 @Deprecated 455 public void sphere (final Matrix4 transform, float width, float height, float depth, int divisionsU, int divisionsV); 456 457 /** @deprecated Use SphereShapeBuilder.build instead. */ 458 @Deprecated 459 public void sphere (float width, float height, float depth, int divisionsU, int divisionsV, float angleUFrom, float angleUTo, 460 float angleVFrom, float angleVTo); 461 462 /** @deprecated Use SphereShapeBuilder.build instead. */ 463 @Deprecated 464 public void sphere (final Matrix4 transform, float width, float height, float depth, int divisionsU, int divisionsV, 465 float angleUFrom, float angleUTo, float angleVFrom, float angleVTo); 466 467 /** @deprecated Use CapsuleShapeBuilder.build instead. */ 468 @Deprecated 469 public void capsule (float radius, float height, int divisions); 470 471 /** @deprecated Use ArrowShapeBuilder.build instead. */ 472 @Deprecated 473 public void arrow (float x1, float y1, float z1, float x2, float y2, float z2, float capLength, float stemThickness, 474 int divisions); 475 } 476