diff --git a/renderFramework.vcxproj b/renderFramework.vcxproj index 250f4df..38a6a15 100644 --- a/renderFramework.vcxproj +++ b/renderFramework.vcxproj @@ -77,6 +77,7 @@ true true WIN32;_DEBUG;_WINDOWS;%(PreprocessorDefinitions) + /D_CRT_SECURE_NO_WARNINGS %(AdditionalOptions) Windows @@ -127,10 +128,12 @@ + + diff --git a/renderFramework.vcxproj.filters b/renderFramework.vcxproj.filters index fab42c1..82c116f 100644 --- a/renderFramework.vcxproj.filters +++ b/renderFramework.vcxproj.filters @@ -21,6 +21,9 @@ 源文件 + + 源文件 + @@ -29,5 +32,8 @@ 源文件 + + 源文件 + \ No newline at end of file diff --git a/utils.cpp b/utils.cpp new file mode 100644 index 0000000..151166a --- /dev/null +++ b/utils.cpp @@ -0,0 +1,82 @@ +#include "utils.h" +unsigned char* DecodeBMP(unsigned char*bmpFileData, int&width, int&height) { + if (0x4D42 == *((unsigned short*)bmpFileData)) { + int pixelDataOffset = *((int*)(bmpFileData + 10)); + width = *((int*)(bmpFileData + 18)); + height = *((int*)(bmpFileData + 22)); + unsigned char*pixelData = bmpFileData + pixelDataOffset; + for (int i = 0; i < width*height * 3; i += 3) { + unsigned char temp = pixelData[i]; + pixelData[i] = pixelData[i + 2]; + pixelData[i + 2] = temp; + } + return pixelData; + } + return nullptr; +} +GLuint CreateTexture2D(unsigned char*pixelData, int width, int height, GLenum type) { + GLuint texture; + glGenTextures(1, &texture); + glBindTexture(GL_TEXTURE_2D, texture); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + glTexImage2D(GL_TEXTURE_2D, 0, type, width, height, 0, type, GL_UNSIGNED_BYTE, pixelData); + glBindTexture(GL_TEXTURE_2D, 0); + return texture; +} +GLuint CreateTexture2DFromBMP(const char*bmpPath) { + int nFileSize = 0; + unsigned char *bmpFileContent = LoadFileContent(bmpPath, nFileSize); + if (bmpFileContent == nullptr) { + return 0; + } + int bmpWidth = 0, bmpHeight = 0; + unsigned char*pixelData = DecodeBMP(bmpFileContent, bmpWidth, bmpHeight); + if (bmpWidth == 0) { + delete bmpFileContent; + return 0; + } + GLuint texture = CreateTexture2D(pixelData, bmpWidth, bmpHeight, GL_RGB); + delete bmpFileContent; + return texture; +} +GLuint CreateTextureCubeFromBMP(const char *front, const char *back, const char *left, + const char *right, const char *top, const char *bottom) { + GLuint texture; + glGenTextures(1, &texture); + glBindTexture(GL_TEXTURE_CUBE_MAP, texture); + int nFileSize = 0, width = 0, height = 0; + glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); + unsigned char *bmpFileContent = LoadFileContent(front, nFileSize); + unsigned char*pixelData = DecodeBMP(bmpFileContent, width, height); + glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, pixelData); + delete bmpFileContent; + bmpFileContent = LoadFileContent(back, nFileSize); + pixelData = DecodeBMP(bmpFileContent, width, height); + glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, pixelData); + delete bmpFileContent; + bmpFileContent = LoadFileContent(left, nFileSize); + pixelData = DecodeBMP(bmpFileContent, width, height); + glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, pixelData); + delete bmpFileContent; + bmpFileContent = LoadFileContent(right, nFileSize); + pixelData = DecodeBMP(bmpFileContent, width, height); + glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, pixelData); + delete bmpFileContent; + bmpFileContent = LoadFileContent(top, nFileSize); + pixelData = DecodeBMP(bmpFileContent, width, height); + glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, pixelData); + delete bmpFileContent; + bmpFileContent = LoadFileContent(bottom, nFileSize); + pixelData = DecodeBMP(bmpFileContent, width, height); + glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, pixelData); + glBindTexture(GL_TEXTURE_CUBE_MAP, 0); + delete bmpFileContent; + return texture; +} \ No newline at end of file diff --git a/utils.h b/utils.h index 44038fc..03bef46 100644 --- a/utils.h +++ b/utils.h @@ -1,2 +1,8 @@ #pragma once -unsigned char * LoadFileContent(const char*path, int&filesize); \ No newline at end of file +#include "ggl.h" +unsigned char * LoadFileContent(const char*path, int&filesize); +unsigned char * DecodeBMP(unsigned char*bmpFileData, int&width, int&height); +GLuint CreateTexture2D(unsigned char*pixelData, int width, int height, GLenum type); +GLuint CreateTexture2DFromBMP(const char *bmpPath); +GLuint CreateTextureCubeFromBMP(const char *front, const char *back, const char *left, + const char *right, const char *top, const char *bottom); \ No newline at end of file