0

这是我尝试使用的片段着色器代码(在 OpenGL ES 2.0、OpenGLES GLSL ES 1.00 中):

GLchar strFragmentShader[] =  
"precision mediump float;                                              \n"
"varying vec2 vTexCoord;                                               \n"
"uniform sampler2D sTexture;                                           \n"
"uniform float offset[] = float[]( 0.0000, 1.3846, 3.2307);            \n"
"uniform float weight[] = float[]( 0.2270, 0.3162, 0.0702);            \n"
"void main()                                                           \n"
"{                                                                     \n"
"  vec4 sum = texture2D( sTexture, vec2(vTexCoord)/1024.0)*weight[0];  \n"
"  for (int i=0;i<3;i++) {                                                                 \n"
"    sum += texture2D( sTexture, ( vec2(vTexCoord+vec2(0.0,offset[i])/1024.0 )*weight[i];  \n"
"    sum += texture2D( sTexture, ( vec2(vTexCoord-vec2(0.0,offset[i])/1024.0 )*weight[i];  \n"
"  }                                                                                       \n"  
"  gl_FragColor = sum;                                                 \n"
"}                                                                     \n";

我基本上遵循此页面上的示例,但我明白了ERROR:LEX/PARSE-1 (fragment shader, line 4) Syntax error

如果我尝试将它们声明为const而不是此处uniform建议的(在数组构造函数下),我会得到. ERROR:CUSTOM-5 (fragment shader, line 4) Array cannot be const

显然第 4 行和第 5 行会有同样的问题。我如何让它编译?什么是正确的语法?

4

2 回答 2

0

我会这样做:

GLchar strFragmentShader[] =  
"precision mediump float;                                              \n"
"varying vec2 vTexCoord;                                               \n"
"uniform sampler2D sTexture;                                           \n"
"void main()                                                           \n"
"{ 
"   vec3 offset = vec3(0.0000, 1.3846, 3.2307);            \n"
"   vec3 weight = vec3( 0.2270, 0.3162, 0.0702);            \n"                                                                    \n"
"  vec4 sum = texture2D( sTexture, vec2(vTexCoord)/1024.0)*weight[0];  \n"
"  for (int i=0;i<3;i++) {                                                                 \n"
"    sum += texture2D( sTexture, ( vec2(vTexCoord+vec2(0.0,offset[i])/1024.0 )*weight[i];  \n"
"    sum += texture2D( sTexture, ( vec2(vTexCoord-vec2(0.0,offset[i])/1024.0 )*weight[i];  \n"
"  }                                                                                       \n"  
"  gl_FragColor = sum;                                                 \n"
"}" 

或者如果您需要它作为统一传递给着色器:

GLchar strFragmentShader[] =  
"precision mediump float;                                              \n"
"varying vec2 vTexCoord;                                               \n"
"uniform sampler2D sTexture;                                           \n"
"uniform vec3 offset;                                                    \n"
"uniform  vec3 weight;                                                  \n" 
"void main()                                                           \n"
"{                                                                    \n"
"  vec4 sum = texture2D( sTexture, vec2(vTexCoord)/1024.0)*weight[0];  \n"
"  for (int i=0;i<3;i++) {                                                                 \n"
"    sum += texture2D( sTexture, ( vec2(vTexCoord+vec2(0.0,offset[i])/1024.0 )*weight[i];  \n"
"    sum += texture2D( sTexture, ( vec2(vTexCoord-vec2(0.0,offset[i])/1024.0 )*weight[i];  \n"
"  }                                                                                       \n"  
"  gl_FragColor = sum;                                                 \n"
"}" 

或者,如果您需要一个数组,则在第二个示例中使用uniform float[n]wheren是所需的(常量)大小,或者const float[n]如果您需要一个数组并且不想从 C++ 代码中传递值,则在第一个示例中使用。

于 2013-10-16T12:36:39.347 回答
0

原来这只是 GLES 的一个限制。这是不可能的。需要找到一种替代方法。

于 2013-10-18T17:41:45.367 回答