Hi, I have a problem.
In my app all goes well, but I thought to do some optimization of the Java code, to reduce the amount of the Garbage collector work. I draw a quad with border, using a custom mesh. The code is:
The difference between two versions is only the reuse of the old containers. Vector3f[] vertices is global to the class, but can also be a local created variable, in the caller’s code
[java]
private void fillVerticeBuffer2(Vector3f downLeft, Vector3f upLeft, Vector3f downRight, Vector3f upRight, float edgeThickness, float borderSize, float overrun, Vector3f[] vertices)
{
Vector3f lToRUnitLength = downRight.subtract(downLeft).normalize();
Vector3f lToRBorderVect = lToRUnitLength.mult(borderSize);
Vector3f dToUpUnitLength = upLeft.subtract(downLeft).normalize();
Vector3f dToUpBorderVect = dToUpUnitLength.mult(borderSize);
Vector3f overrunVector = lToRUnitLength.mult(overrun);
Vector3f edgeThicknessVector = dToUpUnitLength.mult(edgeThickness);
Vector3f tmp = downLeft.subtract(overrunVector);
vertices[5] = new Vector3f(tmp.x, tmp.y, tmp.z);
tmp = downRight.add(overrunVector);
vertices[9] = new Vector3f(tmp.x, tmp.y, tmp.z);
tmp = vertices[5].subtract(lToRBorderVect);
vertices[2] = new Vector3f(tmp.x, tmp.y, tmp.z);
tmp = vertices[9].add(lToRBorderVect);
vertices[3] = new Vector3f(tmp.x, tmp.y, tmp.z);
tmp = vertices[2].subtract(dToUpBorderVect);
vertices[0] = new Vector3f(tmp.x, tmp.y, tmp.z);
tmp = vertices[3].subtract(dToUpBorderVect);
vertices[1] = new Vector3f(tmp.x, tmp.y, tmp.z);
vertices[4] = new Vector3f(vertices[2].x, vertices[2].y, vertices[2].z);
tmp = vertices[5].add(edgeThicknessVector);
vertices[7] = new Vector3f(tmp.x, tmp.y, tmp.z);
tmp = vertices[7].subtract(lToRBorderVect);
vertices[6] = new Vector3f(tmp.x, tmp.y, tmp.z);
vertices[8] = new Vector3f(vertices[5].x, vertices[5].y, vertices[5].z);
vertices[10] = new Vector3f(vertices[7].x, vertices[7].y, vertices[7].z);
tmp = vertices[9].add(edgeThicknessVector);
vertices[11] = new Vector3f(tmp.x, tmp.y, tmp.z);
vertices[12] = new Vector3f(vertices[9].x, vertices[9].y, vertices[9].z);
vertices[13] = new Vector3f(vertices[3].x, vertices[3].y, vertices[3].z);
vertices[14] = new Vector3f(vertices[11].x, vertices[11].y, vertices[11].z);
tmp = vertices[14].add(lToRBorderVect);
vertices[15] = new Vector3f(tmp.x, tmp.y, tmp.z);
vertices[16] = new Vector3f(vertices[6].x, vertices[6].y, vertices[6].z);
vertices[17] = new Vector3f(vertices[15].x, vertices[15].y, vertices[15].z);
tmp = vertices[16].add(dToUpBorderVect);
vertices[18] = new Vector3f(tmp.x, tmp.y, tmp.z);
tmp = vertices[17].add(dToUpBorderVect);
vertices[19] = new Vector3f(tmp.x, tmp.y, tmp.z);
}
public void updateVerticeBuffer(Vector3f downLeft, Vector3f upLeft, Vector3f downRight, Vector3f upRight, float edgeThickness, float borderSize, float overrun)
{
fillVerticeBuffer2(downLeft, upLeft, downRight, upRight, edgeThickness, borderSize, overrun, vertices);
this.clearBuffer(Type.Position);
this.setBuffer(Type.Position, 3, BufferUtils.createFloatBuffer(vertices));
this.setDynamic();
this.updateBound();
}
[/java]
The result is:
http://i.imgur.com/JJ2m6.png
This is called every simulation step in my app, and you can imagine that always there are new Vector3f created. And also the JMonkeyEngine BufferUtils create new buffer every time. I thought not to create a new vector in vertices (Vector3f [] vertices;), but to reuse the ones I crated in constructor, just to assign the x, y, z values. So, I changed the code to:
[java]
private void fillVerticeBuffer(Vector3f downLeft, Vector3f upLeft, Vector3f downRight, Vector3f upRight, float edgeThickness, float borderSize, float overrun, Vector3f[] vertices)
{
Vector3f lToRUnitLength = downRight.subtract(downLeft).normalize();
Vector3f lToRBorderVect = lToRUnitLength.mult(borderSize);
Vector3f dToUpUnitLength = upLeft.subtract(downLeft).normalize();
Vector3f dToUpBorderVect = dToUpUnitLength.mult(borderSize);
Vector3f overrunVector = lToRUnitLength.mult(overrun);
Vector3f edgeThicknessVector = dToUpUnitLength.mult(edgeThickness);
Vector3f tmp = downLeft.subtract(overrunVector);
vertices[5].x = tmp.x;
vertices[5].y = tmp.y;
vertices[5].x = tmp.z;
tmp = downRight.add(overrunVector);
vertices[9].x = tmp.x;
vertices[9].y = tmp.y;
vertices[9].z = tmp.z;
tmp = vertices[5].subtract(lToRBorderVect);
vertices[2].x = tmp.x;
vertices[2].y = tmp.y;
vertices[2].z = tmp.z;
tmp = vertices[9].add(lToRBorderVect);
vertices[3].x = tmp.x;
vertices[3].y = tmp.y;
vertices[3].z = tmp.z;
tmp = vertices[2].subtract(dToUpBorderVect);
vertices[0].x = tmp.x;
vertices[0].y = tmp.y;
vertices[0].z = tmp.z;
tmp = vertices[3].subtract(dToUpBorderVect);
vertices[1].x = tmp.x;
vertices[1].y = tmp.y;
vertices[1].z = tmp.z;
vertices[4].x = vertices[2].x;
vertices[4].y = vertices[2].y;
vertices[4].z = vertices[2].z;
tmp = vertices[5].add(edgeThicknessVector);
vertices[7].x = tmp.x;
vertices[7].y = tmp.y;
vertices[7].z = tmp.z;
tmp = vertices[7].subtract(lToRBorderVect);
vertices[6].x = tmp.x;
vertices[6].y = tmp.y;
vertices[6].z = tmp.z;
vertices[8].x = vertices[5].x;
vertices[8].y = vertices[5].y;
vertices[8].z = vertices[5].z;
vertices[10].x = vertices[7].x;
vertices[10].y = vertices[7].y;
vertices[10].z = vertices[7].z;
tmp = vertices[9].add(edgeThicknessVector);
vertices[11].x = tmp.x;
vertices[11].y = tmp.y;
vertices[11].z = tmp.z;
vertices[12].x = vertices[9].x;
vertices[12].y = vertices[9].y;
vertices[12].z = vertices[9].z;
vertices[13].x = vertices[3].x;
vertices[13].y = vertices[3].y;
vertices[13].z = vertices[3].z;
vertices[14].x = vertices[11].x;
vertices[14].y = vertices[11].y;
vertices[14].z = vertices[11].z;
tmp = vertices[14].add(lToRBorderVect);
vertices[15].x = tmp.x;
vertices[15].y = tmp.y;
vertices[15].z = tmp.z;
vertices[16].x = vertices[6].x;
vertices[16].y = vertices[6].y;
vertices[16].z = vertices[6].z;
vertices[17].x = vertices[15].x;
vertices[17].y = vertices[15].y;
vertices[17].z = vertices[15].z;
tmp = vertices[16].add(dToUpBorderVect);
vertices[18].x = tmp.x;
vertices[18].y = tmp.y;
vertices[18].z = tmp.z;
tmp = vertices[17].add(dToUpBorderVect);
vertices[19].x = tmp.x;
vertices[19].y = tmp.y;
vertices[19].z = tmp.z;
}
[/java]
The result:
Is this a Java problem, or what sort of problem, because I really don’t understand why this works like that

Thank you!