[SOLVED] Ambient occlusion and directional light having weird behaviour

Hello guys,

While I slowly get familiar with jme, I encountered some issues with lighting.

I get the ambient light to work like a charm easily.
But even by copy-pasting the code in http://wiki.jmonkeyengine.org/doku.php/jme3:advanced:light_and_shadow, I can’t manage to get directional light nor ambient occlusion to work properly (and directional light shadows do not even show).

Here are some screenshots of what I got (the top consists on a flat green layer with regularly spaced green blocks dropped on it):

With only an ambient light I got what I expected:

Only directional light:

Ambient light with some ambient occlusion (look like the occlusions are shifted to the side):

Ambient light, ambient occlusion and directional light (it is starting to get creepy):

As you can see, it does not looks as we could expect.

I think the issue is when I set the mesh.

Here is how I build the materials:

Material mat = new Material(assetManager, “Common/MatDefs/Light/Lighting.j3md”);
mat.setBoolean(“UseMaterialColors”,true);
mat.setColor(“Diffuse”, ColorRGBA.White);
mat.setColor(“Ambient”, colorOf(btype));
mat.setColor(“Specular”, colorOf(btype));
mat.setFloat(“Shininess”, 50f);

(On the screenshots, every meshes normals were set to {1,0,0, 1,0,0, 1,0,0}).
I use custom meshes.

And here is the code I copy-pasted from the tutorial for the light sources and the filters :

// Ambient light
AmbientLight al = new AmbientLight();
al.setColor(ColorRGBA.White.mult(0.5f));
rootNode.addLight(al);

// Directionnal light
DirectionalLight sun = new DirectionalLight();
sun.setColor(ColorRGBA.White);
sun.setDirection(new Vector3f(-1,-1,-1));
rootNode.addLight(sun);

// Filter post processor
FilterPostProcessor fpp = new FilterPostProcessor(assetManager);

// Ambient Occlusion
SSAOFilter ssaoFilter = new SSAOFilter(12.94f, 43.92f, 0.33f, 0.61f);
fpp.addFilter(ssaoFilter);
viewPort.addProcessor(fpp);

// Directionnal light shadows
final int SHADOWMAP_SIZE=1024;
DirectionalLightShadowRenderer dlsr = new DirectionalLightShadowRenderer(assetManager, SHADOWMAP_SIZE, 3);
dlsr.setLight(sun);////
viewPort.addProcessor(dlsr);
DirectionalLightShadowFilter dlsf = new DirectionalLightShadowFilter(assetManager, SHADOWMAP_SIZE, 3);
dlsf.setLight(sun);
dlsf.setEnabled(true);
fpp.addFilter(dlsf);

I would realy appreciate if you could help me :slight_smile:

Have a nice day !

Well no wonder then.
That’s your issue
That means every vertex has a normal pointing 45 degrees upward.

Why aren’t you making the model in a modeling tool? it would save you the hassle to compute proper normals.

I just wanted to use this normal until I debug the lighting problem. I did not think that it could be the origin of the issue.

I actually do not want to use a modelling tool since I only use one-color cubes. I just want to keep it simple (and I think that all the data a modelling tool put in a object could only slow down my program).

So I finally wrote the function that gives the normal and it solved the issue.

But now I have another issue that drives me crazy. I don’t know what can cause it, and surprisingly I do not see anything on the web.
One of the two triangles of every quad (the second one) does not receive the directional light. It receives the ambient light well, but not the directional one.

Here is a screenshot of what I get :

(you can see that of the left side, the quads are not buggy since none of their triangles receive the directionnal light)

Anyway, thank you for your help on the normals problem !

Still gotta be normals.

Lighting = normals.

ie:

How much will this triangle be lit by this light = how much facing is this triangle towards the light.

Also, your quads can’t be sharing vertexes properly or you wouldn’t have such a hard line down the center.

You can look at JME’s Quad to see how to make a quad that shares vertexes properly.

Well you took the most difficult way.

That’s a misconception.

Something must still be wrong in your normal calculation

Okay, you guys were totally write. It were still the normals that were not computed correctly. (I admit that I could have tested a little bit the normals computation before asking once more what was wrong !)

Now ambient occlusion and directional light work like a charm. Thank you very much !