码迷,mamicode.com
首页 > Web开发 > 详细

[MetaHook] Load DTX texture to OpenGL

时间:2015-08-04 20:45:16      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:

This function load a LithTech *.dtx texture file and convert to OpenGL pixel format, compressed support.

Use FileSystem interface. :D

 1 #pragma pack(1)
 2 
 3 struct DtxHeader
 4 {
 5     unsigned int iResType;
 6     int iVersion;
 7     unsigned short usWidth;
 8     unsigned short usHeight;
 9     unsigned short usMipmaps;
10     unsigned short usSections;
11     int iFlags;
12     int iUserFlags;
13     unsigned char ubExtra[12];
14     char szCommandString[128];
15 };
16 
17 #pragma pack()

 

 1 bool LoadDTX(const char *pszFileName, unsigned char *pBuffer, int iBufferSize, int *pInternalFormat, int *pWidth, int *pHeight, int *pImageSize)
 2 {
 3     FileHandle_t pFile = g_pFileSystem->Open(pszFileName, "rb");
 4 
 5     if (!pFile)
 6         return false;
 7 
 8     DtxHeader Header;
 9     memset(&Header, 0, sizeof(Header));
10 
11     g_pFileSystem->Read(&Header, sizeof(Header), pFile);
12 
13     if (Header.iResType != 0 || Header.iVersion != -5 || Header.usMipmaps == 0)
14     {
15         g_pFileSystem->Close(pFile);
16         return false;
17     }
18 
19     *pWidth = Header.usWidth;
20     *pHeight = Header.usHeight;
21 
22     int iBpp = Header.ubExtra[2];
23     int iSize;
24     
25     if (iBpp == 0 || iBpp == 3)
26     {
27         iSize = Header.usWidth * Header.usHeight * 4;
28         *pInternalFormat = GL_RGBA;
29     }
30     else if (iBpp == 4)
31     {
32         iSize = (Header.usWidth * Header.usHeight) >> 1;
33         *pInternalFormat = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
34     }
35     else if (iBpp == 5)
36     {
37         iSize = Header.usWidth * Header.usHeight;
38         *pInternalFormat = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
39     }
40     else if (iBpp == 6)
41     {
42         iSize = Header.usWidth * Header.usHeight;
43         *pInternalFormat = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
44     }
45     else
46     {
47         iSize = 0;
48     }
49 
50     *pImageSize = iSize;
51 
52     if (iSize == 0 || iSize > iBufferSize)
53     {
54         g_pFileSystem->Close(pFile);
55         return false;
56     }
57 
58     g_pFileSystem->Read(pBuffer, iSize, pFile);
59 
60     if (iBpp == 3)
61     {
62         for (unsigned int i = 0; i < iSize; i += 4)
63         {
64             pBuffer[i + 0] ^= pBuffer[i + 2];
65             pBuffer[i + 2] ^= pBuffer[i + 0];
66             pBuffer[i + 0] ^= pBuffer[i + 2];
67         }
68     }
69 
70     g_pFileSystem->Close(pFile);
71     return true;
72 }

 

[MetaHook] Load DTX texture to OpenGL

标签:

原文地址:http://www.cnblogs.com/crsky/p/4702916.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!