|
@ -113,19 +113,11 @@ GLuint CreateGPUProgram(const char* vsShaderPath, const char* fsShaderPath) { |
|
|
return program; |
|
|
return program; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
GLuint CreateTextureFromBMP(const char * imagePath) |
|
|
|
|
|
{ |
|
|
|
|
|
unsigned char* imgData = (unsigned char*)LoadFileContent(imagePath); |
|
|
|
|
|
|
|
|
|
|
|
if (*((unsigned short*)imgData) != 0x4D42) { |
|
|
|
|
|
printf("cannot decode %s\n", imagePath); |
|
|
|
|
|
return 0; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static unsigned char* DecodeBMPData(unsigned char* imgData, int &width, int &height) { |
|
|
//decode bmp
|
|
|
//decode bmp
|
|
|
int pixelDataOffset = *((int*)(imgData + 10)); |
|
|
int pixelDataOffset = *((int*)(imgData + 10)); |
|
|
int width = *((int*)(imgData + 18)); |
|
|
|
|
|
int height = *((int*)(imgData + 22)); |
|
|
|
|
|
|
|
|
width = *((int*)(imgData + 18)); |
|
|
|
|
|
height = *((int*)(imgData + 22)); |
|
|
|
|
|
|
|
|
unsigned char* pixelData = imgData + pixelDataOffset; |
|
|
unsigned char* pixelData = imgData + pixelDataOffset; |
|
|
|
|
|
|
|
@ -135,6 +127,20 @@ GLuint CreateTextureFromBMP(const char * imagePath) |
|
|
pixelData[i + 2] = pixelData[i + 0]; |
|
|
pixelData[i + 2] = pixelData[i + 0]; |
|
|
pixelData[i + 0] = tmp; |
|
|
pixelData[i + 0] = tmp; |
|
|
} |
|
|
} |
|
|
|
|
|
return pixelData; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
GLuint CreateTextureFromBMP(const char * imagePath) |
|
|
|
|
|
{ |
|
|
|
|
|
unsigned char* imgData = (unsigned char*)LoadFileContent(imagePath); |
|
|
|
|
|
|
|
|
|
|
|
if (*((unsigned short*)imgData) != 0x4D42) { |
|
|
|
|
|
printf("cannot decode %s\n", imagePath); |
|
|
|
|
|
return 0; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
int width, height; |
|
|
|
|
|
unsigned char* pixelData = DecodeBMPData(imgData, width, height); |
|
|
|
|
|
|
|
|
GLuint texture; |
|
|
GLuint texture; |
|
|
glGenTextures(1, &texture); |
|
|
glGenTextures(1, &texture); |
|
@ -151,7 +157,9 @@ GLuint CreateTextureFromBMP(const char * imagePath) |
|
|
return texture; |
|
|
return texture; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
GLuint CreateTexture(int w, int h, const void* data, GLenum type) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static GLuint CreateTexture(int w, int h, const void* data, GLenum type) |
|
|
{ |
|
|
{ |
|
|
GLuint texId; |
|
|
GLuint texId; |
|
|
glGenTextures(1, &texId); |
|
|
glGenTextures(1, &texId); |
|
@ -196,3 +204,54 @@ GLuint CreateTextureFromFile(const char * imagePath) |
|
|
FreeImage_Unload(dib); |
|
|
FreeImage_Unload(dib); |
|
|
return res; |
|
|
return res; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const unsigned long FORMAT_DXT1 = 0x31545844l; // ÕâÀïÆäʵDXT1µ¹ÐðµÄasciiÂë
|
|
|
|
|
|
static unsigned char* DecodeDXT1Data(unsigned char* imgData, int &width, int &height, int &pixelSize) { |
|
|
|
|
|
height = *((int*)(imgData + sizeof(unsigned long) * 3)); |
|
|
|
|
|
width = *((int*)(imgData + sizeof(unsigned long) * 4)); |
|
|
|
|
|
pixelSize = *((int*)(imgData + sizeof(unsigned long) * 5)); |
|
|
|
|
|
|
|
|
|
|
|
unsigned long compressFormat; |
|
|
|
|
|
compressFormat = *((int*)(imgData + sizeof(unsigned long) * 21)); |
|
|
|
|
|
|
|
|
|
|
|
switch (compressFormat) { |
|
|
|
|
|
case FORMAT_DXT1: |
|
|
|
|
|
printf("decode dxt1\n"); |
|
|
|
|
|
break; |
|
|
|
|
|
} |
|
|
|
|
|
unsigned char* pixelData = new unsigned char[pixelSize]; |
|
|
|
|
|
|
|
|
|
|
|
memcpy(pixelData, imgData + sizeof(unsigned long) * 32, pixelSize); |
|
|
|
|
|
|
|
|
|
|
|
return pixelData; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
GLuint CreateTextureFromDds(const char * imagePath) |
|
|
|
|
|
{ |
|
|
|
|
|
unsigned char* imgData = (unsigned char*)LoadFileContent(imagePath); |
|
|
|
|
|
|
|
|
|
|
|
if (memcmp(imgData, "DDS ", 4) != 0) { |
|
|
|
|
|
printf("cannot decode %s\n", imagePath); |
|
|
|
|
|
return 0; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
int width, height; |
|
|
|
|
|
int pixelSize = 0; |
|
|
|
|
|
unsigned char* pixelData = DecodeDXT1Data(imgData, width, height, pixelSize); |
|
|
|
|
|
|
|
|
|
|
|
GLuint texture; |
|
|
|
|
|
glGenTextures(1, &texture); |
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, texture); |
|
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
|
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
|
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
|
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
|
|
|
|
|
|
|
|
|
|
|
//GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
|
|
|
|
|
|
glCompressedTexImage2D(GL_TEXTURE_2D, 0, GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, width, height, 0, pixelSize, pixelData); |
|
|
|
|
|
|
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, 0); |
|
|
|
|
|
|
|
|
|
|
|
delete imgData; |
|
|
|
|
|
return texture; |
|
|
|
|
|
} |