getting Indexbuffer from Object 3d

Started by javamak, December 16, 2015, 04:25:01 PM

Previous topic - Next topic

javamak

Hello,

I am trying to create a TriangleIndexVertexArray Object from Object3D for GImpactMeshShape (JBullet intergration). How do I get the IndexBuffer? The below code works for BvhTriangleMeshShape but GImpactMeshShape  throws IndexOutOfBoundsException. calculating the indexBuffer is the problem. Please help.

private static TriangleIndexVertexArray createIndexVertexArray(Object3D obj) {
RigidObject ro = RigidObjectManager.getInstance().getRigidObject(obj.getName());

int numTriangles = obj.getMesh().getTriangleCount();
int numVertices = obj.getMesh().getVertexCount();
// int numVertices = obj.getMesh().getUniqueVertexCount();
//4 represents Byte size of Integer
ByteBuffer indexBuffer = BufferUtils.createByteBuffer(numVertices * 4).order(ByteOrder.nativeOrder());
for (int i=0; i<numTriangles; i++) {

indexBuffer.putInt(i);
indexBuffer.putInt(i);
indexBuffer.putInt(i);
}

ByteBuffer vertexBuffer = BufferUtils.createByteBuffer(numVertices * 4).order(ByteOrder.nativeOrder());
vertexBuffer.clear();

SimpleVector[] vertices = ro.getvController().getSourceMesh();
for (int i = 0; i < vertices.length; i++) {
vertexBuffer.putFloat(vertices[i].x);
vertexBuffer.putFloat(vertices[i].y);
vertexBuffer.putFloat(vertices[i].z);
}
vertexBuffer.rewind();
indexBuffer.rewind();

TriangleIndexVertexArray vertexArray = new TriangleIndexVertexArray(numTriangles, indexBuffer, 3*4, obj.getMesh().getVertexCount(), vertexBuffer, 3*4);
return vertexArray;
}


Thanks.

EgonOlsen

I'm not sure what this index buffer is supposed to store, but I guess these are the indices to the vertices that form a triangle!? In that case, your approach to create the index buffer is wrong...it's more complicated than that, I'm afraid. The VertexController gives you access to the vertices. It doesn't hold any information about the triangle to which a vertex belongs. Then, there's also the PolygonManager which gives you access to polygon informations, but it doesn't care about the vertex itself. You have to combine the information from both to get what you want, i.e. do something like:


  • store all vertex information obtained from the VertexController
  • make sure that no rotation, no translation and scale are applied to the object in question at that stage
  • obtain the PolygonManager from the object
  • iterate over all polygons and all (transformed) vertices of each polygon
  • ...because no transformations are applied, these vertices will be equal to the ones in your vertex array which you've created above
  • ...with that in mind, find the corresponding vertex in that array
  • ...and that's your index!

javamak

how to iterate over the polygons? pMgr.getTransformedVertex(polyID, vertexNumber)  I just see this method to get the vertex.

EgonOlsen

By counting the polyId up from 0 to the maximum!?

javamak

Thanks Egon. Got it working now. There were two mistakes
1. Wrong indices
2. Wrong vertex length passed to the TriangleIndexVertexArray Constructor. (Reason for the IndexOutofBoundException).

final code:
private ByteBuffer createIndexBuffer() {
setvController(new VertexController(object3D));

ByteBuffer indexBuffer = BufferUtils.createByteBuffer(object3D.getMesh().getVertexCount() * 4).order(ByteOrder.nativeOrder());
indexBuffer.clear();

PolygonManager pMgr = object3D.getPolygonManager();
SimpleVector vertices[] = vController.getSourceMesh();
SimpleVector vertex;
int index = 0;
for (int i=0; i<pMgr.getMaxPolygonID(); i++) {
vertex = null;
for(int vertexNumber=0; vertexNumber<3; vertexNumber++) {
vertex = pMgr.getTransformedVertex(i, vertexNumber);
index = 0;
for(SimpleVector sourceVertex : vertices){
if(sourceVertex.equals(vertex)) {
indexBuffer.putInt(index);
break;
}
index++;
}
vertexNumber++;
}
}
return indexBuffer;
}



private static TriangleIndexVertexArray createIndexVertexArray(Object3D obj) {
RigidObject ro = RigidObjectManager.getInstance().getRigidObject(obj.getName());


int numTriangles = obj.getMesh().getTriangleCount();
int numVertices = obj.getMesh().getVertexCount();

ByteBuffer indexBuffer = ro.getIndexBuffer();

ByteBuffer vertexBuffer = BufferUtils.createByteBuffer(numVertices * 4).order(ByteOrder.nativeOrder());
vertexBuffer.clear();

SimpleVector[] vertices = ro.getvController().getSourceMesh();
for (int i = 0; i < vertices.length; i++) {
vertexBuffer.putFloat(vertices[i].x);
vertexBuffer.putFloat(vertices[i].y);
vertexBuffer.putFloat(vertices[i].z);
}
vertexBuffer.rewind();
indexBuffer.rewind();

TriangleIndexVertexArray vertexArray = new TriangleIndexVertexArray(numTriangles, indexBuffer, 3*4, vertices.length, vertexBuffer, 3*4);
return vertexArray;
}

javamak

Just another issue on the same. When I do debug draw of bullet objects there is some difference in Y axis between the Object3D and Shape and this difference only appears for Cone , Pyramid and  Cube, sphere or ellipsoid works fine. See the attachment. Both are having same GImpactShape created using the code in my previous replies.




EgonOlsen

Most likely a difference in the rotation pivot between both worlds. Either set the one from bullet (if possible) to the one of jPCT or vice versa. A simple "fix" might be to do a

obj.setRotationPivot(new SimpleVector());

right after calling build();.

javamak

I tried it but no luck. I tried to set the setCenterOfMassTransform of the RigidBody to 0 still no luck. It is just 3 unit difference in Y axis when I add it manually it messes up for other shapes.

EgonOlsen

But it has to be something like that IMHO. The pivot of the cube is at 0,0,0 due to the way in which it's build. The same goes for ellipsoids and spheres. The pivot of the cone is slightly off...so it has to have something to do with it.

javamak

After so many days of trial and error, adding obj.setCenter(new SimpleVector(0, 0, 0)); after build has fixed my problem.

EgonOlsen

The center and the rotation pivot contain the same value after build(). Usually, the center has no purpose except for collision detection methods and such. I don't see how should be related to your problem....however, as long as it helps, all is well...