png 图像加载到纹理时模糊
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
OpenGL(通常)要求纹理本身的大小为 2 的幂,因此(可能)发生的情况是您的纹理被缩放到尺寸为 2 的幂的大小,然后 它被缩小到原始大小——在缩放两次的过程中,你会失去一些质量。
显然,您只想显示位图而不进行任何缩放,而不将其包装到另一个对象的表面或类似的东西(即任何纹理的用途)。既然如此,我只是将其显示为位图,而不是纹理(例如,请参见
glRasterPos2i
和glBitmap
)。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
andglBitmap
).为什么您首先要对开始屏幕上的静态图像使用 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 yourGL_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 andGL_NEAREST
(orGL_LINEAR
, whichever gives you the best result).我建议你让 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.
这里有一个假设:
您的窗口是 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 ?