GLSL <> operators on a vec4

北城余情 提交于 2019-12-04 19:55:23

问题


I'm looking at some newer GLSL code that doesn't compile to my current version of OpenGL and I'm wondering what the short form of the following means:

vec4 base;

if (base < 0.5) {
    result = (2.0 * base * blend);
}

Is this equivalent to:

if (base.r < 0.5 && base.g < 0.5 && base.b < 0.5 && base.a < 0.5) {
    result.r = 2.0 * base.r * blend.r;
    result.g = 2.0 * base.g * blend.g;
    result.b = 2.0 * base.b * blend.b;
    result.a = 2.0 * base.a * blend.a;
}

Edit:

Error:
Fragment shader failed to compile with the following errors:
Wrong operand types no operation '<' exists that takes a left-hand operand of type 'highp 3-component vector of float' and a right operand of type 'const float' (or there is no acceptable conversion)

I've also tried:

(base.rgb < vec3(0.5))
... Wrong operand types no operation '<' exists that takes a left-hand operand of type 'highp 3-component vector of float' and a right operand of type 'const highp 3-component vector of float'

I'm assuming this is because I'm using GLSL 1.2. ATI Radeon 3450


回答1:


From the spec, section 5.9 (top of page 38):

The relational operators greater than (>), less than (<), greater than or equal (>=), and less than or equal (<=) operate only on scalar integer and scalar floating-point expressions. The result is scalar Boolean. Either the operands’ types must match, or the conversions from Section 4.1.10 “Implicit Conversions” will be applied to the integer operand, after which the types must match. To do component-wise relational comparisons on vectors, use the built-in functions lessThan, lessThanEqual, greaterThan, and greaterThanEqual.

Looks like you want the lessThan function. Check section 8.6 (page 62).

  • lessThan() - http://www.opengl.org/ (see also: all())


来源:https://stackoverflow.com/questions/5860033/glsl-operators-on-a-vec4

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!