There is a normal calculation for mesh from class AseToJme.java

[java] private void computeNormals() {
if (numOfObjects <= 0) {
return;
}

        Vector3f vector1 = new Vector3f();
        Vector3f vector2 = new Vector3f();
        Vector3f vector3 = new Vector3f();

        // Go through each of the objects to calculate their normals
        for (int index = 0; index &lt; numOfObjects; index++) {
            // Get the current object
            ASEObject object = objectList.get(index);
            // Here we allocate all the memory we need to calculate the normals
            Vector3f[] tempNormals = new Vector3f[object.faces.length];
            Vector3f[] normals = new Vector3f[object.tm.getVertexCount()];

            // Go though all of the faces of this object
            for (int i = 0; i &lt; object.faces.length; i++) {
                BufferUtils.populateFromBuffer(vector1, object.tm.getVertexBuffer(), object.faces[i].vertIndex[0]);
                BufferUtils.populateFromBuffer(vector2, object.tm.getVertexBuffer(), object.faces[i].vertIndex[1]);
                BufferUtils.populateFromBuffer(vector3, object.tm.getVertexBuffer(), object.faces[i].vertIndex[2]);
                
                vector1.subtractLocal(vector3);
                
                tempNormals[i] = vector1.cross(vector3.subtract(vector2)).normalizeLocal();
            }

            Vector3f sum = new Vector3f();
            int shared = 0;

            for (int i = 0; i &lt; object.tm.getVertexCount(); i++) {
                for (int j = 0; j &lt; object.faces.length; j++) {
                    if (object.faces[j].vertIndex[0] == i
                        || object.faces[j].vertIndex[1] == i
                        || object.faces[j].vertIndex[2] == i) {
                        sum.addLocal(tempNormals[j]);

                        shared++;
                    }
                }

                normals[i] = sum.divide((-shared)).normalizeLocal();

                sum.zero(); // Reset the sum
                shared = 0; // Reset the shared
            }

            object.tm.setNormalBuffer(BufferUtils.createFloatBuffer(normals));

        }
    }[/java]

There is a method above to compute the normals of an object(as well as a mesh object) for smooth shading. I think it belongs to jme and I need to change some to adapt this method to jme3. Can anyone help me on this issue?

such as:

[java]object.faces.length[/java]

–> [java]object.getTriangleCount(); [/java]

and

[java]BufferUtils.populateFromBuffer(vector1, object.tm.getVertexBuffer(), object.faces[i].vertIndex[0]);
BufferUtils.populateFromBuffer(vector2, object.tm.getVertexBuffer(), object.faces[i].vertIndex[1]);
BufferUtils.populateFromBuffer(vector3, object.tm.getVertexBuffer(), object.faces[i].vertIndex[2]);[/java]

–> [java]object.getTriangle(i,vector1,vector2,vector3);[/java]

and so on…

object.tm = Mesh;
faces.length = mesh.getTriangleCount()
object.faces[j] =
Triangle t;
mesh.getTriangle(j,t);
.vertIndex1 = t.get1();
.vertIndex2 = t.get2();
.vertIndex3 = t.get3();

This is should make this jme3 compliant

2 Likes
@t0neg0d said: object.tm = Mesh; faces.length = mesh.getTriangleCount() object.faces[j] = Triangle t; mesh.getTriangle(j,t); .vertIndex1 = t.get1(); .vertIndex2 = t.get2(); .vertIndex3 = t.get3();

This is should make this jme3 compliant

It does not actually since if statement between lines 36 and 39:

[java]if (object.faces[j].vertIndex[0] == i || object.faces[j].vertIndex[1] == i || object.faces[j].vertIndex[2] == i)[/java]
–>turns into
[java] m.getTriangle(j,t);
if (t.get1() == i || t.get2() == i || t.get3() == i)[/java]

For the statement above, (I actually do not know what we check but) [java]t.get1()[/java] returns a [java]Vector3f [/java]and it checks the equality between this [java]Vector3f[/java] and [java]int i[/java] which are incomparable…=(

@t0neg0d said: object.tm = Mesh; faces.length = mesh.getTriangleCount() object.faces[j] = Triangle t; mesh.getTriangle(j,t); .vertIndex1 = t.get1(); .vertIndex2 = t.get2(); .vertIndex3 = t.get3();

This is should make this jme3 compliant

Should below make it?

[java]if (verticeList.indexOf(t.get1()) == i || verticeList.indexOf(t.get2()) == i || verticeList.indexOf(t.get3()) == i) [/java]

where verticeList is my arraylist that holds the vertices to be converted to an array later to set the buffers.

Well, when I run this code and see what my normals are after all, I got them as follows:

[java]for (int i =0; i<normals.length;i++){
System.out.println(normals[i]);
}[/java]

Output:
(0.38268343, -0.0, 0.92387956)
(-0.0, -0.0, 1.0)
(0.92387956, -0.0, 0.38268346)
(0.3826835, -0.0, 0.92387956)
(-0.0, -0.0, 1.0)
(0.92387956, -0.0, 0.3826834)
(0.27059805, -0.27059805, 0.92387956)
(-0.0, -0.3826835, 0.92387956)
(0.5, -0.5, 0.7071068)
(0.7071068, -0.0, -0.7071068)
(0.70710677, -0.0, -0.70710677)
(0.7071068, -0.0, -0.7071068)
(0.92387956, -0.0, -0.3826834)
(0.92387956, -0.0, -0.3826835)
(0.92387956, -0.0, -0.38268343)
(1.0, -0.0, -0.0)
(1.0, -0.0, -0.0)
(1.0, -0.0, -0.0)
(0.92387956, -0.0, 0.38268343)
(0.92387956, -0.0, 0.3826835)
(0.92387956, -0.0, 0.38268343)
(0.7071068, -0.0, 0.7071068)
(0.70710677, -0.0, 0.70710677)
(0.7071068, -0.0, 0.7071068)
(0.38268343, -0.0, 0.92387956)
(0.32744157, -0.0, 0.9448714)
(0.18611532, 0.37223065, 0.9092884)
(-0.0, -0.0, 1.0)
(-0.0, -0.0, 1.0)
(-0.0, 0.43658867, 0.8996612)
(0.92387956, -0.0, 0.38268343)
(0.8384759, -0.0, 0.5449388)
(0.24791996, 0.49583992, 0.832273)
(0.7071068, -0.0, -0.7071068)
(0.70710677, -0.0, -0.70710677)
(0.7071068, -0.0, -0.7071068)
(0.92387956, -0.0, -0.38268343)
(0.92387956, -0.0, -0.38268343)
(0.92387956, -0.0, -0.3826834)
(1.0, -0.0, -0.0)
(1.0, -0.0, -0.0)
(1.0, -0.0, -0.0)
(0.92387956, -0.0, 0.38268343)
(0.92387956, -0.0, 0.3826835)
(0.92387956, -0.0, 0.38268343)
(0.7071068, -0.0, 0.7071068)
(0.70710677, -0.0, 0.70710677)
(0.7071068, -0.0, 0.7071068)
(-0.0, -0.7071068, 0.7071068)
(-0.0, -0.70710677, 0.70710677)
(-0.0, -0.7071068, 0.7071068)
(-0.0, -0.92387956, 0.38268343)
(-0.0, -0.92387956, 0.38268343)
(-0.0, -0.92387956, 0.38268343)
(-0.0, -1.0, -0.0)
(-0.0, -1.0, -0.0)
(-0.0, -1.0, -0.0)
(-0.0, -0.38268343, 0.92387956)
(-0.0, -0.38268352, 0.92387956)
(-0.0, -0.38268343, 0.92387956)
(-0.0, 0.70710677, 0.70710677)
(-0.0, 0.7071068, 0.7071068)
(-0.0, 0.70710677, 0.70710677)
(NaN, NaN, NaN)
(NaN, NaN, NaN)

I could not smoothened my mesh=(

In a PM you mentioned that you only have an ambient light.

Just to point out again, lighting requires a directional light. A “smooth mesh” makes no sense without light direction as light direction is required to know how bright to make a certain point.

1 Like