Shadows and Zbuffer

Hey I'm new to JME and I'm just trying to figure out how to get shadows working properly. The first thing I discovered was that if I didn't have ZBuffering enabled then my Box was rendered incorrectly (meaning that parts which shouldn't be visible were). Enabling ZBuffering fixed that problem. After that I wanted to enable shadows so I followed various tutorials on the wiki. Finally, I ended up with the following code:



    @Override
    protected void initGame() {
        root = new Node("root");
        Node occluders = new Node("occluders");
        root.attachChild(occluders);

        ZBufferState buffer = display.getRenderer().createZBufferState();
        buffer.setEnabled(true);
        buffer.setFunction(ZBufferState.TestFunction.LessThanOrEqualTo);
        root.setRenderState(buffer);

        passManager = new BasicPassManager();

        shadowPass = new ShadowedRenderPass();
        shadowPass.setEnabled(true);
        shadowPass.setRenderShadows(true);
        shadowPass.setLightingMethod(LightingMethod.Additive);
        shadowPass.addOccluder(occluders);
        shadowPass.add(root);

        passManager.add(shadowPass);

        RenderPass renderPass = new RenderPass();
        renderPass.add(root);
        passManager.add(renderPass);

        DirectionalLight light = new DirectionalLight();
        light.setEnabled(true);
        light.setShadowCaster(true);
        light.setDirection(new Vector3f(0, 0, -.5f));

        lightState = display.getRenderer().createLightState();
        lightState.setEnabled(true);
        lightState.attach(light);
        root.setRenderState(lightState);

        initBox(occluders);

        root.updateRenderState();
    }



The goal of course is to have the Box I create in initBox rendered with all sides being lit except its back. Unfortunately the result is the top is lit -- and I can't seem to change this by changing the direction of the DirectionalLight. However, I can fix things by turning off ZBuffering. I say fix but really it's just trades one problem for another because as before the box is no longer rendered correctly but at least the shadow is correct.

Can anyone help me out? Is there something about ZBuffering I'm not aware of (very likely)? Also, I ultimately want to have a cast shadow is that supported by JME or do I have to implement that on top of JME's lighting system?

Thanks! Look forward to getting unblocked and productive with JME!

You are confusing lighting and shadows here. In realtime 3d, rendering parts of models pointing towards light sources brighter than those pointing away from them is called "shading" or "lighting".

The term "shadow" is usually reserved for what you call "a cast shadow" at the end of your post - and that's what ShadowedRenderPass is supposed to do in jME.

I suggest you drop the shadow stuff and go back to get a better understanding of basic lighting for now - there shouldn't be anything else necessary for that than just adding a Box (or maybe a Torus to make the image a bit more interesting :)) to a subclass of SimpleGame in order to see lighting in action.

Once you are sure you have understood what lighting can do for you (shading) and what it can't (namely casting shadows) you can move on to more interesting things like multipass rendering and shadows.

If you need any more documentation, check out the Test* classes in jmetest, they demonstrate every single feature of jME in a simple setup.