opengl纹理映射偏离5-8像素 [英] opengl texture mapping off by 5-8 pixels

查看:215
本文介绍了opengl纹理映射偏离5-8像素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在纹理贴图/精灵表中紧挨着一堆缩略图/图标.从像素到像素的关系,这些像素的比例从145像素平方增加到238屏幕像素平方.我希望访问纹理坐标时框的边缘上的精度为+ -1或2像素,因此我还在缩略图上方绘制了4像素的轮廓以隐藏此可能的伪像.但是,我看到了准确性方面的巨大差异.有时它朝一个方向关闭,有时又朝另一个方向移动.

I've got a bunch of thumbnails/icons packed right up next to each other in a texture map / sprite sheet. From a pixel to pixel relationship, these are being scaled up from being 145 pixels square to 238 screen pixels square. I was expecting to get +-1 or 2 pixel accuracy on the edges of the box when accessing the texture coordinates, so I'm also drawing a 4 pixel outline overtop of the thumbnail to hide this probable artifact. But I'm seeing huge variations in accuracy. Sometimes it's off in one direction, sometimes the other.

我已经检查过数学,无法弄清楚到底发生了什么.

I've checked over the math and I can't figure out what's happening.

缩略图将被放大约1.64倍.因此,源纹理坐标中的单个像素偏移可能会导致屏幕上的像素偏移约2个像素.顶部的4像素白框以1-1像素的比例绘制关系,并且应该在框的边缘的任一侧覆盖约2像素.那部分正在工作.在这里,我关闭了边框以显示纹理坐标距离....

The the thumbnail is being scaled up about 1.64 times. So a single pixel off in the source texture coordinate could result in around 2 pixels off on the screen. The 4 pixel white frame over top is being drawn at a 1-1 pixel to fragment relationship and is supposed to cover about 2 pixels on either side of the edge of the box. That part is working. Here I've turned off the border to show how far off the texture coordinates are....

我可以手动调整数字以使其消失.但是我必须将纹理坐标的宽度/高度缩小几个源像素,并且在某些情况下,要向起点添加(或减去)5或6个像素.我真的只是想算出数学或弄清楚我在这里做错了什么.这种东西使我发疯!

I can tweak the numbers manually to make it go away. But I have to shrink the texture coordinate width/height by several source pixels and in some cases add (or subtract) 5 or 6 pixels to the starting point. I really just want the math to work out or to figure out what I'm doing wrong here. This sort of stuff drives me nuts!

一堆废话要知道.

  • 着色器正在顶点着色器中进行纹理坐标偏移...

  • The shader is doing the texture coordinate offsetting in the vertex shader...

v_fragmentTexCoord0 = vec2((a_vertexTexCoord0.x * u_texScale) + u_texOffset.s, (a_vertexTexCoord0.y * u_texScale) + u_texOffset.t);
gl_Position = u_modelViewProjectionMatrix * vec4(a_vertexPosition,1.0);

  • 此对象是一个具有2个tris的三角形带的框.

  • This object is a box which is a triangle strip with 2 tris.

    没关系,但是应用于模型的矩阵没有进行任何缩放.该框用于屏幕缩放.缩放仅在提供的纹理坐标中发生.

    Not that it should matter, but matrix applied to the model isn't doing any scaling. The box is to screen scale. The scaling is happening only in the texture coordinates that are being supplied.

    如上所示,对象的纹理坐标为0.00-0.07,然后在着色器中添加了一个偏移量,该偏移量对于每个缩略图是不同的. 2048中的.07就像143.最初我在.0708拥有它,应该更接近145,这更糟,并且从纹理中显示出更多像148像素.要使其仅显示145个源像素,我必须将其设置为140个像素.0.06835.

    The texture coordinates of the object as seen above are 0.00 - 0.07, then in the shader have an addition of an offset amount which is different per thumbnail. .07 out of 2048 is like 143. Originally I had it at .0708 which should be closer to 145 it was worse and showed more like 148 pixels from the texture. To get it to only show 145 source pixels I have to make it .0.06835 which is 140 pixels.

    我尝试在计算器中进行数学运算并直接输入数字.我也尝试过像= 1305/2048.这些进入GLfloats不会翻倍.

    I've tried doing the math in a calculator and typing in the numbers directly. I've also tried doing like =1305/2048. These are going in to GLfloats not doubles.

    此纹理贴图图像为PNG,并加载了以下设置:

    This texture map image is PNG and is loaded with these settings:

    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
    
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
    

  • 但是我也尝试了GL_LINEAR,没有明显的区别.

    but I've also tried GL_LINEAR with no apparent difference.

    • 在没有进行纹理缩放的其他纹理(在同一纹理贴图中)上,我没有任何精度问题.

    • I'm not having any accuracy problems on other textures (in the same texture map) where I'm not doing the texture scaling.

    随着坐标的升高,距离不会越来越远.在NEG MAP拇指上方的图像中,NEG MAP拇指紧靠HEAT MAP拇指,并且朝向不同的方向,但在接缝处正确.

    It doesn't get farther off as the coords get higher. In the image above the NEG MAP thumb is right next to the HEAT MAP thumb and are off in different directions but correct at the seam.

    这是这两个的偏移量数据.

    here's the offset data for those two..

    filterTypes[FT_gradientMap20].thumbTexOffsetS = 0.63720703125;
    filterTypes[FT_gradientMap20].thumbTexOffsetT = 0.1416015625;
    filterTypes[FT_gradientMap21].thumbTexOffsetS = 0.7080078125;
    filterTypes[FT_gradientMap21].thumbTexOffsetT = 0.1416015625;
    

    ====更新====

    ==== UPDATE ====

    我意识到我做错了几件事,并在这里进行了讨论:

    A couple of things off the bat I realized I was doing wrong and are discussed over here: OpenGL Texture Coordinates in Pixel Space

    单个缩略图的宽度为145.但这将是0-144,下一个缩略图以145开头.我使用的宽度为145,因此太大了1个像素.使用上面的像素类型数学中心,我们实际上应该从0的中心转到144的中心.144.5-0.5 = 144.

    The width of a single thumbnail is 145. But that would be 0-144, with 145 starting the next one. I was using a width of 145 so that's going to be 1 pixel too big. Using the above center of pixel type math, we should actually go from the center of 0 to the center of 144. 144.5 - 0.5 = 144.

    使用他的(2i +1)/(2N)公式,我为每个起点设置了新的偏移量,并使用144/2048作为宽度.这使情况变得更好,但在某些方面仍然存在.有时仍然朝一个方向移动,有时又朝另一个方向移动.尽管每个x或y位置都一致.

    Using his formula of (2i + 1)/(2N) I made new offset amounts for each of the starting points and used the 144/2048 as the width. That made things better but still off in some areas. And again still off in one direction sometimes and the other other times. Although consistent for each x or y position.

    使用143的宽度证明效果更好.但是我可以通过手动调整数字来解决所有问题.我想算一算就可以解决问题.

    Using a width of 143 proves better results. But I can fix them all by just adjusting the numbers manually to work. I want to have the math to make it work out right.

    ...或..也许与最小/磁力过滤有关-尽管我阅读了此书,在这种情况下我正在做的事情似乎是正确的.

    ... or.. maybe it has something to do with min/mag filtering - although I read up on that and what I'm doing seems right for this case.

    推荐答案

    经过大量实验并且必须创建网格状的引导纹理,这样我才能确切地看到每种纹理的距离...我终于明白了!

    After a lot of experiments and having to create a grid-lined guide texture so I could see exactly how far off each texture was... I finally got it!

    实际上很简单.

    uniform mat4 u_modelViewProjectionMatrix;
    uniform mediump vec2 u_texOffset;
    uniform mediump float u_texScale;
    attribute vec3 a_vertexPosition;
    attribute mediump vec2 a_vertexTexCoord0;
    

    纹理坐标的精度.通过指定mediap,它就可以自行修复.我怀疑这也将有助于解决我在此问题中遇到的问题:

    The precision of the texture coordinates. By specifying mediump it just fixed itself. I suspect this also would help solve the problem I was having in this question:

    一旦这样做,我就必须回到原来的145宽度(这似乎仍然不对,但是很好).值得的是,我最后回到所有纹理坐标上的所有原始数学知识. 像素中心"方法显示的相邻像素比直线/2048的要多.

    Once I did that, I had to go back to my original 145 width (which still seems wrong but oh well). And for what it's worth I ended up then going back to all my original math on all the texture coordinates. The "center of pixel" method was showing more of the neighboring pixels than the straight /2048 did.

    这篇关于opengl纹理映射偏离5-8像素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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