Search code examples
c++openglbmp

Problems loading bitmap data into an OpenGL texture


Unfortunatly, I have some problems with glBindTexture().

When I try to bind a bitmap image to my skybox I only see some white. I tested it with other figures and also that does not work. I am sure the problem is in the Engine class.

This is my engine.cpp, here I load the bitmap et cetra:

#include <windows.h>
#include <gl/GL.h>

#include <iostream>
#include "engine.h"

unsigned char* Engine::getImageContents(char path[], BITMAPINFOHEADER* bih) {
    FILE* input; fopen_s(&input, path, "rb");

    if (input != NULL) {
        BITMAPFILEHEADER bfh;
        fread(&bfh, sizeof(bfh), 1, input);
        fread(&bih, sizeof(bih), 1, input);

        unsigned char* data =
            new unsigned char[bih->biWidth * bih->biHeight * bih->biBitCount / 8];

        fread(data, sizeof(data), 1, input);
        fclose(input);

        return data;
    } else {
        return NULL;
    }
}

void Engine::bindTexture(char path[]) {
    this->bindTexture(this->getTextureId(path));
}

void Engine::bindTexture(unsigned int id) {
    if (id != this->boundTexture) {
        glBindTexture(GL_TEXTURE_2D, id);
        this->boundTexture = id;
    }
}

unsigned int Engine::getTextureId(char path[]) {
    if (this->textureMap.find(path) != this->textureMap.end()) {
        return this->textureMap[path];
    } else {
        unsigned int id; glGenTextures(1, &id);
        BITMAPINFOHEADER bih;
        unsigned char* data = this->getImageContents(path, &bih);
        this->setupTexture(data, id, bih.biWidth, bih.biHeight);
        this->textureMap.insert(std::pair<char*, unsigned int>(path, id));
        return id;
    }
}

void Engine::setupTexture(unsigned char* data, unsigned int id, long width, long height) {
    this->setupTexture(data, id, width, height, false);
}

void Engine::setupTexture(unsigned char* data, unsigned int id, long width, long height, bool clamp) {
    std::cout << height << std::endl;
    this->bindTexture(id);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, clamp ? GL_CLAMP : GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, clamp ? GL_CLAMP : GL_REPEAT);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_INT, data);
}

I use the class like this:

Engine::bindTexture(path);

Any ideas?


Solution

  • I see few possible problems.

    1. Are you sure you load not compressed .bmp file, please check bfh.biCompression
    2. You can use GL_RGBA if bih->biBitCount == 32, try to use GL_RGB
    3. Try to use GL_UNSIGNED_BYTE instead of GL_UNSIGNED_INT
    4. Please check image size, I think for skybox it should be something like 128x128, 256x256, 512x512, i.e. power-of-2 texture size
    5. Replace
      fread(data, sizeof(data), 1, input);
      on
      fread(data, 1, bih->biWidth * bih->biHeight * bih->biBitCount / 8, input);