Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenGL Palette Swap shader?

I'm coding a fighting game in which each character has a great number of sprites, and each character has several different palette swaps. I've been trying to use a grayscale texture for each sprite, and a 256x1 texture for each palette.

My shaders look like this:

Vertex:

varying vec2 coord;
void main(void)
{
    coord = gl_MultiTexCoord0.xy;
    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}

Fragment:

uniform sampler2D palette;
uniform sampler2D texture;
varying vec2 coord;
void main()
{
    gl_FragColor = texture2D(palette, vec2(texture2D(texture, coord).r, 0));
}

"palette" is set to 1 and "texture" is set to 0 with glGetUniformLocation and glUniform1i. They compile fine and link fine. I'm able to use the program object in my draw code.

The problem is, I have no idea how to use multitexturing to get the "palette" and "texture" textures cooperating with each other in the shader. This is my first time using multitexturing, and I can't seem to figure out how to get it to work for this kind of solution. Does anyone know how, with these shaders (if they are correct) to simulate a palette swap?

EDIT: Here's my drawing code right now:

glUseProgram(paletteProgram);
glActiveTexture(GL_TEXTURE1);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, palette);
glActiveTexture(GL_TEXTURE0);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture);

glBegin(GL_QUADS);
glTexCoord2f(w1, h1);
glVertex3f(x1, y1, 0);
glTexCoord2f(w1, h2);
glVertex3f(x1, y2, 0);
glTexCoord2f(w2, h2);
glVertex3f(x2, y2, 0);
glTexCoord2f(w2, h1);
glVertex3f(x2, y1, 0);
glEnd();

glUseProgram(0);
like image 323
Mathew Velasquez Avatar asked Dec 05 '25 15:12

Mathew Velasquez


1 Answers

Your logic seems sound. What you need to do is to set the active texture using glActiveTexture and then bind your texture as usual (for texture units 0 and 1). Now also bind the values 0 to the uniform called texture and 1 to the uniform called palette.

Inside GLSL, you can then use texture2D to fetch texels from the appropriate sampler.

Hope this helps!

This is actually explained in detail over here in this NeHe tutorial (section: Using textures with GLSL).

This is what you need to do

    glUseProgramObject(my_program);
    int my_sampler_uniform_location = glGetUniformLocation(my_program, my_color_texture);

    glActiveTexture(GL_TEXTURE0 + i);
    glBindTexture(GL_TEXTURE_2D, my_texture_object);

    glUniform1i(my_sampler_uniform_location, i);
like image 152
Ani Avatar answered Dec 07 '25 23:12

Ani