DevIL图像无法正确渲染

时间:2014-04-21 07:41:21

标签: c++ opengl textures devil

我正在使用OpenGL,我可以正确加载tga文件,但出于某种原因,当我呈现jpg文件时,我看不到它们。

这就是图像的样子 -

enter image description here

这就是它的样子......为什么它会拉长?是因为坐标?

enter image description here

这是我用于绘图的代码。

void Renderer::DrawJpg(GLuint tex, int xi, int yq, int width, int height) const
{
glBindTexture(GL_TEXTURE_2D, tex);
glBegin(GL_QUADS);
glTexCoord2i(0, 0); glVertex2i(0+xi,   0+xi);
glTexCoord2i(0, 1); glVertex2i(0+xi,   height+xi);
glTexCoord2i(1, 1); glVertex2i(width+xi, height+xi);
glTexCoord2i(1, 0); glVertex2i(width+xi, 0+xi);
glEnd();
}

这是我加载图片的方式......

imagename=s;
ILboolean success;
ilInit();
ilGenImages(1, &id);
ilBindImage(id);
success = ilLoadImage((const ILstring)imagename.c_str());
if (success)
{
    success = ilConvertImage(IL_RGB, IL_UNSIGNED_BYTE); /* Convert every colour component into
                                                         unsigned byte. If your image contains alpha channel you can replace IL_RGB with IL_RGBA */
    if (!success)
    {
        printf("image conversion failed.");
    }
    glGenTextures(1, &id);
    glBindTexture(GL_TEXTURE_2D, id);

    width = ilGetInteger(IL_IMAGE_WIDTH);
    height = ilGetInteger(IL_IMAGE_HEIGHT);

    glTexImage2D(GL_TEXTURE_2D, 0, ilGetInteger(IL_IMAGE_BPP), ilGetInteger(IL_IMAGE_WIDTH),
                 ilGetInteger(IL_IMAGE_HEIGHT), 0, ilGetInteger(IL_IMAGE_FORMAT), GL_UNSIGNED_BYTE,
                 ilGetData());
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);       // Linear Filtered
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);       // Linear Filtered
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

我可能应该提到这一点,但有些图像确实被正确渲染,我认为这是因为宽度!=高度。但事实并非如此,宽度为!= height的图像也可以很好地加载。 但对于其他图像,我仍然会遇到这个问题。

2 个答案:

答案 0 :(得分:1)

总是尝试将图像的宽度和高度设置为2,因为某些GPU仅支持NPOT分辨率的纹理。 (例如128x128,512x512但不是123x533,128x532)

我认为这里应该使用GL_CLAMP_TO_EDGE而不是GL_REPEAT :) 当纹理坐标为>时使用GL_REPEAT。 1.0f,CLAMP_TO_EDGE也可以保证图像将填充多边形而边缘上没有不需要的线条。 (它阻止你在边缘的线性过滤)

请记住尝试使用浮点数的代码(来自评论的样本):)

以下是http://open.gl/textures的好解释:)

答案 1 :(得分:1)

您可能需要致电

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
在使用glTexImage2D上传纹理数据之前

来自reference pages

  

GL_UNPACK_ALIGNMENT

     

指定每个像素行开头的对齐要求   在记忆中。允许值为1(字节对齐),2(行   对齐到偶数字节),4(字对齐)和8(行开始   在双字边界上。)

对齐的默认值是4,您的图像加载库可能返回带有字节对齐行的像素数据,这解释了为什么您的某些图像看起来没问题(当宽度是4的倍数时)。