Skip to content

Instantly share code, notes, and snippets.

@xoppa
Created July 20, 2014 19:01
Show Gist options
  • Save xoppa/b75aef57829eb4509709 to your computer and use it in GitHub Desktop.
Save xoppa/b75aef57829eb4509709 to your computer and use it in GitHub Desktop.
BaseMeshBuilder
package com.badlogic.gdx.graphics.g3d.utils;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Mesh;
import com.badlogic.gdx.graphics.VertexAttribute;
import com.badlogic.gdx.graphics.VertexAttributes;
import com.badlogic.gdx.graphics.VertexAttributes.Usage;
import com.badlogic.gdx.graphics.g3d.model.MeshPart;
import com.badlogic.gdx.math.Matrix3;
import com.badlogic.gdx.math.Matrix4;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.FloatArray;
import com.badlogic.gdx.utils.GdxRuntimeException;
import com.badlogic.gdx.utils.IntIntMap;
import com.badlogic.gdx.utils.NumberUtils;
import com.badlogic.gdx.utils.ShortArray;
/** Base class to provide basic functionality required for building meshes, such as adding vertices and indices. This class is intended
* to be extended, see {@link MeshBuilder} for an implementation.
* @author Xoppa */
public class BaseMeshBuilder {
private IntIntMap indicesMap;
private Matrix3 tempM3 = new Matrix3();
/** The vertex attributes of the resulting mesh */
protected VertexAttributes attributes;
/** The size (in number of floats) of each vertex */
protected int stride;
/** The vertices to construct, no size checking is done */
private FloatArray vertices = new FloatArray();
/** The indices to construct, no size checking is done */
private ShortArray indices = new ShortArray();
/** The position attribute within the {@link #attributes} to use when building the mesh */
protected VertexAttribute positionAttribute;
/** The normal attribute within the {@link #attributes} to use when building the mesh, or null when not available */
protected VertexAttribute normalAttribute;
/** The binormal attribute within the {@link #attributes} to use when building the mesh, or null when not available */
protected VertexAttribute binormalAttribute;
/** The tangent attribute within the {@link #attributes} to use when building the mesh, or null when not available */
protected VertexAttribute tangentAttribute;
/** The color attribute within the {@link #attributes} to use when building the mesh, or null when not available */
protected VertexAttribute colorAttribute;
/** The texture coordinates attribute within the {@link #attributes} to use when building the mesh, or null when not available */
protected VertexAttribute texCoordsAttribute;
protected BaseMeshBuilder() {}
protected void begin (final VertexAttributes attributes) {
if (this.attributes != null) throw new RuntimeException("Call end() first");
this.attributes = attributes;
this.vertices.clear();
this.indices.clear();
this.stride = attributes.vertexSize / 4;
positionAttribute = attributes.findByUsage(Usage.Position);
if (positionAttribute == null) throw new GdxRuntimeException("Cannot build mesh without position attribute");
normalAttribute = attributes.findByUsage(Usage.Normal);
binormalAttribute = attributes.findByUsage(Usage.BiNormal);
tangentAttribute = attributes.findByUsage(Usage.Tangent);
colorAttribute = attributes.findByUsage(Usage.Color);
if (colorAttribute == null)
colorAttribute = attributes.findByUsage(Usage.ColorPacked);
texCoordsAttribute = attributes.findByUsage(Usage.TextureCoordinates);
}
protected Mesh end () {
if (attributes == null) throw new RuntimeException("Call begin() first");
final Mesh mesh = new Mesh(true, vertices.size / stride, indices.size, attributes);
mesh.setVertices(vertices.items, 0, vertices.size);
mesh.setIndices(indices.items, 0, indices.size);
attributes = null;
vertices.clear();
indices.clear();
return mesh;
}
/** @return The size (number of indices) of the Mesh currently being build. */
protected int getSize() {
return indices.size;
}
/** Increases the size of the backing vertices array to accommodate for at least the specified number of vertices to be added. */
protected void ensureVertices (int numVertices) {
vertices.ensureCapacity(stride * numVertices);
}
/** Increases the size of the backing indices array to accommodate for at least the specified number of indices to be added. */
protected void ensureIndices (int numIndices) {
indices.ensureCapacity(numIndices);
}
/** Adds one vertex to the mesh and returns its index. Vertices are NOT guaranteed to be added consecutive.
* @param values the buffer containing the vertex data, the position of the buffer will be added to the specified offset
* @param offsetInBytes the offset in bytes within the buffer to the vertex to add, relative to the current position of the buffer
* @return the index of the just added vertex */
protected int vertexb (final Buffer values, final int offsetInBytes) {
return vertexf(asFloatArray(asFloatBuffer(values), stride), offsetInBytes / 4);
}
/** Adds one vertex to the mesh and returns its index. Vertices are NOT guaranteed to be added consecutive.
* @param values the array containing the vertex data
* @param offsetInFloats the offset in within the array to the vertex to add
* @return the index of the just added vertex */
protected int vertexf (final float[] values, final int offsetInFloats) {
vertices.addAll(values, offsetInFloats, stride);
return (vertices.size / stride) - 1;
}
/** Adds one vertex to the mesh and returns its index. Vertices are NOT guaranteed to be added consecutive.
* @param values the buffer containing the vertex data, the position of the buffer will be added to the specified offset
* @param offsetInBytes the offset in bytes within the buffer to the vertex to add, relative to the current position of the buffer
* @param color the {@link Color} the multiply the color component with, must not be null
* @return the index of the just added vertex */
protected int vertexb (final Buffer values, final int offsetInBytes, final Color color) {
return vertexf(asFloatArray(asFloatBuffer(values), stride), offsetInBytes / 4, color);
}
/** Adds one vertex to the mesh and returns its index. Vertices are NOT guaranteed to be added consecutive.
* @param values the array containing the vertex data
* @param offsetInFloats the offset in within the array to the vertex to add
* @param color the {@link Color} the multiply the color component with, must not be null
* @return the index of the just added vertex */
protected int vertexf (final float[] values, final int offsetInFloats, final Color color) {
vertices.addAll(values, offsetInFloats, stride);
final int offset = vertices.size - stride;
if (colorAttribute != null)
colorize(vertices.items, offset, colorAttribute, color);
return (vertices.size / stride) - 1;
}
/** Adds one vertex to the mesh and returns its index. Vertices are NOT guaranteed to be added consecutive.
* @param values the buffer containing the vertex data, the position of the buffer will be added to the specified offset
* @param offsetInBytes the offset in bytes within the buffer to the vertex to add, relative to the current position of the buffer
* @param posTransform the transformation matrix to apply to the position vector(s) of the vertex while adding it to the mesh, must not be null
* @param norTransform the transformation matrix to apply to the normal vector(s) of the vertex while adding it to the mesh, must not be null
* @return the index of the just added vertex */
protected int vertexb (final Buffer values, final int offsetInBytes, final Matrix4 posTransform, final Matrix3 norTransform) {
return vertexf(asFloatArray(asFloatBuffer(values), stride), offsetInBytes / 4, posTransform, norTransform);
}
/** Adds one vertex to the mesh and returns its index. Vertices are NOT guaranteed to be added consecutive.
* @param values the array containing the vertex data
* @param offsetInFloats the offset in within the array to the vertex to add
* @param posTransform the transformation matrix to apply to the position vector(s) of the vertex while adding it to the mesh, must not be null
* @param norTransform the transformation matrix to apply to the normal vector(s) of the vertex while adding it to the mesh, must not be null
* @return the index of the just added vertex */
protected int vertexf (final float[] values, final int offsetInFloats, final Matrix4 posTransform, final Matrix3 norTransform) {
vertices.addAll(values, offsetInFloats, stride);
final int offset = vertices.size - stride;
transform(vertices.items, offset, positionAttribute, posTransform);
if (normalAttribute != null)
transform(vertices.items, offset, normalAttribute, norTransform);
if (binormalAttribute != null)
transform(vertices.items, offset, binormalAttribute, norTransform);
if (tangentAttribute != null)
transform(vertices.items, offset, tangentAttribute, norTransform);
return (vertices.size / stride) - 1;
}
/** Adds one vertex to the mesh and returns its index. Vertices are NOT guaranteed to be added consecutive.
* @param values the buffer containing the vertex data, the position of the buffer will be added to the specified offset
* @param offsetInBytes the offset in bytes within the buffer to the vertex to add, relative to the current position of the buffer
* @param color the {@link Color} the multiply the color component with, must not be null
* @param posTransform the transformation matrix to apply to the position vector(s) of the vertex while adding it to the mesh, must not be null
* @param norTransform the transformation matrix to apply to the normal vector(s) of the vertex while adding it to the mesh, must not be null
* @return the index of the just added vertex */
protected int vertexb (final Buffer values, final int offsetInBytes, final Color color, final Matrix4 posTransform, final Matrix3 norTransform) {
return vertexf(asFloatArray(asFloatBuffer(values), stride), offsetInBytes / 4, color, posTransform, norTransform);
}
/** Adds one vertex to the mesh and returns its index. Vertices are NOT guaranteed to be added consecutive.
* @param values the array containing the vertex data
* @param offsetInFloats the offset in within the array to the vertex to add
* @param color the {@link Color} the multiply the color component with, must not be null
* @param posTransform the transformation matrix to apply to the position vector(s) of the vertex while adding it to the mesh, must not be null
* @param norTransform the transformation matrix to apply to the normal vector(s) of the vertex while adding it to the mesh, must not be null
* @return the index of the just added vertex */
protected int vertexf (final float[] values, final int offsetInFloats, final Color color, final Matrix4 posTransform, final Matrix3 norTransform) {
vertices.addAll(values, offsetInFloats, stride);
final int offset = vertices.size - stride;
if (colorAttribute != null)
colorize(vertices.items, offset, colorAttribute, color);
transform(vertices.items, offset, positionAttribute, posTransform);
if (normalAttribute != null)
transform(vertices.items, offset, normalAttribute, norTransform);
if (binormalAttribute != null)
transform(vertices.items, offset, binormalAttribute, norTransform);
if (tangentAttribute != null)
transform(vertices.items, offset, tangentAttribute, norTransform);
return (vertices.size / stride) - 1;
}
/** Adds one vertex to the mesh and returns its index. Vertices are NOT guaranteed to be added consecutive.
* @param values the buffer containing the vertex data, the position of the buffer will be added to the specified offset
* @param offsetInBytes the offset in bytes within the buffer to the vertex to add, relative to the current position of the buffer
* @return the index of the just added vertex */
protected int vertexb (final Buffer values, final int offsetInBytes, final VertexAttributes attributes) {
return vertexf(asFloatArray(asFloatBuffer(values), stride), offsetInBytes / 4, attributes);
}
/** Adds one vertex to the mesh and returns its index. Vertices are NOT guaranteed to be added consecutive.
* @param values the array containing the vertex data
* @param offsetInFloats the offset in within the array to the vertex to add
* @return the index of the just added vertex */
protected int vertexf (final float[] values, final int offsetInFloats, final VertexAttributes attributes) {
vertices.addAll(values, offsetInFloats, stride);
//FIXME attributes
return (vertices.size / stride) - 1;
}
/** Adds one vertex to the mesh and returns its index. Vertices are NOT guaranteed to be added consecutive.
* @param values the buffer containing the vertex data, the position of the buffer will be added to the specified offset
* @param offsetInBytes the offset in bytes within the buffer to the vertex to add, relative to the current position of the buffer
* @param color the {@link Color} the multiply the color component with, must not be null
* @return the index of the just added vertex */
protected int vertexb (final Buffer values, final int offsetInBytes, final VertexAttributes attributes, final Color color) {
return vertexf(asFloatArray(asFloatBuffer(values), stride), offsetInBytes / 4, attributes, color);
}
/** Adds one vertex to the mesh and returns its index. Vertices are NOT guaranteed to be added consecutive.
* @param values the array containing the vertex data
* @param offsetInFloats the offset in within the array to the vertex to add
* @param color the {@link Color} the multiply the color component with, must not be null
* @return the index of the just added vertex */
protected int vertexf (final float[] values, final int offsetInFloats, final VertexAttributes attributes, final Color color) {
vertices.addAll(values, offsetInFloats, stride);
//FIXME attributes
final int offset = vertices.size - stride;
if (colorAttribute != null)
colorize(vertices.items, offset, colorAttribute, color);
return (vertices.size / stride) - 1;
}
/** Adds one vertex to the mesh and returns its index. Vertices are NOT guaranteed to be added consecutive.
* @param values the buffer containing the vertex data, the position of the buffer will be added to the specified offset
* @param offsetInBytes the offset in bytes within the buffer to the vertex to add, relative to the current position of the buffer
* @param posTransform the transformation matrix to apply to the position vector(s) of the vertex while adding it to the mesh, must not be null
* @param norTransform the transformation matrix to apply to the normal vector(s) of the vertex while adding it to the mesh, must not be null
* @return the index of the just added vertex */
protected int vertexb (final Buffer values, final int offsetInBytes, final VertexAttributes attributes, final Matrix4 posTransform, final Matrix3 norTransform) {
return vertexf(asFloatArray(asFloatBuffer(values), stride), offsetInBytes / 4, attributes, posTransform, norTransform);
}
/** Adds one vertex to the mesh and returns its index. Vertices are NOT guaranteed to be added consecutive.
* @param values the array containing the vertex data
* @param offsetInFloats the offset in within the array to the vertex to add
* @param posTransform the transformation matrix to apply to the position vector(s) of the vertex while adding it to the mesh, must not be null
* @param norTransform the transformation matrix to apply to the normal vector(s) of the vertex while adding it to the mesh, must not be null
* @return the index of the just added vertex */
protected int vertexf (final float[] values, final int offsetInFloats, final VertexAttributes attributes, final Matrix4 posTransform, final Matrix3 norTransform) {
vertices.addAll(values, offsetInFloats, stride);
//FIXME attributes
final int offset = vertices.size - stride;
transform(vertices.items, offset, positionAttribute, posTransform);
if (normalAttribute != null)
transform(vertices.items, offset, normalAttribute, norTransform);
if (binormalAttribute != null)
transform(vertices.items, offset, binormalAttribute, norTransform);
if (tangentAttribute != null)
transform(vertices.items, offset, tangentAttribute, norTransform);
return (vertices.size / stride) - 1;
}
/** Adds one vertex to the mesh and returns its index. Vertices are NOT guaranteed to be added consecutive.
* @param values the buffer containing the vertex data, the position of the buffer will be added to the specified offset
* @param offsetInBytes the offset in bytes within the buffer to the vertex to add, relative to the current position of the buffer
* @param color the {@link Color} the multiply the color component with, must not be null
* @param posTransform the transformation matrix to apply to the position vector(s) of the vertex while adding it to the mesh, must not be null
* @param norTransform the transformation matrix to apply to the normal vector(s) of the vertex while adding it to the mesh, must not be null
* @return the index of the just added vertex */
protected int vertexb (final Buffer values, final int offsetInBytes, final VertexAttributes attributes, final Color color, final Matrix4 posTransform, final Matrix3 norTransform) {
return vertexf(asFloatArray(asFloatBuffer(values), stride), offsetInBytes / 4, attributes, color, posTransform, norTransform);
}
/** Adds one vertex to the mesh and returns its index. Vertices are NOT guaranteed to be added consecutive.
* @param values the array containing the vertex data
* @param offsetInFloats the offset in within the array to the vertex to add
* @param color the {@link Color} the multiply the color component with, must not be null
* @param posTransform the transformation matrix to apply to the position vector(s) of the vertex while adding it to the mesh, must not be null
* @param norTransform the transformation matrix to apply to the normal vector(s) of the vertex while adding it to the mesh, must not be null
* @return the index of the just added vertex */
protected int vertexf (final float[] values, final int offsetInFloats, final VertexAttributes attributes, final Color color, final Matrix4 posTransform, final Matrix3 norTransform) {
vertices.addAll(values, offsetInFloats, stride);
//FIXME attributes
final int offset = vertices.size - stride;
if (colorAttribute != null)
colorize(vertices.items, offset, colorAttribute, color);
transform(vertices.items, offset, positionAttribute, posTransform);
if (normalAttribute != null)
transform(vertices.items, offset, normalAttribute, norTransform);
if (binormalAttribute != null)
transform(vertices.items, offset, binormalAttribute, norTransform);
if (tangentAttribute != null)
transform(vertices.items, offset, tangentAttribute, norTransform);
return (vertices.size / stride) - 1;
}
private final static Vector3 tmpV = new Vector3();
private static void transform(final float[] values, int offset, VertexAttribute attribute, Matrix4 transform) {
offset += attribute.offset/4;
if (attribute.numComponents > 2) {
tmpV.set(values[offset], values[offset+1], values[offset+2]).mul(transform);
values[offset] = tmpV.x;
values[offset+1] = tmpV.y;
values[offset+2] = tmpV.z;
} else {
tmpV.set(values[offset], values[offset+1], 0f).mul(transform);
values[offset] = tmpV.x;
values[offset+1] = tmpV.y;
}
}
private static void transform(final float[] values, int offset, VertexAttribute attribute, Matrix3 transform) {
offset += attribute.offset/4;
if (attribute.numComponents > 2) {
tmpV.set(values[offset], values[offset+1], values[offset+2]).mul(transform);
values[offset] = tmpV.x;
values[offset+1] = tmpV.y;
values[offset+2] = tmpV.z;
} else {
tmpV.set(values[offset], values[offset+1], 0f).mul(transform);
values[offset] = tmpV.x;
values[offset+1] = tmpV.y;
}
}
private final static Color tmpC = new Color();
private static void colorize(final float[] values, int offset, VertexAttribute attribute, Color color) {
if (attribute.type == GL20.GL_UNSIGNED_BYTE) {
values[offset] = tmpC.set(NumberUtils.floatToIntColor(values[offset])).mul(color).toFloatBits();
} else {
values[offset] *= color.r;
values[offset + 1] *= color.g;
values[offset + 2] *= color.b;
if (attribute.numComponents > 3)
values[offset + 3] *= color.a;
}
}
/** Adds an index to the mesh. The specified value is not checked on validity.
* @param value the index to add, must not be negative */
protected void index (final short value) {
indices.add(value);
}
/** Adds the specified mesh part to the mesh, optionally transforming and coloring the vertices while adding them.
* @param vertices the array containing the vertex data, which is indexed using the indices array
* @param indices the array containing the indices within the vertices array of the vertices to add
* @param offset the index of the first index to add if indexed or if not indexed the offset in number of floats in the vertices array
* @param count the number of indices to add if indexed or the number of vertices if not indexed
* @param color the color to multiply the color components of the vertices with while adding them to the mesh, may be null
* @param posTransform the transformation matrix to apply to the position vector(s) of the vertices while adding them to the mesh, may be null
* @param norTransform the transformation matrix to apply to the normal vector(s) of the vertex while adding it to the mesh, may be null */
protected void add (final float[] vertices, final short[] indices, final VertexAttributes attributes, int offset, final int count, final Color color,
final Matrix4 posTransform, final Matrix3 norTransform) {
if (indices != null && indices.length > 0) {
if (indicesMap == null)
indicesMap = new IntIntMap(count);
if (posTransform == null) {
if (color != null) {
if (attributes != null) {
for (int i = 0; i < count; ++i) {
final int sIndex = indices[i];
int dIndex = indicesMap.get(sIndex, -1);
if (dIndex < 0)
indicesMap.put(sIndex, dIndex = vertexf(vertices, sIndex * stride, attributes, color));
index((short)dIndex);
}
} else {
for (int i = 0; i < count; ++i) {
final int sIndex = indices[i];
int dIndex = indicesMap.get(sIndex, -1);
if (dIndex < 0)
indicesMap.put(sIndex, dIndex = vertexf(vertices, sIndex * stride, color));
index((short)dIndex);
}
}
} else {
if (attributes != null) {
for (int i = 0; i < count; ++i) {
final int sIndex = indices[i];
int dIndex = indicesMap.get(sIndex, -1);
if (dIndex < 0)
indicesMap.put(sIndex, dIndex = vertexf(vertices, sIndex * stride));
index((short)dIndex);
}
} else {
for (int i = 0; i < count; ++i) {
final int sIndex = indices[i];
int dIndex = indicesMap.get(sIndex, -1);
if (dIndex < 0)
indicesMap.put(sIndex, dIndex = vertexf(vertices, sIndex * stride, attributes));
index((short)dIndex);
}
}
}
} else {
if (color != null) {
if (attributes != null) {
for (int i = 0; i < count; ++i) {
final int sIndex = indices[i];
int dIndex = indicesMap.get(sIndex, -1);
if (dIndex < 0)
indicesMap.put(sIndex, dIndex = vertexf(vertices, sIndex * stride, attributes, color, posTransform, norTransform));
index((short)dIndex);
}
} else {
for (int i = 0; i < count; ++i) {
final int sIndex = indices[i];
int dIndex = indicesMap.get(sIndex, -1);
if (dIndex < 0)
indicesMap.put(sIndex, dIndex = vertexf(vertices, sIndex * stride, color, posTransform, norTransform));
index((short)dIndex);
}
}
} else {
if (attributes != null) {
for (int i = 0; i < count; ++i) {
final int sIndex = indices[i];
int dIndex = indicesMap.get(sIndex, -1);
if (dIndex < 0)
indicesMap.put(sIndex, dIndex = vertexf(vertices, sIndex * stride, attributes, posTransform, norTransform));
index((short)dIndex);
}
} else {
for (int i = 0; i < count; ++i) {
final int sIndex = indices[i];
int dIndex = indicesMap.get(sIndex, -1);
if (dIndex < 0)
indicesMap.put(sIndex, dIndex = vertexf(vertices, sIndex * stride, posTransform, norTransform));
index((short)dIndex);
}
}
}
}
} else {
if (posTransform == null) {
if (color != null) {
if (attributes != null) {
for (int i = 0; i < count; ++i) {
index((short)vertexf(vertices, offset, attributes, color));
offset += stride;
}
} else {
for (int i = 0; i < count; ++i) {
index((short)vertexf(vertices, offset, color));
offset += stride;
}
}
} else {
if (attributes != null) {
for (int i = 0; i < count; ++i) {
index((short)vertexf(vertices, offset, attributes));
offset += stride;
}
} else {
for (int i = 0; i < count; ++i) {
index((short)vertexf(vertices, offset));
offset += stride;
}
}
}
} else {
final Matrix3 nt = norTransform != null ? norTransform : tempM3.set(posTransform).inv().transpose();
if (color != null) {
if (attributes != null) {
for (int i = 0; i < count; ++i) {
index((short)vertexf(vertices, offset, attributes, posTransform, nt));
offset += stride;
}
} else {
for (int i = 0; i < count; ++i) {
index((short)vertexf(vertices, offset, posTransform, nt));
offset += stride;
}
}
} else {
if (attributes != null) {
for (int i = 0; i < count; ++i) {
index((short)vertexf(vertices, offset, attributes, color, posTransform, nt));
offset += stride;
}
} else {
for (int i = 0; i < count; ++i) {
index((short)vertexf(vertices, offset, color, posTransform, nt));
offset += stride;
}
}
}
}
}
}
/** Adds the specified mesh part to the mesh, optionally transforming and coloring the vertices while adding them.
* @param vertices the array containing the vertex data, which is indexed using the indices array
* @param indices the array containing the indices within the vertices array of the vertices to add
* @param offset the index of the first index to add if indexed or if not indexed the offset in number of bytes in the vertices array
* @param count the number of indices to add if indexed or the number of vertices if not indexed
* @param color the color to multiply the color components of the vertices with while adding them to the mesh, may be null
* @param posTransform the transformation matrix to apply to the position vector(s) of the vertices while adding them to the mesh, may be null
* @param norTransform the transformation matrix to apply to the normal vector(s) of the vertex while adding it to the mesh, may be null */
protected void add (final Buffer vertices, final short[] indices, final VertexAttributes attributes, final int offset, final int count,
final Color color, final Matrix4 posTransform, final Matrix3 norTransform) {
add(asFloatArray(asFloatBuffer(vertices)), indices, attributes, indices != null && indices.length > 0 ? offset : offset / 4, count,
color, posTransform, norTransform);
}
/** Adds the specified mesh part to the mesh, optionally transforming and coloring the vertices while adding them.
* @param vertices the array containing the vertex data, which is indexed using the indices array
* @param indices the array containing the indices within the vertices array of the vertices to add
* @param offset the index of the first index to add if indexed or if not indexed the offset in number of bytes in the vertices array
* @param count the number of indices to add if indexed or the number of vertices if not indexed
* @param color the color to multiply the color components of the vertices with while adding them to the mesh, may be null
* @param posTransform the transformation matrix to apply to the position vector(s) of the vertices while adding them to the mesh, may be null
* @param norTransform the transformation matrix to apply to the normal vector(s) of the vertex while adding it to the mesh, may be null */
protected void add (final Buffer vertices, final ShortBuffer indices, final VertexAttributes attributes, final int offset, final int count,
final Color color, final Matrix4 posTransform, final Matrix3 norTransform) {
final short[] data = indices != null ? new short[count] : null;
if (data != null) {
final int pos = indices.position();
indices.position(offset);
indices.get(data);
indices.position(pos);
}
add(vertices, data, attributes, data == null ? offset : 0, count, color, posTransform, norTransform);
}
private final static FloatBuffer asFloatBuffer(final Buffer data) {
FloatBuffer buffer = null;
if (data instanceof ByteBuffer)
buffer = ((ByteBuffer)data).asFloatBuffer();
else if (data instanceof FloatBuffer) buffer = (FloatBuffer)data;
if (buffer == null) throw new GdxRuntimeException("data must be a ByteBuffer or FloatBuffer");
return buffer;
}
private final static float[] asFloatArray(final FloatBuffer buffer) {
return asFloatArray(buffer, buffer.remaining());
}
private final static float[] asFloatArray(final FloatBuffer buffer, int len) {
final int pos = buffer.position();
final float[] result = new float[len];
buffer.get(result);
buffer.position(pos);
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment