LoadTGA

LoadTGA.h

 1 #include <windows.h>
 2
 3 #pragma pack(push, 1)
 4
 5 typedef struct Header_s
 6 {
 7     unsigned char        descLen;
 8     unsigned char        cmapType;
 9     unsigned char        imageType;
10     short                campStart;
11     unsigned short        cmapEntries;
12     unsigned char        cmapBits;
13     unsigned short        xOffset;
14     unsigned short        yOffset;
15
16 }Header_t;
17
18 typedef struct TGAHeader_s
19 {
20     Header_t            head;
21     unsigned short        width;
22     unsigned short        height;
23     unsigned char        bpp;
24     unsigned char        attrib;
25
26 }TGAHeader_t;
27
28 typedef struct TextureImage_s
29 {
30     unsigned char    *imageData;
31     unsigned int    width;
32     unsigned int    height;
33     unsigned int    bpp;
34     unsigned int    texId;
35     unsigned int    imageType;
36     bool            bCompressed;
37
38     TextureImage_s():imageData(0){}
39
40 }TextureImage_t;
41
42 #pragma pack(pop)
43
44 class TGAImage
45 {
46 public:
47     TGAImage(void);
48     ~TGAImage(void);
49
50 public:
51     bool    LoadTGA(TextureImage_t *tex, char *fileName);
52
53 protected:
54     bool    LoadUncompressedTGA(TextureImage_t *tex, HANDLE file);
55     bool    LoadCompressedTGA(TextureImage_t *tex, HANDLE file);
56 };

LoadTGA.cpp

  1 #include "LoadTGA.h"
  2
  3 TGAImage::TGAImage(void)
  4 {
  5 }
  6
  7 TGAImage::~TGAImage(void)
  8 {
  9 }
 10
 11 bool TGAImage::LoadTGA(TextureImage_t *tex, char *fileName)
 12 {
 13     Header_t uTGAcompare = { 0, 0,  2, 0, 0, 0, 0, 0 };
 14     Header_t cTGAcompare = { 0, 0, 10, 0, 0, 0, 0, 0 };
 15
 16     TGAHeader_t header;
 17
 18     HANDLE file = CreateFile(fileName, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
 19
 20     if(file == INVALID_HANDLE_VALUE)
 21     {
 22         return FALSE;
 23     }
 24
 25     DWORD dwRead;
 26
 27     ReadFile(file, &header, sizeof(TGAHeader_t), &dwRead, NULL);
 28
 29     if(dwRead != sizeof(TGAHeader_t))
 30     {
 31         CloseHandle(file);
 32         return FALSE;
 33     }
 34
 35     tex->width = header.width;
 36     tex->height = header.height;
 37     tex->bpp = header.bpp;
 38
 39     if(tex->bpp != 32)
 40     {
 41         CloseHandle(file);
 42         return FALSE;
 43     }
 44
 45     if(!memcmp(&uTGAcompare, &header.head, sizeof(header.head)))
 46     {
 47         tex->bCompressed = FALSE;
 48
 49         if(!LoadUncompressedTGA(tex, file))
 50         {
 51             CloseHandle(file);
 52             return FALSE;
 53         }
 54     }
 55     else if(!memcmp(&cTGAcompare, &header.head, sizeof(header.head)))
 56     {
 57         tex->bCompressed = TRUE;
 58
 59         if(!LoadCompressedTGA(tex, file))
 60         {
 61             CloseHandle(file);
 62             return FALSE;
 63         }
 64     }
 65
 66     return TRUE;
 67 }
 68
 69 bool TGAImage::LoadUncompressedTGA(TextureImage_t *tex, HANDLE file)
 70 {
 71     unsigned int bytePerPixel = tex->bpp / 8;
 72     unsigned int imgSize = tex->width * tex->height * bytePerPixel;
 73
 74     tex->imageData = new unsigned char [ imgSize ];
 75
 76     DWORD dwRead;
 77
 78     ReadFile(file, tex->imageData, imgSize, &dwRead, NULL);
 79
 80     if(dwRead != imgSize)
 81     {
 82         return FALSE;
 83     }
 84
 85     for(int i = 0; i < (int)imgSize; i += bytePerPixel)
 86     {
 87         tex->imageData[i] ^= tex->imageData[i + 2] ^= tex->imageData[i] ^= tex->imageData[i + 2];
 88     }
 89
 90     CloseHandle(file);
 91     return TRUE;
 92 }
 93
 94 bool TGAImage::LoadCompressedTGA(TextureImage_t *tex, HANDLE file)
 95 {
 96     unsigned int bytePerPixel = tex->bpp / 8;
 97     unsigned int imgSize = tex->width * tex->height * bytePerPixel;
 98
 99     tex->imageData = new unsigned char [ imgSize ];
100
101     unsigned int pixelcount = tex->width * tex->height;
102     unsigned int currentPixel = 0;
103     unsigned int currentByte = 0;
104     unsigned char *colorbuffer = (unsigned char *)malloc(bytePerPixel);
105
106     do
107     {
108         unsigned char chunkHeader = 0;
109         DWORD dwRead = 0;
110
111         ReadFile(file, &chunkHeader, sizeof(unsigned char), &dwRead, NULL);
112
113         if(chunkHeader < 128)
114         {
115             chunkHeader++;
116
117             for(int i = 0; i < chunkHeader; ++i)
118             {
119                 ReadFile(file, colorbuffer, sizeof(bytePerPixel), &dwRead, NULL);
120
121                 tex->imageData[currentByte + 0] = colorbuffer[2];
122                 tex->imageData[currentByte + 1] = colorbuffer[1];
123                 tex->imageData[currentByte + 2] = colorbuffer[0];
124
125                 if(bytePerPixel == 4)
126                 {
127                     tex->imageData[currentByte + 3] = colorbuffer[3];
128                 }
129
130                 currentPixel++;
131                 currentByte += bytePerPixel;
132             }
133         }
134         else
135         {
136             chunkHeader -= 127;
137
138             ReadFile(file, colorbuffer, sizeof(bytePerPixel), &dwRead, NULL);
139
140             for(int i = 0; i < chunkHeader; ++i)
141             {
142                 tex->imageData[currentByte + 0] = colorbuffer[2];
143                 tex->imageData[currentByte + 1] = colorbuffer[1];
144                 tex->imageData[currentByte + 2] = colorbuffer[0];
145
146                 if(bytePerPixel == 4)
147                 {
148                     tex->imageData[currentByte + 3] = colorbuffer[3];
149                 }
150
151                 currentPixel++;
152                 currentByte += bytePerPixel;
153             }
154         }
155
156     }
157     while(currentPixel < pixelcount);
158
159     free(colorbuffer);
160     CloseHandle(file);
161     return TRUE;
162 }

exportfuncs.cpp

 1 #include <metahook.h>
 2 #include "Surface.h"
 3 #include "DrawTGA.h"
 4 #include <triangleapi.h>
 5 #include "LoadTGA.h"
 6
 7 cl_enginefunc_t gEngfuncs;
 8 TGAImage TGALoad;
 9
10
11 int Initialize(struct cl_enginefuncs_s *pEnginefuncs, int iVersion)
12 {
13     memcpy(&gEngfuncs, pEnginefuncs, sizeof(gEngfuncs));
14     return gExportfuncs.Initialize(pEnginefuncs, iVersion);
15 }
16
17 int HUD_Init(void)
18 {
19     return gExportfuncs.HUD_Init();
20 }
21
22 int HUD_VidInit(void)
23 {
24     if(g_pSurface == NULL)
25     {
26         CreateInterfaceFn fnEngineCreateInterface = g_pMetaHookAPI->GetEngineFactory();
27         vgui::ISurface *pSurface = (vgui::ISurface *)fnEngineCreateInterface(VGUI_SURFACE_INTERFACE_VERSION, NULL);
28         Surface_InstallHook(pSurface);
29     }
30
31     /*g_TextureId = g_pSurface->CreateNewTextureID();
32     g_pSurface->DrawSetTextureFile(g_TextureId, "TEST", true, true);*/
33
34     TextureImage_t TGA;
35     TGALoad.LoadTGA(&TGA, "cstrike/TEST1.tga");
36
37     g_TextureId = g_pSurface->CreateNewTextureID();
38     g_pSurface->DrawSetTextureRGBA(g_TextureId, TGA.imageData, TGA.width, TGA.height, true, true);
39
40     return gExportfuncs.HUD_VidInit();
41 }
42
43 int HUD_Redraw(float time, int intermission)
44 {
45     if(g_TextureId != NULL)
46     {
47         gEngfuncs.pTriAPI->RenderMode(kRenderTransTexture);
48         g_pSurface->DrawSetColor(255, 255, 255, 255);
49         g_pSurface->DrawSetTexture(g_TextureId);
50         g_pSurface->DrawTexturedRect(115, 115, 512, 512);
51     }
52
53     return gExportfuncs.HUD_Redraw(time, intermission);
54 }
时间: 2024-11-03 14:54:33

LoadTGA的相关文章

NeHe OpenGL教程 第三十三课:TGA文件

转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线教程的编写,以及yarn的翻译整理表示感谢. NeHe OpenGL第三十三课:TGA文件 加载压缩和未压缩的TGA文件: 在这一课里,你将学会如何加载压缩和为压缩的TGA文件,由于它使用RLE压缩,所以非常的简单,你能很快地熟悉它的. 我见过很多人在游戏开发论坛或其它地方询问关于TGA读取的问题.

outdated: 32.Picking, Alpha Blending, Alpha Testing, Sorting

一个射击类的小demo.分为三个文件,Previous.h.Previous.cpp和Main.cpp. 在Previous.cpp的CreateWindowGL中新增了AdjustWindowRectEx()函数,可以根据客户端的期望大小计算窗口大小,矩形通过函数创建一个理想大小的窗口. ChoosePixelFormat()函数试图匹配一个最接近的像素格式通过上下文给的像素格式规格. 在Mian.cpp文件的Selection()函数中,glGetIntegerv()函数返回一个选定后的参数

[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_CMapSt

outdated: 33.Loading Compressed And Uncompressed TGA&#39;s

这一篇主要讲的是TGA文件的加载,TGA文件分为压缩与未压缩的两种. uncompressed和compressed的标头是不一样, // Uncompressed TGA header GLubyte uTGAcompare[12] = { 0,0,2,0,0,0,0,0,0,0,0,0 }; // Compressed TGA header GLubyte cTGAcompare[12] = { 0,0,10,0,0,0,0,0,0,0,0,0 }; 未压缩的TGA文件,在最后直接读取ima

outdated: 24. Tokens, Extensions, Scissor Testing And TGA Loading

这一篇主要说的是,怎样实现在窗口中裁剪一个小的视图窗口(即glScissor()函数)?以及对glGetString()函数的应用. 在本文中,用到的贴图材质是TGA格式图片,相对于JPG格式,会更加清晰,因使用不失真的压缩.不了解TGA格式图片的可以看看这篇.基于对TGA格式的了解,具有独有的一个8位的Alpha通道.在其代码中,在24位和32位之间来转换,差的8位即为Alpha.所以,一开始会有一个变量type,默认为32位---GL_RGBA.支持扩展是很好的一个特性,使得DrawGLSc

openGL+VS2010的例程--特效(三维)

效果图如上: 步骤:略. 实现代码如下: main.cpp 1 /********************************************************************** 2 3 Particle Engine / Billboarding 4 5 October, 21st, 2002 6 7 This tutorial was written by Philipp Crocoll 8 Contact: 9 [email protected] 10 www.