OpenGL es 中的纹理未声明标识符

Texture undeclared identifier in OpenGL es

我尝试编译这个着色器:

varying vec2 TexCoords;
varying vec4 color;

uniform sampler2D text;
uniform vec3 textColor;

void main()
{    
    vec4 sampled = vec4(1.0, 1.0, 1.0, texture(text, TexCoords).r);
    color = vec4(textColor, 1.0) * sampled;
}

这是针对 OpenGL es 的。我得到的错误:

ERROR: 0:2: 'vec2' : No precision defined for this type
ERROR: 0:3: 'vec4' : No precision defined for this type
ERROR: 0:6: 'vec3' : No precision defined for this type
ERROR: 0:10: 'texture' : undeclared identifier
ERROR: 0:10: invalid function call

请注意,它在 OpenGL 3.3 上编译,但在 es 上失败。

参见OpenGL ES Shading Language 1.00 Specification - 4.5.3 Default Precision Qualifiers

The fragment language has no default precision qualifier for floating point types. Hence for float, floating point vector and matrix variable declarations, either the declaration must include a precision qualifier or the default float precision must have been previously declared.

因此您必须添加精度限定符。例如:

precision mediump float;

重载函数 texture 是在 GLSL ES 3.00 中引入的,但在 GLSL ES 1.00 中不受支持。您必须使用函数 texture2D:

vec4 sampled = vec4(1.0, 1.0, 1.0, texture(text, TexCoords).r);

vec4 sampled = vec4(1.0, 1.0, 1.0, texture2D(text, TexCoords).r);