/sigh… I seem to be having significant issues using the BufferUtils class.
I put together a relatively simple test to try and narrow down the issue… and here is what the test consists of:
I load a .j3o and extract the buffers like so:
[java]
Mesh template = ((Geometry)((Node)assetManager.loadModel(“Models/SomeRandomJ3O.j3o”)).getChild(0)).getMesh();
// Extract template buffers
FloatBuffer verts = template.getFloatBuffer(VertexBuffer.Type.Position);
FloatBuffer coords = template.getFloatBuffer(VertexBuffer.Type.TexCoord);
ShortBuffer indexes = template.getShortBuffer(VertexBuffer.Type.Index);
FloatBuffer normals = template.getFloatBuffer(VertexBuffer.Type.Normal);
[/java]
I pass these into a custom mesh and only attempt to recreate the buffers as they exist by creating an empty buffer of the appropriate type using the template buffers limit as a size… like such:
[java]
this.finVerts = BufferUtils.createFloatBuffer(templateVerts.limit());
this.finCoords = BufferUtils.createFloatBuffer(templateCoords.limit());
this.finIndexes = BufferUtils.createShortBuffer(templateIndexes.limit());
this.finNormals = BufferUtils.createFloatBuffer(templateNormals.limit());
[/java]
Then I proceed to populate the buffers by looping through the original using .get() and.put():
[java]
Quaternion qR = new Quaternion();
qR = qR.fromAngleAxis( ((float)(Math.random()*360f))2fFastMath.DEG_TO_RAD, Vector3f.UNIT_Y);
Vector3f position = new Vector3f(someXValue, someYValue, someZValue);
Vector3f tempVec = new Vector3f();
int index = 0;
for (int v = 0; v < templateVerts.limit(); v += 3) {
tempVec.set(templateVerts.get(v), templateVerts.get(v+1), templateVerts.get(v+2));
tempVec = qR.mult(tempVec);
tempVec.add(position);
finVerts.put(index, tempVec.getX());
finVerts.put(index+1, tempVec.getY());
finVerts.put(index+2, tempVec.getZ());
index += 3;
}
index = 0;
for (int v = 0; v < templateCoords.limit(); v++) {
finCoords.put(index, templateCoords.get(v));
index++;
}
index = 0;
for (int v = 0; v < templateIndexes.limit(); v++) {
finIndexes.put(index, templateIndexes.get(v));
index++;
}
index = 0;
for (int v = 0; v < templateNormals.limit(); v++) {
finNormals.put(index, templateNormals.get(v));
index++;
}
this.clearBuffer(Type.Position);
this.setBuffer(Type.Position, 3, finVerts);
this.clearBuffer(Type.TexCoord);
this.setBuffer(Type.TexCoord, 2, finCoords);
this.clearBuffer(Type.Index);
this.setBuffer(Type.Index, 3, finIndexes);
this.clearBuffer(Type.Normal);
this.setBuffer(Type.Normal, 3, finNormals);
this.updateBound();
[/java]
Sometimes this works, other times I get random indexOutOfBounds exceptions, sometimes I get OutOfMemory exceptions.
There doesn't seem to be any common factor as I am running the exact same code, but getting random results.
Anyone have ANY clue as to why this is happening?