1
votes

I am trying to convert glsl for opengl to glsl for opengl es.

Here is glsl code that I'm going to translate.

  • For vertex shader

    varying vec2 texcoord0;
    varying vec2 texcoord1;
    varying vec2 texcoord2;
    varying vec2 texcoord3;
    varying vec2 texcoord4;
    varying vec2 texdim0;
    
    varying vec2 texcoordLUT;
    
    uniform float sharpness;
    
    void main()
    {
        gl_Position = ftransform();
    
        texcoord0 = vec2(gl_TextureMatrix[0] * gl_MultiTexCoord0);
        texcoordLUT = vec2(gl_TextureMatrix[1] * gl_MultiTexCoord1);
        texdim0 = vec2 (abs(gl_TextureMatrix[0][0][0]),abs(gl_TextureMatrix[0][1][1]));
    
        texcoord1 = texcoord0 + vec2(-sharpness, -sharpness);
        texcoord2 = texcoord0 + vec2(+sharpness, -sharpness);
        texcoord3 = texcoord0 + vec2(+sharpness, +sharpness);
        texcoord4 = texcoord0 + vec2(-sharpness, +sharpness);
    
    }
    
  • For fragment shader

    uniform float amount;
    uniform float vignette;
    
    uniform sampler4DRect tex0;
    uniform sampler1D tex1;
    
    varying vec2 texcoord0;
    varying vec2 texcoord1;
    varying vec2 texcoord2;
    varying vec2 texcoord3;
    varying vec2 texcoord4;
    
    varying vec2 texdim0;
    
    varying vec2 texcoordLUT;
    
    const vec4 one = vec4(1.0); 
    const vec4 two = vec4(2.0);
    const vec4 lumcoeff = vec4(0.299,0.587,0.114, 0.);
    
    vec4 vignetteFucntion(vec2 normalizedTexcoord)
    {
        normalizedTexcoord = 2.0 * normalizedTexcoord - 1.0;
        float r = length(normalizedTexcoord);
        return 1.0 - vec4(smoothstep(0.5,1.0,r)) + 0.5;
    }
    
    vec4 hardlight(vec4 a, vec4 b, float amount)
    {
        vec4 result;
        vec4 branch1;
        vec4 branch2;
        float luminance = dot(b,lumcoeff);
        float mixamount;
    
        mixamount = clamp((luminance - 0.45) * 10., 0., 1.);
        branch1 = two * a * b;
        branch2 = one - (two * (one - a) * (one - b));
    
        result =  mix(branch1,branch2, vec4(mixamount));
    
        return mix(a,result, amount);
    }
    
    void main (void) 
    {       
        vec2 normcoord = texcoord0/texdim0;
    
        vec4 vignetteResult = vignetteFucntion(normcoord);
    
        vec4 input0 = texture2DRect(tex0, texcoord0);
        vec4 input1 = texture2DRect(tex0, texcoord1);
        vec4 input2 = texture2DRect(tex0, texcoord2);
        vec4 input3 = texture2DRect(tex0, texcoord3);
        vec4 input4 = texture2DRect(tex0, texcoord4);
    
        vec4 sharpened = 5.0 * input0 - (input1 + input2 + input3 + input4);
    
        vec4 hardlighted = hardlight(sharpened,input0, .5);
    
        vec4 saturated = mix(vec4(dot(hardlighted,lumcoeff)), hardlighted, 0.75);
    
        vec4 result;
    
        result.r = texture1D(tex1, saturated.r).r;
        result.g = texture1D(tex1, saturated.g).g;
        result.b = texture1D(tex1, saturated.b).b;
        result.a = saturated.a;
    
        gl_FragColor = mix(input0, result *  (mix(vec4(1.0),vignetteResult, vignette)),amount);
    }
    

I want to know how I can translate gl_TextureMatrix[0], gl_TextureMatrix[1], and gl_TextureMatrix[0][0][0]. What do they mean?

1

1 Answers

2
votes

gl_TextureMatrix is a transform matrix that transforms the texture coordinates (for example if you wanted to rotate or scale the texture on a static shape.

It is a deprecated built-in variable in standard OpenGL. The proper way to handle this in modern OpenGL/OpenGLES would be to declare your own uniform matrices instead of using the built-in gl_TextureMatrix, and update these uniform matrices instead of performing rotations/translations to OpenGL's GL_TEXTURE_MATRIX.