天天看点

Android OpenGL 纹理绘制图像---Native实现

本文纹理贴图的native实现,是指指定纹理的功能放在了native中实现,其他流程和Java实现类似,在这里就不赘述了。再回头看一下指定纹理数据的方法:

void glTexImage2D(GLenum target,GLint level,GLint internalFormat,
   GLsizei width,GLsizei height,Glint border,GLenum format,
   GLenum type,const void *texles);
           

通过以上我们可以知道,需要指定图像的像素数据和图像的一些参数才可以指定纹理,以PNG图像为例,实现指定纹理的步骤:

(1)解析PNG文件

其实网上有很多各种版本解析PNG文件的库,基本上不需要自己实现,本文采用为Android重新包装的libpng 1.6.23,具体代码可参考github地址:https://github.com/julienr/libpng-android。

(2)获取PNG像素数据和其他一些参数

bool ParsePngFile::parse() {

    std::istringstream pngDataStream(pngDataString);

    const int PNG_SIG_BYTES = ;
    char pngSignature[PNG_SIG_BYTES];
    pngDataStream.read(pngSignature, PNG_SIG_BYTES * sizeof(char));

    if(!png_check_sig( (png_bytep)pngSignature, PNG_SIG_BYTES) )
    {
        return false;
    }

    /**
     * Create and initialize the png_struct
     * with the desired error handler
     * functions.  If you want to use the
     * default stderr and longjump method,
     * you can supply NULL for the last
     * three parameters.  We also supply the
     * the compiler header file version, so
     * that we know if the application
     * was compiled with a compatible version
     * of the library.  REQUIRED
     */
    png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
    if (png_ptr == NULL)
    {
        return false;
    }

    /**
     * Allocate/initialize the memory
     * for image information.  REQUIRED. */
    png_infop info_ptr = png_create_info_struct(png_ptr);
    if (info_ptr == NULL)
    {
        png_destroy_read_struct(&png_ptr, NULL, NULL);
        return false;
    }

    /**
     * Set error handling if you are
     * using the setjmp/longjmp method
     * (this is the normal method of
     * doing things with libpng).
     * REQUIRED unless you  set up
     * your own error handlers in
     * the png_create_read_struct()
     * earlier.
     */
    if (setjmp(png_jmpbuf(png_ptr))) {
        /* Free all of the memory associated
         * with the png_ptr and info_ptr */
        png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
        return false;
    }


    /**
     * Set custom input stream READER / handler
     */
    png_set_read_fn( png_ptr, (void*)&pngDataStream, readFileCallback );

    /* If we have already
     * read some of the signature */
    png_set_sig_bytes( png_ptr,  );

    png_read_info( png_ptr, info_ptr);

    int  _depth;
    int color_type, interlace_type;

    png_get_IHDR( png_ptr, info_ptr, (png_uint_32*)&_width, (png_uint_32*)&_height, &_depth, &color_type, &interlace_type, NULL, NULL );

    switch(color_type)
    {
        case PNG_COLOR_TYPE_RGB:
            _hasAlpha = false;
            break;
        case PNG_COLOR_TYPE_RGBA:
            _hasAlpha = true;
            break;
        default:
            return false;
    }

    png_size_t cols = png_get_rowbytes(png_ptr, info_ptr);

    png_bytepp row_pp = new png_bytep[_height];
    pngData= new char[ cols * _height ];

    for( int i = ; i < _height; ++i )
    {
        // note that png is ordered top to
        // bottom, but OpenGL expect it bottom to top
        // so the order or swapped
        row_pp[i] = (png_bytep)&((char *)pngData)[ i * cols ];
    }

    png_read_image( png_ptr, row_pp );
    png_read_end( png_ptr, info_ptr );

    png_destroy_read_struct( &png_ptr, &info_ptr,  );

    delete [] row_pp;

    return true;

}
           

(3)实现指定纹理函数

void ParsePngFile::glPngTexImage2D(int target, int level) {

   if(pngData != NULL){
       glTexImage2D(target, level, _hasAlpha ? GL_RGBA : GL_RGB, _width, _height, , _hasAlpha ? GL_RGBA : GL_RGB, GL_UNSIGNED_BYTE, pngData);
   }

}
           

附上github地址:https://github.com/qiaoyanfei/OpenGL_Image.git