views:

969

answers:

5

I am making an old-school 2d game, and I want to animate a specific color in my texture.

Only ways I know are:

  1. opengl shaders.
  2. animating one color channel only.
  3. white texture under the color-animated texture.

But I don't want to use shaders, I want to make this game as simple as possible, not many extra openGL functions etc..

And the color channel animating doesnt fit this because I need all color channels in my textures.

Currently I am doing it with 2 textures: white texture under the other texture, translated the specific pixel color into transparent, then I change white texture color with glColor3f() function to what ever I want, and I see the "palet animation" on that specific color.

But that style sounds pretty hacky, so I am wondering if there is some better trick to this?

A: 

You may also use the stencil buffer to do something equivalent. Write into the stencil buffer at the positions where your animation should occur (= the positions that should have the animated color). Then render the box with the normal texture only on non-set positions of the stencil buffer using the corresponding stencil op. After that, render the animated color into the resp. positions using different stencil op.

I don't know if this really would be an improvement - depends also on in which way you have the data on your hands - but is probably less "hackish".

ypnos
+1  A: 

How about just using paletted textures? There are extensions to do just that. If using extensions is out of question you can just make palette handling by your own. Just do your palette tricks, there are lot of them, and just write RGB texture using palette. Ofcourse this limits number of colors, but thats whole point of using palette.

Paint programs that are good for palette handling are nowadays rare. That's why I will not remove Deluxe Paint from my drive.

Virne
ahh, Deluxe Paint!
aib
+2  A: 

While I am unfamiliar with the palette texture extension I still recommend using a fragment shader for this sort of effect. It is almost trivial to do a color-key replacement with a shader, versus the other methods you mentioned, and will be way faster than writing the palette functionality yourself.

Here's an example GLSL fragment shader that would replace the color white in a texture for whatever color is passed in.

uniform vec4 fvReplaceColor;
uniform sampler2D baseMap;
varying vec2 Texcoord;

void main( void )
{
     vec4  fvBaseColor = texture2D( baseMap, Texcoord);

     if(fvBaseColor == vec4(1.0, 1.0, 1.0, 1.0))
          fvBaseColor = fvReplaceColor;

     gl_FragColor = fvBaseColor;
}

Yes, it does take a little bit extra to set up shader, but but what it sounds like you are trying to do I feel it's the best approach.

Brandorf
+1  A: 

If you want your game to work on modern hardware the fragment shaders are pretty much your only option since paletted textures are deprecated and may not be available on newer hardware.

The solution you have right now sounds reasonable, especially since it will probably work everywhere.

Spudd86
A: 

For a 2D game, I think it's better to use ARB_vertex_program extension than GLSL. There are still OpenGL drivers out there that do not support GLSL, but support ARB_fragment_program. Support for paletted textures is nowadays non-existent, but pre-shader-age video cards may support it. See the links for statistics.

The old ARB language is a low-level assembly language, but you can use NVidia's high level Cg language to compile shaders to this format. Despite it being a NVidia technology, it works for all GPUs. That said, if you are programming the game for your own amusement, it may not be worth the trouble.

ahnurmi