GLSL“未找到匹配的重载函数”(hsv2rgb)

问题描述 投票:1回答:1

我想念什么明显的东西?我正在尝试编译/运行此顶点着色器:

// an attribute will receive data from a buffer
attribute vec2 a_position;

uniform vec2 u_resolution;

varying vec4 v_color;

// all shaders have a main function
void main() {
    // convert the position from pixels to 0.0 to 1.0
    vec2 zeroToOne = a_position / u_resolution;
    // convert from 0->1 to 0->2
    vec2 zeroToTwo = zeroToOne * 2.0;
    // convert from 0->2 to -1->+1 (clip space)
    vec2 clipSpace = zeroToTwo - 1.0;
    gl_Position = vec4(clipSpace, 0, 1);
    vec3 c = hsv2rgb(vec3(0.5, 0.5, 0.5));
    /*temporary*/ v_color = gl_Position * 0.5 + 0.5;
    gl_PointSize = 1.0;
}

// All components are in the range [0…1], including hue.
vec3 hsv2rgb(vec3 c)
{
    vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
    vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
    return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
}

使用我在From RGB to HSV in OpenGL GLSL处找到的代码,但收到错误:

https://webglfundamentals.org/webgl/lessons/webgl-boilerplate.html
webgl-utils.js:66 *** Error compiling shader '[object WebGLShader]':ERROR: 0:19: 'hsv2rgb' : no matching overloaded function found
ERROR: 0:19: '=' : dimension mismatch
ERROR: 0:19: '=' : cannot convert from 'const mediump float' to 'highp 3-component vector of float'

该错误特定于hsv2rgb调用。我尝试了很多方法,包括将参数设置为变量(即添加vec3 v = vec3(0.5, 0.5, 0.5)并将v传递到hsv2rgb),以及制作仅返回其参数的框架hbv2rgb。在引用的SO帖子中,我看到另一个用户似乎也存在完全相同的问题,但是我正确地传入了vec3而不是3个浮点数。

如果有什么不同,这里也是片段着色器:

// fragment shaders don't have a default precision so we need
// to pick one. mediump is a good default
precision mediump float;

varying vec4 v_color;

void main() {
    // gl_FragColor is a special variable a fragment shader
    // is responsible for setting
    gl_FragColor = v_color;
}
glsl shader webgl
1个回答
3
投票

来自OpenGL ES Shading Language 1.00 Specification - 6.1 Function Definitions

所有函数必须在调用前用原型声明或用主体定义。

因此,您必须在第一次使用函数之前声明函数hsv2rgb,否则必须声明函数原型:

vec3 hsv2rgb(vec3 c);

void main() {
    // [...]

    vec3 c = hsv2rgb(vec3(0.5, 0.5, 0.5));

    // [...]
}

vec3 hsv2rgb(vec3 c)
{
    vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
    vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
    return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
}
© www.soinside.com 2019 - 2024. All rights reserved.