use of java.nio.FloatBuffer in project jmonkeyengine by jMonkeyEngine.
the class TerrainPatch method getMeshNormal.
protected Vector3f getMeshNormal(int x, int z) {
if (x >= size || z >= size)
// out of range
return null;
int index = (z * size + x) * 3;
FloatBuffer nb = (FloatBuffer) this.getMesh().getBuffer(Type.Normal).getData();
Vector3f normal = new Vector3f();
normal.x = nb.get(index);
normal.y = nb.get(index + 1);
normal.z = nb.get(index + 2);
return normal;
}
use of java.nio.FloatBuffer in project jmonkeyengine by jMonkeyEngine.
the class TerrainPatch method updateNormals.
/**
* recalculate all of the normal vectors in this terrain patch
*/
protected void updateNormals() {
FloatBuffer newNormalBuffer = geomap.writeNormalArray(null, getWorldScale());
getMesh().getBuffer(Type.Normal).updateData(newNormalBuffer);
FloatBuffer newTangentBuffer = null;
FloatBuffer newBinormalBuffer = null;
FloatBuffer[] tb = geomap.writeTangentArray(newNormalBuffer, newTangentBuffer, newBinormalBuffer, (FloatBuffer) getMesh().getBuffer(Type.TexCoord).getData(), getWorldScale());
newTangentBuffer = tb[0];
newBinormalBuffer = tb[1];
getMesh().getBuffer(Type.Tangent).updateData(newTangentBuffer);
getMesh().getBuffer(Type.Binormal).updateData(newBinormalBuffer);
}
use of java.nio.FloatBuffer in project jmonkeyengine by jMonkeyEngine.
the class EntropyComputeUtil method computeLodEntropy.
public static float computeLodEntropy(Mesh terrainBlock, Buffer lodIndices) {
// Bounding box for the terrain block
BoundingBox bbox = (BoundingBox) terrainBlock.getBound();
// Vertex positions for the block
FloatBuffer positions = terrainBlock.getFloatBuffer(Type.Position);
// Prepare to cast rays
Vector3f pos = new Vector3f();
Vector3f dir = new Vector3f(0, -1, 0);
Ray ray = new Ray(pos, dir);
// Prepare collision results
CollisionResults results = new CollisionResults();
// Set the LOD indices on the block
VertexBuffer originalIndices = terrainBlock.getBuffer(Type.Index);
terrainBlock.clearBuffer(Type.Index);
if (lodIndices instanceof IntBuffer)
terrainBlock.setBuffer(Type.Index, 3, (IntBuffer) lodIndices);
else if (lodIndices instanceof ShortBuffer) {
terrainBlock.setBuffer(Type.Index, 3, (ShortBuffer) lodIndices);
}
// Recalculate collision mesh
terrainBlock.createCollisionData();
float entropy = 0;
for (int i = 0; i < positions.limit() / 3; i++) {
BufferUtils.populateFromBuffer(pos, positions, i);
float realHeight = pos.y;
pos.addLocal(0, bbox.getYExtent(), 0);
ray.setOrigin(pos);
results.clear();
terrainBlock.collideWith(ray, Matrix4f.IDENTITY, bbox, results);
if (results.size() > 0) {
Vector3f contactPoint = results.getClosestCollision().getContactPoint();
float delta = Math.abs(realHeight - contactPoint.y);
entropy = Math.max(delta, entropy);
}
}
// Restore original indices
terrainBlock.clearBuffer(Type.Index);
terrainBlock.setBuffer(originalIndices);
return entropy;
}
use of java.nio.FloatBuffer in project jmonkeyengine by jMonkeyEngine.
the class FractalTileLoader method getHeightMapAt.
private HeightMap getHeightMapAt(Vector3f location) {
AbstractHeightMap heightmap = null;
FloatBuffer buffer = this.base.getBuffer(location.x * (this.quadSize - 1), location.z * (this.quadSize - 1), 0, this.quadSize);
float[] arr = buffer.array();
for (int i = 0; i < arr.length; i++) {
arr[i] = arr[i] * this.heightScale;
}
heightmap = new FloatBufferHeightMap(buffer);
heightmap.load();
return heightmap;
}
use of java.nio.FloatBuffer in project jmonkeyengine by jMonkeyEngine.
the class LODGeomap method writeTangentArray.
public FloatBuffer[] writeTangentArray(FloatBuffer normalBuffer, FloatBuffer tangentStore, FloatBuffer binormalStore, FloatBuffer textureBuffer, Vector3f scale) {
if (!isLoaded()) {
throw new NullPointerException();
}
if (tangentStore != null) {
if (tangentStore.remaining() < getWidth() * getHeight() * 3) {
throw new BufferUnderflowException();
}
} else {
tangentStore = BufferUtils.createFloatBuffer(getWidth() * getHeight() * 3);
}
tangentStore.rewind();
if (binormalStore != null) {
if (binormalStore.remaining() < getWidth() * getHeight() * 3) {
throw new BufferUnderflowException();
}
} else {
binormalStore = BufferUtils.createFloatBuffer(getWidth() * getHeight() * 3);
}
binormalStore.rewind();
Vector3f normal = new Vector3f();
Vector3f tangent = new Vector3f();
Vector3f binormal = new Vector3f();
for (int r = 0; r < getHeight(); r++) {
for (int c = 0; c < getWidth(); c++) {
int idx = (r * getWidth() + c) * 3;
normal.set(normalBuffer.get(idx), normalBuffer.get(idx + 1), normalBuffer.get(idx + 2));
tangent.set(normal.cross(new Vector3f(0, 0, 1)));
binormal.set(new Vector3f(1, 0, 0).cross(normal));
// save the tangent
BufferUtils.setInBuffer(tangent.normalizeLocal(), tangentStore, (r * getWidth() + c));
// save the binormal
BufferUtils.setInBuffer(binormal.normalizeLocal(), binormalStore, (r * getWidth() + c));
}
}
/* for (int r = 0; r < getHeight(); r++) {
for (int c = 0; c < getWidth(); c++) {
int texIdx = ((getHeight() - 1 - r) * getWidth() + c) * 2; // pull from the end
int texIdxAbove = ((getHeight() - 1 - (r - 1)) * getWidth() + c) * 2; // pull from the end
int texIdxNext = ((getHeight() - 1 - (r + 1)) * getWidth() + c) * 2; // pull from the end
v1.set(c, getValue(c, r), r);
t1.set(textureBuffer.get(texIdx), textureBuffer.get(texIdx + 1));
// below
if (r == getHeight()-1) { // last row
v3.set(c, getValue(c, r), r + 1);
float u = textureBuffer.get(texIdx) - textureBuffer.get(texIdxAbove);
u += textureBuffer.get(texIdx);
float v = textureBuffer.get(texIdx + 1) - textureBuffer.get(texIdxAbove + 1);
v += textureBuffer.get(texIdx + 1);
t3.set(u, v);
} else {
v3.set(c, getValue(c, r + 1), r + 1);
t3.set(textureBuffer.get(texIdxNext), textureBuffer.get(texIdxNext + 1));
}
//right
if (c == getWidth()-1) { // last column
v2.set(c + 1, getValue(c, r), r);
float u = textureBuffer.get(texIdx) - textureBuffer.get(texIdx - 2);
u += textureBuffer.get(texIdx);
float v = textureBuffer.get(texIdx + 1) - textureBuffer.get(texIdx - 1);
v += textureBuffer.get(texIdx - 1);
t2.set(u, v);
} else {
v2.set(c + 1, getValue(c + 1, r), r); // one to the right
t2.set(textureBuffer.get(texIdx + 2), textureBuffer.get(texIdx + 3));
}
calculateTangent(new Vector3f[]{v1.mult(scale), v2.mult(scale), v3.mult(scale)}, new Vector2f[]{t1, t2, t3}, tangent, binormal);
BufferUtils.setInBuffer(tangent, tangentStore, (r * getWidth() + c)); // save the tangent
BufferUtils.setInBuffer(binormal, binormalStore, (r * getWidth() + c)); // save the binormal
}
}
*/
return new FloatBuffer[] { tangentStore, binormalStore };
}
Aggregations