使用 C++,我尝试使用 DevIL 将纹理加载到 OpenGL 中。在四处寻找不同的代码段后,我完成了一些代码(如下所示),但它似乎无法完全工作。
加载纹理(Texture2D 类的一部分):
void Texture2D::LoadTexture(const char *file_name)
{
unsigned int image_ID;
ilInit();
iluInit();
ilutInit();
ilutRenderer(ILUT_OPENGL);
image_ID = ilutGLLoadImage((char*)file_name);
sheet.texture_ID = image_ID;
sheet.width = ilGetInteger(IL_IMAGE_WIDTH);
sheet.height = ilGetInteger(IL_IMAGE_HEIGHT);
}
这可以编译并且工作正常。我确实意识到我应该只执行一次 ilInit()、iluInit() 和 ilutInit(),但是如果我删除这些行,程序会在加载任何图像时立即中断(编译正常,但运行时出错)。
在 OpenGL 中显示纹理(同一类的一部分):
void Texture2D::Draw()
{
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
glPushMatrix();
u = v = 0;
// this is the origin point (the position of the button)
VectorXYZ point_TL; // Top Left
VectorXYZ point_BL; // Bottom Left
VectorXYZ point_BR; // Bottom Right
VectorXYZ point_TR; // Top Right
/* For the sake of simplicity, I've removed the code calculating the 4 points of the Quad. Assume that they are found correctly. */
glColor3f(1,1,1);
// bind the appropriate texture frame
glBindTexture(GL_TEXTURE_2D, sheet.texture_ID);
// draw the image as a quad the size of the first loaded image
glEnable(GL_TEXTURE_2D);
glBegin(GL_QUADS);
glTexCoord2f (0, 0);
glVertex3f (point_TL.x, point_TL.y, point_TL.z); // Top Left
glTexCoord2f (0, 1);
glVertex3f (point_BL.x, point_BL.y, point_BL.z); // Bottom Left
glTexCoord2f (1, 1);
glVertex3f (point_BR.x, point_BR.y, point_BR.z); // Bottom Right
glTexCoord2f (1, 0);
glVertex3f (point_TR.x, point_TR.y, point_TR.z); // Top Right
glEnd();
glPopMatrix();
glDisable(GL_BLEND);
glDisable(GL_TEXTURE_2D);
}
目前,四边形出现了,但它完全是白色的(给定的背景颜色)。我正在加载的图像存在并且加载正常(使用加载的尺寸值验证)。
还有几点需要注意: 1)我正在使用深度缓冲区。我听说这不适用于 GL_BLEND? 2) 我真的很想使用 ilutGLLoadImage 函数。 3) 我很欣赏示例代码,因为我是 openGL 和 DevIL 的新手。
最佳答案
是的,你有问题。 ilutGLLoadImage() 可能存在问题。
尝试手动操作:
- 使用 ilLoadImage 加载图像
- 使用 glGenTextures 生成 OpenGL 纹理句柄
- 将图像上传到 OpenGL glTextImage2D 和 ilGetData()
查看此链接以获得可行的解决方案
http://r3dux.org/tag/ilutglloadimage/
我知道,这个解决方案似乎“有点”复杂,但没有人知道你会花多少时间来解决这个隐藏在 DevIL 深处的错误。
另一种修复方法:检查 GL 纹理设置代码。过滤中的任何内容都可能是 GL_INVALID_OPERATION 的原因。
我们在为旧的 ATI 卡编程时多次遇到“白色纹理”问题。
哦!最大的猜测:非二次方纹理。您的纹理文件是 2^N x 2^N 还是其他?
要使用非矩形纹理,您只需使用 GL 扩展。
另一个:你是在同一个线程中还是在另一个线程中使用纹理?请记住,您应该在同一个线程中使用 glGenTextures() 和 glBindTexture()/glBegin/glEnd。
关于c++ - 使用 DevIL 在 C++ OpenGL 中加载纹理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10707230/