Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone OpenGL ES 2.0 - Pixel Perfect Textures

How can I get my textures to align with the screen pixels for pixel perfect graphics in OpenGL ES 2.0? This is critical for a project I'm working on which uses pixel art graphics. Any help on this would be great!

like image 374
Marc Avatar asked Sep 01 '11 15:09

Marc


2 Answers

See my answer here: OpenGL Texture Coordinates in Pixel Space


This has been asked a few times, but I don't have the links at hand, so a quick and rough explanation. Let's say the texture is 8 pixels wide:

 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
 ^   ^   ^   ^   ^   ^   ^   ^   ^
0.0  |   |   |   |   |   |   |  1.0
 |   |   |   |   |   |   |   |   |
0/8 1/8 2/8 3/8 4/8 5/8 6/8 7/8 8/8

The digits denote the texture's pixels, the bars the edges of the texture and in case of nearest filtering the border between pixels. You however want to hit the pixels' centers. So you're interested in the texture coordinates

(0/8 + 1/8)/2 = 1 / (2 * 8)

(1/8 + 2/8)/2 = 3 / (2 * 8)

...

(7/8 + 8/8)/2 = 15 / (2 * 8)

Or more generally for pixel i in a N wide texture the proper texture coordinate is

(2i + 1)/(2N)

However if you want to perfectly align your texture with the screen pixels, remember that what you specify as coordinates are not a quad's pixels, but edges, which, depending on projection may align with screen pixel edges, not centers, thus may require other texture coordinates.

like image 79
datenwolf Avatar answered Sep 28 '22 08:09

datenwolf


This link may be usefull:

http://glprogramming.com/red/appendixg.html#name1

Look at "If exact two-dimensional rasterization is desired...". This OpenGl tip tells you how to best setup your ortho matrix for 2D.

Also, you may need to disable any kind of texture filtering.

like image 45
neodelphi Avatar answered Sep 28 '22 09:09

neodelphi