use of com.jme3.scene.mesh.IndexBuffer in project jmonkeyengine by jMonkeyEngine.
the class Converter method convert.
public static synchronized IndexedMesh convert(Mesh mesh) {
IndexedMesh jBulletIndexedMesh = new IndexedMesh();
jBulletIndexedMesh.triangleIndexBase = ByteBuffer.allocate(mesh.getTriangleCount() * 3 * 4);
jBulletIndexedMesh.vertexBase = ByteBuffer.allocate(mesh.getVertexCount() * 3 * 4);
IndexBuffer indices = mesh.getIndicesAsList();
FloatBuffer vertices = mesh.getFloatBuffer(Type.Position);
vertices.rewind();
int verticesLength = mesh.getVertexCount() * 3;
jBulletIndexedMesh.numVertices = mesh.getVertexCount();
//3 verts * 4 bytes per.
jBulletIndexedMesh.vertexStride = 12;
for (int i = 0; i < verticesLength; i++) {
float tempFloat = vertices.get();
jBulletIndexedMesh.vertexBase.putFloat(tempFloat);
}
int indicesLength = mesh.getTriangleCount() * 3;
jBulletIndexedMesh.numTriangles = mesh.getTriangleCount();
//3 index entries * 4 bytes each.
jBulletIndexedMesh.triangleIndexStride = 12;
for (int i = 0; i < indicesLength; i++) {
jBulletIndexedMesh.triangleIndexBase.putInt(indices.get(i));
}
vertices.rewind();
vertices.clear();
return jBulletIndexedMesh;
}
use of com.jme3.scene.mesh.IndexBuffer in project jmonkeyengine by jMonkeyEngine.
the class Converter method convert.
public static Mesh convert(IndexedMesh mesh) {
Mesh jmeMesh = new Mesh();
jmeMesh.setBuffer(Type.Index, 3, BufferUtils.createShortBuffer(mesh.numTriangles * 3));
jmeMesh.setBuffer(Type.Position, 3, BufferUtils.createFloatBuffer(mesh.numVertices * 3));
IndexBuffer indicess = jmeMesh.getIndexBuffer();
FloatBuffer vertices = jmeMesh.getFloatBuffer(Type.Position);
for (int i = 0; i < mesh.numTriangles * 3; i++) {
indicess.put(i, mesh.triangleIndexBase.getInt(i * 4));
}
for (int i = 0; i < mesh.numVertices * 3; i++) {
vertices.put(i, mesh.vertexBase.getFloat(i * 4));
}
jmeMesh.updateCounts();
jmeMesh.updateBound();
jmeMesh.getFloatBuffer(Type.Position).clear();
return jmeMesh;
}
use of com.jme3.scene.mesh.IndexBuffer in project jmonkeyengine by jMonkeyEngine.
the class LODGeomap method writeIndexArrayLodDiff.
/**
* Create the LOD index array that will seam its edges with its neighbour's LOD.
* This is a scary method!!! It will break your mind.
*
* @param store to store the index buffer
* @param lod level of detail of the mesh
* @param rightLod LOD of the right neighbour
* @param topLod LOD of the top neighbour
* @param leftLod LOD of the left neighbour
* @param bottomLod LOD of the bottom neighbour
* @return the LOD-ified index buffer
*/
public IndexBuffer writeIndexArrayLodDiff(int lod, boolean rightLod, boolean topLod, boolean leftLod, boolean bottomLod, int totalSize) {
int numIndexes = calculateNumIndexesLodDiff(lod);
IndexBuffer ib = IndexBuffer.createIndexBuffer(numIndexes, numIndexes);
VerboseBuffer buffer = new VerboseBuffer(ib);
//System.out.println(" for (z="+lod+"; z<"+(getWidth()-(1*lod))+"; z+="+lod+")");
for (int r = lod; r < getWidth() - (2 * lod); r += lod) {
// row
int rowIdx = r * getWidth();
int nextRowIdx = (r + 1 * lod) * getWidth();
for (int c = lod; c < getWidth() - (1 * lod); c += lod) {
// column
int idx = rowIdx + c;
buffer.put(idx);
idx = nextRowIdx + c;
buffer.put(idx);
}
// add degenerate triangles
if (r < getWidth() - (3 * lod)) {
int idx = nextRowIdx + getWidth() - (1 * lod) - 1;
buffer.put(idx);
// inset by 1
idx = nextRowIdx + (1 * lod);
buffer.put(idx);
//System.out.println("");
}
}
//System.out.println("\nright:");
//int runningBufferCount = buffer.getCount();
//System.out.println("buffer start: "+runningBufferCount);
// right
int br = getWidth() * (getWidth() - lod) - 1 - lod;
// bottom right -1
buffer.put(br);
int corner = getWidth() * getWidth() - 1;
// bottom right corner
buffer.put(corner);
if (rightLod) {
// if lower LOD
for (int row = getWidth() - lod; row >= 1 + lod; row -= 2 * lod) {
int idx = (row) * getWidth() - 1 - lod;
buffer.put(idx);
idx = (row - lod) * getWidth() - 1;
buffer.put(idx);
if (row > lod + 1) {
//if not the last one
idx = (row - lod) * getWidth() - 1 - lod;
buffer.put(idx);
idx = (row - lod) * getWidth() - 1;
buffer.put(idx);
} else {
}
}
} else {
//br+1);//degenerate to flip winding order
buffer.put(corner);
for (int row = getWidth() - lod; row > lod; row -= lod) {
// mult to get row
int idx = row * getWidth() - 1;
buffer.put(idx);
buffer.put(idx - lod);
}
}
buffer.put(getWidth() - 1);
// top (the order gets reversed here so the diagonals line up)
if (topLod) {
// if lower LOD
if (rightLod) {
buffer.put(getWidth() - 1);
}
for (int col = getWidth() - 1; col >= lod; col -= 2 * lod) {
// next row
int idx = (lod * getWidth()) + col - lod;
buffer.put(idx);
idx = col - 2 * lod;
buffer.put(idx);
if (col > lod * 2) {
//if not the last one
idx = (lod * getWidth()) + col - 2 * lod;
buffer.put(idx);
idx = col - 2 * lod;
buffer.put(idx);
} else {
}
}
} else {
if (rightLod) {
buffer.put(getWidth() - 1);
}
for (int col = getWidth() - 1 - lod; col > 0; col -= lod) {
int idx = col + (lod * getWidth());
buffer.put(idx);
idx = col;
buffer.put(idx);
}
buffer.put(0);
}
buffer.put(0);
// left
if (leftLod) {
// if lower LOD
if (topLod) {
buffer.put(0);
}
for (int row = 0; row < getWidth() - lod; row += 2 * lod) {
int idx = (row + lod) * getWidth() + lod;
buffer.put(idx);
idx = (row + 2 * lod) * getWidth();
buffer.put(idx);
if (row < getWidth() - 1 - 2 * lod) {
//if not the last one
idx = (row + 2 * lod) * getWidth() + lod;
buffer.put(idx);
idx = (row + 2 * lod) * getWidth();
buffer.put(idx);
} else {
}
}
} else {
if (!topLod) {
buffer.put(0);
}
//buffer.put(0); // degenerate winding-flip
for (int row = lod; row < getWidth() - lod; row += lod) {
int idx = row * getWidth();
buffer.put(idx);
idx = row * getWidth() + lod;
buffer.put(idx);
}
}
buffer.put(getWidth() * (getWidth() - 1));
// bottom
if (bottomLod) {
// if lower LOD
if (leftLod) {
buffer.put(getWidth() * (getWidth() - 1));
}
// seemed to be fixed by making "getWidth()-1-2-lod" this: "getWidth()-1-2*lod", which seems more correct
for (int col = 0; col < getWidth() - lod; col += 2 * lod) {
int idx = getWidth() * (getWidth() - 1 - lod) + col + lod;
buffer.put(idx);
idx = getWidth() * (getWidth() - 1) + col + 2 * lod;
buffer.put(idx);
if (col < getWidth() - 1 - 2 * lod) {
//if not the last one
idx = getWidth() * (getWidth() - 1 - lod) + col + 2 * lod;
buffer.put(idx);
idx = getWidth() * (getWidth() - 1) + col + 2 * lod;
buffer.put(idx);
} else {
}
}
} else {
if (leftLod) {
buffer.put(getWidth() * (getWidth() - 1));
}
for (int col = lod; col < getWidth() - lod; col += lod) {
// up
int idx = getWidth() * (getWidth() - 1 - lod) + col;
buffer.put(idx);
// down
idx = getWidth() * (getWidth() - 1) + col;
buffer.put(idx);
}
//buffer.put(getWidth()*getWidth()-1-lod); // <-- THIS caused holes at the end!
}
buffer.put(getWidth() * getWidth() - 1);
// fill in the rest of the buffer with degenerates, there should only be a couple
for (int i = buffer.getCount(); i < numIndexes; i++) {
buffer.put(getWidth() * getWidth() - 1);
}
return buffer.delegate;
}
use of com.jme3.scene.mesh.IndexBuffer in project jmonkeyengine by jMonkeyEngine.
the class TerrainPatch method reIndexGeometry.
protected void reIndexGeometry(HashMap<String, UpdatedTerrainPatch> updated, boolean useVariableLod) {
UpdatedTerrainPatch utp = updated.get(getName());
if (utp != null && utp.isReIndexNeeded()) {
int pow = (int) Math.pow(2, utp.getNewLod());
boolean left = utp.getLeftLod() > utp.getNewLod();
boolean top = utp.getTopLod() > utp.getNewLod();
boolean right = utp.getRightLod() > utp.getNewLod();
boolean bottom = utp.getBottomLod() > utp.getNewLod();
IndexBuffer idxB;
if (useVariableLod)
idxB = geomap.writeIndexArrayLodVariable(pow, (int) Math.pow(2, utp.getRightLod()), (int) Math.pow(2, utp.getTopLod()), (int) Math.pow(2, utp.getLeftLod()), (int) Math.pow(2, utp.getBottomLod()), totalSize);
else
idxB = geomap.writeIndexArrayLodDiff(pow, right, top, left, bottom, totalSize);
Buffer b;
if (idxB.getBuffer() instanceof IntBuffer)
b = (IntBuffer) idxB.getBuffer();
else
b = (ShortBuffer) idxB.getBuffer();
utp.setNewIndexBuffer(b);
}
}
Aggregations