Skip to content

Instantly share code, notes, and snippets.

@XProger
Last active January 20, 2019 22:34
Show Gist options
  • Save XProger/c8f8e49790370f2df8939d3d8f692e57 to your computer and use it in GitHub Desktop.
Save XProger/c8f8e49790370f2df8939d3d8f692e57 to your computer and use it in GitHub Desktop.
PVR file loader for OGL
#define PVR_RGBA8 0x61626772
#define PVR_PVRTC4 0x00000002
#define PVR_ETC1 0x00000006
#define PVR_BC1 0x00000007
#define PVR_ALPHA 0x00000061
#define GL_COMPRESSED_RGB_S3TC_DXT1 0x83F0
#define GL_ETC1_RGB8_OES 0x8D64
#define GL_COMPRESSED_RGB_PVRTC_4BPPV1 0x8C00
Texture::Texture(Stream *stream, int param) : ResObject(stream, param), width(0), height(0), depth(false) {
struct {
int version;
int flags;
long format;
long ext;
int color;
int channel;
int height;
int width;
int depth;
int surfaces;
int faces;
int mipCount;
int metaSize;
} header;
stream->read(&header, sizeof(header));
stream->seek(header.metaSize);
GLenum fmt, minSize;
switch (header.format) {
case PVR_ALPHA : minSize = 1; fmt = GL_ALPHA; break;
case PVR_RGBA8 : minSize = 1; fmt = GL_RGBA; break;
case PVR_BC1 : minSize = 4; fmt = GL_COMPRESSED_RGB_S3TC_DXT1; break;
case PVR_ETC1 : minSize = 4; fmt = GL_ETC1_RGB8_OES; break;
case PVR_PVRTC4 : minSize = 8; fmt = GL_COMPRESSED_RGB_PVRTC_4BPPV1; break;
default : ASSERT(false);
}
int sizeX = width = header.width;
int sizeY = height = header.height;
glGenTextures(1, &ID);
setFilter(tfAniso);
char *data = new char[width * height * 4];
for (int i = 0; i < header.mipCount; i++) {
if (header.format == PVR_RGBA8 || header.format == PVR_ALPHA) {
stream->read(data, sizeX * sizeY * (header.format == PVR_RGBA8 ? 4 : 1));
glTexImage2D(GL_TEXTURE_2D, i, fmt, sizeX, sizeY, 0, fmt, GL_UNSIGNED_BYTE, data);
} else {
int size = (_max(sizeX, minSize) * _max(sizeY, minSize) * 4 + 7) / 8;
stream->read(data, size);
glCompressedTexImage2D(GL_TEXTURE_2D, i, fmt, sizeX, sizeY, 0, size, data);
}
sizeX >>= 1;
sizeY >>= 1;
}
delete[] data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment