1
votes

I have these two shaders i want these two to combine one shader draws texture perfectly and the other draw colors of lines perfectly?

private final String fragmentShaderCode =
        "precision lowp float;              \n"
                +"uniform vec4 uTex0;       \n"
                +"void main(){              \n"
                +" gl_FragColor = uTex0;    \n"
                +"}                         \n";

other one

private final String fCode =
        "precision mediump float;                                    \n"
                +"uniform sampler2D uTex0;                           \n"
                +"uniform vec4 uTex1;                                \n"
                +"varying vec2 v_TexCoordinate;                      \n"
                +"void main(){                                       \n"
                +" gl_FragColor = texture2D(uTex0, v_TexCoordinate); \n"
                +"}                                                  \n";

when i use the first one it will draw line colors perfectly but when i use the second one the line colors will be of the texture color how can i control this? if i use the just second shader result is like this enter image description here

lines are black but in actual i am setting line colors like this through handle

texHandle = GLES20.glGetUniformLocation(sProgram, "uTex0");
GLES20.glUniform4fv(texHandle, 1, LineColor, 0); 

it should draw like this

enter image description here

1

1 Answers

2
votes

You are mixing up two concepts.

Set line color as uniform vec4: Your first shader reads the line color as a 4-element vector. This is perfectly valid, but you should use a better variable name. For example uniform vec4 lineColor;. In your Java code, you then set the uniform value with

lineColorHandle = GLES20.glGetUniformLocation(sProgram, "lineColor");
GLES20.glUniform4fv(lineColorHandle, 1, LineColor, 0);

Set texture unit as uniform sampler2D: Your second shader has uTex0 declared as uniform sampler2D uTex0;. In this case, uTex0 is not a 4-element vector. It is an integer index that tells your shader to which texture unit uTex0 is bound to. You should set the uniform like this:

texHandle = GLES20.glGetUniformLocation(sProgram, "uTex0");
GLES20.glUniform1i(texHandle, 0); // 0 is default texture unit.