Browse Source

完善utils

master
blobt 4 years ago
parent
commit
3934ce96dd
  1. 3
      renderFramework.vcxproj
  2. 6
      renderFramework.vcxproj.filters
  3. 82
      utils.cpp
  4. 8
      utils.h

3
renderFramework.vcxproj

@ -77,6 +77,7 @@
<SDLCheck>true</SDLCheck> <SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode> <ConformanceMode>true</ConformanceMode>
<PreprocessorDefinitions>WIN32;_DEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions> <PreprocessorDefinitions>WIN32;_DEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalOptions>/D_CRT_SECURE_NO_WARNINGS %(AdditionalOptions)</AdditionalOptions>
</ClCompile> </ClCompile>
<Link> <Link>
<SubSystem>Windows</SubSystem> <SubSystem>Windows</SubSystem>
@ -127,10 +128,12 @@
<ItemGroup> <ItemGroup>
<ClCompile Include="main.cpp" /> <ClCompile Include="main.cpp" />
<ClCompile Include="scene.cpp" /> <ClCompile Include="scene.cpp" />
<ClCompile Include="utils.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="ggl.h" /> <ClInclude Include="ggl.h" />
<ClInclude Include="scene.h" /> <ClInclude Include="scene.h" />
<ClInclude Include="utils.h" />
</ItemGroup> </ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets"> <ImportGroup Label="ExtensionTargets">

6
renderFramework.vcxproj.filters

@ -21,6 +21,9 @@
<ClCompile Include="scene.cpp"> <ClCompile Include="scene.cpp">
<Filter>源文件</Filter> <Filter>源文件</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="utils.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="ggl.h"> <ClInclude Include="ggl.h">
@ -29,5 +32,8 @@
<ClInclude Include="scene.h"> <ClInclude Include="scene.h">
<Filter>源文件</Filter> <Filter>源文件</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="utils.h">
<Filter>源文件</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
</Project> </Project>

82
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;
}

8
utils.h

@ -1,2 +1,8 @@
#pragma once #pragma once
unsigned char * LoadFileContent(const char*path, int&filesize);
#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);
Loading…
Cancel
Save