LoadBMP icon indicating copy to clipboard operation
LoadBMP copied to clipboard

Converting BMP to texture help

Open willos2006 opened this issue 5 years ago • 1 comments

Hello,

Sorry if this isn't the right place to ask. I'm new to all of openGL and i was wondering how i could take what comes back from your function and turn it into a texture.

Thanks for your help!

willos2006 avatar Jun 14 '20 19:06 willos2006

No problem, feel free to ask here.

Here's a quick example I made, so there might be a typo or two. But this is how you'd load an image, create an OpenGL texture, and upload the image data to the texture.

unsigned char *pixels = NULL;
unsigned int width, height;

unsigned int err = loadbmp_decode_file("image.bmp", &pixels, &width, &height, LOADBMP_RGBA);

if (err) {
    printf("LoadBMP Load Error: %u\n", err);
} else {
    GLuint handle;
    glGenTextures(1, &handle);
    glBindTexture(GL_TEXTURE_2D, handle);

    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
    //                                                        ^ must match loadbmp_decode_file

    free(pixels);
}

So if you use LOADBMP_RGBA then you ofc need to use GL_RGBA, and if LOADBMP_RGB then ofc GL_RGB.

If alpha is not needed, then you can change the internal format (the third argument) from GL_RGBA to GL_RGB.

vallentin avatar Jun 14 '20 22:06 vallentin