gpt4 book ai didi

c++ - DevIL 图像无法正确呈现

转载 作者:行者123 更新时间:2023-11-28 02:50:26 24 4
gpt4 key购买 nike

我正在使用 OpenGL,我可以正确加载 tga 文件,但由于某些原因,当我渲染 jpg 文件时,我看不到它们。

这就是图像应该看起来的样子——

enter image description here

这就是它的样子..为什么它被拉伸(stretch)了?是因为坐标吗?

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);

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

最佳答案

你可能需要打电话

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

在使用 glTexImage2D 上传纹理数据之前。

来自reference pages :

GL_UNPACK_ALIGNMENT

Specifies the alignment requirements for the start of each pixel row in memory. The allowable values are 1 (byte-alignment), 2 (rows aligned to even-numbered bytes), 4 (word-alignment), and 8 (rows start on double-word boundaries).

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

关于c++ - DevIL 图像无法正确呈现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23193080/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com