将位图图像加载到OpenGL程序中

发布于 2025-01-25 03:12:10 字数 2045 浏览 3 评论 0原文

我正在尝试将位图加载到我的OpenGL 3D对象上,但是我会遇到此错误:

loadbitmap - bitcount failed = 8;

我要做的是我有一个对象和3个单独的位图图像(身体,眼睛,头部)位图像,所以我将尝试绘制纹理直至向量6498,这是身体部位的三角形。

此错误消息不是来自该文件手动打印的编译器:

#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

我使用此纹理如下:

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

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);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谁人与我共长歌 2025-02-01 03:12:10

根据已发布的代码,错误消息loadbitmap -BitCount Failed = 8表示已加载的图像使用每个像素8位,可能是8位灰度图像。

bitmapinfoheader结构(wingdi.h)说:

bibitcount

指定每个像素(BPP)的位数。对于未压缩
格式,此值是每个像素的平均位数。为了
压缩格式,此值是隐含的位深度
未压缩图像,在解码图像后。

因此,尝试加载一个RGB位图文件,而不是以24 bpp的颜色深度(每个颜色通道8位)。

为了使其与原始文件一起使用,可以将灰度图像转换为图像编辑器中所需的格式(例如,在 gimp )。 Alternatively, the bitmap loader code could be modified to accept grayscale images (related topic:

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 the original file, the 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?).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文