glsl - GLSL“找不到匹配的重载函数”(hsv2rgb)
问题描述
我错过了什么明显的东西?我正在尝试编译/运行这个顶点着色器:
// 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);
}
使用我在从 RGB 到 HSV 在 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 帖子中,我看到另一个用户似乎有完全相同的问题,但我正确地传递了 avec3
而不是 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;
}
解决方案
来自OpenGL ES Shading Language 1.00 Specification - 6.1 函数定义:
所有函数都必须在调用之前使用原型声明或使用主体定义。
因此,您必须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);
}
推荐阅读
- python - 如何通过嵌套列表中的某个索引位置运行 for 循环?
- google-chrome-extension - chrome.downloads.download 立即选择保存目的地
- r - 在数据帧之间进行比较以在两个数据帧中的相同位置找到观察值的 +/- 1
- sql - 超表和“普通”表之间多对多关系的替代方案
- java - 运行 JLink 可执行文件的 Alpine Docker Image
- c - CMake - 与静态和动态库的链接
- java - STS not picking up Open JDK 11 JRE
- xcode - 如何防止 SPM 项目中的示例代码导入 Xcode?
- r - shinydashboard 切换框 - 默认隐藏
- javascript - 构造函数对象总是返回一个函数而不是一个字符串