简体   繁体   English

如何使用Android NDK将整数颜色的像素数组绑定到纹理?

[英]How can I bind a pixel array of integer colors to a texture using the Android NDK?

I'm trying to port my Java OpenGL code on Android to the Native SDK and I need an IntBuffer implementation. 我正在尝试将Android上的Java OpenGL代码移植到本机SDK,并且需要一个IntBuffer实现。

Basically what I do in Java to load up an arbitrary integer RGBA pixel color array into a texture is: 基本上,我在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);

in C so that I can push a texture to a quad using a buffer. 在C中,这样我就可以使用缓冲区将纹理推到四边形了。

Currently I'm just binding it to my pixelIntArray in C and the texture comes out distorted. 目前,我只是将其绑定到C语言中的pixelIntArray,纹理变形了。

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. 基本上,我需要能够通过类似于Java NIO类的缓冲区将整数像素数组中的一系列颜色绑定到纹理。

I think this may be how to solve it: 我认为这可能是解决方法:


Initialize 初始化


    int length = width * height;

    int* pixels = (int*) malloc(sizeof(int) * length);
    unsigned char * buffer = (unsigned char*) malloc(sizeof(char) * length * 4);

Copy to buffer 复制到缓冲区


    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);

    }

Source 资源


From: http://www.c-sharpcorner.com/Forums/Thread/32972/ 来自: http : //www.c-sharpcorner.com/Forums/Thread/32972/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM