Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Translating GLSL to C++ float / vec3?

Tags:

c++

glsl

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?)

like image 280
John Du Avatar asked Jan 15 '14 19:01

John Du


1 Answers

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)
like image 91
Andon M. Coleman Avatar answered Oct 02 '22 17:10

Andon M. Coleman