Pixel perfect sprite rendering

60 views
Skip to first unread message

Mike Wyatt

unread,
Oct 27, 2010, 11:04:44 PM10/27/10
to pyglet...@googlegroups.com
I'm rendering a hex map with a batched set of sprites (one per cell).  The batch looks pretty good, but on close inspection I can see that the pixels are not rendered exactly as the source image.

Here's a image with the original image and a small cropped view of a few rendered sprites in the hex grid:  http://img830.imageshack.us/img830/812/hexes.png

I'm guessing I either need to adjust the modelview transformation or change some gl* setting.  I've played around with GL_TEXTURE_MIN_FILTER and GL_TEXTURE_MAG_FILTER and glHint, but haven't had any luck.

How can I fix this?

Tristam MacDonald

unread,
Oct 27, 2010, 11:31:08 PM10/27/10
to pyglet...@googlegroups.com
On Wed, Oct 27, 2010 at 11:04 PM, Mike Wyatt <mikejo...@gmail.com> wrote:
I'm rendering a hex map with a batched set of sprites (one per cell).  The batch looks pretty good, but on close inspection I can see that the pixels are not rendered exactly as the source image.

Here's a image with the original image and a small cropped view of a few rendered sprites in the hex grid:  http://img830.imageshack.us/img830/812/hexes.png

Are you sure you didn't create a multi-sample context and request anti-aliasing? That looks a bit like it could be anti-aliasing on the polygon edges.

Also make sure that you disable both GL_LINE_SMOOTH and GL_POLYGON_SMOOTH, just in case something else had enabled them.

 
I'm guessing I either need to adjust the modelview transformation or change some gl* setting.  I've played around with GL_TEXTURE_MIN_FILTER and GL_TEXTURE_MAG_FILTER and glHint, but haven't had any luck.

For testing, set both your MIN and MAG filters to GL_NEAREST, to prevent any chance of bilinear interpolation. 

How can I fix this?

If none of the above did the trick, then double check your math - you might have introduced rounding error somewhere...

--
Tristam MacDonald
http://swiftcoder.wordpress.com/

Mike Wyatt

unread,
Oct 28, 2010, 12:16:32 AM10/28/10
to pyglet...@googlegroups.com
Thanks, Tristam.  I tried all that, and double-checked my math, but it still looked a little off.

I was just able to fix it, though.  The problem was in my cocos2d initialization.  I apologize for not mentioning I was using that library in my original post.  I started looking at the cocos2d director code after exhausting the other options, and realized it was scaling the projection matrix.  I added `do_not_scale=True` to the director instantiation, and the sprites are now being drawn without any "blurriness".

From: Tristam MacDonald <swiftco...@gmail.com>
Date: Oct 27, 10:31 pm
Subject: Pixel perfect sprite rendering
To: pyglet-users


On Wed, Oct 27, 2010 at 11:04 PM, Mike Wyatt
Reply all
Reply to author
Forward
0 new messages