Android OpenGL glTexImage2D没有绘图

时间:2015-01-26 15:05:43

标签: android opengl-es android-ndk

我正在尝试使用ndk在opengl纹理上显示图像。 我有一个图像结构:

struct image {
png_uint_32 imWidth, imHeight; //real size
png_uint_32 glWidth, glHeight; //size for OpenGL
int bit_depth, color_type;
char* data; //data RGB/RGBA
};

我读过图片:

image imJpeg = readJpeg("/data/data/com.lodoss.app/app_imageDir/0.jpg");

readJpeg实现:

static int reNpot(int w) {
    bool NON_POWER_OF_TWO_SUPPORTED = false;
    if (NON_POWER_OF_TWO_SUPPORTED) {
        if (w % 2) w++;
    } else {
        if (w <= 4) w = 4;
        else if (w <= 8) w = 8;
        else if (w <= 16) w = 16;
        else if (w <= 32) w = 32;
        else if (w <= 64) w = 64;
        else if (w <= 128) w = 128;
        else if (w <= 256) w = 256;
        else if (w <= 512) w = 512;
        else if (w <= 1024) w = 1024;
        else if (w <= 2048) w = 2048;
        else if (w <= 4096) w = 4096;
    }
    return w;
}

//read JPEG from file
static image readJpeg(const char* fileName) {
    image im;
    FILE* file = fopen(fileName, "rb");

    struct jpeg_decompress_struct cinfo;
    struct my_error_mgr jerr;

    cinfo.err = jpeg_std_error(&jerr.pub);
    jerr.pub.error_exit = my_error_exit;
    if (setjmp(jerr.setjmp_buffer)) {
        jpeg_destroy_decompress(&cinfo);
        return im;
    }

    jpeg_create_decompress(&cinfo);
    jpeg_stdio_src(&cinfo, file);

    //получаем данные о картинке
    jpeg_read_header(&cinfo, TRUE);
    jpeg_start_decompress(&cinfo);

    im.imWidth = cinfo.image_width;
    im.imHeight = cinfo.image_height;
    im.glWidth = reNpot(im.imWidth);
    im.glHeight = reNpot(im.imHeight);

    //JPEG не имеет прозрачности так что картинка всегда 3х-байтная (RGB)
    int row = im.glWidth * 3;
    im.data = new char[row * im.glHeight];

    //построчно читаем данные
    unsigned char* line = (unsigned char*) (im.data);
    while (cinfo.output_scanline < cinfo.output_height) {
        jpeg_read_scanlines(&cinfo, &line, 1);
        line += row;
    }

    //подчищаем
    jpeg_finish_decompress(&cinfo);
    jpeg_destroy_decompress(&cinfo);

    return im;
}

我实现了这个功能here(对不起,但仅限俄语)

然后显示它:

    GLuint texture2;
    glGenTextures(1, &texture2);
    glBindTexture(GL_TEXTURE_2D, texture2);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, engine->state.im.imWidth, engine->state.im.imHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, engine->state.im.data);

引擎指向我的图片

但没有任何反应。没有错误,没有显示图像。只有黑屏 为什么会这样?

0 个答案:

没有答案