<i id='aZjpF'><tr id='aZjpF'><dt id='aZjpF'><q id='aZjpF'><span id='aZjpF'><b id='aZjpF'><form id='aZjpF'><ins id='aZjpF'></ins><ul id='aZjpF'></ul><sub id='aZjpF'></sub></form><legend id='aZjpF'></legend><bdo id='aZjpF'><pre id='aZjpF'><center id='aZjpF'></center></pre></bdo></b><th id='aZjpF'></th></span></q></dt></tr></i><div id='aZjpF'><tfoot id='aZjpF'></tfoot><dl id='aZjpF'><fieldset id='aZjpF'></fieldset></dl></div>

    <small id='aZjpF'></small><noframes id='aZjpF'>

    <legend id='aZjpF'><style id='aZjpF'><dir id='aZjpF'><q id='aZjpF'></q></dir></style></legend>
      • <bdo id='aZjpF'></bdo><ul id='aZjpF'></ul>
      <tfoot id='aZjpF'></tfoot>

    1. 在 C++/OpenGL 中加载 tga/bmp 文件

      时间:2023-09-18
      <tfoot id='tgE0d'></tfoot>

          <tbody id='tgE0d'></tbody>
        <legend id='tgE0d'><style id='tgE0d'><dir id='tgE0d'><q id='tgE0d'></q></dir></style></legend>
      • <i id='tgE0d'><tr id='tgE0d'><dt id='tgE0d'><q id='tgE0d'><span id='tgE0d'><b id='tgE0d'><form id='tgE0d'><ins id='tgE0d'></ins><ul id='tgE0d'></ul><sub id='tgE0d'></sub></form><legend id='tgE0d'></legend><bdo id='tgE0d'><pre id='tgE0d'><center id='tgE0d'></center></pre></bdo></b><th id='tgE0d'></th></span></q></dt></tr></i><div id='tgE0d'><tfoot id='tgE0d'></tfoot><dl id='tgE0d'><fieldset id='tgE0d'></fieldset></dl></div>

            <small id='tgE0d'></small><noframes id='tgE0d'>

              <bdo id='tgE0d'></bdo><ul id='tgE0d'></ul>
                本文介绍了在 C++/OpenGL 中加载 tga/bmp 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我正在尝试加载 tga/bmp 文件.这工作正常,但最终结果如下所示:

                I'm trying to load a tga/bmp file. This works fine, but the end result looks like this:

                我尝试加载的图像如下所示:

                The image I'm trying to load looks like this:

                我正在使用的一些代码:

                Some of the code I'm using:

                GLuint texture;
                const char* filename = "/Users/Admin/Documents/Visual Studio 2013/Projects/OpenGL/OpenGL/image.tga";
                
                unsigned char* data;
                data = (unsigned char *) malloc(128 * 128 * 3);
                FILE* f;
                fopen_s(&f, filename, "rb");
                fread(data, 128 * 128 * 3, 1, f);
                
                glGenTextures(1, &texture);
                glBindTexture(GL_TEXTURE_2D, texture);
                
                glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 128, 128, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
                

                有人知道我做错了什么吗?

                Does anybody have an idea what I'm doing wrong?

                推荐答案

                您可以使用这些加载位图和 tga 文件..

                You can load a bitmap and a tga file using these..

                #include <vector>
                #include <fstream>
                
                #ifdef __APPLE__
                #include <OpenGL/gl.h>
                #include <OpenGL/glu.h>
                #endif
                
                
                #ifdef _WIN32
                #include <GL/gl.h>
                #include <GL/glu.h>
                #endif
                
                
                typedef union PixelInfo
                {
                    std::uint32_t Colour;
                    struct
                    {
                        std::uint8_t B, G, R, A;
                    };
                } *PPixelInfo;
                
                
                class BMP
                {
                private:
                    std::uint32_t width, height;
                    std::uint16_t BitsPerPixel;
                    std::vector<std::uint8_t> Pixels;
                
                public:
                    BMP(const char* FilePath);
                    std::vector<std::uint8_t> GetPixels() const {return this->Pixels;}
                    std::uint32_t GetWidth() const {return this->width;}
                    std::uint32_t GetHeight() const {return this->height;}
                    bool HasAlphaChannel() {return BitsPerPixel == 32;}
                };
                
                BMP::BMP(const char* FilePath)
                {
                    std::fstream hFile(FilePath, std::ios::in | std::ios::binary);
                    if (!hFile.is_open()) throw std::invalid_argument("Error: File Not Found.");
                
                    hFile.seekg(0, std::ios::end);
                    std::size_t Length = hFile.tellg();
                    hFile.seekg(0, std::ios::beg);
                    std::vector<std::uint8_t> FileInfo(Length);
                    hFile.read(reinterpret_cast<char*>(FileInfo.data()), 54);
                
                    if(FileInfo[0] != 'B' && FileInfo[1] != 'M')
                    {
                        hFile.close();
                        throw std::invalid_argument("Error: Invalid File Format. Bitmap Required.");
                    }
                
                    if (FileInfo[28] != 24 && FileInfo[28] != 32)
                    {
                        hFile.close();
                        throw std::invalid_argument("Error: Invalid File Format. 24 or 32 bit Image Required.");
                    }
                
                    BitsPerPixel = FileInfo[28];
                    width = FileInfo[18] + (FileInfo[19] << 8);
                    height = FileInfo[22] + (FileInfo[23] << 8);
                    std::uint32_t PixelsOffset = FileInfo[10] + (FileInfo[11] << 8);
                    std::uint32_t size = ((width * BitsPerPixel + 31) / 32) * 4 * height;
                    Pixels.resize(size);
                
                    hFile.seekg (PixelsOffset, std::ios::beg);
                    hFile.read(reinterpret_cast<char*>(Pixels.data()), size);
                    hFile.close();
                }
                
                int main()
                {
                    BMP info = BMP("C:/Users/....../Desktop/SomeBmp.bmp");
                
                    GLuint texture = 0;
                    glGenTextures(1, &texture);
                    glBindTexture(GL_TEXTURE_2D, texture);
                    glTexImage2D(GL_TEXTURE_2D, 0, info.HasAlphaChannel() ? GL_RGBA : GL_RGB, info.GetWidth(), info.GetWidth(), 0, info.HasAlphaChannel() ? GL_BGRA : GL_BGR, GL_UNSIGNED_BYTE, info.GetPixels().data());
                }
                

                TGA:

                #include <vector>
                #include <fstream>
                
                #ifdef __APPLE__
                #include <OpenGL/gl.h>
                #include <OpenGL/glu.h>
                #endif
                
                
                #ifdef _WIN32
                #include <GL/gl.h>
                #include <GL/glu.h>
                #endif
                
                typedef union PixelInfo
                {
                    std::uint32_t Colour;
                    struct
                    {
                        std::uint8_t R, G, B, A;
                    };
                } *PPixelInfo;
                
                class Tga
                {
                private:
                    std::vector<std::uint8_t> Pixels;
                    bool ImageCompressed;
                    std::uint32_t width, height, size, BitsPerPixel;
                
                public:
                    Tga(const char* FilePath);
                    std::vector<std::uint8_t> GetPixels() {return this->Pixels;}
                    std::uint32_t GetWidth() const {return this->width;}
                    std::uint32_t GetHeight() const {return this->height;}
                    bool HasAlphaChannel() {return BitsPerPixel == 32;}
                };
                
                Tga::Tga(const char* FilePath)
                {
                    std::fstream hFile(FilePath, std::ios::in | std::ios::binary);
                    if (!hFile.is_open()){throw std::invalid_argument("File Not Found.");}
                
                    std::uint8_t Header[18] = {0};
                    std::vector<std::uint8_t> ImageData;
                    static std::uint8_t DeCompressed[12] = {0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
                    static std::uint8_t IsCompressed[12] = {0x0, 0x0, 0xA, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
                
                    hFile.read(reinterpret_cast<char*>(&Header), sizeof(Header));
                
                    if (!std::memcmp(DeCompressed, &Header, sizeof(DeCompressed)))
                    {
                        BitsPerPixel = Header[16];
                        width  = Header[13] * 256 + Header[12];
                        height = Header[15] * 256 + Header[14];
                        size  = ((width * BitsPerPixel + 31) / 32) * 4 * height;
                
                        if ((BitsPerPixel != 24) && (BitsPerPixel != 32))
                        {
                            hFile.close();
                            throw std::invalid_argument("Invalid File Format. Required: 24 or 32 Bit Image.");
                        }
                
                        ImageData.resize(size);
                        ImageCompressed = false;
                        hFile.read(reinterpret_cast<char*>(ImageData.data()), size);
                    }
                    else if (!std::memcmp(IsCompressed, &Header, sizeof(IsCompressed)))
                    {
                        BitsPerPixel = Header[16];
                        width  = Header[13] * 256 + Header[12];
                        height = Header[15] * 256 + Header[14];
                        size  = ((width * BitsPerPixel + 31) / 32) * 4 * height;
                
                        if ((BitsPerPixel != 24) && (BitsPerPixel != 32))
                        {
                            hFile.close();
                            throw std::invalid_argument("Invalid File Format. Required: 24 or 32 Bit Image.");
                        }
                
                        PixelInfo Pixel = {0};
                        int CurrentByte = 0;
                        std::size_t CurrentPixel = 0;
                        ImageCompressed = true;
                        std::uint8_t ChunkHeader = {0};
                        int BytesPerPixel = (BitsPerPixel / 8);
                        ImageData.resize(width * height * sizeof(PixelInfo));
                
                        do
                        {
                            hFile.read(reinterpret_cast<char*>(&ChunkHeader), sizeof(ChunkHeader));
                
                            if(ChunkHeader < 128)
                            {
                                ++ChunkHeader;
                                for(int I = 0; I < ChunkHeader; ++I, ++CurrentPixel)
                                {
                                    hFile.read(reinterpret_cast<char*>(&Pixel), BytesPerPixel);
                
                                    ImageData[CurrentByte++] = Pixel.B;
                                    ImageData[CurrentByte++] = Pixel.G;
                                    ImageData[CurrentByte++] = Pixel.R;
                                    if (BitsPerPixel > 24) ImageData[CurrentByte++] = Pixel.A;
                                }
                            }
                            else
                            {
                                ChunkHeader -= 127;
                                hFile.read(reinterpret_cast<char*>(&Pixel), BytesPerPixel);
                
                                for(int I = 0; I < ChunkHeader; ++I, ++CurrentPixel)
                                {
                                    ImageData[CurrentByte++] = Pixel.B;
                                    ImageData[CurrentByte++] = Pixel.G;
                                    ImageData[CurrentByte++] = Pixel.R;
                                    if (BitsPerPixel > 24) ImageData[CurrentByte++] = Pixel.A;
                                }
                            }
                        } while(CurrentPixel < (width * height));
                    }
                    else
                    {
                        hFile.close();
                        throw std::invalid_argument("Invalid File Format. Required: 24 or 32 Bit TGA File.");
                    }
                
                    hFile.close();
                    this->Pixels = ImageData;
                }
                
                
                int main()
                {
                    Tga info = Tga("C:/Users/...../Desktop/SomeTGA.tga");
                
                    GLuint texture = 0;
                    glGenTextures(1, &texture);
                    glBindTexture(GL_TEXTURE_2D, texture);
                    glTexImage2D(GL_TEXTURE_2D, 0, info.HasAlphaChannel() ? GL_RGBA : GL_RGB, info.GetWidth(), info.GetWidth(), 0, info.HasAlphaChannel() ? GL_RGBA : GL_RGB, GL_UNSIGNED_BYTE, info.GetPixels().data());
                
                }
                

                这篇关于在 C++/OpenGL 中加载 tga/bmp 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:CMake 找不到源文件 (add_executable) 下一篇:在没有窗口的情况下创建 OpenGL 上下文

                相关文章

                最新文章

                  <bdo id='IJf6v'></bdo><ul id='IJf6v'></ul>
                <legend id='IJf6v'><style id='IJf6v'><dir id='IJf6v'><q id='IJf6v'></q></dir></style></legend>

              • <tfoot id='IJf6v'></tfoot>

                1. <i id='IJf6v'><tr id='IJf6v'><dt id='IJf6v'><q id='IJf6v'><span id='IJf6v'><b id='IJf6v'><form id='IJf6v'><ins id='IJf6v'></ins><ul id='IJf6v'></ul><sub id='IJf6v'></sub></form><legend id='IJf6v'></legend><bdo id='IJf6v'><pre id='IJf6v'><center id='IJf6v'></center></pre></bdo></b><th id='IJf6v'></th></span></q></dt></tr></i><div id='IJf6v'><tfoot id='IJf6v'></tfoot><dl id='IJf6v'><fieldset id='IJf6v'></fieldset></dl></div>
                2. <small id='IJf6v'></small><noframes id='IJf6v'>