• [MetaHook] Load TGA texture to OpenGL


    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 }
  • 相关阅读:
    requirejs实现对动态combo的支持
    遇见Javascript类型数组(Typed Array)
    【转发】2012年HTML5的14个大胆预言
    跟我学canvas(三,应用图像)
    贡献一个连jquery都觉的大的时候可以用的 js库
    websocket终成标准
    Django中的form不足之处
    Eclipse+Pydev 开发Django中的Debug模式
    Javascript中指定周末日期的计算
    Python中递归的最大次数
  • 原文地址:https://www.cnblogs.com/crsky/p/4706262.html
Copyright © 2020-2023  润新知