Illuminating a trimesh?


I have created a TriMesh which has the shape of a typical roof (it's created dynamically during the course of the program). My problem is that each triangle in it are illuminated independently from each other, which creates sharp edges between the triangles instead of an smooth transition. What I want is that the triangles which are in the same plane should be illuminated by the lightning as a unit, just as if they were a Quad for an example.

You can see the picture below what I mean. The box next to it shows more the way I would like it to look.

I tried calculating the normals for it, but I don't know if that helps or if I did it wrong. In the code below I left out how I calculate the vertexes, since these are correct and shouldn't affect anything (correct me if I'm wrong).

Anyone know how I can fix this?

Vector3f[] roofVertexes = {pointsXaxis[0].add(worldtranslation), pointsXaxis[1].add(worldtranslation), new Vector3f(pointsXaxis[0].x, height, 0).add(worldtranslation),
                         remainingEdges[0].add(worldtranslation), remainingEdges[1].add(worldtranslation), new Vector3f(remainingEdges[0].x, height, 0).add(worldtranslation),

int[] roofIndexes = {0,1,2,

// calculate normals
Triangle[] triangleVec = new Triangle[6];
triangleVec[0] = new Triangle(roofVertexes[0], roofVertexes[1], roofVertexes[2]);
triangleVec[1] = new Triangle(roofVertexes[3], roofVertexes[4], roofVertexes[5]);
triangleVec[2] = new Triangle(roofVertexes[6], roofVertexes[7], roofVertexes[8]);
triangleVec[3] = new Triangle(roofVertexes[7], roofVertexes[8], roofVertexes[9]);
triangleVec[4] = new Triangle(roofVertexes[10], roofVertexes[11], roofVertexes[12]);
triangleVec[5] = new Triangle(roofVertexes[11], roofVertexes[12], roofVertexes[13]);

for (Triangle triangle : triangleVec) {

Vector3f[] normals = {triangleVec[0].getNormal(), triangleVec[0].getNormal(), triangleVec[0].getNormal(),
                    triangleVec[1].getNormal(), triangleVec[1].getNormal(), triangleVec[1].getNormal(),
                    triangleVec[2].getNormal(), triangleVec[2].getNormal(), triangleVec[2].getNormal(),
                    triangleVec[3].getNormal(), triangleVec[3].getNormal(), triangleVec[3].getNormal(),
                    triangleVec[4].getNormal(), triangleVec[4].getNormal(), triangleVec[4].getNormal(),
                    triangleVec[5].getNormal(), triangleVec[5].getNormal(), triangleVec[5].getNormal()};

TriMesh roof = new TriMesh("roof" +newObject(), BufferUtils.createFloatBuffer(roofVertexes), BufferUtils.createFloatBuffer(normals), null, null, BufferUtils.createIntBuffer(roofIndexes));

roof.setModelBound(new BoundingBox());


Your indexes. -Reverse the order you name the three vertexes for the dark triangle. If they are added clockwise switch to counter-clockwise and vice versa. That will make the normal of this triangle point in the right direction.

i.e. go from



c, b, a

where a,b,c are the vertexes of the offending triangle.

tnx! that works fine!

I understand now that this occurs beacuse the triangles aren't aware of the trimesh as a whole and set their normals independently, that's why some of them isn't displayed "correctly".