How to use the getTriangle() method of the mesh class

Hello,

I am trying to calculate the surface normals for a custom mesh. For this, I need to retrieve the verts of each triangle and do certain calculations on them. However, when I try to get the triangles, I always get an OutofBounds error near the end. The following code is just a test program. The getTriangleCount() method returns the correct number of triangles(10). However when I loop the getTriangle() method always fails near the end. I also have 10 vertices and 30 indexes which connect them into 10 triangles.

For example, when I supply my full data, I have 512 triangles and I can only succesfully read 508, then I get the out of bounds error. Any suggestions?

Thanks, following is the code:

public class Test extends SimpleApplication {

private int total_points, total_triangles;

private Geometry geom;

private Vector3f[] vert_arr;

int[] indexes;

private Vector3f[] normals;

public Test(int total_points, int total_triangles, Vector3f[] vert_arr, int[] indexes, Vector3f[] normals)

{

this.total_points = total_points;

this.total_triangles = total_triangles;

this.vert_arr = vert_arr;

this.indexes = indexes;

this.normals = normals;

}

@Override

public void simpleInitApp() {

Mesh m = new Mesh();

m.setBuffer(Type.Position, 3, BufferUtils.createFloatBuffer(vert_arr));

m.setBuffer(Type.Index, 1, BufferUtils.createIntBuffer(indexes));

ObjCalcNormals(m);

geom = new Geometry(“OurMesh”, m);

Material mat = new Material(assetManager, “Common/MatDefs/Light/Lighting.j3md”);

//mat.setColor(“GlowColor”, ColorRGBA.Gray);

//geom.setShadowMode(ShadowMode.InPass);

mat.setBoolean(“m_UseMaterialColors”, true);

mat.setColor(“m_Ambient”, new ColorRGBA(0.0f, 0.0f, 0.0f, 0.0f));

mat.setColor(“m_Diffuse”, new ColorRGBA(6.0f, 6.0f, 6.0f, 0.0f));

mat.setColor(“m_Specular”, new ColorRGBA(0.5f, 0.5f, 0.5f, 0.0f));

mat.setFloat(“m_Shininess”, 1.0f);

geom.setMaterial(mat);

PointLight sun = new PointLight();

sun.setColor(ColorRGBA.White);

sun.setPosition(new Vector3f(0.0f, 10.0f, 0.0f));

sun.setRadius(15.0f);

rootNode.attachChild(geom);

rootNode.addLight(sun);

Vector3f camLoc = cam.getLocation();

System.out.println(“Camera location is:n” + "X: " + camLoc.getX() + ", Y: " + camLoc.getY() + ", Z: " + camLoc.getZ());

Vector3f newCamLoc = new Vector3f(0.0f, 0.0f, 10.0f);

cam.setLocation(newCamLoc);

cam.setFrustumPerspective(15.0f, 1.0f, 1.0f, 1000.0f);

}

public void ObjCalcNormals(Mesh p_object)

{

int l_connections_qty[] = new int [2000];

MathVect mv = new MathVect();

// some local vectors

Vector3f l_vect1 = new Vector3f();

Vector3f l_vect2 = new Vector3f();

Vector3f l_vect3 = new Vector3f();

Vector3f l_vect_b1 = new Vector3f();

Vector3f l_vect_b2 = new Vector3f();

Vector3f l_normal = new Vector3f();

normals = new Vector3f[p_object.getVertexCount()];

System.out.println("No. triangles: " + p_object.getTriangleCount());

System.out.println("No. verteces: " + p_object.getVertexCount());

//Resetting the vertices’ normals…

for (int i = 0; i < p_object.getVertexCount(); i++)

{

normals = new Vector3f(0, 0, 0);

l_connections_qty = 0;

}

System.out.println("t. triangles " + total_triangles);

VertexBuffer vb = p_object.getBuffer(Type.Index);

System.out.println(“THE VERTEX BUFFER:n” + vb.toString());

int tt = p_object.getTriangleCount();

for (int i = 0; i < tt; i++)

{

int j = p_object.getTriangleCount() - i;

p_object.getTriangle(i, l_vect1, l_vect2, l_vect3);

System.out.println("index: " + i + ", v1: " + l_vect1 + ", v2: " + l_vect2 + ", v3: " + l_vect3);

System.out.println("THE j: " + j);

// Polygon Normal Calculation

mv.VectCreate(l_vect1, l_vect2, l_vect_b1);

mv.VectCreate(l_vect1, l_vect3, l_vect_b2);

l_normal = mv.VectDotProduct(l_vect_b1, l_vect_b2, l_normal);

mv.VectNormalize(l_normal);



}

}

/**

  • @param args

    */

    public static void main(String[] args) {

    int[] ind = { 1, 2, 3, 1, 3, 4, 1, 4, 5, 1, 5, 2, 2, 6, 7, 7, 3, 2, 3, 7, 8, 3, 8, 9, 9, 4, 3, 4, 9, 10};

    Vector3f[] verts = new Vector3f[10];

    for(int i = 0; i <= 9; i++)

    {

    verts = new Vector3f();

    }

    verts[0].setX(0);

    verts[0].setY(0);

    verts[0].setZ((float)0.198816186);

    verts[1].setX((float) 0.03973767);

    verts[1].setY(0);

    verts[1].setZ((float)0.19977479);

    verts[2].setX((float)-1.66978482E-0);

    verts[2].setY((float)0.038200222);

    verts[2].setZ((float)0.192045482);

    verts[3].setX((float) -0.038517527);

    verts[3].setY((float) -3.36730936E-0);

    verts[3].setZ((float)0.193640683);

    verts[4].setX((float) 4.92177686E-0);

    verts[4].setY((float)-0.037532375);

    verts[4].setZ((float)0.188687986);

    verts[5].setX((float)0.082237725);

    verts[5].setY(0);

    verts[5].setZ((float)0.19853942);

    verts[6].setX((float) 0.0556650173);

    verts[6].setY((float) 0.0556650198);

    verts[6].setZ((float)0.190052255);

    verts[7].setX((float) -3.16140645E-09);

    verts[7].setY((float) 0.0723245463);

    verts[7].setZ((float) 0.174606895);

    verts[8].setX((float) -0.0525072799);

    verts[8].setY((float) 0.0525072731);

    verts[8].setZ((float) 0.17927105);

    verts[9].setX((float) -0.0742223862);

    verts[9].setY((float) -6.48872734E-09);

    verts[9].setZ((float) 0.179188686);

    Test tst = new Test(10, 10, verts, ind, null);

    tst.start();

    }

    }

    Note: I am using JME3 under Eclipse on a Linux Machine

The error is on the following line:



p_object.getTriangle(i, l_vect1, l_vect2, l_vect3);

The problem was with some of my indices when reading from the file. I changed the way I read the input and now my mesh is correctly lit.