Search in sources :

Example 21 with HeightMap

use of com.jme3.terrain.heightmap.HeightMap in project jmonkeyengine by jMonkeyEngine.

the class TerrainTestCollision method simpleInitApp.

@Override
public void simpleInitApp() {
    bulletAppState = new BulletAppState();
    bulletAppState.setThreadingType(BulletAppState.ThreadingType.PARALLEL);
    stateManager.attach(bulletAppState);
    setupKeys();
    matRock = new Material(assetManager, "Common/MatDefs/Terrain/Terrain.j3md");
    matRock.setTexture("Alpha", assetManager.loadTexture("Textures/Terrain/splat/alphamap.png"));
    Texture heightMapImage = assetManager.loadTexture("Textures/Terrain/splat/mountains512.png");
    Texture grass = assetManager.loadTexture("Textures/Terrain/splat/grass.jpg");
    grass.setWrap(WrapMode.Repeat);
    matRock.setTexture("Tex1", grass);
    matRock.setFloat("Tex1Scale", 64f);
    Texture dirt = assetManager.loadTexture("Textures/Terrain/splat/dirt.jpg");
    dirt.setWrap(WrapMode.Repeat);
    matRock.setTexture("Tex2", dirt);
    matRock.setFloat("Tex2Scale", 32f);
    Texture rock = assetManager.loadTexture("Textures/Terrain/splat/road.jpg");
    rock.setWrap(WrapMode.Repeat);
    matRock.setTexture("Tex3", rock);
    matRock.setFloat("Tex3Scale", 128f);
    matWire = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    matWire.getAdditionalRenderState().setWireframe(true);
    matWire.setColor("Color", ColorRGBA.Green);
    AbstractHeightMap heightmap = null;
    try {
        heightmap = new ImageBasedHeightMap(heightMapImage.getImage(), 0.25f);
        heightmap.load();
    } catch (Exception e) {
    }
    terrain = new TerrainQuad("terrain", 65, 513, heightmap.getHeightMap());
    TerrainLodControl control = new TerrainLodControl(terrain, getCamera());
    // patch size, and a multiplier
    control.setLodCalculator(new DistanceLodCalculator(65, 2.7f));
    terrain.addControl(control);
    terrain.setMaterial(matRock);
    terrain.setLocalScale(new Vector3f(2, 2, 2));
    // unlock it so we can edit the height
    terrain.setLocked(false);
    rootNode.attachChild(terrain);
    /**
         * Create PhysicsRigidBodyControl for collision
         */
    terrain.addControl(new RigidBodyControl(0));
    bulletAppState.getPhysicsSpace().addAll(terrain);
    // let them drop from the sky
    for (int i = 0; i < 5; i++) {
        float r = (float) (8 * Math.random());
        Geometry sphere = new Geometry("cannonball", new Sphere(10, 10, r));
        sphere.setMaterial(matWire);
        // random position
        float x = (float) (20 * Math.random()) - 40;
        // random position
        float y = (float) (20 * Math.random()) - 40;
        // random position
        float z = (float) (20 * Math.random()) - 40;
        sphere.setLocalTranslation(new Vector3f(x, 100 + y, z));
        sphere.addControl(new RigidBodyControl(new SphereCollisionShape(r), 2));
        rootNode.attachChild(sphere);
        bulletAppState.getPhysicsSpace().add(sphere);
    }
    collisionBox = new Geometry("collisionBox", new Box(2, 2, 2));
    collisionBox.setModelBound(new BoundingBox());
    collisionBox.setLocalTranslation(new Vector3f(20, 95, 30));
    collisionBox.setMaterial(matWire);
    rootNode.attachChild(collisionBox);
    selectedCollisionObject = collisionBox;
    DirectionalLight dl = new DirectionalLight();
    dl.setDirection(new Vector3f(1, -0.5f, -0.1f).normalizeLocal());
    dl.setColor(new ColorRGBA(0.50f, 0.40f, 0.50f, 1.0f));
    rootNode.addLight(dl);
    cam.setLocation(new Vector3f(0, 25, -10));
    cam.lookAtDirection(new Vector3f(0, -1, 0).normalizeLocal(), Vector3f.UNIT_Y);
}
Also used : SphereCollisionShape(com.jme3.bullet.collision.shapes.SphereCollisionShape) Material(com.jme3.material.Material) BoundingBox(com.jme3.bounding.BoundingBox) Box(com.jme3.scene.shape.Box) Texture(com.jme3.texture.Texture) DistanceLodCalculator(com.jme3.terrain.geomipmap.lodcalc.DistanceLodCalculator) RigidBodyControl(com.jme3.bullet.control.RigidBodyControl) ImageBasedHeightMap(com.jme3.terrain.heightmap.ImageBasedHeightMap) Geometry(com.jme3.scene.Geometry) Sphere(com.jme3.scene.shape.Sphere) AbstractHeightMap(com.jme3.terrain.heightmap.AbstractHeightMap) ColorRGBA(com.jme3.math.ColorRGBA) BulletAppState(com.jme3.bullet.BulletAppState) Vector3f(com.jme3.math.Vector3f) BoundingBox(com.jme3.bounding.BoundingBox) DirectionalLight(com.jme3.light.DirectionalLight) TerrainLodControl(com.jme3.terrain.geomipmap.TerrainLodControl) TerrainQuad(com.jme3.terrain.geomipmap.TerrainQuad)

Example 22 with HeightMap

use of com.jme3.terrain.heightmap.HeightMap in project jmonkeyengine by jMonkeyEngine.

the class TerrainQuad method createQuad.

/**
     * Quadrants, world coordinates, and heightmap coordinates (Y-up):
     *
     *         -z
     *      -u |
     *    -v  1|3
     *  -x ----+---- x
     *        2|4 u
     *         | v
     *         z
     * <code>createQuad</code> generates four new quads from this quad.
     * The heightmap's top left (0,0) coordinate is at the bottom, -x,-z
     * coordinate of the terrain, so it grows in the positive x.z direction.
     */
protected void createQuad(int blockSize, float[] heightMap) {
    // create 4 terrain quads
    int quarterSize = size >> 2;
    int split = (size + 1) >> 1;
    Vector2f tempOffset = new Vector2f();
    offsetAmount += quarterSize;
    //if (lodCalculator == null)
    //    lodCalculator = createDefaultLodCalculator(); // set a default one
    // 1 upper left of heightmap, upper left quad
    float[] heightBlock1 = createHeightSubBlock(heightMap, 0, 0, split);
    Vector3f origin1 = new Vector3f(-quarterSize * stepScale.x, 0, -quarterSize * stepScale.z);
    tempOffset.x = offset.x;
    tempOffset.y = offset.y;
    tempOffset.x += origin1.x;
    tempOffset.y += origin1.z;
    TerrainQuad quad1 = new TerrainQuad(getName() + "Quad1", blockSize, split, stepScale, heightBlock1, totalSize, tempOffset, offsetAmount);
    quad1.setLocalTranslation(origin1);
    quad1.quadrant = 1;
    this.attachChild(quad1);
    // 2 lower left of heightmap, lower left quad
    float[] heightBlock2 = createHeightSubBlock(heightMap, 0, split - 1, split);
    Vector3f origin2 = new Vector3f(-quarterSize * stepScale.x, 0, quarterSize * stepScale.z);
    tempOffset = new Vector2f();
    tempOffset.x = offset.x;
    tempOffset.y = offset.y;
    tempOffset.x += origin2.x;
    tempOffset.y += origin2.z;
    TerrainQuad quad2 = new TerrainQuad(getName() + "Quad2", blockSize, split, stepScale, heightBlock2, totalSize, tempOffset, offsetAmount);
    quad2.setLocalTranslation(origin2);
    quad2.quadrant = 2;
    this.attachChild(quad2);
    // 3 upper right of heightmap, upper right quad
    float[] heightBlock3 = createHeightSubBlock(heightMap, split - 1, 0, split);
    Vector3f origin3 = new Vector3f(quarterSize * stepScale.x, 0, -quarterSize * stepScale.z);
    tempOffset = new Vector2f();
    tempOffset.x = offset.x;
    tempOffset.y = offset.y;
    tempOffset.x += origin3.x;
    tempOffset.y += origin3.z;
    TerrainQuad quad3 = new TerrainQuad(getName() + "Quad3", blockSize, split, stepScale, heightBlock3, totalSize, tempOffset, offsetAmount);
    quad3.setLocalTranslation(origin3);
    quad3.quadrant = 3;
    this.attachChild(quad3);
    // 4 lower right of heightmap, lower right quad
    float[] heightBlock4 = createHeightSubBlock(heightMap, split - 1, split - 1, split);
    Vector3f origin4 = new Vector3f(quarterSize * stepScale.x, 0, quarterSize * stepScale.z);
    tempOffset = new Vector2f();
    tempOffset.x = offset.x;
    tempOffset.y = offset.y;
    tempOffset.x += origin4.x;
    tempOffset.y += origin4.z;
    TerrainQuad quad4 = new TerrainQuad(getName() + "Quad4", blockSize, split, stepScale, heightBlock4, totalSize, tempOffset, offsetAmount);
    quad4.setLocalTranslation(origin4);
    quad4.quadrant = 4;
    this.attachChild(quad4);
}
Also used : Vector2f(com.jme3.math.Vector2f) Vector3f(com.jme3.math.Vector3f)

Example 23 with HeightMap

use of com.jme3.terrain.heightmap.HeightMap in project jmonkeyengine by jMonkeyEngine.

the class TerrainQuad method getHeightmapHeight.

/**
     * This will just get the heightmap value at the supplied point,
     * not an interpolated (actual) height value.
     */
protected float getHeightmapHeight(int x, int z) {
    int quad = findQuadrant(x, z);
    int split = (size + 1) >> 1;
    if (children != null) {
        for (int i = children.size(); --i >= 0; ) {
            Spatial spat = children.get(i);
            int col = x;
            int row = z;
            boolean match = false;
            // get the childs quadrant
            int childQuadrant = 0;
            if (spat instanceof TerrainQuad) {
                childQuadrant = ((TerrainQuad) spat).getQuadrant();
            } else if (spat instanceof TerrainPatch) {
                childQuadrant = ((TerrainPatch) spat).getQuadrant();
            }
            if (childQuadrant == 1 && (quad & 1) != 0) {
                match = true;
            } else if (childQuadrant == 2 && (quad & 2) != 0) {
                row = z - split + 1;
                match = true;
            } else if (childQuadrant == 3 && (quad & 4) != 0) {
                col = x - split + 1;
                match = true;
            } else if (childQuadrant == 4 && (quad & 8) != 0) {
                col = x - split + 1;
                row = z - split + 1;
                match = true;
            }
            if (match) {
                if (spat instanceof TerrainQuad) {
                    return ((TerrainQuad) spat).getHeightmapHeight(col, row);
                } else if (spat instanceof TerrainPatch) {
                    return ((TerrainPatch) spat).getHeightmapHeight(col, row);
                }
            }
        }
    }
    return Float.NaN;
}
Also used : Spatial(com.jme3.scene.Spatial)

Example 24 with HeightMap

use of com.jme3.terrain.heightmap.HeightMap in project jmonkeyengine by jMonkeyEngine.

the class FractalTileLoader method getTerrainQuadAt.

public TerrainQuad getTerrainQuadAt(Vector3f location) {
    HeightMap heightMapAt = getHeightMapAt(location);
    TerrainQuad q = new TerrainQuad("Quad" + location, patchSize, quadSize, heightMapAt == null ? null : heightMapAt.getHeightMap());
    return q;
}
Also used : AbstractHeightMap(com.jme3.terrain.heightmap.AbstractHeightMap) HeightMap(com.jme3.terrain.heightmap.HeightMap) TerrainQuad(com.jme3.terrain.geomipmap.TerrainQuad)

Example 25 with HeightMap

use of com.jme3.terrain.heightmap.HeightMap in project jmonkeyengine by jMonkeyEngine.

the class LODGeomap method writeTexCoordArray.

public FloatBuffer writeTexCoordArray(FloatBuffer store, Vector2f offset, Vector2f scale, float offsetAmount, int totalSize) {
    if (store != null) {
        if (store.remaining() < getWidth() * getHeight() * 2) {
            throw new BufferUnderflowException();
        }
    } else {
        store = BufferUtils.createFloatBuffer(getWidth() * getHeight() * 2);
    }
    if (offset == null) {
        offset = new Vector2f();
    }
    Vector2f tcStore = new Vector2f();
    // work from bottom of heightmap up, so we don't flip the coords
    for (int y = getHeight() - 1; y >= 0; y--) {
        for (int x = 0; x < getWidth(); x++) {
            getUV(x, y, tcStore, offset, offsetAmount, totalSize);
            float tx = tcStore.x * scale.x;
            float ty = tcStore.y * scale.y;
            store.put(tx);
            store.put(ty);
        }
    }
    return store;
}
Also used : Vector2f(com.jme3.math.Vector2f) BufferUnderflowException(java.nio.BufferUnderflowException)

Aggregations

TerrainQuad (com.jme3.terrain.geomipmap.TerrainQuad)18 AbstractHeightMap (com.jme3.terrain.heightmap.AbstractHeightMap)17 Material (com.jme3.material.Material)16 Texture (com.jme3.texture.Texture)16 ImageBasedHeightMap (com.jme3.terrain.heightmap.ImageBasedHeightMap)15 Vector3f (com.jme3.math.Vector3f)13 TerrainLodControl (com.jme3.terrain.geomipmap.TerrainLodControl)11 Camera (com.jme3.renderer.Camera)8 DistanceLodCalculator (com.jme3.terrain.geomipmap.lodcalc.DistanceLodCalculator)8 ArrayList (java.util.ArrayList)7 DirectionalLight (com.jme3.light.DirectionalLight)6 BoundingBox (com.jme3.bounding.BoundingBox)4 RigidBodyControl (com.jme3.bullet.control.RigidBodyControl)4 Vector2f (com.jme3.math.Vector2f)3 TextureKey (com.jme3.asset.TextureKey)2 BulletAppState (com.jme3.bullet.BulletAppState)2 ChaseCamera (com.jme3.input.ChaseCamera)2 AssetNotFoundException (com.jme3.asset.AssetNotFoundException)1 CapsuleCollisionShape (com.jme3.bullet.collision.shapes.CapsuleCollisionShape)1 SphereCollisionShape (com.jme3.bullet.collision.shapes.SphereCollisionShape)1