Imagination PowerVR SDK Blog

Pixel-perfect texturing with GL_NEAREST


#1

Hi,

I’m trying to represent a sprite from a texture atlas using a quad with a 1:1 texel-to-pixel mapping, using GL_NEAREST as the filter. I’m getting occasional artifacts depending on where the sprites are being positioned (I am only using integer coordinates).

I’ve found all kinds of contradictory advice online, ranging from having to offset the vertices being drawn by 0.5, to having to offset and/or scale the texels being drawn by various amounts. Nothing seems to produce consistently accurate results.

What is the correct method in OpenGL ES to get a 1:1 texel-to-pixel mapping of a sub-rectangle of a texture onto a quad of matching dimensions, when using GL_NEAREST?

Thanks,

Dan.




#2

Hi Dan.


What artefacts are you getting exactly? I assume you mean that you’re getting sub-pixel results? Or are you getting borders around the sprites?


I also assume that your orthographic projection matrix matches the dimensions of your viewport?





If you have a screenshot of the artefact it may help us diagnose the issue.