zoukankan      html  css  js  c++  java
  • [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 }
  • 相关阅读:
    [HTML]安卓下<video>无法点击播放
    [JSP]解决Maven创建项目失败
    [JSP]Maven+SSM框架(Spring+SpringMVC+MyBatis)
    [转]Express入门教程:一个简单的博客
    [转][译]关于CSS中的float和position和z-index
    [CSS]多浏览器兼容的垂直居中,兼容多个IE
    [转]非常实用的15款开源PHP类库
    [PHP]使用PHPMailer发送带附件并支持HTML内容的邮件
    ROS中阶笔记(一):机器人系统设计—ROS系统下连接外部传感器
    ROS入门笔记(十三):分布式通信
  • 原文地址:https://www.cnblogs.com/crsky/p/4706262.html
Copyright © 2011-2022 走看看