use of com.jme3.terrain.geomipmap.TerrainPatch in project jmonkeyengine by jMonkeyEngine.
the class TerrainQuad method findMatchingChild.
private QuadrantChild findMatchingChild(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)
return new QuadrantChild(col, row, spat);
}
}
return null;
}
use of com.jme3.terrain.geomipmap.TerrainPatch in project jmonkeyengine by jMonkeyEngine.
the class PerspectiveLodCalculator method getCenterLocation.
public Vector3f getCenterLocation(TerrainPatch patch) {
Vector3f loc = patch.getWorldTranslation().clone();
loc.x += patch.getSize() / 2;
loc.z += patch.getSize() / 2;
return loc;
}
use of com.jme3.terrain.geomipmap.TerrainPatch in project jmonkeyengine by jMonkeyEngine.
the class BresenhamTerrainPicker method getTerrainIntersection.
public Vector3f getTerrainIntersection(Ray worldPick, CollisionResults results) {
worldPickRay.set(worldPick);
List<TerrainPickData> pickData = new ArrayList<TerrainPickData>();
root.findPick(worldPick.clone(), pickData);
Collections.sort(pickData);
if (pickData.isEmpty())
return null;
workRay.set(worldPick);
for (TerrainPickData pd : pickData) {
TerrainPatch patch = pd.targetPatch;
tracer.getGridSpacing().set(patch.getWorldScale());
tracer.setGridOrigin(patch.getWorldTranslation());
workRay.getOrigin().set(worldPick.getDirection()).multLocal(pd.cr.getDistance() - .1f).addLocal(worldPick.getOrigin());
tracer.startWalk(workRay);
final Vector3f intersection = new Vector3f();
final Vector2f loc = tracer.getGridLocation();
if (tracer.isRayPerpendicularToGrid()) {
Triangle hit = new Triangle();
checkTriangles(loc.x, loc.y, workRay, intersection, patch, hit);
float distance = worldPickRay.origin.distance(intersection);
CollisionResult cr = new CollisionResult(intersection, distance);
cr.setGeometry(patch);
cr.setContactNormal(hit.getNormal());
results.addCollision(cr);
return intersection;
}
while (loc.x >= -1 && loc.x <= patch.getSize() && loc.y >= -1 && loc.y <= patch.getSize()) {
//System.out.print(loc.x+","+loc.y+" : ");
// check the triangles of main square for intersection.
Triangle hit = new Triangle();
if (checkTriangles(loc.x, loc.y, workRay, intersection, patch, hit)) {
// we found an intersection, so return that!
float distance = worldPickRay.origin.distance(intersection);
CollisionResult cr = new CollisionResult(intersection, distance);
cr.setGeometry(patch);
results.addCollision(cr);
cr.setContactNormal(hit.getNormal());
return intersection;
}
// because of how we get our height coords, we will
// sometimes be off by a grid spot, so we check the next
// grid space up.
int dx = 0, dz = 0;
Direction d = tracer.getLastStepDirection();
switch(d) {
case PositiveX:
case NegativeX:
dx = 0;
dz = 1;
break;
case PositiveZ:
case NegativeZ:
dx = 1;
dz = 0;
break;
}
if (checkTriangles(loc.x + dx, loc.y + dz, workRay, intersection, patch, hit)) {
// we found an intersection, so return that!
float distance = worldPickRay.origin.distance(intersection);
CollisionResult cr = new CollisionResult(intersection, distance);
results.addCollision(cr);
cr.setGeometry(patch);
cr.setContactNormal(hit.getNormal());
return intersection;
}
tracer.next();
}
}
return null;
}
use of com.jme3.terrain.geomipmap.TerrainPatch in project jmonkeyengine by jMonkeyEngine.
the class TerrainQuad method createQuadPatch.
/**
* <code>createQuadPatch</code> creates four child patches from this quad.
*/
protected void createQuadPatch(float[] heightMap) {
// create 4 terrain patches
int quarterSize = size >> 2;
int halfSize = size >> 1;
int split = (size + 1) >> 1;
//if (lodCalculator == null)
// lodCalculator = createDefaultLodCalculator(); // set a default one
offsetAmount += quarterSize;
// 1 lower left
float[] heightBlock1 = createHeightSubBlock(heightMap, 0, 0, split);
Vector3f origin1 = new Vector3f(-halfSize * stepScale.x, 0, -halfSize * stepScale.z);
Vector2f tempOffset1 = new Vector2f();
tempOffset1.x = offset.x;
tempOffset1.y = offset.y;
tempOffset1.x += origin1.x / 2;
tempOffset1.y += origin1.z / 2;
TerrainPatch patch1 = new TerrainPatch(getName() + "Patch1", split, stepScale, heightBlock1, origin1, totalSize, tempOffset1, offsetAmount);
patch1.setQuadrant((short) 1);
this.attachChild(patch1);
patch1.setModelBound(new BoundingBox());
patch1.updateModelBound();
//patch1.setLodCalculator(lodCalculator);
//TangentBinormalGenerator.generate(patch1);
// 2 upper left
float[] heightBlock2 = createHeightSubBlock(heightMap, 0, split - 1, split);
Vector3f origin2 = new Vector3f(-halfSize * stepScale.x, 0, 0);
Vector2f tempOffset2 = new Vector2f();
tempOffset2.x = offset.x;
tempOffset2.y = offset.y;
tempOffset2.x += origin1.x / 2;
tempOffset2.y += quarterSize * stepScale.z;
TerrainPatch patch2 = new TerrainPatch(getName() + "Patch2", split, stepScale, heightBlock2, origin2, totalSize, tempOffset2, offsetAmount);
patch2.setQuadrant((short) 2);
this.attachChild(patch2);
patch2.setModelBound(new BoundingBox());
patch2.updateModelBound();
//patch2.setLodCalculator(lodCalculator);
//TangentBinormalGenerator.generate(patch2);
// 3 lower right
float[] heightBlock3 = createHeightSubBlock(heightMap, split - 1, 0, split);
Vector3f origin3 = new Vector3f(0, 0, -halfSize * stepScale.z);
Vector2f tempOffset3 = new Vector2f();
tempOffset3.x = offset.x;
tempOffset3.y = offset.y;
tempOffset3.x += quarterSize * stepScale.x;
tempOffset3.y += origin3.z / 2;
TerrainPatch patch3 = new TerrainPatch(getName() + "Patch3", split, stepScale, heightBlock3, origin3, totalSize, tempOffset3, offsetAmount);
patch3.setQuadrant((short) 3);
this.attachChild(patch3);
patch3.setModelBound(new BoundingBox());
patch3.updateModelBound();
//patch3.setLodCalculator(lodCalculator);
//TangentBinormalGenerator.generate(patch3);
// 4 upper right
float[] heightBlock4 = createHeightSubBlock(heightMap, split - 1, split - 1, split);
Vector3f origin4 = new Vector3f(0, 0, 0);
Vector2f tempOffset4 = new Vector2f();
tempOffset4.x = offset.x;
tempOffset4.y = offset.y;
tempOffset4.x += quarterSize * stepScale.x;
tempOffset4.y += quarterSize * stepScale.z;
TerrainPatch patch4 = new TerrainPatch(getName() + "Patch4", split, stepScale, heightBlock4, origin4, totalSize, tempOffset4, offsetAmount);
patch4.setQuadrant((short) 4);
this.attachChild(patch4);
patch4.setModelBound(new BoundingBox());
patch4.updateModelBound();
//patch4.setLodCalculator(lodCalculator);
//TangentBinormalGenerator.generate(patch4);
}
Aggregations