Search code examples
glslshaderwebgl

GLSL "No Matching Overloaded Function Found" (hsv2rgb)


What obvious thing am I missing? I'm trying to compile/run this vertex shader:

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

using code I found at From RGB to HSV in OpenGL GLSL but get the error:

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'

The error is specific to the hsv2rgb call. I have tried a number of things, including making the parameter a variable (i.e. adding vec3 v = vec3(0.5, 0.5, 0.5) and passing v into hsv2rgb), and making a skeleton hbv2rgb that simply returns its parameter. In the referenced SO post, I saw that another user seemed to have exactly the same problem, but I am properly passing in a vec3 rather than 3 floats.

If it makes any difference, here is the fragment shader as well:

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

Solution

  • From OpenGL ES Shading Language 1.00 Specification - 6.1 Function Definitions:

    All functions must be either declared with a prototype or defined with a body before they are called.

    Hence, you have to declare the function hsv2rgb before it is used the first time or you have to declare a function prototype:

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