From f80d6474a4c9e69725a1de1fe13acdb07a56c2b4 Mon Sep 17 00:00:00 2001 From: abies Date: Mon, 28 Jul 2014 12:14:14 +0200 Subject: [PATCH] Utility methods for getting arbitrary properties for triangles from mesh buffers --- .../src/main/java/com/jme3/scene/Mesh.java | 57 ++++++++++++++++++- 1 file changed, 54 insertions(+), 3 deletions(-) diff --git a/jme3-core/src/main/java/com/jme3/scene/Mesh.java b/jme3-core/src/main/java/com/jme3/scene/Mesh.java index 8b3ffc80e..5717350fa 100644 --- a/jme3-core/src/main/java/com/jme3/scene/Mesh.java +++ b/jme3-core/src/main/java/com/jme3/scene/Mesh.java @@ -824,12 +824,28 @@ public class Mesh implements Savable, Cloneable { * @param v3 Vector to contain third vertex position */ public void getTriangle(int index, Vector3f v1, Vector3f v2, Vector3f v3){ - VertexBuffer pb = getBuffer(Type.Position); + getTriangle(Type.Position,index,v1,v2,v3); + } + + /** + * Gets the triangle vertex data at the given triangle index + * and stores them into the v1, v2, v3 arguments. Works for 3-value components like position or normals + * + * @param type buffer type to retrieve data from + * @param index The index of the triangle. + * Should be between 0 and {@link #getTriangleCount()}. + * + * @param v1 Vector to contain first vertex data + * @param v2 Vector to contain second vertex data + * @param v3 Vector to contain third vertex data + */ + public void getTriangle(Type type, int index, Vector3f v1, Vector3f v2, Vector3f v3){ + VertexBuffer pb = getBuffer(type); IndexBuffer ib = getIndicesAsList(); if (pb != null && pb.getFormat() == Format.Float && pb.getNumComponents() == 3){ FloatBuffer fpb = (FloatBuffer) pb.getData(); - // aquire triangle's vertex indices + // acquire triangle's vertex indices int vertIndex = index * 3; int vert1 = ib.get(vertIndex); int vert2 = ib.get(vertIndex+1); @@ -839,11 +855,46 @@ public class Mesh implements Savable, Cloneable { BufferUtils.populateFromBuffer(v2, fpb, vert2); BufferUtils.populateFromBuffer(v3, fpb, vert3); }else{ - throw new UnsupportedOperationException("Position buffer not set or " + throw new UnsupportedOperationException(type + " buffer not set or " + " has incompatible format"); } } + /** + * Gets the triangle vertex data at the given triangle index + * and stores them into the v1, v2, v3 arguments. Works for 2-value components like texture coordinates + * + * @param type buffer type to retrieve data from + * @param index The index of the triangle. + * Should be between 0 and {@link #getTriangleCount()}. + * + * @param v1 Vector to contain first vertex data + * @param v2 Vector to contain second vertex data + * @param v3 Vector to contain third vertex data + */ + + public void getTriangle(Type type, int index, Vector2f v1, Vector2f v2, Vector2f v3){ + VertexBuffer pb = getBuffer(type); + IndexBuffer ib = getIndicesAsList(); + if (pb != null && pb.getFormat() == Format.Float && pb.getNumComponents() == 2){ + FloatBuffer fpb = (FloatBuffer) pb.getData(); + + // acquire triangle's vertex indices + int vertIndex = index * 3; + int vert1 = ib.get(vertIndex); + int vert2 = ib.get(vertIndex+1); + int vert3 = ib.get(vertIndex+2); + + BufferUtils.populateFromBuffer(v1, fpb, vert1); + BufferUtils.populateFromBuffer(v2, fpb, vert2); + BufferUtils.populateFromBuffer(v3, fpb, vert3); + }else{ + throw new UnsupportedOperationException(type + " buffer not set or " + + " has incompatible format"); + } + } + + /** * Gets the triangle vertex positions at the given triangle index * and stores them into the {@link Triangle} argument.