Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**Создает ограничивающую фигуру для динамичного объекта
- * @param model - 3D модель
- * @param optimize - флаг определяющий необходимость применения оптимизации (упрощения)
- * @param numMesh - более не используется (ограничивающая фигура строится по всем имеющимся Mesh)*/
- public static btConvexHullShape createConvexHullShape (final Model model, boolean optimize, int numMesh) {
- btConvexHullShape shape = null;
- int workBufferSize=0;
- int numVertex=0;
- for (int i = 0; i <= model.meshParts.size-1;i++) {
- final Mesh mesh = model.meshParts.get(i).mesh;
- numVertex += mesh.getNumVertices();
- workBufferSize += mesh.getVerticesBuffer().capacity();
- }
- FloatBuffer workBuffer = BufferUtils.newFloatBuffer(workBufferSize);
- for (int i = 0; i <= model.meshParts.size-1;i++) {
- Matrix4 transform = new Matrix4(model.nodes.get(0).localTransform);
- // transform.setToTranslation(model.meshParts.get(i).center.add(model.meshParts.get(i).halfExtents));
- final Mesh mesh = model.meshParts.get(i).mesh;
- FloatBuffer tempWorkBuffer = BufferUtils.newFloatBuffer(mesh.getVerticesBuffer().capacity());
- BufferUtils.copy(mesh.getVerticesBuffer(), tempWorkBuffer, mesh.getNumVertices() * mesh.getVertexSize() / 4);
- BufferUtils.transform(tempWorkBuffer, 3, mesh.getVertexSize(), mesh.getNumVertices(), transform);
- BufferUtils.copy(tempWorkBuffer, workBuffer, mesh.getNumVertices() * mesh.getVertexSize() / 4);
- }
- // Без оптимизации сверх точная ограничивающая фигура (но очень напряжно для CPU)
- if (optimize) {
- shape = new btConvexHullShape(workBuffer, numVertex, model.meshes.get(0).getVertexSize());
- return shape;
- }
- // First create a shape using all the vertices, then use the built in tool to reduce
- // the number of vertices to a manageable amount.
- btConvexShape convexShape = new btConvexHullShape(workBuffer, numVertex, model.meshes.get(0).getVertexSize());
- btShapeHull hull = new btShapeHull(convexShape);
- hull.buildHull(convexShape.getMargin());
- shape = new btConvexHullShape(hull);
- convexShape.dispose();
- hull.dispose();
- return shape;
- }
Add Comment
Please, Sign In to add comment