use of java.nio.FloatBuffer in project StarWars.Android by Yalantis.
the class Buffers method makeInterleavedBuffer.
public static FloatBuffer makeInterleavedBuffer(float[] positionData, float[] normals, float[] uvData, float[] tileXyData, int tiles) {
int dataLength = positionData.length + normals.length * tiles + uvData.length + tileXyData.length;
final FloatBuffer interleavedBuffer = ByteBuffer.allocateDirect(dataLength * Const.BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
int positionOffset = 0, normalOffset = 0, textureOffset = 0, tileXyOffset = 0;
for (int i = 0; i < tiles; i++) {
for (int j = 0; j < Const.POINTS_PER_TILE; j++) {
interleavedBuffer.put(positionData, positionOffset, Const.POSITION_DATA_SIZE);
positionOffset += Const.POSITION_DATA_SIZE;
interleavedBuffer.put(normals, normalOffset, Const.NORMALS_DATA_SIZE);
// Normals are the same
interleavedBuffer.put(uvData, textureOffset, Const.TEXTURE_COORDS_DATA_SIZE);
textureOffset += Const.TEXTURE_COORDS_DATA_SIZE;
interleavedBuffer.put(tileXyData, tileXyOffset, Const.TILE_DATA_SIZE);
tileXyOffset += Const.TILE_DATA_SIZE;
}
}
interleavedBuffer.position(0);
return interleavedBuffer;
}
use of java.nio.FloatBuffer in project StarWars.Android by Yalantis.
the class ParticlesGenerator method makeInterleavedBuffer.
private FloatBuffer makeInterleavedBuffer(float[] posData, float[] uvData, float[] miscData, int numStars) {
int dataLength = posData.length + uvData.length * numStars + miscData.length;
final FloatBuffer interleavedBuffer = ByteBuffer.allocateDirect(dataLength * Const.BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
int positionOffset = 0, uvOffset = 0, starDataOffset = 0;
for (int i = 0; i < numStars; i++) {
for (int v = 0; v < 6; v++) {
interleavedBuffer.put(posData, positionOffset, ParticleSystem.POS_DATA_SIZE);
positionOffset += ParticleSystem.POS_DATA_SIZE;
interleavedBuffer.put(uvData, uvOffset, ParticleSystem.TEXTURE_COORDS_DATA_SIZE);
uvOffset = (uvOffset + ParticleSystem.TEXTURE_COORDS_DATA_SIZE) % uvData.length;
interleavedBuffer.put(miscData, starDataOffset, ParticleSystem.MISC_DATA_SIZE);
starDataOffset += ParticleSystem.MISC_DATA_SIZE;
}
}
interleavedBuffer.position(0);
return interleavedBuffer;
}
use of java.nio.FloatBuffer in project jmonkeyengine by jMonkeyEngine.
the class SkeletonControl method resetToBind.
//only do this for software updates
void resetToBind() {
for (Mesh mesh : targets) {
if (mesh.isAnimated()) {
Buffer bwBuff = mesh.getBuffer(Type.BoneWeight).getData();
Buffer biBuff = mesh.getBuffer(Type.BoneIndex).getData();
if (!biBuff.hasArray() || !bwBuff.hasArray()) {
// prepare for software animation
mesh.prepareForAnim(true);
}
VertexBuffer bindPos = mesh.getBuffer(Type.BindPosePosition);
VertexBuffer bindNorm = mesh.getBuffer(Type.BindPoseNormal);
VertexBuffer pos = mesh.getBuffer(Type.Position);
VertexBuffer norm = mesh.getBuffer(Type.Normal);
FloatBuffer pb = (FloatBuffer) pos.getData();
FloatBuffer nb = (FloatBuffer) norm.getData();
FloatBuffer bpb = (FloatBuffer) bindPos.getData();
FloatBuffer bnb = (FloatBuffer) bindNorm.getData();
pb.clear();
nb.clear();
bpb.clear();
bnb.clear();
//reseting bind tangents if there is a bind tangent buffer
VertexBuffer bindTangents = mesh.getBuffer(Type.BindPoseTangent);
if (bindTangents != null) {
VertexBuffer tangents = mesh.getBuffer(Type.Tangent);
FloatBuffer tb = (FloatBuffer) tangents.getData();
FloatBuffer btb = (FloatBuffer) bindTangents.getData();
tb.clear();
btb.clear();
tb.put(btb).clear();
}
pb.put(bpb).clear();
nb.put(bnb).clear();
}
}
}
use of java.nio.FloatBuffer in project jmonkeyengine by jMonkeyEngine.
the class NativeMeshUtil method getTriangleIndexVertexArray.
public static long getTriangleIndexVertexArray(Mesh mesh) {
ByteBuffer triangleIndexBase = BufferUtils.createByteBuffer(mesh.getTriangleCount() * 3 * 4);
ByteBuffer vertexBase = BufferUtils.createByteBuffer(mesh.getVertexCount() * 3 * 4);
int numVertices = mesh.getVertexCount();
//3 verts * 4 bytes per.
int vertexStride = 12;
int numTriangles = mesh.getTriangleCount();
//3 index entries * 4 bytes each.
int triangleIndexStride = 12;
IndexBuffer indices = mesh.getIndicesAsList();
FloatBuffer vertices = mesh.getFloatBuffer(Type.Position);
vertices.rewind();
int verticesLength = mesh.getVertexCount() * 3;
for (int i = 0; i < verticesLength; i++) {
float tempFloat = vertices.get();
vertexBase.putFloat(tempFloat);
}
int indicesLength = mesh.getTriangleCount() * 3;
for (int i = 0; i < indicesLength; i++) {
triangleIndexBase.putInt(indices.get(i));
}
vertices.rewind();
vertices.clear();
return createTriangleIndexVertexArray(triangleIndexBase, vertexBase, numTriangles, numVertices, vertexStride, triangleIndexStride);
}
use of java.nio.FloatBuffer in project jmonkeyengine by jMonkeyEngine.
the class GeometryBatchFactory method mergeGeometries.
/**
* Merges all geometries in the collection into
* the output mesh. Creates a new material using the TextureAtlas.
*
* @param geometries
* @param outMesh
*/
public static void mergeGeometries(Collection<Geometry> geometries, Mesh outMesh) {
int[] compsForBuf = new int[VertexBuffer.Type.values().length];
Format[] formatForBuf = new Format[compsForBuf.length];
boolean[] normForBuf = new boolean[VertexBuffer.Type.values().length];
int totalVerts = 0;
int totalTris = 0;
int totalLodLevels = 0;
int maxWeights = -1;
Mode mode = null;
for (Geometry geom : geometries) {
totalVerts += geom.getVertexCount();
totalTris += geom.getTriangleCount();
totalLodLevels = Math.min(totalLodLevels, geom.getMesh().getNumLodLevels());
Mode listMode;
int components;
switch(geom.getMesh().getMode()) {
case Points:
listMode = Mode.Points;
components = 0;
break;
case LineLoop:
case LineStrip:
case Lines:
listMode = Mode.Lines;
components = 2;
break;
case TriangleFan:
case TriangleStrip:
case Triangles:
listMode = Mode.Triangles;
components = 3;
break;
default:
throw new UnsupportedOperationException();
}
for (VertexBuffer vb : geom.getMesh().getBufferList().getArray()) {
int currentCompsForBuf = compsForBuf[vb.getBufferType().ordinal()];
if (vb.getBufferType() != Type.Index && currentCompsForBuf != 0 && currentCompsForBuf != vb.getNumComponents()) {
throw new UnsupportedOperationException("The geometry " + geom + " buffer " + vb.getBufferType() + " has different number of components than the rest of the meshes " + "(this: " + vb.getNumComponents() + ", expected: " + currentCompsForBuf + ")");
}
compsForBuf[vb.getBufferType().ordinal()] = vb.getNumComponents();
formatForBuf[vb.getBufferType().ordinal()] = vb.getFormat();
normForBuf[vb.getBufferType().ordinal()] = vb.isNormalized();
}
maxWeights = Math.max(maxWeights, geom.getMesh().getMaxNumWeights());
if (mode != null && mode != listMode) {
throw new UnsupportedOperationException("Cannot combine different" + " primitive types: " + mode + " != " + listMode);
}
mode = listMode;
compsForBuf[Type.Index.ordinal()] = components;
}
outMesh.setMaxNumWeights(maxWeights);
outMesh.setMode(mode);
if (totalVerts >= 65536) {
// make sure we create an UnsignedInt buffer so
// we can fit all of the meshes
formatForBuf[Type.Index.ordinal()] = Format.UnsignedInt;
} else {
formatForBuf[Type.Index.ordinal()] = Format.UnsignedShort;
}
// generate output buffers based on retrieved info
for (int i = 0; i < compsForBuf.length; i++) {
if (compsForBuf[i] == 0) {
continue;
}
Buffer data;
if (i == Type.Index.ordinal()) {
data = VertexBuffer.createBuffer(formatForBuf[i], compsForBuf[i], totalTris);
} else {
data = VertexBuffer.createBuffer(formatForBuf[i], compsForBuf[i], totalVerts);
}
VertexBuffer vb = new VertexBuffer(Type.values()[i]);
vb.setupData(Usage.Static, compsForBuf[i], formatForBuf[i], data);
vb.setNormalized(normForBuf[i]);
outMesh.setBuffer(vb);
}
int globalVertIndex = 0;
int globalTriIndex = 0;
for (Geometry geom : geometries) {
Mesh inMesh = geom.getMesh();
geom.computeWorldMatrix();
Matrix4f worldMatrix = geom.getWorldMatrix();
int geomVertCount = inMesh.getVertexCount();
int geomTriCount = inMesh.getTriangleCount();
for (int bufType = 0; bufType < compsForBuf.length; bufType++) {
VertexBuffer inBuf = inMesh.getBuffer(Type.values()[bufType]);
VertexBuffer outBuf = outMesh.getBuffer(Type.values()[bufType]);
if (inBuf == null || outBuf == null) {
continue;
}
if (Type.Index.ordinal() == bufType) {
int components = compsForBuf[bufType];
IndexBuffer inIdx = inMesh.getIndicesAsList();
IndexBuffer outIdx = outMesh.getIndexBuffer();
for (int tri = 0; tri < geomTriCount; tri++) {
for (int comp = 0; comp < components; comp++) {
int idx = inIdx.get(tri * components + comp) + globalVertIndex;
outIdx.put((globalTriIndex + tri) * components + comp, idx);
}
}
} else if (Type.Position.ordinal() == bufType) {
FloatBuffer inPos = (FloatBuffer) inBuf.getDataReadOnly();
FloatBuffer outPos = (FloatBuffer) outBuf.getData();
doTransformVerts(inPos, globalVertIndex, outPos, worldMatrix);
} else if (Type.Normal.ordinal() == bufType) {
FloatBuffer inPos = (FloatBuffer) inBuf.getDataReadOnly();
FloatBuffer outPos = (FloatBuffer) outBuf.getData();
doTransformNorms(inPos, globalVertIndex, outPos, worldMatrix);
} else if (Type.Tangent.ordinal() == bufType) {
FloatBuffer inPos = (FloatBuffer) inBuf.getDataReadOnly();
FloatBuffer outPos = (FloatBuffer) outBuf.getData();
int components = inBuf.getNumComponents();
doTransformTangents(inPos, globalVertIndex, components, outPos, worldMatrix);
} else {
inBuf.copyElements(0, outBuf, globalVertIndex, geomVertCount);
}
}
globalVertIndex += geomVertCount;
globalTriIndex += geomTriCount;
}
}
Aggregations