【发布时间】:2012-05-22 17:45:50
【问题描述】:
使用 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 整体的新手。
【问题讨论】:
-
您确定在加载纹理时存在有效的 OpenGL 上下文吗?
-
glGetError()在您的ilutGLLoadImage()呼叫返回后会发生什么? -
@datenwolf:我很确定,但不完全确定。如果您能指导我如何确保存在有效的 OpenGL 上下文,我很乐意检查。
-
@genpfault:我不确定如何从 glGetError() 获取输出,所以我使用 if 语句检查 glGetError() == GL_NO_ERROR。它 - 不 - 在 ilutGLLoadImage() 语句的任一侧输入 if。
-
所以您在
ilutGLLoadImage()之前遇到了某种错误。哪一个?我打赌GL_INVALID_OPERATION。