[HOWTO] Reflection my own way

Hi to everyone,

I hope to contribute to the engine this time, after several questions posted here.

I’ve been struggling last few days with mirroring along a given axis. Since JMonkey (as some contributor tell me) does not support the back face culling, my mirrored models showed a nasty look.

My solution acts to the mesh level, changing the position in space, the orientation of the triangles (CCW → CW or viceversa) and the normals orientation according to the mirroring axis.

Here is my solution:


public static void mirrorAlong(int axis, Geometry geometry){

int i = axis-1;

Mesh oldMesh = geometry.getMesh().deepClone();

FloatBuffer oldPoints = (FloatBuffer)oldMesh.getBuffer(Type.Position).getData();

ShortBuffer oldIndexes = (ShortBuffer)oldMesh.getBuffer(Type.Index).getData();

FloatBuffer oldNormals = (FloatBuffer)oldMesh.getBuffer(Type.Normal).getData();

//Mirror along given axis, invert normals also

while(i < oldPoints.capacity()){

float oldPoint = oldPoints.get(i);

float oldNormal = oldNormals.get(i);

oldPoints.put(i, -oldPoint);

oldNormals.put(i, -oldNormal);



//Invert wise order

i = 0;

while(i < oldIndexes.capacity()){

short oldValue = oldIndexes.get(i+2);

oldIndexes.put(i+2, oldIndexes.get(i+1));

oldIndexes.put(i+1, oldValue);



oldMesh.setBuffer(Type.Position, 3, oldPoints);

oldMesh.setBuffer(Type.Normal, 3, oldNormals);

oldMesh.setBuffer(Type.Index, 1, oldIndexes);





Is neither the fastest nor the most elegant solution, but it works. If you have suggestion to make it better and better, just let me know. I hope it will be useful or included in some future release of JME.



1 Like

Cool, you can also use Planes to mirror Vectors.

what are you planning to do with this? I guess if it’s just for a mirror on the wall or similar a shader would be a way better solution.(or is that the problem with backface culling?)


No, it is not for a mirror on the wall :slight_smile:

I’m using this routine in a clojure 3d library i’ve created (clj3d) which uses elementary primitives and affine trasformations as well, including mirroring :slight_smile:

Simply scaling by -1 doesn’t works, due to the absence of back faces and normal inversion :slight_smile: