I want to change the pixel color of a texture. I don't know where to start and what to add. Really need help, I only find useless snippets in the internet.

enter image description here

My current Code:

public void onSurfaceCreated(GL10 gl, EGLConfig config) {
GLES20.glAttachShader(riGraphicTools.sp_SolidColor, vertexShader);
GLES20.glAttachShader(riGraphicTools.sp_SolidColor, fragmentShader);

vertexShader = riGraphicTools.loadShader(GLES20.GL_VERTEX_SHADER, riGraphicTools.vs_Image);
fragmentShader = riGraphicTools.loadShader(GLES20.GL_FRAGMENT_SHADER, riGraphicTools.test_Image);

riGraphicTools.sp_Image = GLES20.glCreateProgram();            
GLES20.glAttachShader(riGraphicTools.sp_Image, fragmentShader);

The riGraphicTools Class:

public class riGraphicTools {

public static final String test_Image =
        "precision mediump float;" +
        "varying vec2 v_texCoord;" +
        "uniform sampler2D s_texture;" +
        "void main() {" +
        "  gl_FragColor = texture2D( s_texture, v_texCoord );" +

public static final String vs_Image =
    "uniform mat4 uMVPMatrix;" +
    "attribute vec4 vPosition;" +
    "attribute vec2 a_texCoord;" +
    "varying vec2 v_texCoord;" +
    "void main() {" +
    "  gl_Position = uMVPMatrix * vPosition;" +
    "  v_texCoord = a_texCoord;" +
  • \$\begingroup\$ What exactly is the objective here? In the sample image you've posted, it seems that you have applied a grayscale filter to that yellow square. Is this the objective? Or is it just to test the color of a given pixel and change it by some other color, depending on the result? \$\endgroup\$
    – glampert
    Commented Jun 18, 2014 at 3:16

1 Answer 1


The texture2D() function returns a vec4 containing the rgba values of that texel. You can then manipulate this just like any other vec4. For example, to make everything darker, you can do...

gl_FragColor = texture2D( s_texture, v_texCoord ) * vec4(0.5, 0.5, 0.5, 1);

The trick is the fragment shader runs on every fragment in whatever you're drawing, and choosing a single texel to manipulate is generally unreasonable. If that's what you want to do, I recommend doing so outside of the shader, perhaps with glTexSubImage2D.

  • \$\begingroup\$ Ty for you answer. So I am giving texture2D the image(s_texture) and the coordinates of the current pixel (v_texCoord), and texture2D gives me the color value of that pixel? If yes, how can I see what s_texture, v_texCoord and texture2D is? Where do I have to enter glTexSubImage2D in that Code? What I have to enter as last Parameter 'data'? What is glTexSubImage2D exactly doing? :) \$\endgroup\$
    – csnewb
    Commented Jun 17, 2014 at 6:14
  • \$\begingroup\$ glTexSubImage2D is exactly like glTexImage2D, except it allows you to only modify a portion of the texture instead of the whole thing. Also, you can only use glTexSubImage2D after using glTexImage2D on that texture at least once. For the rest of your questions, google will help you a lot. \$\endgroup\$ Commented Jun 17, 2014 at 13:06

You must log in to answer this question.

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