不幸的是,我在使用glBindTexture()时遇到了一些问题。
当我尝试将位图图像绑定到我的天空盒时,我只看到一些白色。我用其他数字测试了它,但这也行不通。我确定问题出在Engine类中。
这是我的engine.cpp,这里我加载了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);
}
我使用这样的类:
Engine::bindTexture(path);
有什么想法吗?
答案 0 :(得分:2)
这是不对的:
fread(&bih, sizeof(bih), 1, input);
bih
已经是指向结构的指针。你想要:
fread(bih, sizeof *bih, 1, input);
此外,您确实应该使用bfOffBits
中的BITMAPFILEHEADER
,而不是假设位图位紧跟BITMAPINFOHEADER
。请参阅the MSDN topic describing the format of bitmap files。
答案 1 :(得分:1)
我看到几个可能的问题。