
The function Decode extracts a floating point value in the range from a vec4: float Decode( in vec4 pack )įloat value = dot( pack, 1.0 / vec4(1.0, 256.0, 256.0*256.0, 256.0*256.0*256.0) ) The function Encode packs a floating point value in the range into a vec4: vec4 Encode( in float value ) If you have defined a value range, it has to be mapped to the range : float mapVal = clamp((value-minVal)/(maxVal-minVal), 0.0, 1.0) In order to pack a floating-point value in 4 * 8-bit buffers, the range of the source values must first be specified. Encode a floating point number in a predefined range In general, if you want to pack the significant digits of a floating-point number in bytes, you have consecutively to extract 8 bits packages of the significant digits and store it in a byte.
#VEC2 PACK CODE#
GLM has some useful constants but the way to use them might be a little bit weird at first.You can bitshift by multiplying/dividing by powers of two.Īs pointed out in the comments the approach I originally posted was working but incorrect, here's one by Aras Pranckevičius, note that the source code in the post itself contains a typo and is HLSL, this is a GLSL port with the typo corrected: const vec4 bitEnc = vec4(1.,255.,65025.,16581375.) Įnc -= enc.yzww * vec2(1./255., 0.).xxxy Or in the opposite case: glm::vec3 v3(2.f, 2.f, 2.f) Won't work anymore, you need to do now: glm::vec3 v3 = glm::vec3(v2, 0.f) Glm has a strict type system, similar to how things work in glsl, meaning that you can't autoconvert from one type to another automatically as it was the case with ofVectorMath. When doing that, most modern compilers should show a warning because of calling a static method on an instance instead of a class, so be on the look for those when porting old code to GLM. Mostly when porting old code form ofVec to glm, because ofVec included such a method, it's easy to try and call that function which will compile without errors but won't do what you expect. The correct way of doing that would be: glm::vec3 v(2.f, 2.f, 2.f) To try and get the length of the vector instead of the number of dimensions of the type.

GLM vector have a static length function which returns the dimension of the vector type, so glm::vec2::length() returns 2, glm::vec3::length() returns 3.īecause C++ allows to call static methods on instances of that class you can make the error of calling: glm::vec3 v(2.f, 2.f, 2.f) The only exceptions to this rule are operators which you don't use directly but instead allow to do arithmetic operations so you can do things like: glm::vec3 v1(1.f, 1.f, 1.f) Instead glm uses functions to operate on those classes so if you want to for example normalize a vector you would do: glm::vec3 v(2.f, 2.f, 2.f)

GLM classes like vectors, matrices or quaternions don't have methods. cpp files import the glm namespace at the very beginning: using namespace glm Īnd then use the classes and functions without prefix: vec3 v(2.f, 2.f, 2.f) GLM classes and functions are in the glm namespace so to use them you need to either prefix them with glm::: glm::vec3 v(2.f, 2.f, 2.f) OpenFrameworks, since version 0.10 uses GLM as it's default vector math library in the core and although old projects using ofVec* classes should work with minimal changes, if you are starting a new project we recomend to use GLM instead. makes things easier cause the syntax is almost the same across the different languages. It's syntax mimics that of glsl so working with it while working with OpenGL, shaders. OpenGL Mathematics GLM has become the defacto standard for graphics vector math in C++ in the latest years.
