Browse Source

解码dxtx1

master
blobt 4 years ago
parent
commit
7a922371c3
  1. BIN
      .vs/shader2/v14/.suo
  2. 3
      main.cpp
  3. 85
      misc.cpp
  4. 1
      misc.h
  5. BIN
      res/image/150001.dds
  6. 2
      res/shader/test.fs

BIN
.vs/shader2/v14/.suo

3
main.cpp

@ -118,6 +118,7 @@ INT WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
/*´´½¨ÎÆÀí*/
GLuint mainTexture = CreateTextureFromFile("res/image/stone.jpg");
GLuint secondTexture = CreateTextureFromDds("res/image/150001.dds");
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glEnable(GL_DEPTH_TEST);
@ -167,7 +168,7 @@ INT WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
glUniformMatrix4fv(PLocation, 1, GL_FALSE, glm::value_ptr(projection));
glUniformMatrix4fv(NMLocation, 1, GL_FALSE, glm::value_ptr(normalMatrix));
glBindTexture(GL_TEXTURE_2D, mainTexture);
glBindTexture(GL_TEXTURE_2D, secondTexture);
glUniform1i(textureLocation, 0);
glBindBuffer(GL_ARRAY_BUFFER, vbo);

85
misc.cpp

@ -113,28 +113,34 @@ GLuint CreateGPUProgram(const char* vsShaderPath, const char* fsShaderPath) {
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
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;
//bgr ת rgb
for (int i = 0; i < width * height * 3; i+=3) {
for (int i = 0; i < width * height * 3; i += 3) {
unsigned char tmp = pixelData[i + 2];
pixelData[i + 2] = pixelData[i + 0];
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;
glGenTextures(1, &texture);
@ -151,7 +157,9 @@ GLuint CreateTextureFromBMP(const char * imagePath)
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;
glGenTextures(1, &texId);
@ -196,3 +204,54 @@ GLuint CreateTextureFromFile(const char * imagePath)
FreeImage_Unload(dib);
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;
}

1
misc.h

@ -7,3 +7,4 @@ GLuint CompileShader(GLenum shaderType, const char* shaderPath);
GLuint CreateGPUProgram(const char* vsShaderPath, const char* fsShaderPath);
GLuint CreateTextureFromBMP(const char* imagePath);
GLuint CreateTextureFromFile(const char* imagePath);
GLuint CreateTextureFromDds(const char* imagePath);

BIN
res/image/150001.dds

2
res/shader/test.fs

@ -26,7 +26,7 @@ void main() {
vec4 SpecularMaterial = vec4(0.9,0.9,0.9,1.0);
vec3 reflectDir = normalize(reflect(-L,n));//-L线线
vec3 viewDir = normalize(vec3(0.0) - V_WorldPos.xyz);//vec3(0.0)线
vec4 specularColor = SpecularLightColor*SpecularMaterial*pow(max(0.0,dot(viewDir,reflectDir)),28.0);
vec4 specularColor = SpecularLightColor*SpecularMaterial*pow(max(0.0,dot(viewDir,reflectDir)),128.0);
//gl_FragColor = texture2D(U_MainTexture, V_Texcoord) * (AmbientColor + diffuseColor + specularColor);

Loading…
Cancel
Save