GLSL绕过浮点阵列均匀

时间:2012-06-14 09:10:54

标签: opengl-es glsl

setShaders

    muOffsetHandle = getUniformLocation("offset");      
    muWeightHandle = getUniformLocation("weight");

useProgram

    GLES20.glUniform1fv(muOffsetHandle, 1, mOffset, 0);
    GLES20.glUniform1fv(muWeightHandle, 1, mWeight, 0);

VARS

private int muOffsetHandle;
private int muWeightHandle;

protected float mOffset[] =new float[] {0.0f, 1.3f, 3.3f}; 
protected float mWeight[] =new float[] {0.2f, 0.3f, 0.3f};

FragmentShader

        "uniform float offset[3];\n" +
        "uniform float weight[3];\n" +

然后试图达到:体重[i]

我明白了:

着色器日志:0:13:S0004:在非结构和非向量上尝试成员引用或混合

第13行:tc += texture2D(uFrameBufferTexture, vTextureCoord[0].xy + vec2(0.0, offset[i])/uScreenHeight).rgb * weight[i];

(i = 0到2)

所以我的问题:如何绕过浮点数组到统一?浮[3]

代码

protected static final String mFShader = 
        "precision mediump float;\n" +
        "varying vec2 vTextureCoord;\n" +
        "uniform float uTime;\n" +
        "uniform float uScreenWidth;\n" +
        "uniform float uScreenHeight;\n" +
        "uniform sampler2D uFrameBufferTexture;\n"+

        "uniform float offset[3];\n" +
        "uniform float weight[3];\n" +

        "void main() {\n"+
        "  vec3 tc = vec3(1.0, 0.0, 0.0);\n"+
        "  if (vTextureCoord[0].x<(uTime-0.01)) {\n"+
        "    tc = texture2D(uFrameBufferTexture, vTextureCoord[0].xy).rgb * weight[0];\n"+
        "    for (int i=1; i<3; i++) {\n"+
        "      tc += texture2D(uFrameBufferTexture, vTextureCoord[0].xy + vec2(0.0, offset[i])/uScreenHeight).rgb * weight[i];\n"+
        "      tc += texture2D(uFrameBufferTexture, vTextureCoord[0].xy - vec2(0.0, offset[i])/uScreenHeight).rgb * weight[i];\n"+
        "    }\n"+
        "  }\n"+
        "  else if (vTextureCoord[0].x>=(uTime+0.01)) {\n"+
        "    tc = texture2D(uFrameBufferTexture, vTextureCoord[0].xy).rgb;\n"+
        "  }\n"+
        "   gl_FragColor = vec4(tc, 1.0);\n"+
        "}\n";

2 个答案:

答案 0 :(得分:3)

问题实际上不在于weightoffset。编译器抱怨您说vTextureCoord[0].xy,但您将vTextureCoord声明为varying vec2 vTextureCoord;

vTextureCoord声明为数组,或者不要说[0]

答案 1 :(得分:1)

您不需要将其声明为数组,但如果您需要,可以使用:

varying vec4 vTextureCoord[3]; // you can use anything you want between the brackets

另外,不要忘记修改顶点着色器的变化,以使它们相同。