将位图图像加载到OpenGL程序中
我正在尝试将位图加载到我的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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据已发布的代码,错误消息
loadbitmap -BitCount Failed = 8
表示已加载的图像使用每个像素8位,可能是8位灰度图像。bitmapinfoheader结构(wingdi.h)说:
因此,尝试加载一个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: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?).