简体   繁体   English

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

[英]SDL_image/C++ OpenGL Program: IMG_Load() produces fuzzy images

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. 我正在使用SDL_image来做到这一点。

原始图像

I used this image because I've found it in various file formats (tga, tif, jpg, png, bmp) 我用这个图像是因为我发现它有各种文件格式(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 : 我已经厌倦了所有的文件格式,但这总会产生丑陋的结果:

结果

I'm using : SDL_image 1.2.9 & SDL 1.2.14 with XCode 3.2 under 10.6.2 我正在使用:SDL_image 1.2.9和SDL 1.2.14,XCode 3.2低于10.6.2

Does anyone knows how to fix this ? 有谁知道如何解决这个问题?

The reason the image is distorted is because it's not in the RGBA format that you've specified. 图像失真的原因是因为它不是您指定的RGBA格式。 Check the texture->format to find out the format it's in and select the appropriate GL_ constant that represents the format. 检查texture->format以找出它所在的格式,并选择代表格式的相应GL_常量。 (Or, transform it yourself to the format of your choice.) (或者,将其转换为您选择的格式。)

I think greyfade has the right answer, but another thing you should be aware of is the need to lock surfaces. 我认为greyfade有正确的答案,但你应该注意的另一件事是需要锁定表面。 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() . 这可能不是这种情况,因为您正在使用内存中的表面,但通常需要在使用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);

If you have an alpha chanel every pixel is 4 unsigned bytes, if you don't it's 3 unsigned bytes. 如果你有一个alpha chanel,每个像素都是4个无符号字节,如果不是,那就是3个无符号字节。 This image has no transpareny and when I try to save it, its a .jpg. 这个图像没有透明度,当我尝试保存它时,它是一个.jpg。

change 更改

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture->w, texture->h, 0, GL_RGB, GL_UNSIGNED_BYTE, texture-> pixels); 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); 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 对于使用alpha通道的.png

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM