diff --git a/example/DirectGLTexture/CMakeLists.txt b/example/DirectGLTexture/CMakeLists.txt index 09cad961..0a6fae4a 100644 --- a/example/DirectGLTexture/CMakeLists.txt +++ b/example/DirectGLTexture/CMakeLists.txt @@ -1,4 +1,4 @@ -add_executable(DirectGLTexture main.cpp) +add_executable(DirectGLTexture main.cpp TGATexture.cpp) target_link_libraries(DirectGLTexture PRIVATE ${ULRE}) diff --git a/example/DirectGLTexture/TGATexture.cpp b/example/DirectGLTexture/TGATexture.cpp new file mode 100644 index 00000000..67df9310 --- /dev/null +++ b/example/DirectGLTexture/TGATexture.cpp @@ -0,0 +1,114 @@ +#include +#include +#include +#include + +namespace hgl +{ + namespace graph + { + namespace + { + #pragma pack(push,1) + struct TGAHeader + { + uint8 id; + uint8 color_map_type; + uint8 image_type; // 1 colormap image ,2 true-color,3 grayscale + + uint16 color_map_first; + uint16 color_map_length; + uint8 color_map_size; + + uint16 x_origin; + uint16 y_origin; + + uint16 width; + uint16 height; + uint8 bit; + + uint8 image_desc; + }; + + union TGAImageDesc + { + //不要把此union放到上面的struct中,否则Visual C++会将此union编译成4字节。GCC无此问题 + uint8 image_desc; + struct + { + uint alpha_depth:4; + uint reserved:1; + uint direction:1; //0 lower-left,1 upper left + }; + }; + #pragma pack(pop) + }//namespace + + bool LoadTGATexture(const std::string &filename,uint &tex_id) + { + std::ifstream file; + + file.open(filename,std::ifstream::in|std::ifstream::binary); + + if(!file.is_open()) + { + std::cerr<<"[ERROR] open file<"< failed."<image_type==2) + { + if(header->bit==24) + { + videomemory_format=GL_RGB8; + source_format=GL_BGR; + } + else if(header->bit==32) + { + videomemory_format=GL_RGBA8; + source_format=GL_BGRA; + } + } + else if(header->image_type==3&&header->bit==8) + { + videomemory_format=GL_R8; + source_format=GL_RED; + } + else + { + std::cerr<<"[ERROR] Image format error,filename: "<width,header->height); + glTextureSubImage2D(tex_id,0,0,0,header->width,header->height,source_format,GL_UNSIGNED_BYTE,data+sizeof(TGAHeader)); + + glTextureParameteri(tex_id,GL_TEXTURE_MIN_FILTER,GL_LINEAR); + glTextureParameteri(tex_id,GL_TEXTURE_MAG_FILTER,GL_LINEAR); + glTextureParameteri(tex_id,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE); + glTextureParameteri(tex_id,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE); + + std::cout<<"load image file<"<:<"<width<<"x"<height<<"> to texture ok"<Show(); while(win->IsOpen())