png 图像加载到纹理时模糊

发布于 2024-08-30 16:01:05 字数 2134 浏览 8 评论 0原文

我在 Photoshop 中创建了一个 png 图像,其中包含加载到 OpenGL 程序中的透明胶片。我已将其绑定到纹理,在程序中图片看起来很模糊,我不知道为什么。

替代文本 http://img685.imageshack.us/img685/9130/upload2.png< /a>

替代文本 http://img695.imageshack.us/img695/2424/upload1e .png

加载代码

// Texture loading object
nv::Image title;

// Return true on success
if(title.loadImageFromFile("test.png"))
{
    glGenTextures(1, &titleTex);
    glBindTexture(GL_TEXTURE_2D, titleTex);
    glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
    glTexImage2D(GL_TEXTURE_2D, 0, title.getInternalFormat(), title.getWidth(), title.getHeight(), 0, title.getFormat(), title.getType(), title.getLevel(0));
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 16.0f);
}

else
    MessageBox(NULL, "Failed to load texture", "End of the world", MB_OK | MB_ICONINFORMATION);

显示代码

glEnable(GL_TEXTURE_2D);    
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glBindTexture(GL_TEXTURE_2D, titleTex);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glTranslatef(-800, 0, 0.0);
glColor3f(1,1,1);

glBegin(GL_QUADS);
    glTexCoord2f(0.0, 0.0); glVertex2f(0,0);
    glTexCoord2f(0.0, 1.0); glVertex2f(0,600);
    glTexCoord2f(1.0, 1.0); glVertex2f(1600,600);
    glTexCoord2f(1.0, 0.0); glVertex2f(1600,0);
glEnd();
glDisable(GL_BLEND);    
glDisable(GL_TEXTURE_2D);

编辑:我不认为我正在拉伸每个像素在坐标系中应该是两个

int width=800, height=600;
int left = -400-(width-400);
int right = 400+(width-400);
int top = 400+(height-400);
int bottom = -400-(height-400);
gluOrtho2D(left,right,bottom,top);

I have created a png image in photoshop with transparencies that I have loaded into and OpenGL program. I have binded it to a texture and in the program the picture looks blurry and I'm not sure why.

alt text http://img685.imageshack.us/img685/9130/upload2.png

alt text http://img695.imageshack.us/img695/2424/upload1e.png

Loading Code

// Texture loading object
nv::Image title;

// Return true on success
if(title.loadImageFromFile("test.png"))
{
    glGenTextures(1, &titleTex);
    glBindTexture(GL_TEXTURE_2D, titleTex);
    glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
    glTexImage2D(GL_TEXTURE_2D, 0, title.getInternalFormat(), title.getWidth(), title.getHeight(), 0, title.getFormat(), title.getType(), title.getLevel(0));
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 16.0f);
}

else
    MessageBox(NULL, "Failed to load texture", "End of the world", MB_OK | MB_ICONINFORMATION);

Display Code

glEnable(GL_TEXTURE_2D);    
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glBindTexture(GL_TEXTURE_2D, titleTex);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glTranslatef(-800, 0, 0.0);
glColor3f(1,1,1);

glBegin(GL_QUADS);
    glTexCoord2f(0.0, 0.0); glVertex2f(0,0);
    glTexCoord2f(0.0, 1.0); glVertex2f(0,600);
    glTexCoord2f(1.0, 1.0); glVertex2f(1600,600);
    glTexCoord2f(1.0, 0.0); glVertex2f(1600,0);
glEnd();
glDisable(GL_BLEND);    
glDisable(GL_TEXTURE_2D);

EDIT: I don't think i'm stretching each pixel should be two in the co-ordinate system

int width=800, height=600;
int left = -400-(width-400);
int right = 400+(width-400);
int top = 400+(height-400);
int bottom = -400-(height-400);
gluOrtho2D(left,right,bottom,top);

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

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

发布评论

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

评论(5

再可℃爱ぅ一点好了 2024-09-06 16:01:05

OpenGL(通常)要求纹理本身的大小为 2 的幂,因此(可能)发生的情况是您的纹理被缩放到尺寸为 2 的幂的大小,然后 它被缩小到原始大小——在缩放两次的过程中,你会失去一些质量。

显然,您只想显示位图而不进行任何缩放,而不将其包装到另一个对象的表面或类似的东西(即任何纹理的用途)。既然如此,我只是将其显示为位图,而不是纹理(例如,请参见 glRasterPos2iglBitmap)。

OpenGL will (normally) require that the texture itself have a size that's a power of 2, so what's (probably) happening is that your texture is being scaled to a size where the dimensions are a power of 2, then it's being scaled back to the original size -- in the process of being scaled twice, you're losing some quality.

You apparently just want to display your bitmap without any scaling, without wrapping it to the surface of another object, or anything like that (i.e., any of the things textures are intended for). That being the case, I'd just display it as a bitmap, not a texture (e.g. see glRasterPos2i and glBitmap).

傾城如夢未必闌珊 2024-09-06 16:01:05

为什么您首先要对开始屏幕上的静态图像使用 mipmapping 和各向异性过滤?看起来图像不太可能被旋转(各向异性过滤的用途)或者必须非常快地多次调整大小(mipmapping 的用途)。

如果纹理被拉伸:尝试对 GL_MAG_FILTER 使用 GL_NEAREST,在这种情况下它可以提供更好的结果(GL_LINEAR 更准确,但是具有模糊的性质)。

如果纹理最小化:同样的情况,请尝试使用 GL_NEAREST_MIPMAP_NEAREST,或者甚至更好,尝试不使用 mipmap 和 GL_NEAREST(或 GL_LINEAR,以提供者为准)你就是最好的结果)。

Why would you want to use mipmapping and anisotropic filtering for a static image on your start screen in the first place? It looks unlikely the image will be rotated (what anisotropic filtering is for) or has to be resized many times really fast (what mipmapping is for).

If the texture is being stretched: try using GL_NEAREST for your GL_MAG_FILTER, in this case it could give better results (GL_LINEAR is more accurate, but has a nature of blurring).

If the texture is minimized: same thing, try using GL_NEAREST_MIPMAP_NEAREST, or even better, try using no mipmaps and GL_NEAREST (or GL_LINEAR, whichever gives you the best result).

删除→记忆 2024-09-06 16:01:05

我建议你让 png 的分辨率为 2 的幂。即 1024x512 并将要绘制的部分放置在其左上角,仍然以屏幕的分辨率进行。然后将纹理坐标重新调整为 800.0/1024.0 和 600.0/512.0,以从纹理中获取正确的部分。我相信,glTexImage2D 有时可以处理不是 2 的幂的宽度和高度,但可以缩放输入图像,从而对其进行过滤。

可以在此处查看处理此问题的示例(一个 iPhone - OpenGLES - 项目,抓取非 2 的幂的屏幕部分,并将其绘制到 512x512 纹理中,并重新缩放 GL_TEXTURE 矩阵,第 123 行,而不是手动重新缩放 texcoords)

< a href="http://www.gamedev.net/community/forums/topic.asp?topic_id=400339&whichpage=1�" rel="nofollow noreferrer">这里是另一篇提到这一点的帖子方法。

I'd suggest that you make the png have a resolution in a power of 2's. Ie 1024x512 and place the part you want to drawn in the upper left corner of it still in the resolution for the screen. Then rescale the texcoords to be 800.0/1024.0 and 600.0/512.0 to get the right part from the texture. I belive that what is going on is glTexImage2D can sometime handle width and height that are not a power of 2 but can then scale the input image thus filter it.

An example of handling this can be viewed here (a iPhone - OpenGLES - project that grabs a screen part non-power of 2 and draws that into a 512x512 texture and rescales the GL_TEXTURE matrix, line 123, instead of doing a manual rescale of the texcoords)

Here is another post mentioning this method.

绻影浮沉 2024-09-06 16:01:05

这里有一个假设:

您的窗口是 800x600(也许您的帧缓冲区也是),但您的客户区不是,因为侧面有窗口装饰。

因此,当您的帧缓冲区被传输到窗口的客户区域时,它的大小会被调整。你能检查一下你的窗口创建代码吗?

Here is an hypothesis:

Your window is 800x600 (and maybe your framebuffer too), but your client area is not, because of the window decoration on the sides.

So your frame-buffer gets resized when being blitted to the client area of your window. Can you check your window creation code ?

蓝眸 2024-09-06 16:01:05
  • 请注意窗口客户区的确切大小。仔细检查它是否符合您的预期,
  • 谨防像素对齐规则。您可能需要将 0.5 添加到 x/y 坐标才能到达像素中心。 DirectX 的说明可以在此处找到 -不过 OpenGL 规则可能有所不同。
  • Beware of exact size of the client area of your window. Double check it is what you expect it to be
  • Beware of pixel alignment rules. You might need to add 0.5 to your x/y coordinates to hit the pixel centers. Description for DirectX can be found here - OpenGL rules may be different, though.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文