Use a Pixel Shader or Make More Textures?

Started by
12 comments, last by Cornstalks 12 years, 5 months ago
I'm currently working on a 2D game, using OpenGL for my graphics library. I've never used shaders before, but I'm wondering if a pixel/fragment (or some other type) shader would be the optimal solution to one of my needs. It's a 2D sprite based game, where for the most part the sprites will be high quality. However, I want to have a special effect for various sprites, where a sprite "transforms" to and from its high quality version to a low-quality/pixelated version.

For example, you're walking a long, and then two enemies "apparate" in front of you. I want the enemies to appear in a very, very pixelated manner at first, but then to increase in quality (become less pixelated) over the course of a second or so, so that after the apparation animation sequence the enemies are now in full quality. Or I want to be able to do the reverse if the two enemies "disapparate".

One solution I've come up with is to generate "pixelated" sprite sheets. So for every frame in every animation sequence, I generate a series of pixelated versions (from low pixelation to high pixelation), so that during any animation sequence I can adjust the "pixelated" appearance of the object/character just by adjusting the texture coordinates of the sprite.

Is this the best option, or is it possible to use a shader to do this?



Sorry for using Harry Potter terms, I just watched the movie last night and can't think of a better word than "apparate."
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
Advertisement
usually you create textures already with pixelated versions, called "mipmaps", those are usually selected automatically by the GPU, in a 2d game this probably makes no sense, as your pixel/texel ration is probably 1:1.

You could use http://www.opengl.org/registry/specs/EXT/texture_lod_bias.txt to select a different mipmap level, the GPU would blend between mipmap levels for you.




Turn mip-mapping on first. Which builds your sprites down to a 1x1 pixel (the most pixelated in your case).

With glTexParameter() you can set the GL_TEXTURE_MAX_LOD, (or maybe it will be the min) and you can select to use the 1x1 version, or 2x2 version 4x4, all the way up to level 0 (which will be your original image).
http://www.opengl.org/sdk/docs/man/xhtml/glTexParameter.xml

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal

you won

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal

I like the idea of using mipmapping to do it, basically force the LoD bias to select a low resolution image. Also make sure to change your filtering mode to NEAREST_MIPMAP_NEAREST.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
Wow, you guys are awesome! I thought about mipmaps but wasn't sure if I could force select them or if I could set the filter mode to nearest. Thanks! That'll save quite a bit of memory and time.
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
if by "transform" you mean a blending between both quality levels, I'd recommend GL_NEAREST_MIPMAP_LINEAR, you would have no filtering on the mipmap levels, but blending between levels.
What exactly do you mean by blending between levels? I would prefer to have "hard" transitions, where each frame is only displaying one mipmap level (and not a blending between two mipmap levels). Would the NEAREST flag give this result?
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
just remember that doing it by having the gpu automatically generate mipmap's takes away any control that a graphics artist might be able to "touch-up" the pix-elated animation, or do any special animation's.
Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.

What exactly do you mean by blending between levels? I would prefer to have "hard" transitions, where each frame is only displaying one mipmap level (and not a blending between two mipmap levels). Would the NEAREST flag give this result?


yes. GL_NEAREST_MIPMAP_NEAREST will switch hard between two versions, I thought when you said 'where a sprite "transforms" to and from its high quality version to a low-quality/pixelated version;, you meant you want to blend between two levels.

This topic is closed to new replies.

Advertisement