OpenGL sprite半像素位置偏移 [英] OpenGL sprite half pixel position offset

查看:140
本文介绍了OpenGL sprite半像素位置偏移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我注意到,即使我创建纹理的宽度和高度的像素没有小数部分,他们似乎有点模糊。经过几个小时的调试,我发现,问题出在偏移。
如果我提供没有小数部分的偏移量,我得到:

Recently I noticed that even though I create textures with width and height in pixels without fractional part, they seem a little blurry. After hours of debugging I found, that the problem lies in offsets. If I provide offsets without fractional part I get this:

但是如果我用小数部分添加偏移量,我得到:

But if I add offsets with fractional part, I get:

请以全尺寸查看这些图片,否则您不会注意到问题:)

我有这种设置纹理的方法:

I have this method for setting texture:

bool WindowsGraphicsManager::setTexture(
    UINT32& id, UINT8* image, UINT32 width, UINT32 height,
    bool wrapURepeat, bool wrapVRepeat, bool useMipmaps, int textureType)
{
    glGenTextures(1, &id);
    bindTexture(id);
    int type = GL_RGBA;
    if (textureType == Texture::MONO) {
        type = GL_ALPHA;
        glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    }
    glTexImage2D(
        GL_TEXTURE_2D, 0, type,
        width, height, 0, type,
        GL_UNSIGNED_BYTE, image);
    int minFilter = GL_LINEAR;
    if (useMipmaps) {
        glGenerateMipmap(GL_TEXTURE_2D);
        minFilter = GL_LINEAR_MIPMAP_LINEAR;
    }
    glTexParameteri(GL_TEXTURE_2D,
        GL_TEXTURE_MIN_FILTER, minFilter);
    glTexParameteri(GL_TEXTURE_2D,
        GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    int wrap_u = wrapURepeat ?
        GL_REPEAT : GL_CLAMP_TO_EDGE;
    int wrap_v = wrapVRepeat ?
        GL_REPEAT : GL_CLAMP_TO_EDGE;
    glTexParameteri(GL_TEXTURE_2D,
        GL_TEXTURE_WRAP_S, wrap_u);
    glTexParameteri(GL_TEXTURE_2D,
        GL_TEXTURE_WRAP_T, wrap_v);
    return !checkGLError("Loading texture.");
}



我知道有点模糊,但这似乎也太很多,是太可怕了。
我能做什么来解决这个问题,或者至少我应该在哪里寻找这个问题?着色器?矩阵?模型?质地?渲染设置?

I know that a little bit of bluriness is expected, but this seems like too much and is way too noticable. What could I do to solve this, or at least where should I look for the problem? Shader? Matrix? Model? Texture? Render settings?

推荐答案

它与您的实际纹理无关。只需确保在渲染之前舍入你的坐标。你在这里看到的(加上奇怪的拉伸)是舍入错误。

It has nothing to do with your actual texture. Just make sure you round your coordinates before rendering. What you're seeing here (together with the weird looking stretching) are rounding errors.

这篇关于OpenGL sprite半像素位置偏移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆