PDA

View Full Version : [GLM] Operator overload



Onumis
12-16-2011, 08:37 AM
Hey guys.

So i'm using GLM and I have this code:


vec3 vhalf = (vec_v+vec_l) / 2;

But it does not compile because it says that no operator "/" matches the operators.

I can get work-around it by doing this instead:


vec3 vhalf = (vec_v+vec_l);
vhalf /= 2;

Which is ok, but not as much pleasant to program or read.

Am I missing something?
Why isn't this operator supported in GLM since it is perfectly doable in GLSL ?

thx

Groovounet
12-16-2011, 09:36 AM
Because it's not possible to do in C++ without explicitly define the operator for each type.

The problem here is cast. You can do:
vec3 vhalf = (vec_v+vec_l) / 2.0f;

Onumis
12-16-2011, 09:57 AM
yeah, it solved the problem

funny how I actually tryed it with " (vec_v+vec_l) / 2.0 " and didn't work, forgot to put the 'f' after "2.0", silly...

Thank you!!

Zyx_2000
12-16-2011, 12:02 PM
A minor thing, it is usually better to multiply by 0.5f than to divide by 2.0f. It is faster (though most compilers probably optimize the division into the multiplication), but more importantly multiplication is commutative. I have had bugs originating from that division isn't.