I have a tileset of 8x8 pixel images, and I want to resize them in my game so they'd be double that (16x16 pixels, e.g. turning each pixel into a 2x2 block.) What I'm trying to achieve is a Minecraft-like effect, where you have small pixel images scale to larger blockier pixels.
In Pyglet, the sprite's scale
property blurs the pixels. Is there some other way?
Working Code:
Here's the solution that works (thanks to DMan's persistence and Jimmy's insight):
image = resource.image('tileset.png')
texture = image.get_texture()
gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_NEAREST)
texture.width = 16 # resize from 8x8 to 16x16
texture.height = 16
texture.blit(100, 30) # draw
Answer
At the request of commenters...
Warning to Pyglet professionals: There may be a nice Pyglet way to do this, and this isn't it. It's a nice OpenGL way. You have been warned!
You can do this in OpenGL by first binding the texture, then calling glTexParameteri
or similar varients. You can do this in Pyglet by importing OpenGL:
from pyglet.gl import *
You can then enable GL_TEXTURE_2D
to set the target. This is not always needed, but I'm keeping this complete for some other bindings.
glEnable(GL_TEXTURE_2D)
In Pyglet, it seems like you don't have to bind the texture either. Sometimes you have to load in your image, get the texture id, then use glBindTexture(GL_TEXTURE_2D, texture.id)
. My assumption that these are already the state set for you in Pyglet.
The next step is to call glTexParameteri
. Thanks to Jimmy on correcting me on this: the correct call is:
gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_NEAREST)
A quick break down: the first is just to set the target so OpenGL knows what you're talking about, then the second parameter is the type of filter you would like to set. MAG_FILTER
is correct because it controls the magnifications of textures. The final parameter is GL_NEAREST
, which controls how the texture is scaled. GL_NEAREST
is basically a fast scale that gives pixel-styled textures. GL_LINEAR
is normal and will instead smoothly blur your texture.
This version of OpenGL I'm talking about (about < OpenGL 3.2 officially) uses immediate mode, which sets the state. Now that the state is set, you can scale your texture. I can see no better than to show the code that Renold himself posted as working code.
image = resource.image('tileset.png')
texture = image.get_texture()
gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_NEAREST)
texture.width = 16 # resize from 8x8 to 16x16
texture.height = 16
texture.blit(100, 30) # draw
No comments:
Post a Comment