我目前正在尝试使用 HSV 颜色空间来渲染我的观点。为此,我在片段着色器中使用了一个函数,生成正确的转换向量。现在我的问题是如何告诉 OpenGL 它应该将此向量解释为 HSV 颜色而不是 RGB 颜色。
我的片段着色器如下所示:
const GLchar *fragment_shader2 =
"#version 330\n"
""
"/* Output Variables */"
"out vec4 out_color;"
""
"/* Function Prototypes */"
"vec3 convertRGBtoHSV(vec3 rgbColorVector);"
""
"void main()"
"{"
" vec3 rgbColor = vec3(1.0, 0.0, 0.55);"
" "
" out_color = vec4(convertRGBtoHSV(rgbColor), 1.0);"
"}"
""
"vec3 convertRGBtoHSV(vec3 rgbColorVector)"
"{"
" vec3 hsvColorVector = vec3(0.0, 0.0, 0.0);"
" float maxValue = max(rgbColorVector.r, max(rgbColorVector.g, rgbColorVector.b));"
" float minValue = min(rgbColorVector.r, min(rgbColorVector.g, rgbColorVector.b));"
" "
" /* Set Value */"
" hsvColorVector[2] = maxValue;"
" "
" /* Set Saturation */"
" if(maxValue != 0.0)"
" {"
" hsvColorVector[1] = (maxValue - minValue) / maxValue;"
" }"
" "
" /* Set Hue */"
" float hue = 0.0;"
" if(maxValue == rgbColorVector.r)"
" {"
" hue = (0.0 + (rgbColorVector.g - rgbColorVector.b) / (maxValue - minValue)) * 60.0;"
" }"
" else if(maxValue == rgbColorVector.g)"
" {"
" hue = (2.0 + (rgbColorVector.b - rgbColorVector.r) / (maxValue - minValue)) * 60.0;"
" }"
" else if(maxValue == rgbColorVector.b)"
" {"
" hue = (4.0 + (rgbColorVector.r - rgbColorVector.g) / (maxValue - minValue)) * 60.0;"
" }"
" "
" if(hue < 0.0)"
" {"
" hue = hue + 360.0;"
" }"
" "
" hsvColorVector[0] = hue / 360.0;"
" "
" /* Return converted color vector */"
" return hsvColorVector;"
"}"
"\0";
在此示例中,输出为 vec3(0.908, 1.0, 1.0),显示为白色而不是粉红色。