Vitali Grabovski
Vitali Grabovski

Reputation: 183

How to pass texture through two shaders?

All I need is to pass texture through fragment shader 1, get the result and pass it to fragment shader 2.

I know how to link vertex and fragment shader together into a program and get the shader object.

I don't know how to get the result of shader 1, switch the shaders (GLES20.glUseProgram ?) and pass the result of shader 1 to shader 2.

Any ideas how to do it?

UPDATE This is an example what I want to achive

Effect 1:

enter image description here

Effect 2:

enter image description here

My goal is to combine Effect 1 and Effect 2. UPDATE 2

effect 2 function:

...    
    uniform float effect2;

    vec2 getEffect_() {
        float mType = effect2;
        vec2 newCoordinate = vec2(textureCoordinate.x, textureCoordinate.y);
        vec2 res = vec2(textureCoordinate.x, textureCoordinate.y);

        //case 1
        if(mType==3.5) {
            if (newCoordinate.x > 0.5) {
                res = vec2(1.25 - newCoordinate.x, newCoordinate.y); }
        }
        else
        //case 2
        ...

        return res;
    }
...

Upvotes: 0

Views: 857

Answers (1)

Sung
Sung

Reputation: 1066

If you want to pass the result as a texture to another shader, you should use RTT(render to texture) so that you can get a texture to pass to another shader.

Yes, you should use glUseProgram(name) to switch another shader but not only this, you should render it at the original FBO(now you use)

  1. make one FBO to make the result as a texture.
  2. render to texture using the first shader then you can get a texture
  3. Draw the texture with the second shader at the main fbo(now you use)

If you just want to combine two effect, just combine the two fragment shaders.

//At the end of the second frag shader
// skip this below
//gl_FragColor = result;
// put these codes 
 float grayScale = dot(result.rgb, vec3(0.299, 0.587, 0.114));
 gl_FragColor = vec4(grayScale, grayScale, grayScale, 1.0);

Only use one shader program with the second effect fragment shader.

I will assume you don't need to show those 30 effects at once.

  1. define uniform float effect2 in the 10 fragments like effect1.
  2. pass the effect2 like 0.5, 1.5 or 2.5
  3. according to the value you pass, differently mix the effect. For example,

    if(effec2>2.0) {
            float grayScale = dot(result.rgb, vec3(0.299, 0.587, 0.114));
             gl_FragColor = vec4(grayScale, grayScale, grayScale, 1.0); 
        } else if(effect2>1.0) {
            vec3 final_result_of_effec2_2 = fun_2(result.rgb);
             gl_FragColor = vec4(final_result_of_effec2_2, 1.0); 
        } else {
              vec3 final_result_of_effec2_3 = fun_3(result.rgb);
             gl_FragColor = vec4(final_result_of_effec2_3, 1.0); 
        }
    

Upvotes: 1

Related Questions