I'm trying to port my Java OpenGL code on Android to the Native SDK and I need an IntBuffer implementation.
Basically what I do in Java to load up an arbitrary integer RGBA pixel color array into a texture is:
// 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);
in C so that I can push a texture to a quad using a buffer.
Currently I'm just binding it to my pixelIntArray in C and the texture comes out distorted.
Basically I need to be able to bind a series of colors in an integer pixel array to a texture through a buffer similar to Java's NIO class.
I think this may be how to solve it:
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);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.