Home > Enterprise >  Loading a Bitmap Image into openGL program
Loading a Bitmap Image into openGL program

Time:04-30

I am trying to load a bitmap to my openGL 3d object, however I get this error:

loadbitmap - bitcount failed = 8;

what I am trying to do is that I have an object and 3 separate bitmap images (Body, Eye, Head) bitmap images, so I`ll try to draw the texture till vector 6498, which are the triangles for the body part.

This error message isnt from the compiler it is printed manually from this file:

#ifndef _Bitmap_
#define _Bitmap_

#include "glad/glad.h"
#include <stdio.h>
#include <windows.h>
#include <wingdi.h>

GLuint loadbitmap(const char* filename, unsigned char*& pixelBuffer, BITMAPINFOHEADER* infoHeader, BITMAPFILEHEADER* fileHeader)
{
    FILE* bitmapFile;

    errno_t err = fopen_s(&bitmapFile, filename, "rb");
    if (err != 0 || bitmapFile == NULL)
    {
        printf("loadbitmap - open failed for %s\n", filename);
        return NULL;
    }

    fread(fileHeader, sizeof(BITMAPFILEHEADER), 1, bitmapFile);

    if (fileHeader->bfType != 0x4D42)
    {
        printf("loadbitmap - type failed \n");
        return NULL;
    }

    fread(infoHeader, sizeof(BITMAPINFOHEADER), 1, bitmapFile);

    if (infoHeader->biBitCount < 24)
    {
        printf("loadbitmap - bitcount failed = %d\n", infoHeader->biBitCount);
        return NULL;
    }

    fseek(bitmapFile, fileHeader->bfOffBits, SEEK_SET);

    int nBytes = infoHeader->biWidth * infoHeader->biHeight * 3;
    pixelBuffer = new unsigned char[nBytes];
    fread(pixelBuffer, sizeof(unsigned char), nBytes, bitmapFile);

    fclose(bitmapFile);

    for (int i = 0; i < nBytes; i  = 3)
    {
        unsigned char tmp = pixelBuffer[i];
        pixelBuffer[i] = pixelBuffer[i   2];
        pixelBuffer[i   2] = tmp;
    }

    printf("loadbitmap - loaded %s w=%d h=%d bits=%d\n", filename, infoHeader->biWidth, infoHeader->biHeight, infoHeader->biBitCount);
}

#endif

and I use this texture as follows:

GLuint texture = setup_texture("OBJFiles/Body.bmp");
        glBindTexture(GL_TEXTURE_2D, texture);
        glUseProgram(shaderProgram);
        glBindVertexArray(VAO);
        glDrawArrays(GL_TRIANGLES, 0, 6498);
        glBindVertexArray(0);

CodePudding user response:

According to the posted code, the error message loadbitmap - bitcount failed = 8; indicates that the loaded image uses 8 bits per pixel, probably an 8-bit grayscale image.

The documentation of the biBitCount member of the BITMAPINFOHEADER structure (wingdi.h) says:

biBitCount

Specifies the number of bits per pixel (bpp). For uncompressed formats, this value is the average number of bits per pixel. For compressed formats, this value is the implied bit depth of the uncompressed image, after the image has been decoded.

So, try to load an RGB bitmap file instead with a color depth of 24 bpp (8 bits per color channel).

In order to make it work with original file, the original grayscale image could be converted to the required format in an image editor (for example in Gimp). Alternatively, the bitmap loader code could be modified to accept grayscale images (related topic: How do I display a grayscale image with an OpenGL texture?).

  • Related