Android OpenGL 纹理绘制图像---Native实现
来源:互联网 发布:tomcat在java中怎么用 编辑:程序博客网 时间:2024/05/08 08:13
本文纹理贴图的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 = 8; 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, 8 ); 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 = 0; 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, 0 ); 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, 0, _hasAlpha ? GL_RGBA : GL_RGB, GL_UNSIGNED_BYTE, pngData); }}
附上github地址:https://github.com/qiaoyanfei/OpenGL_Image.git
0 0
- Android OpenGL 纹理绘制图像---Native实现
- Android OpenGL 纹理绘制图像---JAVA实现
- Android OpenGL 纹理绘制图像---基础知识
- Android OpenGL 纹理绘制图像---总结
- android opengl es绘制三角形+纹理
- Android OpenGL ES 2.0绘图:绘制纹理
- Android OpenGL ES 2.0绘图:绘制纹理
- opengl 三角纹理绘制
- opengl绘制纹理立方体
- Android移动开发-使用OpenGL来绘制3D纹理图的实现
- openGL绘制正方体分别实现简单颜色绘制、纹理绘制、光照绘制
- android opengl es添加纹理,绘制立方体纹理,立方体使用不同纹理
- Android OpenGL ES绘制透明纹理立方体(未解决)
- android openGL ES2 一切从绘制纹理开始
- android openGL ES2 一切从绘制纹理开始
- android OpenGl Es实现正方体多纹理
- opengl 开发笔记-绘制纹理
- openGL绘制图像
- mybatis关联查询问题(一对多、多对一)
- Android限制文本字符数
- 获取当前月第一天、最后一天、获取当前年第一天、获取当前近一年
- php中如何把html中的内容生成到word文档
- 百度搜索引擎的工作原理
- Android OpenGL 纹理绘制图像---Native实现
- ubuntu下编译FFmpeg
- redis - cluster命令
- python文件操作
- Realm为Node.js发布对象数据库
- Google C++ Style Guide中英对照(三)
- mybatis Association标签 column属性应对多值的情况
- pyWavelet 小波工具箱的使用笔记
- nyoj 234 吃土豆