SDL_image/C++ OpenGL 程序:IMG_Load() 产生模糊图像
我正在尝试加载图像文件并将其用作立方体的纹理。我正在使用 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);
代码编译时没有错误或警告!
我已经厌倦了所有的文件格式,但这总是产生丑陋的结果:
我正在使用: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.
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 :
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
图像扭曲的原因是它不是您指定的 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 appropriateGL_
constant that represents the format. (Or, transform it yourself to the format of your choice.)我认为 greyfade 有正确的答案,但您应该注意的另一件事是需要锁定表面。情况可能并非如此,因为您正在使用内存中的表面,但通常您需要在使用
SDL_LockSurface()
。例如: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:如果您有 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);