Water GameState

I've been trying to make a GameState that renders the water. so far that has not been working for me. first of all i don't think i understand how to make the projected grid update, and second of all, it ain't rendering the water.



is there a way to do this?

clwillingham said:

I've been trying to make a GameState that renders the water. so far that has not been working for me. first of all i don't think i understand how to make the projected grid update, and second of all, it ain't rendering the water.

is there a way to do this?


Are you getting a Null Pointer Exception when initializing the water?

Ok i just figured it out. and yes i was getting a null pointer exception. eventually i realized i had to setup the Pass Manager correctly. so now i have turned it into two gamestates. the WaterState and the PassManager.

package base.gamestates;

import base.Main;

import com.jme.image.Texture;
import com.jme.math.Plane;
import com.jme.math.Vector3f;
import com.jme.renderer.Camera;
import com.jme.renderer.ColorRGBA;
import com.jme.renderer.Renderer;
import com.jme.renderer.pass.BasicPassManager;
import com.jme.renderer.pass.RenderPass;
import com.jme.scene.Node;
import com.jme.scene.Skybox;
import com.jme.scene.Spatial;
import com.jme.scene.Spatial.TextureCombineMode;
import com.jme.scene.state.CullState;
import com.jme.scene.state.FogState;
import com.jme.scene.state.ZBufferState;
import com.jme.system.DisplaySystem;
import com.jme.util.TextureManager;
import com.jmex.effects.water.ProjectedGrid;
import com.jmex.effects.water.WaterHeightGenerator;
import com.jmex.effects.water.WaterRenderPass;
import com.jmex.game.state.GameState;

public class WaterState extends GameState {
   private WaterRenderPass waterEffectRenderPass;
   BasicPassManager pManager = Main.passManager.getpManager();
   private float farPlane = 10000.0f;
   Node scene = new Node("WaterState");
   private Skybox skybox;
    private ProjectedGrid projectedGrid;
   
   Camera cam;
   public WaterState(Camera camera)
   {
      setupFog();
      
      cam = camera;
      
      //cam.lookAt(new Vector3f(0, 0, 0), Vector3f.UNIT_Y);
      
      Node reflectedNode = new Node("reflectNode");
      buildSkyBox();
      reflectedNode.attachChild(skybox);
      scene.attachChild(reflectedNode);
      System.out.println("loaded Pass");
      waterEffectRenderPass = new WaterRenderPass(cam, 4, true, true);
      
      waterEffectRenderPass.setClipBias(0.5f);
        waterEffectRenderPass.setWaterMaxAmplitude(5.0f);
        // setting to default value just to show
        waterEffectRenderPass.setWaterPlane(new Plane(new Vector3f(0.0f, 1.0f,
                0.0f), 0.0f));
       
        projectedGrid = new ProjectedGrid("ProjectedGrid", cam, 100, 70, 0.01f,
                new WaterHeightGenerator());
       
        waterEffectRenderPass.setWaterEffectOnSpatial(projectedGrid);
        scene.attachChild(projectedGrid);
        waterEffectRenderPass.addReflectedScene(reflectedNode);
        waterEffectRenderPass.setSkybox(skybox);
        pManager.add(waterEffectRenderPass);
        RenderPass rootPass = new RenderPass();
        rootPass.add(scene);
        pManager.add(rootPass);
        scene.setCullHint(Spatial.CullHint.Never);
        scene.setRenderQueueMode(Renderer.QUEUE_OPAQUE);
        scene.updateRenderState();
       
   }
   
   private void setupFog() {
        FogState fogState = DisplaySystem.getDisplaySystem().getRenderer().createFogState();
        fogState.setDensity(1.0f);
        fogState.setEnabled(true);
        fogState.setColor(new ColorRGBA(1.0f, 1.0f, 1.0f, 1.0f));
        fogState.setEnd(farPlane);
        fogState.setStart(farPlane / 10.0f);
        fogState.setDensityFunction(FogState.DensityFunction.Linear);
        fogState.setQuality(FogState.Quality.PerVertex);
        scene.setRenderState(fogState);
    }
   
   private void buildSkyBox() {
        skybox = new Skybox("skybox", 10, 10, 10);

        String dir = "jmetest/data/skybox1/";
        Texture north = TextureManager.loadTexture(WaterState.class
                .getClassLoader().getResource(dir + "1.jpg"),
                Texture.MinificationFilter.BilinearNearestMipMap,
                Texture.MagnificationFilter.Bilinear);
        Texture south = TextureManager.loadTexture(WaterState.class
                .getClassLoader().getResource(dir + "3.jpg"),
                Texture.MinificationFilter.BilinearNearestMipMap,
                Texture.MagnificationFilter.Bilinear);
        Texture east = TextureManager.loadTexture(WaterState.class
                .getClassLoader().getResource(dir + "2.jpg"),
                Texture.MinificationFilter.BilinearNearestMipMap,
                Texture.MagnificationFilter.Bilinear);
        Texture west = TextureManager.loadTexture(WaterState.class
                .getClassLoader().getResource(dir + "4.jpg"),
                Texture.MinificationFilter.BilinearNearestMipMap,
                Texture.MagnificationFilter.Bilinear);
        Texture up = TextureManager.loadTexture(WaterState.class
                .getClassLoader().getResource(dir + "6.jpg"),
                Texture.MinificationFilter.BilinearNearestMipMap,
                Texture.MagnificationFilter.Bilinear);
        Texture down = TextureManager.loadTexture(WaterState.class
                .getClassLoader().getResource(dir + "5.jpg"),
                Texture.MinificationFilter.BilinearNearestMipMap,
                Texture.MagnificationFilter.Bilinear);

        skybox.setTexture(Skybox.Face.North, north);
        skybox.setTexture(Skybox.Face.West, west);
        skybox.setTexture(Skybox.Face.South, south);
        skybox.setTexture(Skybox.Face.East, east);
        skybox.setTexture(Skybox.Face.Up, up);
        skybox.setTexture(Skybox.Face.Down, down);
        skybox.preloadTextures();

        CullState cullState = DisplaySystem.getDisplaySystem().getRenderer().createCullState();
        cullState.setCullFace(CullState.Face.None);
        cullState.setEnabled(true);
        skybox.setRenderState(cullState);

        ZBufferState zState = DisplaySystem.getDisplaySystem().getRenderer().createZBufferState();
        zState.setEnabled(false);
        skybox.setRenderState(zState);

        FogState fs = DisplaySystem.getDisplaySystem().getRenderer().createFogState();
        fs.setEnabled(false);
        skybox.setRenderState(fs);

        skybox.setLightCombineMode(Spatial.LightCombineMode.Off);
        skybox.setCullHint(Spatial.CullHint.Never);
        skybox.setTextureCombineMode(TextureCombineMode.Replace);
        skybox.updateRenderState();

        skybox.lockBounds();
        skybox.lockMeshes();
    }

   @Override
   public void cleanup() {
      // TODO Auto-generated method stub
      //pManager.cleanUp();
   }

   @Override
   public void render(float tpf) {
      // TODO Auto-generated method stub
      //pManager.renderPasses(DisplaySystem.getDisplaySystem().getRenderer());
      DisplaySystem.getDisplaySystem().getRenderer().draw(scene);
   }

   @Override
   public void update(float tpf) {
      // TODO Auto-generated method stub
      //pManager.updatePasses(tpf);
      skybox.getLocalTranslation().set(cam.getLocation());
      scene.updateGeometricState(tpf, true);
      cam.update();
   }
}


package base.gamestates;

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

public class PassManager extends GameState {
   
   private BasicPassManager pManager;

   public PassManager()
   {
      setpManager(new BasicPassManager());
   }
   
   @Override
   public void cleanup() {
      // TODO Auto-generated method stub
      pManager.cleanUp();
   }

   @Override
   public void render(float tpf) {
      // TODO Auto-generated method stub
      pManager.renderPasses(DisplaySystem.getDisplaySystem().getRenderer());
   }

   @Override
   public void update(float tpf) {
      // TODO Auto-generated method stub
      pManager.updatePasses(tpf);
   }

   public void setpManager(BasicPassManager pManager) {
      this.pManager = pManager;
   }

   public BasicPassManager getpManager() {
      return pManager;
   }

}



this worked pretty well, however it has two problems.

first is that it is running slower than i would like even though it is doing just about exactly what the example is showing.

second is that the Skybox always jumps around when you move the camera. kind of like it can't keep up. the odd part though is that the reflection of the Skybox is just fine. it is running full speed and when the camera moves it isn't jerking around like the actual one is.

clwillingham said:


this worked pretty well, however it has two problems.

first is that it is running slower than i would like even though it is doing just about exactly what the example is showing.

second is that the Skybox always jumps around when you move the camera. kind of like it can't keep up. the odd part though is that the reflection of the Skybox is just fine. it is running full speed and when the camera moves it isn't jerking around like the actual one is.




... huh? I'm not sure I understand the problem you're having...