0
加載BMP存儲器中的BMP文件時出現問題。(幫助)加載BMP文件時出錯
很明顯,圖像加載的很好,但是當它被OpenGL程序顯示時看起來很糟糕。該程序不引發任何錯誤(這是在C++)
的代碼:
TEXTURE::TEXTURE(const char *path) {
unsigned char header[54];
unsigned int dataPos;
unsigned int width, height, size;
unsigned char *data;
FILE *filePointer = fopen(path, "rb");
if(filePointer == NULL)
printf_s("Error openning image file at '%s'!", path);
if(!fread(header, 1, 54, filePointer))
printf_s("Error, file at '%s' isn't a real BMP file.", path);
if(header[0] != 'B' || header[1] != 'M')
printf_s("Error, file at '%s' isn't a real BMP file.", path);
dataPos = *(int *)&header[0x0A];
size = *(int *)&header[0x22];
width = *(int *)&header[0x12];
height = *(int *)&header[0x16];
if(!size)
size = width * height * 3;
if(!dataPos)
dataPos = 54;
data = new unsigned char[size];
fread(data, 1, size, filePointer);
fclose(filePointer);
glGenTextures(1, &texID);
glBindTexture(GL_TEXTURE_2D, texID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
glBindTexture(GL_TEXTURE_2D, 0);
}
結果(信息在圖像上是不重要的):
(I不能發表圖片...)http://i.stack.imgur.com/Ny2YD.jpg
原始圖像:http://k33.kn3.net/C/B/A/4/0/C/DB2.bmp
頂點着色器來源:
#version 330 core
layout(location = 0) in vec4 position;
layout(location = 1) in vec2 texCoords;
out DATA {
vec2 texCoords;
} vs_out;
void main() {
gl_Position = position;
vs_out.texCoords = texCoords;
}
片段着色器源:
#version 330 core
layout(location = 0) out vec4 color;
in DATA {
vec2 texCoords;
} fs_in;
uniform sampler2D sampler;
void main() {
color = texture(sampler, fs_in.texCoords);
}
你知道文件內容是RGB,而不是RGBA嗎?此外,如果它確實是RGB,如果寬度不是4的倍數,則需要調用'glPIxelStorei(GL_UNPACK_ALIGNMENT,1)'。 –
圖像大小爲512x512像素(非常完美)。我確定我將它保存爲24位BMP圖像(RGB)。 – Seyro97
魔法藝術對我的代碼有意義!現在,沒有做任何事情,它做了正確的事情......非常感謝 – Seyro97