Package net.anzix.fsz.sceneComponents

Source Code of net.anzix.fsz.sceneComponents.PlainWater

package net.anzix.fsz.sceneComponents;




import com.ardor3d.extension.effect.water.WaterNode;
import com.ardor3d.math.Plane;
import com.ardor3d.math.Vector3;
import com.ardor3d.renderer.Camera;
import com.ardor3d.scenegraph.Node;
import com.ardor3d.scenegraph.extension.Skybox;
import com.ardor3d.scenegraph.shape.Quad;
import com.ardor3d.util.ReadOnlyTimer;
import java.nio.FloatBuffer;

/**
*
* @author elcsiga
*/
public class PlainWater extends WaterNode{

    private Quad waterQuad;
    float quadHalfSize;
    float waterHeight;

    private final double textureScale = 0.05;


    public PlainWater(Camera camera, Node reflectedNode, Skybox skybox, float farPlane, float waterHeight ) {

         // Create a new WaterNode with refraction enabled.
        super(camera, 2, false, true);
        // Setup textures to use for the water.
        setNormalMapTextureString("images/water/normalmap3.dds");
        setDudvMapTextureString("images/water/dudvmap.png");
        setFallbackMapTextureString("images/water/water2.png");
        useFadeToFogColor(true);

        setSpeedReflection(0.02);
        setSpeedReflection(-0.01);

        // setting to default value just to show
        this.waterHeight = waterHeight;
        setWaterPlane(new Plane(new Vector3(0.0, 1.0, 0.0), waterHeight));

        // Create a quad to use as geometry for the water.
        waterQuad = new Quad("waterQuad", 1, 1);
        // Hack the quad normals to point up in the y-axis. Since we are manipulating the vertices as
        // we move this is more convenient than rotating the quad.
        final FloatBuffer normBuf = waterQuad.getMeshData().getNormalBuffer();
        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);

        attachChild(waterQuad);

        addReflectedScene(reflectedNode);
        setSkybox(skybox);

        quadHalfSize = farPlane;
    }

    public void update(final ReadOnlyTimer timer, Camera camera ) {
        final Vector3 transVec = new Vector3(camera.getLocation().getX(), getWaterHeight(), camera
                .getLocation().getZ());
        setTextureCoords(0, transVec.getX(), -transVec.getZ(), textureScale);
        setVertexCoords(transVec.getXf(), transVec.getYf(), transVec.getZf());
        update(timer.getTimePerFrame());
    }

    private void setVertexCoords(final float x, final float y, final float z) {
        final FloatBuffer vertBuf = waterQuad.getMeshData().getVertexBuffer();
        vertBuf.clear();

        vertBuf.put(x - quadHalfSize).put(y).put(z - quadHalfSize);
        vertBuf.put(x - quadHalfSize).put(y).put(z + quadHalfSize);
        vertBuf.put(x + quadHalfSize).put(y).put(z + quadHalfSize);
        vertBuf.put(x + quadHalfSize).put(y).put(z - quadHalfSize);
    }

    private void setTextureCoords(final int buffer, double x, double y, double textureScale) {
        x *= textureScale * 0.5f;
        y *= textureScale * 0.5f;
        textureScale = quadHalfSize * textureScale;
        FloatBuffer texBuf;
        texBuf = waterQuad.getMeshData().getTextureBuffer(buffer);
        texBuf.clear();
        texBuf.put((float) x).put((float) (textureScale + y));
        texBuf.put((float) x).put((float) y);
        texBuf.put((float) (textureScale + x)).put((float) y);
        texBuf.put((float) (textureScale + x)).put((float) (textureScale + y));
    }
}
TOP

Related Classes of net.anzix.fsz.sceneComponents.PlainWater

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.