Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 25 Oct 2011
Offline Last Active Jul 28 2012 04:43 PM

Posts I've Made

In Topic: [Solved] Applying Color Palette to Texture

11 July 2012 - 04:47 AM

Thanks guys! I've made it work and thought I'd post the relevant code here to help anyone else trying to do this.

So I did basically exactly what mhagain suggested. I load both my sprite and palette as textures (both 2D textures for now) and then I have a fragment shader which gets the brightness value from my sprite and returns a color from the palette based on that value.

Fragment Shader:
uniform sampler2D texture;
uniform sampler2D palette;
void main()
vec4 textureColor = texture2D(texture, gl_TexCoord[0].xy).rgba;
vec3 paletteColor = texture2D(palette, vec2(textureColor.r, 0)).rgb;
gl_FragColor = vec4(paletteColor.r, paletteColor.g, paletteColor.b, textureColor.a);

In the above code only the red value of the texture color is used to look up the color in the palette. The reason for this is that the sprite is greyscaled, meaning that the R,G and B values are equal, and I felt it would be a waste to calculate the average (brightness) since it would give the same value. The gl_TexCoord[0] is set in the vertex shader which is quite simple as well.

Vertex Shader:
void main() {
gl_Position = ftransform();
gl_TexCoord[0] = gl_MultiTexCoord0;

Lastly I'll just include the Java code for actually using these shaders (excluding the loading/compiling part):

   int paletteLocation = GL20.glGetUniformLocation(shaderProgram, "palette");
   GL20.glUniform1i(paletteLocation, 1);
   GL11.glBindTexture(GL11.GL_TEXTURE_2D, palette.getTextureID());

   int textureLocation = GL20.glGetUniformLocation(shaderProgram, "texture");
   GL20.glUniform1i(textureLocation, 0);
   GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture.getTextureID());

   GL11.glTexCoord2f(0, 0);
   GL11.glVertex2f(0, 0);
   GL11.glTexCoord2f(1, 0);
   GL11.glVertex2f(64, 0);
   GL11.glTexCoord2f(1, 1);
   GL11.glVertex2f(64, 128);
   GL11.glTexCoord2f(0, 1);
   GL11.glVertex2f(0, 128);

Just to summarize the above code: I first bind the palette texture to GL_TEXTURE1 and enable the shader to reference it using the variable name "palette". Then I do the same thing for the sprite texture except I use GL_TEXTURE0 and the variable name "texture". I then simply draw a quad with the sprite texture in immediate mode.

So thanks again for the help and I hope that this helps anyone else with a similar issue. Posted Image

In Topic: [Solved] Spritesheet Bleeding

06 July 2012 - 01:26 PM

Thanks for the help guys. I've solved the problem. Before posting the OP I tried doing as Koehler mentioned:


This didn't work and I couldn't understand why. After reading Koehler's post I realized that it might be because I'm using the TextureLoader from the Slick2D library (I didn't want to write my own texture loader).
I found the API again and saw that one of the overloads of the getTexture method had a filter input, so the solution to the problem was changing the code in my TextureStore class from:

texture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream(texturesPath + textureRef));


texture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream(texturesPath + textureRef), GL11.GL_NEAREST);

Hope this helps anyone who is having similar issues. Also I feel this is a cleaner solution than having to modify all your texture files to have a 1 pixel border.

Thanks again! Posted Image

In Topic: Java - References

25 October 2011 - 07:37 AM

Wow, I feel foolish now. I just realized that the error was not due to reference issues but something else entirely.
I just assumed that the object wasn't being passed as it should and found several websites saying that java was pass-by-value only which seemed to make sense to me based on the results I was getting.
However I just debugged it and it is definitely doing what it is supposed to do. I'm just handling the result badly.

Anyway, I suppose this thread is now just a waste of space. Thank you all for helping me realize my mistake, I really do appreciate it. :)