OpenGL_ES加载TGA/BMP纹理

清疚 2022-08-05 02:13 350阅读 0赞
  1. typedef struct TGAImage {
  2. GLubyte *imageData; // 图像数据
  3. GLuint bpp; // 像素颜色深度
  4. GLuint width; // 图像宽度
  5. GLuint height; // 图像高度
  6. GLuint texID; // 纹理ID
  7. } TGAImage;
  8. bool LoadTGA(TGAImage *texture, char *filename)
  9. {
  10. GLubyte TGAheader[12]={0,0,2,0,0,0,0,0,0,0,0,0}; // Uncompressed TGA Header 不压缩的TGA头
  11. GLubyte TGAcompare[12]; // Used To Compare TGA Header 压缩的
  12. GLubyte header[6]; // First 6 Useful Bytes From The Header TGA头的前6个有用的字节
  13. GLuint bytesPerPixel; // Holds Number Of Bytes Per Pixel Used In The TGA File TGA文件每个像素使用的字节
  14. GLuint imageSize; // Used To Store The Image Size When Setting Aside Ram
  15. GLuint temp; // Temporary Variable 临时变量
  16. GLuint type=GL_RGBA; // Set The Default GL Mode To RBGA (32 BPP) 默认的GL模式
  17. FILE *file = fopen(filename, "rb"); // Open The TGA File
  18. printf("TGA_file=%d/n",file);
  19. if( file==NULL || // Does File Even Exist?
  20. fread(TGAcompare,1,sizeof(TGAcompare),file)!=sizeof(TGAcompare) || // Are There 12 Bytes To Read? 是否读到12字节数据
  21. memcmp(TGAheader,TGAcompare,sizeof(TGAheader))!=0 || // Does The Header Match What We Want? 是否是压缩的,压缩的就读取失败,关闭file
  22. fread(header,1,sizeof(header),file)!=sizeof(header)) // If So Read Next 6 Header Bytes 再读6个字节
  23. {
  24. if (file == NULL) // Did The File Even Exist? *Added Jim Strong*
  25. return false; // Return False
  26. else // Otherwise
  27. {
  28. fclose(file); // If Anything Failed, Close The File
  29. return false; // Return False
  30. }
  31. }
  32. //12到17字节前四字节是图像宽高,第五字节是图像位深度
  33. texture->width = header[1] * 256 + header[0]; // Determine The TGA Width (highbyte*256+lowbyte)//低字节,高字节
  34. texture->height = header[3] * 256 + header[2]; // Determine The TGA Height (highbyte*256+lowbyte)
  35. if( texture->width <=0 || // Is The Width Less Than Or Equal To Zero
  36. texture->height <=0 || // Is The Height Less Than Or Equal To Zero
  37. (header[4]!=24 && header[4]!=32)) // Is The TGA 24 or 32 Bit?
  38. {
  39. fclose(file); // If Anything Failed, Close The File
  40. return false; // Return False
  41. }
  42. texture->bpp = header[4]; // Grab The TGA's Bits Per Pixel (24 or 32)
  43. bytesPerPixel = texture->bpp/8; // Divide By 8 To Get The Bytes Per Pixel 每个像素使用的字节数
  44. imageSize = texture->width*texture->height*bytesPerPixel; // Calculate The Memory Required For The TGA Data
  45. texture->imageData=(GLubyte *)malloc(imageSize); // Reserve Memory To Hold The TGA Data 申请内存
  46. if( texture->imageData==NULL || // Does The Storage Memory Exist?
  47. fread(texture->imageData, 1, imageSize, file)!=imageSize) // Does The Image Size Match The Memory Reserved? 图像数据保存到纹理结构数据缓存imageData中
  48. {//失败情况下释放资源
  49. if(texture->imageData!=NULL) // Was Image Data Loaded
  50. free(texture->imageData); // If So, Release The Image Data
  51. fclose(file); // Close The File
  52. return false; // Return False
  53. }
  54. for(GLuint i=0; i<int(imageSize); i+=bytesPerPixel) // Loop Through The Image Data
  55. { // Swaps The 1st And 3rd Bytes ('R'ed and 'B'lue)
  56. temp=texture->imageData[i]; // Temporarily Store The Value At Image Data 'i'
  57. texture->imageData[i] = texture->imageData[i + 2]; // Set The 1st Byte To The Value Of The 3rd Byte
  58. texture->imageData[i + 2] = temp; // Set The 3rd Byte To The Value In 'temp' (1st Byte Value)
  59. }
  60. fclose (file); // Close The File
  61. glGenTextures(1, &texture->texID); // Generate生产,产生 OpenGL texture IDs
  62. printf("纹理ID=%d/n",texture->texID );
  63. glBindTexture(GL_TEXTURE_2D, texture->texID); // Bind 绑定Our Texture
  64. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); // Linear Filtered
  65. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // Linear Filtered
  66. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  67. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  68. if (texture[0].bpp==24) // Was The TGA 24 Bits
  69. {
  70. type=GL_RGB; // If So Set The 'type' To GL_RGB
  71. }
  72. //创建纹理
  73. glTexImage2D(GL_TEXTURE_2D, 0, type, texture->width, texture->height, 0, type, GL_UNSIGNED_BYTE, texture->imageData);
  74. return true;
  75. }
  76. bool LoadBMP(TGAImage *texture, char *filename)
  77. {
  78. BITMAPFILEHEADER bmpFileHeader; //bmp文件头
  79. BITMAPINFOHEADER bmpInfoHeader; //bmp格式头
  80. GLuint imageSize; //纹理数据大小
  81. GLuint temp; // 临时变量
  82. GLuint type=GL_RGB;
  83. FILE *file = fopen(filename, "rb");
  84. printf("BMP_file=%d/n",file);
  85. if( file==NULL) // Does File Even Exist?
  86. return false;
  87. fread(&bmpFileHeader,1,sizeof(BITMAPFILEHEADER),file); //读取文件头
  88. if(bmpFileHeader.bfType!=0x4d42) //判断是否是BM
  89. return false;
  90. else
  91. printf("是bmp图像/n");
  92. fread(&bmpInfoHeader,1,sizeof(BITMAPINFOHEADER),file); // 读取信息头
  93. texture->width = bmpInfoHeader.biWidth; //图片宽度
  94. texture->height = bmpInfoHeader.biHeight; //图片高度
  95. texture->bpp = bmpInfoHeader.biBitCount; //位深度
  96. imageSize = bmpInfoHeader.biSizeImage ; //图像数据的大小,用作申请纹理缓存
  97. if( texture->width <=0 ||
  98. texture->height <=0 ||
  99. texture->bpp!=24) // bmp不是24位图
  100. {
  101. fclose(file);
  102. return false;
  103. }
  104. //BITMAPFILEHEADER.bfOffBits从文件头开始到颜色数据的偏移量
  105. fseek(file,bmpFileHeader.bfOffBits,SEEK_SET); //移动指针到图像数据
  106. texture->imageData=(GLubyte *)malloc(imageSize);
  107. if( texture->imageData==NULL || // Does The Storage Memory Exist?
  108. fread(texture->imageData, 1, imageSize, file)!=imageSize) // Does The Image Size Match The Memory Reserved? 图像数据保存到纹理结构数据缓存imageData中
  109. {
  110. //失败情况下释放资源
  111. if(texture->imageData!=NULL) // Was Image Data Loaded
  112. free(texture->imageData); // If So, Release The Image Data
  113. fclose(file); // Close The File
  114. return false; // Return False
  115. }
  116. for(GLuint i=0; i<int(imageSize); i+=texture->bpp/8) // Loop Through The Image Data
  117. { // Swaps The 1st And 3rd Bytes ('R'ed and 'B'lue)
  118. temp=texture->imageData[i]; // Temporarily Store The Value At Image Data 'i'
  119. texture->imageData[i] = texture->imageData[i + 2]; // Set The 1st Byte To The Value Of The 3rd Byte
  120. texture->imageData[i + 2] = temp; // Set The 3rd Byte To The Value In 'temp' (1st Byte Value)
  121. }
  122. fclose (file); // Close The File
  123. glGenTextures(1, &texture->texID); // Generate生产,产生 OpenGL texture IDs
  124. printf("纹理ID=%d/n",texture->texID );
  125. glBindTexture(GL_TEXTURE_2D, texture->texID); // Bind 绑定Our Texture
  126. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); // Linear Filtered
  127. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // Linear Filtered
  128. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  129. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  130. //创建纹理
  131. glTexImage2D(GL_TEXTURE_2D, 0, type, texture->width, texture->height, 0, type, GL_UNSIGNED_BYTE, texture->imageData);
  132. return true;
  133. }
  134. 使用:
  135. /*LoadTGA(&MainImage,"//NAND//skyworth//tga//main.tga/0");
  136. LoadTGA(&MovieImage,"//NAND//skyworth//tga//movie.tga");*/
  137. LoadBMP(&MainImage,"//USER//tv.bmp/0");
  138. LoadBMP(&MovieImage,"//USER//music.bmp/0");

本文转自:http://blog.csdn.net/ezhong0812/article/details/6401723

发表评论

表情:
评论列表 (有 0 条评论,350人围观)

还没有评论,来说两句吧...

相关阅读

    相关 OpenGL纹理置换编程

    在OpenGL中,纹理贴图是一种常用的技术,用于将图像或图案应用到3D模型表面上。然而,有时仅仅使用静态纹理贴图可能会显得单调,无法达到想要的效果。在这种情况下,使用纹理置换技

    相关 openGL纹理

    纹理压缩技术已经广泛应用在各种3D游戏之中,它们包括:DXTC(Direct X Texture Compress,DirectX纹理压缩,以S3TC为基础)、S3TC(S3