0
\$\begingroup\$

I want to use LWJGL to create a shader that all it does is change the color of the given texture. For example I tell it to draw the letter A using a sprite sheet then I can tell the shader to draw the letter in a certain color. How would you do something like this without needed to create different colored letter sprite sheets?

Task for the shader: Simply change all pixels to a certain color in the texture. Input: Color , texture. Output: it draws onto the screen the new colored texture.

How do i accomplish such a thing?

\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

I am using the shaders from the OpenGL ES Book Chapter 9 Sample as base link.

First, I'll need a variable to pass the vextex color to the vertex shader. But I would also need to send this information to the fragment shader with a varying variable. Like this:

attribute vec4 a_color;
varying vec4 v_color;

Then, I'll just pass this information to the fragment shader from my vertex shader:

v_color = a_color;

In the fragment shader, where I would have the line gl_FragColor = texture2D( s_texture, v_texCoord );, I could just do something like:

gl_FragColor = texture2D( s_texture, v_texCoord ) * v_color;

So, if you have a texture with values (1,1,1,1), and a v_color with values (1,0,0,1) (Red), it will result in (1, 0, 0, 1).

\$\endgroup\$
1
\$\begingroup\$

If you are using fixed function OpenGL with GL lights, you need to tell it to modulate the base color (from glColor and lighting equations) with the texture:

glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

Otherwise, just multiply the vertex color by the texture color in the fragment shader.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .