Package edu.ups.gamedev.terrain

Source Code of edu.ups.gamedev.terrain.MaterialTerrainPage

/*
* License goes here.
* GPL?
*/

package edu.ups.gamedev.terrain;

import com.jme.bounding.BoundingBox;
import com.jme.math.Vector2f;
import com.jme.math.Vector3f;
import com.jmex.terrain.TerrainBlock;
import com.jmex.terrain.TerrainPage;

import edu.ups.gamedev.scene.Collidable;

/**
*
* @author scanfield
*/
public class MaterialTerrainPage extends TerrainPage implements Collidable {

  public boolean canCollideWith(Collidable object) {
    // TODO handle terrain collisions
    return true;
  }

  public MaterialMap materialMap;

  public MaterialTerrainPage(String name, int blockSize, int size,
      Vector3f stepScale, int[] heightMap, boolean clod,
      MaterialMap matMap) {
    // super( name, blockSize, size, stepScale, heightMap, clod );
    this(name, blockSize, size, stepScale, heightMap, clod, size,
        new Vector2f(), 0, matMap);
    fixNormals();

  }

  public MaterialTerrainPage(String name) {
    super(name);
  }

  public MaterialTerrainPage() {
    super();
  }

  public MaterialTerrainPage(String name, int blockSize, int size,
      Vector3f stepScale, int[] heightMap, boolean clod, int totalSize,
      Vector2f offset, float offsetAmount, MaterialMap matMap) {
    // super( name, blockSize, size, stepScale, heightMap, clod, totalSize, offset, offsetAmount );
    super(name);
    this.materialMap = matMap;
    setOffset(offset);
    setOffsetAmount(offsetAmount);
    setTotalSize(totalSize);
    setSize(size);
    setStepScale(stepScale);
    split(blockSize, heightMap, clod);
  }

  private void split(int blockSize, int[] heightMap, boolean clod) {
    if ((getSize() >> 1) + 1 <= blockSize) {
      createQuadBlock(heightMap, clod);
    } else {
      createQuadPage(blockSize, heightMap, clod);
    }

  }

  /**
   * <code>createQuadPage</code> generates four new pages from this page.
   */
  private void createQuadPage(int blockSize, int[] heightMap, boolean clod) {
    // create 4 terrain pages
    int quarterSize = getSize() >> 2;

    int split = (getSize() + 1) >> 1;

    Vector2f tempOffset = new Vector2f();
    setOffsetAmount(getOffsetAmount() + quarterSize); // getOffsetAmount() += quarterSize;

    // 1 upper left
    int[] heightBlock1 = createHeightSubBlock(heightMap, 0, 0, split);

    Vector3f origin1 = new Vector3f(-quarterSize * getStepScale().x, 0,
        -quarterSize * getStepScale().z);

    tempOffset.x = getOffset().x;
    tempOffset.y = getOffset().y;
    tempOffset.x += origin1.x;
    tempOffset.y += origin1.z;

    MaterialTerrainPage page1 = new MaterialTerrainPage(
        getName() + "Page1", blockSize, split, getStepScale(),
        heightBlock1, clod, getTotalSize(), tempOffset,
        getOffsetAmount(), materialMap);
    page1.setLocalTranslation(origin1);
    page1.setQuadrant((short) 1); // quadrant = 1;
    this.attachChild(page1);

    // 2 lower left
    int[] heightBlock2 = createHeightSubBlock(heightMap, 0, split - 1,
        split);

    Vector3f origin2 = new Vector3f(-quarterSize * getStepScale().x, 0,
        quarterSize * getStepScale().z);

    tempOffset.x = getOffset().x;
    tempOffset.y = getOffset().y;
    tempOffset.x += origin2.x;
    tempOffset.y += origin2.z;

    MaterialTerrainPage page2 = new MaterialTerrainPage(
        getName() + "Page2", blockSize, split, getStepScale(),
        heightBlock2, clod, getTotalSize(), tempOffset,
        getOffsetAmount(), materialMap);
    page2.setLocalTranslation(origin2);
    page2.setQuadrant((short) 2);
    this.attachChild(page2);

    // 3 upper right
    int[] heightBlock3 = createHeightSubBlock(heightMap, split - 1, 0,
        split);

    Vector3f origin3 = new Vector3f(quarterSize * getStepScale().x, 0,
        -quarterSize * getStepScale().z);

    tempOffset.x = getOffset().x;
    tempOffset.y = getOffset().y;
    tempOffset.x += origin3.x;
    tempOffset.y += origin3.z;

    MaterialTerrainPage page3 = new MaterialTerrainPage(
        getName() + "Page3", blockSize, split, getStepScale(),
        heightBlock3, clod, getTotalSize(), tempOffset,
        getOffsetAmount(), materialMap);
    page3.setLocalTranslation(origin3);
    page3.setQuadrant((short) 3);
    this.attachChild(page3);
    // //
    // 4 lower right
    int[] heightBlock4 = createHeightSubBlock(heightMap, split - 1,
        split - 1, split);

    Vector3f origin4 = new Vector3f(quarterSize * getStepScale().x, 0,
        quarterSize * getStepScale().z);

    tempOffset.x = getOffset().x;
    tempOffset.y = getOffset().y;
    tempOffset.x += origin4.x;
    tempOffset.y += origin4.z;

    MaterialTerrainPage page4 = new MaterialTerrainPage(
        getName() + "Page4", blockSize, split, getStepScale(),
        heightBlock4, clod, getTotalSize(), tempOffset,
        getOffsetAmount(), materialMap);
    page4.setLocalTranslation(origin4);
    page4.setQuadrant((short) 4);
    this.attachChild(page4);

  }

  /**
   * <code>createQuadBlock</code> creates four child blocks from this page.
   */
  private void createQuadBlock(int[] heightMap, boolean clod) {
    // create 4 terrain blocks
    int quarterSize = getSize() >> 2;
    int halfSize = getSize() >> 1;
    int split = (getSize() + 1) >> 1;

    Vector2f tempOffset = new Vector2f();
    setOffsetAmount(getOffsetAmount() + quarterSize); // getOffsetAmount() += quarterSize;

    // 1 upper left
    int[] heightBlock1 = createHeightSubBlock(heightMap, 0, 0, split);

    Vector3f origin1 = new Vector3f(-halfSize * getStepScale().x, 0,
        -halfSize * getStepScale().z);

    tempOffset.x = getOffset().x;
    tempOffset.y = getOffset().y;
    tempOffset.x += origin1.x / 2;
    tempOffset.y += origin1.z / 2;

    MaterialTerrainBlock block1 = new MaterialTerrainBlock(getName()
        + "Block1", split, getStepScale(), heightBlock1, origin1, clod,
        getTotalSize(), tempOffset, getOffsetAmount(), materialMap);
    block1.setQuadrant((short) 1);
    this.attachChild(block1);
    block1.setModelBound(new BoundingBox());
    block1.updateModelBound();

    // 2 lower left
    int[] heightBlock2 = createHeightSubBlock(heightMap, 0, split - 1,
        split);

    Vector3f origin2 = new Vector3f(-halfSize * getStepScale().x, 0, 0);

    tempOffset.x = getOffset().x;
    tempOffset.y = getOffset().y;
    tempOffset.x += origin1.x / 2;
    tempOffset.y += quarterSize * getStepScale().z;

    MaterialTerrainBlock block2 = new MaterialTerrainBlock(getName()
        + "Block2", split, getStepScale(), heightBlock2, origin2, clod,
        getTotalSize(), tempOffset, getOffsetAmount(), materialMap);
    block2.setQuadrant((short) 2);
    this.attachChild(block2);
    block2.setModelBound(new BoundingBox());
    block2.updateModelBound();

    // 3 upper right
    int[] heightBlock3 = createHeightSubBlock(heightMap, split - 1, 0,
        split);

    Vector3f origin3 = new Vector3f(0, 0, -halfSize * getStepScale().z);

    tempOffset.x = getOffset().x;
    tempOffset.y = getOffset().y;
    tempOffset.x += quarterSize * getStepScale().x;
    tempOffset.y += origin3.z / 2;

    MaterialTerrainBlock block3 = new MaterialTerrainBlock(getName()
        + "Block3", split, getStepScale(), heightBlock3, origin3, clod,
        getTotalSize(), tempOffset, getOffsetAmount(), materialMap);
    block3.setQuadrant((short) 3);
    this.attachChild(block3);
    block3.setModelBound(new BoundingBox());
    block3.updateModelBound();

    // 4 lower right
    int[] heightBlock4 = createHeightSubBlock(heightMap, split - 1,
        split - 1, split);

    Vector3f origin4 = new Vector3f(0, 0, 0);

    tempOffset.x = getOffset().x;
    tempOffset.y = getOffset().y;
    tempOffset.x += quarterSize * getStepScale().x;
    tempOffset.y += quarterSize * getStepScale().z;

    MaterialTerrainBlock block4 = new MaterialTerrainBlock(getName()
        + "Block4", split, getStepScale(), heightBlock4, origin4, clod,
        getTotalSize(), tempOffset, getOffsetAmount(), materialMap);
    block4.setQuadrant((short) 4);
    this.attachChild(block4);
    block4.setModelBound(new BoundingBox());
    block4.updateModelBound();
  }

}
TOP

Related Classes of edu.ups.gamedev.terrain.MaterialTerrainPage

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.