Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can load a bitmap and a tga file using these..</p> <pre><code>#include &lt;vector&gt; #include &lt;fstream&gt; #ifdef __APPLE__ #include &lt;OpenGL/gl.h&gt; #include &lt;OpenGL/glu.h&gt; #endif #ifdef _WIN32 #include &lt;GL/gl.h&gt; #include &lt;GL/glu.h&gt; #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&lt;std::uint8_t&gt; Pixels; public: BMP(const char* FilePath); std::vector&lt;std::uint8_t&gt; GetPixels() const {return this-&gt;Pixels;} std::uint32_t GetWidth() const {return this-&gt;width;} std::uint32_t GetHeight() const {return this-&gt;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&lt;std::uint8_t&gt; FileInfo(Length); hFile.read(reinterpret_cast&lt;char*&gt;(FileInfo.data()), 54); if(FileInfo[0] != 'B' &amp;&amp; FileInfo[1] != 'M') { hFile.close(); throw std::invalid_argument("Error: Invalid File Format. Bitmap Required."); } if (FileInfo[28] != 24 &amp;&amp; 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] &lt;&lt; 8); height = FileInfo[22] + (FileInfo[23] &lt;&lt; 8); std::uint32_t PixelsOffset = FileInfo[10] + (FileInfo[11] &lt;&lt; 8); std::uint32_t size = ((width * BitsPerPixel + 31) / 32) * 4 * height; Pixels.resize(size); hFile.seekg (PixelsOffset, std::ios::beg); hFile.read(reinterpret_cast&lt;char*&gt;(Pixels.data()), size); hFile.close(); } int main() { BMP info = BMP("C:/Users/....../Desktop/SomeBmp.bmp"); GLuint texture = 0; glGenTextures(1, &amp;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()); } </code></pre> <p>TGA's:</p> <pre><code>#include &lt;vector&gt; #include &lt;fstream&gt; #ifdef __APPLE__ #include &lt;OpenGL/gl.h&gt; #include &lt;OpenGL/glu.h&gt; #endif #ifdef _WIN32 #include &lt;GL/gl.h&gt; #include &lt;GL/glu.h&gt; #endif typedef union PixelInfo { std::uint32_t Colour; struct { std::uint8_t R, G, B, A; }; } *PPixelInfo; class Tga { private: std::vector&lt;std::uint8_t&gt; Pixels; bool ImageCompressed; std::uint32_t width, height, size, BitsPerPixel; public: Tga(const char* FilePath); std::vector&lt;std::uint8_t&gt; GetPixels() {return this-&gt;Pixels;} std::uint32_t GetWidth() const {return this-&gt;width;} std::uint32_t GetHeight() const {return this-&gt;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&lt;std::uint8_t&gt; 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&lt;char*&gt;(&amp;Header), sizeof(Header)); if (!std::memcmp(DeCompressed, &amp;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) &amp;&amp; (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&lt;char*&gt;(ImageData.data()), size); } else if (!std::memcmp(IsCompressed, &amp;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) &amp;&amp; (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&lt;char*&gt;(&amp;ChunkHeader), sizeof(ChunkHeader)); if(ChunkHeader &lt; 128) { ++ChunkHeader; for(int I = 0; I &lt; ChunkHeader; ++I, ++CurrentPixel) { hFile.read(reinterpret_cast&lt;char*&gt;(&amp;Pixel), BytesPerPixel); ImageData[CurrentByte++] = Pixel.B; ImageData[CurrentByte++] = Pixel.G; ImageData[CurrentByte++] = Pixel.R; if (BitsPerPixel &gt; 24) ImageData[CurrentByte++] = Pixel.A; } } else { ChunkHeader -= 127; hFile.read(reinterpret_cast&lt;char*&gt;(&amp;Pixel), BytesPerPixel); for(int I = 0; I &lt; ChunkHeader; ++I, ++CurrentPixel) { ImageData[CurrentByte++] = Pixel.B; ImageData[CurrentByte++] = Pixel.G; ImageData[CurrentByte++] = Pixel.R; if (BitsPerPixel &gt; 24) ImageData[CurrentByte++] = Pixel.A; } } } while(CurrentPixel &lt; (width * height)); } else { hFile.close(); throw std::invalid_argument("Invalid File Format. Required: 24 or 32 Bit TGA File."); } hFile.close(); this-&gt;Pixels = ImageData; } int main() { Tga info = Tga("C:/Users/...../Desktop/SomeTGA.tga"); GLuint texture = 0; glGenTextures(1, &amp;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()); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload