SDL_image/C++ OpenGL 程序:IMG_Load() 产生模糊图像

发布于 2024-08-10 21:02:53 字数 1719 浏览 10 评论 0原文

我正在尝试加载图像文件并将其用作立方体的纹理。我正在使用 SDL_image 来做到这一点。

原始图片

我使用此图像是因为我发现它具有各种文件格式(tga、tif、jpg、png、bmp)

代码:

SDL_Surface * texture;

//load an image to an SDL surface (i.e. a buffer)

texture = IMG_Load("/Users/Foo/Code/xcode/test/lena.bmp");

if(texture == NULL){
    printf("bad image\n");
    exit(1);
}

//create an OpenGL texture object 
glGenTextures(1, &textureObjOpenGLlogo);

//select the texture object you need
glBindTexture(GL_TEXTURE_2D, textureObjOpenGLlogo);

//define the parameters of that texture object
//how the texture should wrap in s direction
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
//how the texture should wrap in t direction
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
//how the texture lookup should be interpolated when the face is smaller than the texture
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
//how the texture lookup should be interpolated when the face is bigger than the texture
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

//send the texture image to the graphic card
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture->w, texture->h, 0, GL_RGB, GL_UNSIGNED_BYTE, texture-> pixels);

//clean the SDL surface
SDL_FreeSurface(texture);

代码编译时没有错误或警告!

我已经厌倦了所有的文件格式,但这总是产生丑陋的结果:

result

我正在使用:SDL_image 1.2.9 & SDL 1.2.14 和 XCode 3.2 在 10.6.2 下

有谁知道如何解决这个问题?

I'm trying to load an image file and use it as a texture for a cube. I'm using SDL_image to do that.

original image

I used this image because I've found it in various file formats (tga, tif, jpg, png, bmp)

The code :

SDL_Surface * texture;

//load an image to an SDL surface (i.e. a buffer)

texture = IMG_Load("/Users/Foo/Code/xcode/test/lena.bmp");

if(texture == NULL){
    printf("bad image\n");
    exit(1);
}

//create an OpenGL texture object 
glGenTextures(1, &textureObjOpenGLlogo);

//select the texture object you need
glBindTexture(GL_TEXTURE_2D, textureObjOpenGLlogo);

//define the parameters of that texture object
//how the texture should wrap in s direction
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
//how the texture should wrap in t direction
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
//how the texture lookup should be interpolated when the face is smaller than the texture
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
//how the texture lookup should be interpolated when the face is bigger than the texture
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

//send the texture image to the graphic card
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture->w, texture->h, 0, GL_RGB, GL_UNSIGNED_BYTE, texture-> pixels);

//clean the SDL surface
SDL_FreeSurface(texture);

The code compiles without errors or warnings !

I've tired all the files formats but this always produces that ugly result :

result

I'm using : SDL_image 1.2.9 & SDL 1.2.14 with XCode 3.2 under 10.6.2

Does anyone knows how to fix this ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

妄断弥空 2024-08-17 21:02:53

图像扭曲的原因是它不是您指定的 RGBA 格式。检查 texture->format 以找出格式并选择代表格式的适当的 GL_ 常量。 (或者,自行将其转换为您选择的格式。)

The reason the image is distorted is because it's not in the RGBA format that you've specified. Check the texture->format to find out the format it's in and select the appropriate GL_ constant that represents the format. (Or, transform it yourself to the format of your choice.)

他是夢罘是命 2024-08-17 21:02:53

我认为 greyfade 有正确的答案,但您应该注意的另一件事是需要锁定表面。情况可能并非如此,因为您正在使用内存中的表面,但通常您需要在使用 SDL_LockSurface()。例如:

bool lock = SDL_MUSTLOCK(texture);
if(lock)
    SDL_LockSurface(texture);  // should check that return value == 0
// access pixel data, e.g. call glTexImage2D
if(lock)
    SDL_UnlockSUrface(texture);

I think greyfade has the right answer, but another thing you should be aware of is the need to lock surfaces. This is probably not the case, since you're working with an in-memory surface, but normally you need to lock surfaces before accessing their pixel data with SDL_LockSurface(). For example:

bool lock = SDL_MUSTLOCK(texture);
if(lock)
    SDL_LockSurface(texture);  // should check that return value == 0
// access pixel data, e.g. call glTexImage2D
if(lock)
    SDL_UnlockSUrface(texture);
余生一个溪 2024-08-17 21:02:53

如果您有 alpha 通道,则每个像素都是 4 个无符号字节,如果没有,则为 3 个无符号字节。该图像没有透明度,当我尝试保存它时,它是 .jpg。

更改

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 纹理->w, 纹理->h, 0, GL_RGB, GL_UNSIGNED_BYTE, 纹理->像素);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 纹理->w, 纹理->h, 0, GL_RGB, GL_UNSIGNED_BYTE, 纹理->像素);

那应该解决它。

对于具有 Alpha 通道的 .png,请使用

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,texture->w,texture->h, 0, GL_RGBA, GL_UNSIGNED_BYTE,texture->pixels);

If you have an alpha chanel every pixel is 4 unsigned bytes, if you don't it's 3 unsigned bytes. This image has no transpareny and when I try to save it, its a .jpg.

change

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture->w, texture->h, 0, GL_RGB, GL_UNSIGNED_BYTE, texture-> pixels);

to

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, texture->w, texture->h, 0, GL_RGB, GL_UNSIGNED_BYTE, texture-> pixels);

That should fix it.

For a .png with an alpha channel use

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture->w, texture->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, texture-> pixels);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文