use of java.nio.FloatBuffer in project jmonkeyengine by jMonkeyEngine.
the class ObjectHelper method flipMeshIfRequired.
/**
* The method flips the mesh if the scale is mirroring it. Mirroring scale has either 1 or all 3 factors negative.
* If two factors are negative then there is no mirroring because a rotation and translation can be found that will
* lead to the same transform when all scales are positive.
*
* @param geometry
* the geometry that is being flipped if necessary
* @param scale
* the scale vector of the given geometry
*/
private void flipMeshIfRequired(Geometry geometry, Vector3f scale) {
float s = scale.x * scale.y * scale.z;
if (s < 0 && geometry.getMesh() != null) {
// negative s means that the scale is mirroring the object
FloatBuffer normals = geometry.getMesh().getFloatBuffer(Type.Normal);
if (normals != null) {
for (int i = 0; i < normals.limit(); i += 3) {
if (scale.x < 0) {
normals.put(i, -normals.get(i));
}
if (scale.y < 0) {
normals.put(i + 1, -normals.get(i + 1));
}
if (scale.z < 0) {
normals.put(i + 2, -normals.get(i + 2));
}
}
}
if (geometry.getMesh().getMode() == Mode.Triangles) {
// there is no need to flip the indexes for lines and points
LOGGER.finer("Flipping index order in triangle mesh.");
Buffer indexBuffer = geometry.getMesh().getBuffer(Type.Index).getData();
for (int i = 0; i < indexBuffer.limit(); i += 3) {
if (indexBuffer instanceof ShortBuffer) {
short index = ((ShortBuffer) indexBuffer).get(i + 1);
((ShortBuffer) indexBuffer).put(i + 1, ((ShortBuffer) indexBuffer).get(i + 2));
((ShortBuffer) indexBuffer).put(i + 2, index);
} else {
int index = ((IntBuffer) indexBuffer).get(i + 1);
((IntBuffer) indexBuffer).put(i + 1, ((IntBuffer) indexBuffer).get(i + 2));
((IntBuffer) indexBuffer).put(i + 2, index);
}
}
}
}
}
use of java.nio.FloatBuffer in project jmonkeyengine by jMonkeyEngine.
the class TestCustomAnim method simpleInitApp.
@Override
public void simpleInitApp() {
AmbientLight al = new AmbientLight();
rootNode.addLight(al);
DirectionalLight dl = new DirectionalLight();
dl.setDirection(Vector3f.UNIT_XYZ.negate());
rootNode.addLight(dl);
Box box = new Box(1, 1, 1);
VertexBuffer weightsHW = new VertexBuffer(Type.HWBoneWeight);
VertexBuffer indicesHW = new VertexBuffer(Type.HWBoneIndex);
indicesHW.setUsage(Usage.CpuOnly);
weightsHW.setUsage(Usage.CpuOnly);
box.setBuffer(weightsHW);
box.setBuffer(indicesHW);
// Setup bone weight buffer
FloatBuffer weights = FloatBuffer.allocate(box.getVertexCount() * 4);
VertexBuffer weightsBuf = new VertexBuffer(Type.BoneWeight);
weightsBuf.setupData(Usage.CpuOnly, 4, Format.Float, weights);
box.setBuffer(weightsBuf);
// Setup bone index buffer
ByteBuffer indices = ByteBuffer.allocate(box.getVertexCount() * 4);
VertexBuffer indicesBuf = new VertexBuffer(Type.BoneIndex);
indicesBuf.setupData(Usage.CpuOnly, 4, Format.UnsignedByte, indices);
box.setBuffer(indicesBuf);
// Create bind pose buffers
box.generateBindPose(true);
// Create skeleton
bone = new Bone("root");
bone.setBindTransforms(Vector3f.ZERO, Quaternion.IDENTITY, Vector3f.UNIT_XYZ);
bone.setUserControl(true);
skeleton = new Skeleton(new Bone[] { bone });
// Assign all verticies to bone 0 with weight 1
for (int i = 0; i < box.getVertexCount() * 4; i += 4) {
// assign vertex to bone index 0
indices.array()[i + 0] = 0;
indices.array()[i + 1] = 0;
indices.array()[i + 2] = 0;
indices.array()[i + 3] = 0;
// set weight to 1 only for first entry
weights.array()[i + 0] = 1;
weights.array()[i + 1] = 0;
weights.array()[i + 2] = 0;
weights.array()[i + 3] = 0;
}
// Maximum number of weights per bone is 1
box.setMaxNumWeights(1);
// Create model
Geometry geom = new Geometry("box", box);
geom.setMaterial(assetManager.loadMaterial("Textures/Terrain/BrickWall/BrickWall.j3m"));
Node model = new Node("model");
model.attachChild(geom);
// Create skeleton control
SkeletonControl skeletonControl = new SkeletonControl(skeleton);
model.addControl(skeletonControl);
rootNode.attachChild(model);
}
use of java.nio.FloatBuffer in project jmonkeyengine by jMonkeyEngine.
the class HelloOpenCL method testKernel.
private boolean testKernel(Context clContext, CommandQueue clQueue) {
try {
//create fill code
String include = "#define TYPE float\n";
Program program = clContext.createProgramFromSourceFilesWithInclude(assetManager, include, "jme3test/opencl/Blas.cl");
program.build();
Kernel kernel = program.createKernel("Fill");
System.out.println("number of args: " + kernel.getArgCount());
//fill buffer
int size = 256 + 128;
Buffer buffer = clContext.createBuffer(size * 4);
float value = 5;
Event event = kernel.Run1(clQueue, new com.jme3.opencl.Kernel.WorkSize(buffer.getSize() / 4), buffer, value);
event.waitForFinished();
//check if filled
ByteBuffer buf = buffer.map(clQueue, MappingAccess.MAP_READ_ONLY);
FloatBuffer buff = buf.asFloatBuffer();
for (int i = 0; i < size; ++i) {
float v = buff.get(i);
assertEquals(value, v, "Buffer filled with the wrong value at index " + i);
}
buffer.unmap(clQueue, buf);
//release
buffer.release();
kernel.release();
program.release();
} catch (AssertionError ex) {
LOG.log(Level.SEVERE, "kernel test failed with an assertion error");
return false;
} catch (Exception ex) {
LOG.log(Level.SEVERE, "kernel test failed with:", ex);
return false;
}
return true;
}
use of java.nio.FloatBuffer in project jmonkeyengine by jMonkeyEngine.
the class HelloOpenCL method testImages.
private boolean testImages(Context clContext, CommandQueue clQueue) {
try {
//query supported formats
for (MemoryAccess ma : MemoryAccess.values()) {
for (Image.ImageType type : Image.ImageType.values()) {
try {
System.out.println("Formats for " + ma + " and " + type + ": " + Arrays.toString(clContext.querySupportedFormats(ma, type)));
} catch (UnsupportedOperationException e) {
LOG.warning(e.getLocalizedMessage());
}
}
}
//create an image
Image.ImageFormat format = new Image.ImageFormat(Image.ImageChannelOrder.RGBA, Image.ImageChannelType.FLOAT);
Image.ImageDescriptor descr = new Image.ImageDescriptor(Image.ImageType.IMAGE_2D, 1920, 1080, 0, 0);
Image image = clContext.createImage(MemoryAccess.READ_WRITE, format, descr);
System.out.println("image created");
//check queries
assertEquals(descr.type, image.getImageType(), "Wrong image type");
assertEquals(format, image.getImageFormat(), "Wrong image format");
assertEquals(descr.width, image.getWidth(), "Wrong width");
assertEquals(descr.height, image.getHeight(), "Wrong height");
//fill with red and blue
ColorRGBA color1 = ColorRGBA.Red;
ColorRGBA color2 = ColorRGBA.Blue;
Event e1 = image.fillAsync(clQueue, new long[] { 0, 0, 0 }, new long[] { descr.width / 2, descr.height, 1 }, color1);
Event e2 = image.fillAsync(clQueue, new long[] { descr.width / 2, 0, 0 }, new long[] { descr.width / 2, descr.height, 1 }, color2);
e1.waitForFinished();
e2.waitForFinished();
//copy to a buffer
Buffer buffer = clContext.createBuffer(4 * 4 * 500 * 1024);
Event e3 = image.copyToBufferAsync(clQueue, buffer, new long[] { 10, 10, 0 }, new long[] { 500, 1024, 1 }, 0);
e3.release();
//this buffer must be completely red
ByteBuffer map1 = buffer.map(clQueue, MappingAccess.MAP_READ_ONLY);
FloatBuffer map1F = map1.asFloatBuffer();
map1F.rewind();
for (int x = 0; x < 500; ++x) {
for (int y = 0; y < 1024; ++y) {
float r = map1F.get();
float g = map1F.get();
float b = map1F.get();
float a = map1F.get();
assertEquals(1, r, "Wrong red component");
assertEquals(0, g, "Wrong green component");
assertEquals(0, b, "Wrong blue component");
assertEquals(1, a, "Wrong alpha component");
}
}
buffer.unmap(clQueue, map1);
//create a second image
format = new Image.ImageFormat(Image.ImageChannelOrder.RGBA, Image.ImageChannelType.FLOAT);
descr = new Image.ImageDescriptor(Image.ImageType.IMAGE_2D, 512, 512, 0, 0);
Image image2 = clContext.createImage(MemoryAccess.READ_WRITE, format, descr);
//copy an area of image1 to image2
image.copyTo(clQueue, image2, new long[] { 1000, 20, 0 }, new long[] { 0, 0, 0 }, new long[] { 512, 512, 1 });
//this area should be completely blue
Image.ImageMapping map2 = image2.map(clQueue, new long[] { 0, 0, 0 }, new long[] { 512, 512, 1 }, MappingAccess.MAP_READ_WRITE);
FloatBuffer map2F = map2.buffer.asFloatBuffer();
for (int y = 0; y < 512; ++y) {
for (int x = 0; x < 512; ++x) {
long index = 4 * x + y * (map2.rowPitch / 4);
map2F.position((int) index);
float r = map2F.get();
float g = map2F.get();
float b = map2F.get();
float a = map2F.get();
assertEquals(0, r, "Wrong red component");
assertEquals(0, g, "Wrong green component");
assertEquals(1, b, "Wrong blue component");
assertEquals(1, a, "Wrong alpha component");
}
}
image2.unmap(clQueue, map2);
//release
image.release();
image2.release();
buffer.release();
} catch (AssertionError ex) {
LOG.log(Level.SEVERE, "image test failed with an assertion error");
return false;
} catch (Exception ex) {
LOG.log(Level.SEVERE, "image test failed with:", ex);
return false;
}
return true;
}
use of java.nio.FloatBuffer in project mongomvcc by igd-geo.
the class DefaultConvertStrategy method convert.
@Override
public long convert(Object data) {
GridFSInputFile file;
if (data instanceof byte[]) {
file = _gridFS.createFile((byte[]) data);
file.put(BINARY_TYPE, BYTEARRAY);
} else if (data instanceof float[]) {
file = _gridFS.createFile(new FloatArrayInputStream((float[]) data));
file.put(BINARY_TYPE, FLOATARRAY);
} else if (data instanceof InputStream) {
file = _gridFS.createFile((InputStream) data);
file.put(BINARY_TYPE, INPUTSTREAM);
} else if (data instanceof ByteBuffer) {
ByteBuffer bb = (ByteBuffer) data;
byte[] buf;
if (bb.hasArray()) {
buf = bb.array();
} else {
bb.rewind();
buf = new byte[bb.remaining()];
bb.get(buf);
}
file = _gridFS.createFile(buf);
file.put(BINARY_TYPE, BYTEBUFFER);
} else if (data instanceof FloatBuffer) {
FloatBuffer bb = (FloatBuffer) data;
float[] buf;
if (bb.hasArray()) {
buf = bb.array();
} else {
bb.rewind();
buf = new float[bb.remaining()];
bb.get(buf);
}
file = _gridFS.createFile(new FloatArrayInputStream(buf));
file.put(BINARY_TYPE, FLOATBUFFER);
} else {
return 0;
}
long oid = _counter.getNextId();
file.put(MongoDBVLargeCollection.OID, oid);
_convertedFiles.add(file);
return oid;
}
Aggregations