Hello i want to add some water to my scene… I am using baseGame and result of my “work” is this:
That big black thing is waterQuad and i dont know why it is black:(
my code:
package marosko;
import java.nio.FloatBuffer;
import java.util.logging.Level;
import java.util.logging.Logger;
import jmetest.renderer.TestSkybox;
import com.jme.app.BaseGame;
import com.jme.image.Texture;
import com.jme.input.KeyBindingManager;
import com.jme.input.KeyInput;
import com.jme.light.DirectionalLight;
import com.jme.math.Plane;
import com.jme.math.Vector3f;
import com.jme.renderer.Camera;
import com.jme.renderer.ColorRGBA;
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.shape.Quad;
import com.jme.scene.state.CullState;
import com.jme.scene.state.LightState;
import com.jme.scene.state.ZBufferState;
import com.jme.system.DisplaySystem;
import com.jme.system.JmeException;
import com.jme.util.TextureManager;
import com.jme.util.Timer;
import com.jmex.effects.water.WaterRenderPass;
public class Water extends BaseGame{
private Camera cam;
private int width, height, depth, freq;
private boolean fullscreen;
private static final Logger logger = Logger.getLogger(Main.class
.getName());
protected Timer timer;
private Node root;
private BasicPassManager pManager;
private Skybox skybox;
private Quad waterQuad;
private WaterRenderPass waterEffectRenderPass;
private float farPlane = 10000.0f;
private float textureScale = 0.02f;
public static void main(String[] args) {
Water water = new Water();
water.start();
}
@Override
protected void cleanup() {
// TODO Auto-generated method stub
}
@Override
protected void initGame() {
root = new Node("Korenovy uzol");
cam.update();
pManager = new BasicPassManager();
ZBufferState buf = display.getRenderer().createZBufferState();
buf.setEnabled(true);
buf.setFunction(ZBufferState.CF_LEQUAL);
root.setRenderState(buf);
CullState cs = display.getRenderer().createCullState();
cs.setCullMode(CullState.CS_BACK);
root.setRenderState(cs);
buildSkyBox();
buildLighting();
waterEffectRenderPass = new WaterRenderPass( cam, 4, false, true );
//setting to default value just to show
waterEffectRenderPass.setWaterPlane( new Plane( new Vector3f( 0.0f, 1.0f, 0.0f ), 0.0f ) );
waterQuad = new Quad( "waterQuad", 1, 1 );
FloatBuffer normBuf = waterQuad.getNormalBuffer( 0 );
normBuf.clear();
normBuf.put( 0 ).put( 1 ).put( 0 );
normBuf.put( 0 ).put( 1 ).put( 0 );
normBuf.put( 0 ).put( 1 ).put( 0 );
normBuf.put( 0 ).put( 1 ).put( 0 );
waterEffectRenderPass.setWaterEffectOnSpatial( waterQuad );
root.attachChild( waterQuad );
waterEffectRenderPass.setSkybox( skybox );
pManager.add( waterEffectRenderPass );
waterQuad.getLocalTranslation().y -= 10;
RenderPass rootPass = new RenderPass();
rootPass.add( root );
pManager.add( rootPass );
root.updateGeometricState(0.0f, true);
root.updateRenderState();
}
@Override
protected void initSystem() {
// TODO Auto-generated method stub
width = properties.getWidth();
height = properties.getHeight();
depth = properties.getDepth();
freq = properties.getFreq();
fullscreen = properties.getFullscreen();
try {
display = DisplaySystem.getDisplaySystem(properties.getRenderer());
display.createWindow(width, height, depth, freq, fullscreen);
display.setMinStencilBits(8);
cam = display.getRenderer().createCamera(width, height);
} catch (JmeException e) {
logger.log(Level.SEVERE, "Could not create displaySystem", e);
System.exit(1);
}
display.getRenderer().setBackgroundColor(ColorRGBA.blue.clone());
cam.setFrustumPerspective(45.0f, (float)width / (float)height, 1, 1000);
Vector3f loc = new Vector3f(20.0f, 0.0f, 0.0f);
Vector3f left = new Vector3f(0.0f, 0.0f, -1.0f);
Vector3f up = new Vector3f(0.0f, 1.0f, 0.0f);
Vector3f dir = new Vector3f(-1.0f, 0f, 0.0f);
cam.setFrame(loc, left, up, dir);
cam.update();
timer = Timer.getTimer();
display.getRenderer().setCamera(cam);
KeyBindingManager.getKeyBindingManager().set("exit",
KeyInput.KEY_ESCAPE);
}
@Override
protected void reinit() {
// TODO Auto-generated method stub
}
@Override
protected void render(float interpolation) {
display.getRenderer().clearBuffers();
pManager.renderPasses(display.getRenderer());
}
@Override
protected void update(float interpolation) {
skybox.getLocalTranslation().set(cam.getLocation());
skybox.updateGeometricState(0.0f, true);
Vector3f transVec = new Vector3f(cam.getLocation().x,
waterEffectRenderPass.getWaterHeight(), cam.getLocation().z);
setTextureCoords(0, transVec.x, -transVec.z, textureScale);
setVertexCoords(transVec.x, transVec.y, transVec.z);
if (KeyBindingManager.getKeyBindingManager().isValidCommand("exit")) {
finished = true;
}
}
private void buildLighting() {
/** Set up a basic, default light. */
DirectionalLight light = new DirectionalLight();
light.setDiffuse(new ColorRGBA(1.0f, 1.0f, 1.0f, 1.0f));
light.setAmbient(new ColorRGBA(0.5f, 0.5f, 0.5f, .5f));
light.setDirection(new Vector3f(1,-1,0));
light.setShadowCaster(true);
light.setEnabled(true);
/** Attach the light to a lightState and the lightState to rootNode. */
LightState lightState = display.getRenderer().createLightState();
lightState.setEnabled(true);
lightState.setGlobalAmbient(new ColorRGBA(.2f, .2f, .2f, 1f));
lightState.attach(light);
root.setRenderState(lightState);
}
private void buildSkyBox() {
skybox = new Skybox("skybox", 10, 10, 10);
Texture north = TextureManager.loadTexture(
TestSkybox.class.getClassLoader().getResource(
"jmetest/data/texture/north.jpg"),
Texture.MM_LINEAR,
Texture.FM_LINEAR);
Texture south = TextureManager.loadTexture(
TestSkybox.class.getClassLoader().getResource(
"jmetest/data/texture/south.jpg"),
Texture.MM_LINEAR,
Texture.FM_LINEAR);
Texture east = TextureManager.loadTexture(
TestSkybox.class.getClassLoader().getResource(
"jmetest/data/texture/east.jpg"),
Texture.MM_LINEAR,
Texture.FM_LINEAR);
Texture west = TextureManager.loadTexture(
TestSkybox.class.getClassLoader().getResource(
"jmetest/data/texture/west.jpg"),
Texture.MM_LINEAR,
Texture.FM_LINEAR);
Texture up = TextureManager.loadTexture(
TestSkybox.class.getClassLoader().getResource(
"jmetest/data/texture/top.jpg"),
Texture.MM_LINEAR,
Texture.FM_LINEAR);
Texture down = TextureManager.loadTexture(
TestSkybox.class.getClassLoader().getResource(
"jmetest/data/texture/bottom.jpg"),
Texture.MM_LINEAR,
Texture.FM_LINEAR);
skybox.setTexture(Skybox.NORTH, north);
skybox.setTexture(Skybox.WEST, west);
skybox.setTexture(Skybox.SOUTH, south);
skybox.setTexture(Skybox.EAST, east);
skybox.setTexture(Skybox.UP, up);
skybox.setTexture(Skybox.DOWN, down);
skybox.preloadTextures();
skybox.updateRenderState();
root.attachChild(skybox);
}
private void setVertexCoords(float x, float y, float z)
{
FloatBuffer vertBuf = waterQuad.getVertexBuffer(0);
vertBuf.clear();
vertBuf.put(x - farPlane).put(y).put(z - farPlane);
vertBuf.put(x - farPlane).put(y).put(z + farPlane);
vertBuf.put(x + farPlane).put(y).put(z + farPlane);
vertBuf.put(x + farPlane).put(y).put(z - farPlane);
}
private void setTextureCoords(int buffer, float x, float y, float textureScale)
{
x *= textureScale * 0.5f;
y *= textureScale * 0.5f;
textureScale = farPlane * textureScale;
FloatBuffer texBuf;
texBuf = waterQuad.getTextureBuffer(0, buffer);
texBuf.clear();
texBuf.put(x).put(textureScale + y);
texBuf.put(x).put(y);
texBuf.put(textureScale + x).put(y);
texBuf.put(textureScale + x).put(textureScale + y);
}
}