[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 }
时间: 2024-10-19 21:25:38

[MetaHook] Load TGA texture to OpenGL的相关文章

[MetaHook] Load DTX texture to OpenGL

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 unsi

[MetaHook] Load large texture from model

We need hook "GL_LoadTexture" engine function. GL_LOADTEXTURE_SIG from hw.dll(3266) engine, can not use for other engine version. 1 #include <metahook.h> 2 #include "qgl.h" 3 #include "surface.h" 4 5 extern DWORD g_dwEn

OpenGL学习--07--模型加载(obj)

1.tutorial07.cpp // Include standard headers #include <stdio.h> #include <stdlib.h> #include <vector> // Include GLEW #include <GL/glew.h> // Include GLFW #include <glfw3.h> GLFWwindow* window; // Include GLM #include <glm

OpenGL学习--08--基本渲染(灯光)

1.tutorial08.cpp // Include standard headers #include <stdio.h> #include <stdlib.h> #include <vector> // Include GLEW #include <GL/glew.h> // Include GLFW #include <glfw3.h> GLFWwindow* window; // Include GLM #include <glm

OpenGL学习--05--纹理立方体--代码

1.tutorial05.cpp // Include standard headers #include <stdio.h> #include <stdlib.h> // Include GLEW #include <GL/glew.h> // Include GLFW #include <glfw3.h> GLFWwindow* window; // Include GLM #include <glm/glm.hpp> #include &l

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.

neHe OpenGL lession 6

加载纹理 // lession6 // Mac OS X / Linux // linux <GL/glut.h> <GL/gl.h> <GL/glu.h> #include <OpenGL/OpenGL.h> #include <GLUT/GLUT.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> /* ascii code for t

OpenGL ES 3.0之Texturting纹理详解(一)

本文流程 1.Texturing基础 2.装载Texturing和mipmapping 3.纹理过滤和包装 4.Texture level-of-detail, swizzles, and depth comparison 3D 图像渲染最基础的操作是使用纹理,它经典应用是坐标系应用在图像的表面, 2D Textures 一个2D纹理是在OpenGL ES 应用中最基础和普遍的.它的坐标系是作(s, t),有时也写作(u,v),纹理的左下角在st坐标系中定义为(0.0,0.0).右上角定义为(1

android openGL ES2 一切从绘制纹理开始

纹理,在openGL中,可以理解为加载到显卡显存中的图片.Android设备在2.2开始支持openGL ES2.0,从前都是ES1.0 和 ES1.1的版本.简单来说,openGL ES是为了嵌入设备进行功能剪裁后的openGL版本.ES2.0是和1.x版本不兼容的,区别和兼容性参见android 官方文档. 首先,android使用openGL提供了特殊的view作为基础叫做GLSurfaceView.我们的view需要继承GLSurfaceView.如下简单示例: public class