不幸的是,我对 glBindTexture() 有一些问题。
当我尝试将位图图像绑定到我的天空盒时,我只看到一些白色。我用其他数字对其进行了测试,但也不起作用。我确定问题出在 Engine 类中。
这是我的engine.cpp,在这里我加载位图等等:
#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);
有任何想法吗?