[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     unsigned short usHeight;
 9     unsigned short usMipmaps;
10     unsigned short usSections;
11     int iFlags;
12     int iUserFlags;
13     unsigned char ubExtra[12];
14     char szCommandString[128];
15 };
16
17 #pragma pack()
 1 bool LoadDTX(const char *pszFileName, unsigned char *pBuffer, int iBufferSize, int *pInternalFormat, int *pWidth, int *pHeight, int *pImageSize)
 2 {
 3     FileHandle_t pFile = g_pFileSystem->Open(pszFileName, "rb");
 4
 5     if (!pFile)
 6         return false;
 7
 8     DtxHeader Header;
 9     memset(&Header, 0, sizeof(Header));
10
11     g_pFileSystem->Read(&Header, sizeof(Header), pFile);
12
13     if (Header.iResType != 0 || Header.iVersion != -5 || Header.usMipmaps == 0)
14     {
15         g_pFileSystem->Close(pFile);
16         return false;
17     }
18
19     *pWidth = Header.usWidth;
20     *pHeight = Header.usHeight;
21
22     int iBpp = Header.ubExtra[2];
23     int iSize;
24
25     if (iBpp == 0 || iBpp == 3)
26     {
27         iSize = Header.usWidth * Header.usHeight * 4;
28         *pInternalFormat = GL_RGBA;
29     }
30     else if (iBpp == 4)
31     {
32         iSize = (Header.usWidth * Header.usHeight) >> 1;
33         *pInternalFormat = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
34     }
35     else if (iBpp == 5)
36     {
37         iSize = Header.usWidth * Header.usHeight;
38         *pInternalFormat = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
39     }
40     else if (iBpp == 6)
41     {
42         iSize = Header.usWidth * Header.usHeight;
43         *pInternalFormat = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
44     }
45     else
46     {
47         iSize = 0;
48     }
49
50     *pImageSize = iSize;
51
52     if (iSize == 0 || iSize > iBufferSize)
53     {
54         g_pFileSystem->Close(pFile);
55         return false;
56     }
57
58     g_pFileSystem->Read(pBuffer, iSize, pFile);
59
60     if (iBpp == 3)
61     {
62         for (unsigned int i = 0; i < iSize; i += 4)
63         {
64             pBuffer[i + 0] ^= pBuffer[i + 2];
65             pBuffer[i + 2] ^= pBuffer[i + 0];
66             pBuffer[i + 0] ^= pBuffer[i + 2];
67         }
68     }
69
70     g_pFileSystem->Close(pFile);
71     return true;
72 }
时间: 2024-08-11 09:48:45

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

[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

[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

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

《OpenGL ES 2.0 Programming Guide》第12章“最简单的ReadPixels并保存为BMP”示例代码【C语言版】

由于<OpenGL ES 2.0 Programming Guide>原书并没有提供第12章的示例代码,书上的代码也只提到关键的步骤,而网上大多是Android/iOS版本的示例,C/C++的大都基于OpenGL或OpenGL ES 3.0,为了加深理解,遂自己实现了一份C语言版本的,希望能够帮助到同样喜欢OpenGL ES 2.0的同学. 废话不多说,直接上代码 #include "stdafx.h" #include "esUtil.h" #incl

Projective Texture的原理与实现 【转】

Projective Texture是比较常见的一种技术,实现起来代码也就区区的不过百行,了解其原理及技术细节是我们的重点,知其然,知其所以然. 粗略的说就是想象场景中有台投影仪(Projector),texture就是我们投影的内容,把纹理放在近裁剪面(near clip plane)上,沿着投影仪的方向把纹理投影到场景中.Xheartblue兄翻译了一篇文章,很好的给投影纹理的原理进行的阐述[1],有兴趣阅读原文 的可以访问这里[2],这本书可以是好东东啊!! 在这里有几个概念不能混淆 了: