Help with Standard Game and Shadows

I have a terrain with trees, i wanted the trees to cast shadows … i took me hours to study the wiki and the TestShadowPass, believe be i tried my best.  :smiley:



I have successfully created a terrain with lots of box casting shadows, only it is on SimpleGame. Problem is when i use a standardgame/gamestate and my tree leaves has alpha that needs to be rendered properly.



Heres the problem,

*The leaves of my trees are missing, i think they are not rendered properly. What should i do with this?

*I dont see shadows on my trees.



Please help me I’m missing something here.



Here are the screenies:





and this one has the LightCombineMode set to OFF,

now the leaves show, still no shadow





Thanks Everyone




Sorry heres  the code:



ForestGameState where i set Everything, rootNode and occluders


package salipawpaw;

import com.jme.bounding.BoundingBox;
import com.jme.input.FirstPersonHandler;
import com.jme.input.InputHandler;
import com.jme.input.KeyBindingManager;
import com.jme.input.KeyInput;
import com.jme.light.DirectionalLight;
import com.jme.light.PointLight;
import com.jme.math.Vector3f;
import com.jme.renderer.Camera;
import com.jme.renderer.ColorRGBA;
import com.jme.renderer.Renderer;
import com.jme.scene.Node;
import com.jme.scene.Spatial;
import com.jme.scene.state.CullState;
import com.jme.scene.state.FogState;
import com.jme.scene.state.LightState;
import com.jme.scene.state.ZBufferState;
import com.jme.system.DisplaySystem;
import com.jme.util.GameTaskQueueManager;
import com.jmex.game.state.GameState;
import com.jmex.terrain.TerrainPage;
import java.util.concurrent.Callable;
import salipawpaw.terrain.DarkGrassTerrain;
import salipawpaw.util.SalipawpawModelLoader;

/**
 *
 * @author dru
 */
public class ForestGameState extends GameState{

    protected Node occluderNode;
    protected Node rootNode;
   
    protected DisplaySystem display;
   
    private TerrainPage terrainPage;

    private InputHandler input;
    private Camera cam;

    //render states
    protected LightState lightState;
    protected CullState cullState;
    protected ZBufferState zBuffState;
    protected FogState fogState;

    public ForestGameState() {

        rootNode = new Node("FOREST ROOT NODE");
        occluderNode = new Node("FOREST OCCLUDER NODE");
        rootNode.attachChild(occluderNode);
       
        display = DisplaySystem.getDisplaySystem();

        this.setName("FOREST");
        setEnvironment();
        setInputHandler();
        setTerrain();
        setTrees();

        KeyBindingManager.getKeyBindingManager().set("exit", KeyInput.KEY_ESCAPE);

        rootNode.setRenderQueueMode(Renderer.QUEUE_OPAQUE);
        rootNode.updateRenderState();
        rootNode.updateGeometricState(0.0f, true);
    }

    private void setInputHandler() {
        FirstPersonHandler fp = new FirstPersonHandler(cam);
        fp.getKeyboardLookHandler().setMoveSpeed(10.0f);
        fp.getMouseLookHandler().setActionSpeed(1.0f);
        input = fp;
    }

    private void setEnvironment() {

        cam = DisplaySystem.getDisplaySystem().getRenderer().createCamera(display.getWidth(), display.getHeight());
        cam.setFrustumPerspective(45.0f, (float)display.getWidth() / (float) display.getHeight(), 1.0f, 1000.0f);
        cam.setDirection(new Vector3f(0.3f, -1, 0));
        cam.setUp(new Vector3f(0, 1, 0));
        cam.setLeft(new Vector3f(-1, 0, 1));  
        cam.setLocation(new Vector3f(100, 100, 100));
        cam.lookAt(new Vector3f(0, 0, 0), Vector3f.UNIT_Y);
        cam.update();

        try {

            GameTaskQueueManager.getManager().update(new Callable<Object>(){
                public Object call() throws Exception {
                    display.getRenderer().setCamera(cam);
                    return null;
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }

        DirectionalLight dl = new DirectionalLight();
        dl.setAmbient(new ColorRGBA(0.2f, 0.2f, 0.2f, 0.3f));
        dl.setDiffuse(new ColorRGBA(1, 1, 1, 1));
        dl.setDirection(new Vector3f(0.5f, -0.4f, 0).normalizeLocal());
        dl.setShadowCaster(true);
        dl.setEnabled(true);

        PointLight pl = new PointLight();
        pl.setAmbient(new ColorRGBA(0.25f, 0.25f, 0.25f, 0.25f));
        pl.setDiffuse(new ColorRGBA(0.7f, 0.7f, 0.7f, 0.7f));
        pl.setLocation(new Vector3f(0, 500, 0));
        pl.setShadowCaster(true);
        pl.setEnabled(true);

        DirectionalLight dl1 = new DirectionalLight();
        dl1.setAmbient(new ColorRGBA(0.2f, 0.2f, 0.2f, 0.4f));
        dl1.setDiffuse(new ColorRGBA(1.0f, 1.0f, 1.0f, 1.0f));
        dl.setDirection(new Vector3f(-0.2f, -0.2f, 0.2f).normalizeLocal());
        dl.setShadowCaster(true);
        dl.setEnabled(true);

        lightState = display.getRenderer().createLightState();
        lightState.attach(dl);
        lightState.attach(dl1);
        lightState.attach(pl);
        lightState.setGlobalAmbient(new ColorRGBA(0.5f, 0.5f, 0.5f, 0.5f));
        lightState.setEnabled(true);
        rootNode.setRenderState(lightState);

        cullState = display.getRenderer().createCullState();
        cullState.setCullFace(CullState.Face.Back);
        cullState.setEnabled(true);
        rootNode.setRenderState(cullState);

        zBuffState = display.getRenderer().createZBufferState();
        zBuffState.setFunction(ZBufferState.TestFunction.LessThanOrEqualTo);
        zBuffState.setEnabled(true);
        rootNode.setRenderState(zBuffState);

        fogState = display.getRenderer().createFogState();
        fogState.setColor(new ColorRGBA(0.5f, 0.5f, 0.5f, 0.5f));
        fogState.setDensity(1.0f);
        fogState.setStart(1.0f);
        fogState.setEnd(500.0f);
        fogState.setQuality(FogState.Quality.PerVertex);
        fogState.setDensityFunction(FogState.DensityFunction.Linear);
        fogState.setEnabled(true);
        rootNode.setRenderState(fogState);
    }

    private void setTerrain() {
        DarkGrassTerrain terrain = new DarkGrassTerrain();
        terrainPage = terrain.getTerrainPage();
        rootNode.attachChild(terrainPage);
    }

    private void setTrees() {

        SalipawpawModelLoader modelLoader;
        float x = 0;
        float z = 0;
        Vector3f normal = new Vector3f();

        for(int i=0; i<50; i++) {

            modelLoader = new SalipawpawModelLoader();
            modelLoader.setObjModel("data/model/obj/tree_01.obj");
            Spatial tree = modelLoader.getModel();
            tree.setModelBound(new BoundingBox());
            tree.updateModelBound();

            x = (float) Math.random() * 500 - 250;
            z = (float) Math.random() * 500 - 250;
            tree.setLocalTranslation(new Vector3f(x, terrainPage.getHeight(x,z),z));
            terrainPage.getSurfaceNormal(tree.getLocalTranslation(), normal);
            if(normal != null)
                tree.rotateUpTo(normal);
            occluderNode.attachChild(tree);
        }

        occluderNode.setRenderQueueMode(Renderer.QUEUE_TRANSPARENT);
        occluderNode.updateRenderState();

        try {
            GameTaskQueueManager.getManager().update(new Callable<Object>(){
               public Object call() {
                   occluderNode.lock();
                   return null;
               }
            }).get();
        } catch (Exception e) {
            e.printStackTrace();
        }
       
    }

    public void update(float tpf) {

        input.update(tpf);
        cam.update();
       
        if(KeyBindingManager.getKeyBindingManager().isValidCommand("exit", false)){
            System.exit(0);
        }
        rootNode.updateGeometricState(tpf, true);
    }

    public void render(float tpf) {
        display.getRenderer().draw(rootNode);
    }

    public void cleanup() {
    }

    public Node getRootNode() {
        return rootNode;
    }

    public Node getOccluderNode() {
        return occluderNode;
    }

    public TerrainPage getTerrainPage() {
        return terrainPage;
    }


}



Next the GameState that suppose to help me with the shadows


package salipawpaw;

import com.jme.renderer.pass.BasicPassManager;
import com.jme.renderer.pass.ShadowedRenderPass;
import com.jme.scene.Node;
import com.jme.system.DisplaySystem;
import com.jmex.game.state.GameState;

/**
 *
 * @author dru
 */
public class RenderPassGameState extends GameState{

    protected Node rootNode;
    protected Node occluderNode;

    protected BasicPassManager pManager;
    protected ShadowedRenderPass sPass;

    public RenderPassGameState(Node rootNode, Node occluderNode) {
        this.rootNode = rootNode;
        this.occluderNode = occluderNode;

        pManager = new BasicPassManager();
        sPass = new ShadowedRenderPass();
       
        sPass.add(rootNode);
        sPass.addOccluder(occluderNode);
        sPass.setRenderShadows(true);
        sPass.setLightingMethod(ShadowedRenderPass.LightingMethod.Additive);
        sPass.setEnabled(true);
        pManager.add(sPass);

    }

    public void update(float tpf) {
        pManager.updatePasses(tpf);
    }

    public void render(float tpf) {
        pManager.renderPasses(DisplaySystem.getDisplaySystem().getRenderer());
    }

    public void cleanup() { 
    }
}



The standardGame


package salipawpaw;

import com.jme.input.InputSystem;
import com.jme.input.joystick.JoystickInput;
import com.jmex.editors.swing.settings.GameSettingsPanel;
import com.jmex.game.StandardGame;
import com.jmex.game.state.GameStateManager;


/**
 *
 * @author dru
 */
public class TryMe2{

    public static void main(String[] args) {

        JoystickInput.setProvider(InputSystem.INPUT_SYSTEM_LWJGL);

        StandardGame game = new StandardGame("THE GAME");
        game.getSettings().setStencilBits(4);
        game.getSettings().setDepth(16);
        game.getSettings().setAlphaBits(0);
        game.getSettings().setSamples(0);
       
        try {
            GameSettingsPanel.prompt(game.getSettings());
        } catch (Exception e) {
        }
        game.start();

        ForestGameState forest = new ForestGameState();
        GameStateManager.getInstance().attachChild(forest);
        forest.setActive(true);

        RenderPassGameState render = new RenderPassGameState(forest.getTerrainPage(), forest.getOccluderNode());
        GameStateManager.getInstance().attachChild(render);
        render.setActive(true);

    }
}