使用DevIL在C ++ OpenGL中加载纹理 [英] loading textures in C++ OpenGL using DevIL
问题描述
我正在使用C ++,尝试使用DevIL将纹理加载到OpenGL中.在搜寻了不同的代码段之后,我完成了一些代码(如下所示),但是它似乎无法完全正常工作.
Using C++, I'm trying to load a texture into OpenGL using DevIL. After scrounging around for different code segments, I have a bit of code done (shown below), but it doesn't seem to work completely.
加载纹理(Texture2D类的一部分):
Loading a texture (Part of a Texture2D class):
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(),但是如果我删除了这些行,程序在加载任何图像时都会立即中断(编译良好,但运行时会出错).
This compiles and works fine. I do realise that I should only do the ilInit(), iluInit(), and ilutInit() once, but if I remove those lines the program instantly breaks upon loading any image (compiles fine, but errors on runtime).
在OpenGL中显示纹理(属于同一类):
Displaying the texture in OpenGL (Part of the same class):
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);
}
当前,四边形显示出来,但是它是完全白色的(给定背景色).我正在加载的图像存在并且已很好加载(已使用加载的尺寸值进行了验证).
Currently, the quad shows up, but its completely white (the background colour it's given). The image I'm loading exists and is loaded fine (verified using the loaded size values).
我应该注意的另外几件事: 1)我正在使用深度缓冲区.我听说这不适用于GL_BLEND吗? 2)我真的很想使用ilutGLLoadImage函数. 3)我很欣赏示例代码,因为我是OpenGL和DevIL的新手.
Another few things I should note: 1) I am using a depth buffer. I've heard this doesn't go well with GL_BLEND? 2) I would really like to use the ilutGLLoadImage function. 3) I appreciate example code, as I'm a newbie to openGL and DevIL as a whole.
推荐答案
是的,您遇到了问题. ilutGLLoadImage()可能有问题.
Yes, you have the problem. There might be issues with ilutGLLoadImage().
尝试手动处理:
- 使用ilLoadImage加载图像
- 使用glGenTextures生成OpenGL纹理句柄
- 将图像上传到OpenGL glTextImage2D和ilGetData()
请参阅此链接以获取可行的解决方案
See this link for a working solution
http://r3dux.org/tag/ilutglloadimage/
我知道,这种解决方案似乎有点"复杂,但是没人知道您会花很多时间与DevIL中隐藏的这个错误进行战斗.
I know, this solution seems to be "a little" complicated, but nobody knows jow much time you would spend fighting with this bug hidden deep in the DevIL.
另一种解决问题的方法:检查GL纹理设置代码.筛选中的任何内容都可能是GL_INVALID_OPERATION的原因.
Another way of fixing things: check you GL texture setup code. Anything in the filtering can be a reason for GL_INVALID_OPERATION.
在对旧的ATI卡进行编程时,我们经常遇到白色纹理"问题.
We've a lot of times into the "White texture" issue while programming the old ATI cards.
哦!最大的猜测:非二次方纹理.您是2 ^ N乘2 ^ N还是其他不同的纹理文件?
Oh! The biggest guess: Non-power-of-two textures. Is you texture file 2^N by 2^N or something different ?
要使用非矩形纹理,您只需要使用GL扩展即可.
To use non-rectangular textures you just have to use GL extensions.
还有另一个:您是在同一线程中还是在另一个线程中使用纹理?请记住,您应该在同一线程中使用glGenTextures()和glBindTexture()/glBegin/glEnd.
And the other one: are you using the textures in the same thread or in the other ? Remember that you should glGenTextures() and glBindTexture()/glBegin/glEnd in the same thread.
这篇关于使用DevIL在C ++ OpenGL中加载纹理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!