Search code examples
c++glm-mathtrigonometrymath.h

Use "custom" sin and cos in glm::rotate


Is there any way of using my own sin and cos functions instead of ::std::sin and ::std::cos in glm::rotate(...) calls?

The only way I can think of, is using macros to replace the sin and cos symbols inside the std namespace, but I really wouldn't like to do that.


Solution

  • Using LD preload will work.

    I will explain with a small example.

    Assume that this is our main code...

    int main()
    {
       std::cout<< std::sin(1000);
       return 0;
    }
    

    if I compile and run it, it should print 0.82688

    Now I define my own sin

    mysin.hpp

    extern "C"
    {
        double sin(double);
    }
    

    mysin.cpp

    #include "mysin.hpp"
    
    double sin(double in)
    {
        return in + 10;//do you computation here
    }
    

    Now compile it as a shared lib

    g++ -O2 -c -fPIC mysin.cpp -o mysin.o
    g++ -shared -Wl,-soname,libmysin.so mysin.o -o libmysin.so
    

    Assuming original program is compiled as "a.out". Run it with our lib preloaded

    LD_PRELOAD=./libmysin.so ./a.out
    

    Now the result will be "1010".