如何从OpenGL GLSL代码转换为OpenGL ES GLSL代码

时间:2012-09-28 19:20:57

标签: opengl opengl-es glsl

我正在尝试将for opengl的glsl转换为glsl for opengl es。

这是我要翻译的glsl代码。

  • 对于顶点着色器

    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);
    
    }
    
  • 对于片段着色器

    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);
    }
    

我想知道如何翻译gl_TextureMatrix [0],gl_TextureMatrix [1]和gl_TextureMatrix [0] [0] [0]。 他们是什么意思?

1 个答案:

答案 0 :(得分:2)

gl_TextureMatrix是一个变换矩阵,用于变换纹理坐标(例如,如果您想在静态形状上旋转或缩放纹理。

它是标准OpenGL中不推荐使用的内置变量。在现代OpenGL / OpenGLES中处理此问题的正确方法是声明自己的统一矩阵,而不是使用内置的gl_TextureMatrix,并更新这些统一矩阵,而不是执行旋转/转换到OpenGL的GL_TEXTURE_MATRIX