Welcome, Guest. Please login or register. Did you miss your activation email?

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Topics - Schizm

Pages: [1]
1
General / Palette shader problem
« on: April 24, 2012, 12:27:45 am »
Well, it's not really related to SFML, but I see that's common thing in General forum :)

I'm writing some game engine (in fact, reverse-engineering one old game).
I need to be able to load an image (it's in game's internal format, uses palette and per-pixel alpha) and then swap it's palette at rendering time; First I used to load full copy of the image for each needed palette, but that seems to consume too much memory (over 70mb per sprite, nearly 200 different possible sprites/palettes, all on the same game level). So then I left old solution as fallback if shaders are not supported and started writing a shader that will apply a palette to my image, so memory usage can be reduced to ~10mb or even ~5mb per sprite (~100 images).

Now, look at this screenshot:

As you probably can notice, the colors are a bit off their real index, but I'm not sure why it happened.

This is how it's supposed to look like (with shaders disabled):


The image loading code is all ok (see second screenshot). Problem should be in the shader code, but I'm not really sure where is it.

Fragment shader code:
#extension GL_ARB_texture_rectangle : enable

uniform sampler2DRect rec1;
uniform sampler1D palette;

void main(void)
{
        vec4 oldcol = texture2DRect(rec1, gl_TexCoord[0].st);
        float index = (oldcol.r + oldcol.g + oldcol.b) / 3;
        vec4 newcol = texture1D(palette, index);

        gl_FragColor.rgb = newcol.rgb * gl_Color.rgb;
        gl_FragColor.a = oldcol.a * gl_Color.a;
}

rec1 = GL_TEXTURE_RECTANGLE_ARB texture containing palette index in (r,g,b) and per-pixel alpha.
palette = GL_TEXTURE_1D texture, 256 texels wide, containing palette colors.

I'm really stuck here, please help :(

Pages: [1]