use of java.nio.IntBuffer in project jmonkeyengine by jMonkeyEngine.
the class TestTangentGen method createTriangleStripMesh.
private Mesh createTriangleStripMesh() {
Mesh strip = new Mesh();
strip.setMode(Mode.TriangleStrip);
// 3 rows * 3 columns * 3 floats
FloatBuffer vb = BufferUtils.createFloatBuffer(3 * 3 * 3);
vb.rewind();
vb.put(new float[] { 0, 2, 0 });
vb.put(new float[] { 1, 2, 0 });
vb.put(new float[] { 2, 2, 0 });
vb.put(new float[] { 0, 1, 0 });
vb.put(new float[] { 1, 1, 0 });
vb.put(new float[] { 2, 1, 0 });
vb.put(new float[] { 0, 0, 0 });
vb.put(new float[] { 1, 0, 0 });
vb.put(new float[] { 2, 0, 0 });
FloatBuffer nb = BufferUtils.createFloatBuffer(3 * 3 * 3);
nb.rewind();
nb.put(new float[] { 0, 0, 1 });
nb.put(new float[] { 0, 0, 1 });
nb.put(new float[] { 0, 0, 1 });
nb.put(new float[] { 0, 0, 1 });
nb.put(new float[] { 0, 0, 1 });
nb.put(new float[] { 0, 0, 1 });
nb.put(new float[] { 0, 0, 1 });
nb.put(new float[] { 0, 0, 1 });
nb.put(new float[] { 0, 0, 1 });
FloatBuffer tb = BufferUtils.createFloatBuffer(3 * 3 * 2);
tb.rewind();
tb.put(new float[] { 0, 0 });
tb.put(new float[] { 0.5f, 0 });
tb.put(new float[] { 1, 0 });
tb.put(new float[] { 0, 0.5f });
tb.put(new float[] { 0.5f, 0.5f });
tb.put(new float[] { 1, 0.5f });
tb.put(new float[] { 0, 1 });
tb.put(new float[] { 0.5f, 1 });
tb.put(new float[] { 1, 1 });
int[] indexes = new int[] { 0, 3, 1, 4, 2, 5, 5, 3, 3, 6, 4, 7, 5, 8 };
IntBuffer ib = BufferUtils.createIntBuffer(indexes.length);
ib.put(indexes);
strip.setBuffer(Type.Position, 3, vb);
strip.setBuffer(Type.Normal, 3, nb);
strip.setBuffer(Type.TexCoord, 2, tb);
strip.setBuffer(Type.Index, 3, ib);
strip.updateBound();
return strip;
}
use of java.nio.IntBuffer in project jmonkeyengine by jMonkeyEngine.
the class TerrainPatch method generateLodEntropies.
/**
* This calculation is slow, so don't use it often.
*/
public void generateLodEntropies() {
float[] entropies = new float[getMaxLod() + 1];
for (int i = 0; i <= getMaxLod(); i++) {
int curLod = (int) Math.pow(2, i);
IndexBuffer idxB = geomap.writeIndexArrayLodDiff(curLod, false, false, false, false, totalSize);
Buffer ib;
if (idxB.getBuffer() instanceof IntBuffer)
ib = (IntBuffer) idxB.getBuffer();
else
ib = (ShortBuffer) idxB.getBuffer();
entropies[i] = EntropyComputeUtil.computeLodEntropy(mesh, ib);
}
lodEntropy = entropies;
}
use of java.nio.IntBuffer 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.IntBuffer in project jmonkeyengine by jMonkeyEngine.
the class LODGeomap method createMesh.
public Mesh createMesh(Vector3f scale, Vector2f tcScale, Vector2f tcOffset, float offsetAmount, int totalSize, boolean center, int lod, boolean rightLod, boolean topLod, boolean leftLod, boolean bottomLod) {
FloatBuffer pb = writeVertexArray(null, scale, center);
FloatBuffer texb = writeTexCoordArray(null, tcOffset, tcScale, offsetAmount, totalSize);
FloatBuffer nb = writeNormalArray(null, scale);
Buffer ib;
IndexBuffer idxB = writeIndexArrayLodDiff(lod, rightLod, topLod, leftLod, bottomLod, totalSize);
if (idxB.getBuffer() instanceof IntBuffer)
ib = (IntBuffer) idxB.getBuffer();
else
ib = (ShortBuffer) idxB.getBuffer();
FloatBuffer bb = BufferUtils.createFloatBuffer(getWidth() * getHeight() * 3);
FloatBuffer tanb = BufferUtils.createFloatBuffer(getWidth() * getHeight() * 3);
writeTangentArray(nb, tanb, bb, texb, scale);
Mesh m = new Mesh();
m.setMode(Mode.TriangleStrip);
m.setBuffer(Type.Position, 3, pb);
m.setBuffer(Type.Normal, 3, nb);
m.setBuffer(Type.Tangent, 3, tanb);
m.setBuffer(Type.Binormal, 3, bb);
m.setBuffer(Type.TexCoord, 2, texb);
if (ib instanceof IntBuffer)
m.setBuffer(Type.Index, 3, (IntBuffer) ib);
else if (ib instanceof ShortBuffer)
m.setBuffer(Type.Index, 3, (ShortBuffer) ib);
m.setStatic();
m.updateBound();
return m;
}
use of java.nio.IntBuffer in project jmonkeyengine by jMonkeyEngine.
the class GeoMap method createMesh.
public Mesh createMesh(Vector3f scale, Vector2f tcScale, boolean center) {
FloatBuffer pb = writeVertexArray(null, scale, center);
FloatBuffer tb = writeTexCoordArray(null, Vector2f.ZERO, tcScale);
FloatBuffer nb = writeNormalArray(null, scale);
IntBuffer ib = writeIndexArray(null);
Mesh m = new Mesh();
m.setBuffer(Type.Position, 3, pb);
m.setBuffer(Type.Normal, 3, nb);
m.setBuffer(Type.TexCoord, 2, tb);
m.setBuffer(Type.Index, 3, ib);
m.setStatic();
m.updateBound();
return m;
}
Aggregations