📄 terrainpage.java
字号:
public void fixNormals() {
if (children != null) {
for (int x = children.size(); --x >= 0;) {
Spatial child = children.get(x);
if (child instanceof TerrainPage) {
((TerrainPage) child).fixNormals();
} else if (child instanceof TerrainBlock) {
TerrainBlock tb = (TerrainBlock) child;
TerrainBlock right = _findRightBlock(tb);
TerrainBlock down = _findDownBlock(tb);
int tbSize = tb.getSize();
if (right != null) {
float[] normData = new float[3];
for (int y = 0; y < tbSize; y++) {
int index1 = ((y + 1) * tbSize) - 1;
int index2 = (y * tbSize);
right.getNormalBuffer().position(index2 * 3);
right.getNormalBuffer().get(normData);
tb.getNormalBuffer().position(index1 * 3);
tb.getNormalBuffer().put(normData);
}
deleteNormalVBO(right);
}
if (down != null) {
int rowStart = ((tbSize - 1) * tbSize);
float[] normData = new float[3];
for (int z = 0; z < tbSize; z++) {
int index1 = rowStart + z;
int index2 = z;
down.getNormalBuffer().position(index2 * 3);
down.getNormalBuffer().get(normData);
tb.getNormalBuffer().position(index1 * 3);
tb.getNormalBuffer().put(normData);
}
deleteNormalVBO(down);
}
deleteNormalVBO(tb);
}
}
}
}
private TerrainBlock getBlock(int quad) {
if (children != null)
for (int x = children.size(); --x >= 0;) {
Spatial child = children.get(x);
if (child instanceof TerrainBlock) {
TerrainBlock tb = (TerrainBlock) child;
if (tb.getQuadrant() == quad)
return tb;
}
}
return null;
}
private TerrainPage getPage(int quad) {
if (children != null)
for (int x = children.size(); --x >= 0;) {
Spatial child = children.get(x);
if (child instanceof TerrainPage) {
TerrainPage tp = (TerrainPage) child;
if (tp.getQuadrant() == quad)
return tp;
}
}
return null;
}
private TerrainBlock _findRightBlock(TerrainBlock tb) {
if (tb.getQuadrant() == 1)
return getBlock(3);
else if (tb.getQuadrant() == 2)
return getBlock(4);
else if (tb.getQuadrant() == 3) {
// find the page to the right and ask it for child 1.
TerrainPage tp = _findRightPage();
if (tp != null)
return tp.getBlock(1);
} else if (tb.getQuadrant() == 4) {
// find the page to the right and ask it for child 2.
TerrainPage tp = _findRightPage();
if (tp != null)
return tp.getBlock(2);
}
return null;
}
private TerrainBlock _findDownBlock(TerrainBlock tb) {
if (tb.getQuadrant() == 1)
return getBlock(2);
else if (tb.getQuadrant() == 3)
return getBlock(4);
else if (tb.getQuadrant() == 2) {
// find the page below and ask it for child 1.
TerrainPage tp = _findDownPage();
if (tp != null)
return tp.getBlock(1);
} else if (tb.getQuadrant() == 4) {
TerrainPage tp = _findDownPage();
if (tp != null)
return tp.getBlock(3);
}
return null;
}
private TerrainPage _findRightPage() {
if (getParent() == null || !(getParent() instanceof TerrainPage))
return null;
TerrainPage pPage = (TerrainPage) getParent();
if (quadrant == 1)
return pPage.getPage(3);
else if (quadrant == 2)
return pPage.getPage(4);
else if (quadrant == 3) {
TerrainPage tp = pPage._findRightPage();
if (tp != null)
return tp.getPage(1);
} else if (quadrant == 4) {
TerrainPage tp = pPage._findRightPage();
if (tp != null)
return tp.getPage(2);
}
return null;
}
private TerrainPage _findDownPage() {
if (getParent() == null || !(getParent() instanceof TerrainPage))
return null;
TerrainPage pPage = (TerrainPage) getParent();
if (quadrant == 1)
return pPage.getPage(2);
else if (quadrant == 3)
return pPage.getPage(4);
else if (quadrant == 2) {
TerrainPage tp = pPage._findDownPage();
if (tp != null)
return tp.getPage(1);
} else if (quadrant == 4) {
TerrainPage tp = pPage._findDownPage();
if (tp != null)
return tp.getPage(3);
}
return null;
}
public static final float[] createHeightSubBlock(float[] heightMap, int x,
int y, int side) {
float[] rVal = new float[side * side];
int bsize = (int) FastMath.sqrt(heightMap.length);
int count = 0;
for (int i = y; i < side + y; i++) {
for (int j = x; j < side + x; j++) {
if (j < bsize && i < bsize)
rVal[count] = heightMap[j + (i * bsize)];
count++;
}
}
return rVal;
}
/**
* <code>setHeightMapValue</code> sets the value of this block's height
* map at the given coords
*
* @param x
* @param y
* @param newVal
*/
public void setHeightMapValue(int x, int y, float newVal) {
int quad = findQuadrant(x, y);
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 = y;
boolean match = false;
// get the childs quadrant
int childQuadrant = 0;
if (spat instanceof TerrainPage) {
childQuadrant = ((TerrainPage) spat).getQuadrant();
} else if (spat instanceof TerrainBlock) {
childQuadrant = ((TerrainBlock) spat).getQuadrant();
}
if (childQuadrant == 1 && (quad & 1) != 0) {
match = true;
} else if (childQuadrant == 2 && (quad & 2) != 0) {
row = y - 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 = y - split + 1;
match = true;
}
if (match) {
if (spat instanceof TerrainPage) {
((TerrainPage) spat)
.setHeightMapValue(col, row, newVal);
} else if (spat instanceof TerrainBlock) {
((TerrainBlock) spat).setHeightMapValue(col, row,
newVal);
}
}
}
}
/**
* <code>addHeightMapValue</code> adds to the value of this block's height
* map at the given coords
*
* @param x
* @param y
* @param toAdd
*/
public void addHeightMapValue(int x, int y, float toAdd) {
int quad = findQuadrant(x, y);
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 = y;
boolean match = false;
// get the childs quadrant
int childQuadrant = 0;
if (spat instanceof TerrainPage) {
childQuadrant = ((TerrainPage) spat).getQuadrant();
} else if (spat instanceof TerrainBlock) {
childQuadrant = ((TerrainBlock) spat).getQuadrant();
}
if (childQuadrant == 1 && (quad & 1) != 0) {
match = true;
} else if (childQuadrant == 2 && (quad & 2) != 0) {
row = y - 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 = y - split + 1;
match = true;
}
if (match) {
if (spat instanceof TerrainPage) {
((TerrainPage) spat).addHeightMapValue(col, row, toAdd);
} else if (spat instanceof TerrainBlock) {
((TerrainBlock) spat)
.addHeightMapValue(col, row, toAdd);
}
}
}
}
/**
* <code>multHeightMapValue</code> multiplies the value of this block's
* height map at the given coords by the value given.
*
* @param x
* @param y
* @param toMult
*/
public void multHeightMapValue(int x, int y, float toMult) {
int quad = findQuadrant(x, y);
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 = y;
boolean match = false;
// get the childs quadrant
int childQuadrant = 0;
if (spat instanceof TerrainPage) {
childQuadrant = ((TerrainPage) spat).getQuadrant();
} else if (spat instanceof TerrainBlock) {
childQuadrant = ((TerrainBlock) spat).getQuadrant();
}
if (childQuadrant == 1 && (quad & 1) != 0) {
match = true;
} else if (childQuadrant == 2 && (quad & 2) != 0) {
row = y - 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 = y - split + 1;
match = true;
}
if (match) {
if (spat instanceof TerrainPage) {
((TerrainPage) spat).multHeightMapValue(col, row,
toMult);
} else if (spat instanceof TerrainBlock) {
((TerrainBlock) spat).multHeightMapValue(col, row,
toMult);
}
}
}
}
// a position can be in multiple quadrants, so use a bit anded value.
private int findQuadrant(int x, int y) {
int split = (size + 1) >> 1;
int quads = 0;
if (x < split && y < split)
quads |= 1;
if (x < split && y >= split - 1)
quads |= 2;
if (x >= split - 1 && y < split)
quads |= 4;
if (x >= split - 1 && y >= split - 1)
quads |= 8;
return quads;
}
/**
* @return Returns the quadrant.
*/
public short getQuadrant() {
return quadrant;
}
/**
* @param quadrant
* The quadrant to set.
*/
public void setQuadrant(short quadrant) {
this.quadrant = quadrant;
}
public void write(JMEExporter e) throws IOException {
super.write(e);
OutputCapsule capsule = e.getCapsule(this);
capsule.write(offset, "offset", Vector3f.ZERO);
capsule.write(totalSize, "totalSize", 0);
capsule.write(size, "size", 0);
capsule.write(stepScale, "stepScale", new Vector2f());
capsule.write(offsetAmount, "offsetAmount", 0);
capsule.write(quadrant, "quadrant", (short) 1);
}
public void read(JMEImporter e) throws IOException {
super.read(e);
InputCapsule capsule = e.getCapsule(this);
offset = (Vector2f) capsule
.readSavable("offset", Vector3f.ZERO.clone());
totalSize = capsule.readInt("totalSize", 0);
size = capsule.readInt("size", 0);
stepScale = (Vector3f) capsule.readSavable("stepScale", new Vector2f());
offsetAmount = capsule.readFloat("offsetAmount", 0);
quadrant = capsule.readShort("quadrant", (short) 1);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -