palette swapping?

Started by
3 comments, last by RealmRPGer 17 years, 10 months ago
I want to do palette swapping in openGL, similar to the way MegaMan changes his colors when he switches weapons. When doing research, I discovered that the extensions for this (such as GL_EXT_paletted_texture ) are no longer supported in the newer graphics cards. I saw somewhere that said to try making a 1D texture with the palette entries and using gltexcoord1 to set the indices, but I can't figure out how that's supposed to work. Does anybody know if that DOES work (and how to use it. I mean, doesn't it also require a 2D texture for the sprite? Or can 1D and 2D textures be stacked? Even in that case, I don't see how setting the tex coord can define a palette entry), or if it doesn't, does anybody know of a simple workaround?
Advertisement
I think the method you are referring to is along the lines of having the pallet in a 1D texture and your sprite in a 2D texture. The sprite (2D) contains indexes into the pallet (instead of colors directly). So when rendering the sprite the result would be 'pixel_color = pallet[index_from_2d_texture + offset(maybe)]'.

I know this can easily be accomplished with shaders but don't know how to go about doing it using the fixed function pipeline.

So... Do I just use it like normal, then? As in:

glBindTexture(GL_TEXTURE_1D,palette);glBindTexture(GL_TEXTURE_2D,texture_with_palette_entries);glBegin(GL_QUADS);glTexCoord2i(0, 0); glVertex3i(x,y,0);glTexCoord2i(1, 0); glVertex3i(x+size.x, y,0);glTexCoord2i(1, 1); glVertex3i(x+size.x,y+size.y,0);glTexCoord2i(0, 1); glVertex3i(x,y+size.y,0);glEnd();


I guess a part of the problem is I don't know exactly how 1D textures work.
Quote:Original post by RealmRPGer
So... Do I just use it like normal, then? As in:

No, that won't work, you'll just get see the 2d texture (because you've bound it over the top and pushed the 1d texture out).

1D textures work just like 2D ones, except that they are always 1xFoo in size and you only set a single texture coord for them. You can even be lazy and load a 1xFoo texture as a 2d texture and it'll work just like normal.

For palettes you're going to either have to use EXT_paletted_texture (which like you say is not available on newer cards) or write a fragment shader to do the equivilent work. The shader would look something like:

- sample current sprite colour
- convert colour into palette index
- use palette index to look up and sample the correct final colour from your 1d texture.

If you imagine your sprite as greyscale (2d texture) and your pallete as a horizontal strip of colours (1d texture), then full white maps to the far left of the palette (and full black maps to the far right). Once you've got this working then switching your palette is simply a matter of changing the palette texture.
Ohhhhhh...

But, how do you create shaders in OpenGL? Perhaps there's a tutorial somewhere, because I don't know how to do any of that. @-@
BUT, it does make sense, I just don't know the syntax. =)

This topic is closed to new replies.

Advertisement