Guest User

Untitled

a guest
Jan 23rd, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.52 KB | None | 0 0
  1. /**Создает ограничивающую фигуру для динамичного объекта
  2. * @param model - 3D модель
  3. * @param optimize - флаг определяющий необходимость применения оптимизации (упрощения)
  4. * @param numMesh - более не используется (ограничивающая фигура строится по всем имеющимся Mesh)*/
  5. public static btConvexHullShape createConvexHullShape (final Model model, boolean optimize, int numMesh) {
  6. btConvexHullShape shape = null;
  7. int workBufferSize=0;
  8. int numVertex=0;
  9. for (int i = 0; i <= model.meshParts.size-1;i++) {
  10. final Mesh mesh = model.meshParts.get(i).mesh;
  11. numVertex += mesh.getNumVertices();
  12. workBufferSize += mesh.getVerticesBuffer().capacity();
  13. }
  14. FloatBuffer workBuffer = BufferUtils.newFloatBuffer(workBufferSize);
  15. for (int i = 0; i <= model.meshParts.size-1;i++) {
  16. Matrix4 transform = new Matrix4(model.nodes.get(0).localTransform);
  17. // transform.setToTranslation(model.meshParts.get(i).center.add(model.meshParts.get(i).halfExtents));
  18. final Mesh mesh = model.meshParts.get(i).mesh;
  19.  
  20. FloatBuffer tempWorkBuffer = BufferUtils.newFloatBuffer(mesh.getVerticesBuffer().capacity());
  21. BufferUtils.copy(mesh.getVerticesBuffer(), tempWorkBuffer, mesh.getNumVertices() * mesh.getVertexSize() / 4);
  22. BufferUtils.transform(tempWorkBuffer, 3, mesh.getVertexSize(), mesh.getNumVertices(), transform);
  23.  
  24. BufferUtils.copy(tempWorkBuffer, workBuffer, mesh.getNumVertices() * mesh.getVertexSize() / 4);
  25. }
  26. // Без оптимизации сверх точная ограничивающая фигура (но очень напряжно для CPU)
  27. if (optimize) {
  28. shape = new btConvexHullShape(workBuffer, numVertex, model.meshes.get(0).getVertexSize());
  29. return shape;
  30. }
  31.  
  32. // First create a shape using all the vertices, then use the built in tool to reduce
  33. // the number of vertices to a manageable amount.
  34. btConvexShape convexShape = new btConvexHullShape(workBuffer, numVertex, model.meshes.get(0).getVertexSize());
  35. btShapeHull hull = new btShapeHull(convexShape);
  36. hull.buildHull(convexShape.getMargin());
  37. shape = new btConvexHullShape(hull);
  38.  
  39. convexShape.dispose();
  40. hull.dispose();
  41. return shape;
  42. }
Add Comment
Please, Sign In to add comment