John Du
John Du

Reputation: 219

Translating GLSL to C++ float / vec3?

What does this line exactly do

ra.rgb * ra.w / max(ra.r, 1e-4) * (bR.r / bR);

The part I am confused about is how to translate

(bR.r / bR);

A float divided by a vec3? I want to translate this to C++ but what is that returning a float divided by all the elements of the vector? etc

(no access to graphics card to check?)

Upvotes: 8

Views: 845

Answers (1)

Andon M. Coleman
Andon M. Coleman

Reputation: 43319

This is an example of component-wise division, and it works as follows:

GLSL 4.40 Specification - 5.9 Expressions - pp. 101-102

If the fundamental types in the operands do not match, then the conversions from section 4.1.10 “Implicit Conversions” are applied to create matching types. [...] After conversion, the following cases are valid:

[...]

  • One operand is a scalar, and the other is a vector or matrix. In this case, the scalar operation is applied independently to each component of the vector or matrix, resulting in the same size vector or matrix.

Given the expression:

        vv vec3
(bR.r / bR);
    ^ float

The scalar bR.r is essentially promoted to vec3 (bR.r, bR.r, bR.r) and then component-wise division is performed, resulting in vec3 (bR.r/bR.r, bR.r/bR.g, bR.r/bR.b).

Thus, this expression is equivalent to:

vec3 (1.0, bR.r/bR.g, bR.r/bR.b)

Upvotes: 4

Related Questions