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

[MetaHook] Load TGA texture to OpenGL

时间:2015-08-06 01:56:06      阅读:269      评论:0      收藏:0      [点我收藏+]

标签:

This function load a *.tga texture file and convert to OpenGL pixel format, uncompress only.

 1 #pragma pack(1)
 2 
 3 struct TgaHeader
 4 {
 5     unsigned char m_IDLength;
 6     unsigned char m_ColorMapType;
 7     unsigned char m_ImageType;
 8     unsigned short m_CMapStart;
 9     unsigned short m_CMapLength;
10     unsigned char m_CMapDepth;
11     unsigned short m_XOffset;
12     unsigned short m_YOffset;
13     unsigned short m_Width;
14     unsigned short m_Height;
15     unsigned char m_PixelDepth;
16     unsigned char m_ImageDescriptor;
17 };
18 
19 #pragma pack()

 

 1 bool LoadTGA(const char *pszFileName, unsigned char *pBuffer, int iBufferSize, int *pInternalFormat, int *pWidth, int *pHeight)
 2 {
 3     FileHandle_t pFile = g_pFileSystem->Open(pszFileName, "rb");
 4 
 5     if (!pFile)
 6         return false;
 7 
 8     TgaHeader Header;
 9     memset(&Header, 0, sizeof(Header));
10 
11     g_pFileSystem->Read(&Header, sizeof(Header), pFile);
12 
13     if (Header.m_ImageType != 2)
14     {
15         g_pFileSystem->Close(pFile);
16         return false;
17     }
18 
19     if (Header.m_PixelDepth != 24 && Header.m_PixelDepth != 32)
20     {
21         g_pFileSystem->Close(pFile);
22         return false;
23     }
24 
25     uint32 iPixelSize, iImageSize;
26 
27     switch (Header.m_PixelDepth)
28     {
29     case 24:
30         iPixelSize = 3;
31         *pInternalFormat = GL_RGB;
32         break;
33     case 32:
34         iPixelSize = 4;
35         *pInternalFormat = GL_RGBA;
36         break;
37     }
38 
39     iImageSize = Header.m_Width * Header.m_Height * iPixelSize;
40 
41     if (iImageSize > (uint32)iBufferSize)
42     {
43         g_pFileSystem->Close(pFile);
44         return false;
45     }
46 
47     uint32 iLineSize = Header.m_Width * iPixelSize;
48     uint32 iLinePos;
49 
50     for (uint32 y = 0; y < Header.m_Height; ++y)
51     {
52         iLinePos = (Header.m_Height - y - 1) * iLineSize;
53         g_pFileSystem->Read(&pBuffer[iLinePos], iLineSize, pFile);
54     }
55 
56     for (uint32 i = 0; i < iImageSize; i += iPixelSize)
57     {
58         pBuffer[i + 0] ^= pBuffer[i + 2];
59         pBuffer[i + 2] ^= pBuffer[i + 0];
60         pBuffer[i + 0] ^= pBuffer[i + 2];
61     }
62 
63     *pWidth = Header.m_Width;
64     *pHeight = Header.m_Height;
65 
66     g_pFileSystem->Close(pFile);
67     return true;
68 }

 

[MetaHook] Load TGA texture to OpenGL

标签:

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

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