use of java.nio.HasArrayBufferView in project libgdx by libgdx.
the class GwtGL20 method glReadPixels.
@Override
public void glReadPixels(int x, int y, int width, int height, int format, int type, Buffer pixels) {
// verify request
if ((format != WebGLRenderingContext.RGBA) || (type != WebGLRenderingContext.UNSIGNED_BYTE)) {
throw new GdxRuntimeException("Only format RGBA and type UNSIGNED_BYTE are currently supported for glReadPixels(...). Create an issue when you need other formats.");
}
if (!(pixels instanceof ByteBuffer)) {
throw new GdxRuntimeException("Inputed pixels buffer needs to be of type ByteBuffer for glReadPixels(...).");
}
// create new ArrayBufferView (4 bytes per pixel)
int size = 4 * width * height;
Uint8Array buffer = TypedArrays.createUint8Array(((HasArrayBufferView) pixels).getTypedArray().buffer(), 0, size);
// read bytes to ArrayBufferView
gl.readPixels(x, y, width, height, format, type, buffer);
}
use of java.nio.HasArrayBufferView in project libgdx by libgdx.
the class GwtGL20 method glTexImage2D.
@Override
public void glTexImage2D(int target, int level, int internalformat, int width, int height, int border, int format, int type, Buffer pixels) {
if (pixels == null) {
gl.texImage2D(target, level, internalformat, width, height, border, format, type, null);
} else {
if (pixels.limit() > 1) {
HasArrayBufferView arrayHolder = (HasArrayBufferView) pixels;
ArrayBufferView webGLArray = arrayHolder.getTypedArray();
ArrayBufferView buffer;
if (pixels instanceof FloatBuffer) {
buffer = webGLArray;
} else {
int remainingBytes = pixels.remaining() * 4;
int byteOffset = webGLArray.byteOffset() + pixels.position() * 4;
buffer = Uint8ArrayNative.create(webGLArray.buffer(), byteOffset, remainingBytes);
}
gl.texImage2D(target, level, internalformat, width, height, border, format, type, buffer);
} else {
Pixmap pixmap = Pixmap.pixmaps.get(((IntBuffer) pixels).get(0));
// Prefer to use the HTMLImageElement when possible, since reading from the CanvasElement can be lossy.
if (pixmap.canUseImageElement()) {
gl.texImage2D(target, level, internalformat, format, type, pixmap.getImageElement());
} else {
gl.texImage2D(target, level, internalformat, format, type, pixmap.getCanvasElement());
}
}
}
}
use of java.nio.HasArrayBufferView in project libgdx by libgdx.
the class GwtGL20 method glTexSubImage2D.
@Override
public void glTexSubImage2D(int target, int level, int xoffset, int yoffset, int width, int height, int format, int type, Buffer pixels) {
if (pixels.limit() > 1) {
HasArrayBufferView arrayHolder = (HasArrayBufferView) pixels;
ArrayBufferView webGLArray = arrayHolder.getTypedArray();
ArrayBufferView buffer;
if (pixels instanceof FloatBuffer) {
buffer = webGLArray;
} else {
int remainingBytes = pixels.remaining() * 4;
int byteOffset = webGLArray.byteOffset() + pixels.position() * 4;
buffer = Uint8ArrayNative.create(webGLArray.buffer(), byteOffset, remainingBytes);
}
gl.texSubImage2D(target, level, xoffset, yoffset, width, height, format, type, buffer);
} else {
Pixmap pixmap = Pixmap.pixmaps.get(((IntBuffer) pixels).get(0));
gl.texSubImage2D(target, level, xoffset, yoffset, format, type, pixmap.getCanvasElement());
}
}
Aggregations