[英]How can I bind a pixel array of integer colors to a texture using the Android NDK?
我正在尝试将Android上的Java OpenGL代码移植到本机SDK,并且需要一个IntBuffer实现。
基本上,我在Java中将任意整数RGBA像素颜色数组加载到纹理中的操作是:
// pixel array
pixelIntArray = new int[width * height];
bb = ByteBuffer.allocateDirect(pixelIntArray.length * 4);
bb.order(ByteOrder.nativeOrder());
// native buffer
pixelBuffer = bb.asIntBuffer();
// push integer array of pixels into buffer
pixelBuffer.put(pixelIntArray);
pixelBuffer.position(0);
// bind buffer to texture
gl.glTexImage2D(GL10.GL_TEXTURE_2D, 0, GL10.GL_RGBA, width, height, 0,
GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, pixelBuffer);
在C中,这样我就可以使用缓冲区将纹理推到四边形了。
目前,我只是将其绑定到C语言中的pixelIntArray,纹理变形了。
基本上,我需要能够通过类似于Java NIO类的缓冲区将整数像素数组中的一系列颜色绑定到纹理。
我认为这可能是解决方法:
int length = width * height;
int* pixels = (int*) malloc(sizeof(int) * length);
unsigned char * buffer = (unsigned char*) malloc(sizeof(char) * length * 4);
int i, j;
for (i = 0; i < length; i++) {
j = 4 * i;
buffer[j] = (char) (pixels[i] & 0xFF);
buffer[j + 1] = (char) (pixels[i] >> 8 & 0xFF);
buffer[j + 2] = (char) (pixels[i] >> 16 & 0xFF);
buffer[j + 3] = (char) (pixels[i] >> 24 & 0xFF);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.