Problems with Lighting.j3md

Can anyone tell me what I’m doing wrong? I set up my scene with simple boxes, using a single material instance created from Common/MatDefs/Light/Lighting.j3md, and create point and ambient lights. I calling TangentBinormalGenerator.generate on all the geometries.

When the scene renders, it looks like the image below, except the faces of the boxes actually jump around from white to gray to black. This happens even when the camera stays still. If I didn’t know better, I’d say the normals are just being randomly set every frame. I’ve also looked at the vertex and fragment shaders for Lighting.j3md, but they’re pretty complicated, so no luck there.

Are you creating the boxes or just using JME’s Box mesh?

Do you set the shininess on the material?

Are you creating the boxes or just using JME’s Box mesh?

A single Box mesh instance given to each Geometry instance, then using a local translation on the Geometry's.
Do you set the shininess on the material?

No, but it makes no difference. I can make the material a bit more shiny, but the same problem occurs.

I just know there is sometimes a problem on certain cards when you don’t specify a shininess at all. And it looks similar to your issue so I thought I would ask.

More information:

I wrote my own shader to get a feel for what was going on*. All it does is pass the normal into the fragment shader. If the normal points straight up (0, 1, 0), the fragment is red, otherwise blue. The attached picture shows something is going on with the surface normals. I really don’t know how the actual normals are calculated, but I know it’s common for cubes to have problems with vertex normals pointing in crazy directions because the corner of a cube is non-smooth and makes interpolation tricky. Is there something I need to set for smooth normals in the fragment shader? I think I know how I would go about this in raw OpenGL, but not with jME.

Next I’m going to try just making my own box to see how normals are assigned and if it makes a difference.

*As an aside, I like how easy the Material API makes it to play around with custom shaders, but (IMO) the documentation on Material Definitions could use some work.

That looks like you have multiple surfaces Z-fighting… I don’t know why that would be, though. It’s just what it looks like. :slight_smile:

If you are using jme’s Box mesh then all of the vertexes should have normals pointing in the appropriate direction. You don’t even need to generate tangents or anything unless you are planning on doing bump mapping of some kind. There is no edge to edge smoothing to worry about since each face has its own vertexes.

hmmmm… are you checking the normal in the fragment shader then? I think it’s already been projected at that point but then I’d expect it never to be 0,1,0… not just sometimes… I guess it depends on what your vert shader is doing.

If I have the fragment shader directly use the normal to color the fragment, I get the expected results (different solid color per face). Also, if I use an epsilon comparison on the normal, I can test for individual vectors (like (0, 1, 0)). So the normal isn’t perfect, but I can accept those slight FP calculation errors. However, I still get the very weird behavior when using the Lighting.j3md material and shaders, so I guess now I’ll try to implement my own Phong shaders in a Material Def, using the Lighting.j3md as an example. Maybe by stripping it to the bone and adding stuff in slowly I can find out what I’m doing wrong.