Changeset 1137


Ignore:
Timestamp:
Jan 30, 2012, 8:53:32 AM (11 years ago)
Author:
sam
Message:

math: finally get the GLSL-like swizzling to work.

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/monsterz/title.cpp

    r1136 r1137  
    145145
    146146    data->ground = Tiler::Register(PNG_TITLE_GROUND, ivec2(384, 80), ivec2(0), 1.0f);
    147 vec2(1,2).xy - vec2(2, 4);
    148147    data->ground_pos = ivec2((bbox[1] - bbox[0]).xy / vec2(2, 4))
    149148                     - ivec2(192, 80);
  • trunk/src/lol/math/matrix.h

    r1136 r1137  
    7777};
    7878
     79/*
     80 * Helper macros for vector type member functions
     81 */
     82
    7983#define MEMBER_OPS() \
    8084    inline T& operator[](int n) { return *(&x + n); } \
     
    152156
    153157    template<int I, int J>
    154     inline Vec2<T>(MagicVec2<T, I, J> const &v)
     158    inline Vec2(MagicVec2<T, I, J> const &v)
     159      : x(v.ptr[I]), y(v.ptr[J]) {}
     160
     161    template<typename U, int I, int J>
     162    explicit inline Vec2(MagicVec2<U, I, J> const &v)
    155163      : x(v.ptr[I]), y(v.ptr[J]) {}
    156164
     
    275283
    276284    template<int I, int J, int K>
    277     inline Vec3<T>(MagicVec3<T, I, J, K> const &v)
     285    inline Vec3(MagicVec3<T, I, J, K> const &v)
     286      : x(v.ptr[I]), y(v.ptr[J]), z(v.ptr[K]) {}
     287
     288    template<typename U, int I, int J, int K>
     289    explicit inline Vec3(MagicVec3<U, I, J, K> const &v)
    278290      : x(v.ptr[I]), y(v.ptr[J]), z(v.ptr[K]) {}
    279291
     
    434446
    435447    template<int I, int J, int K, int L>
    436     inline Vec4<T>(MagicVec4<T, I, J, K, L> const &v)
     448    inline Vec4(MagicVec4<T, I, J, K, L> const &v)
    437449      : x(v.ptr[I]), y(v.ptr[J]), z(v.ptr[K]), w(v.ptr[L]) {}
    438450
    439451    template<typename U, int I, int J, int K, int L>
    440     explicit inline Vec4<T>(MagicVec4<U, I, J, K, L> const &v)
     452    explicit inline Vec4(MagicVec4<U, I, J, K, L> const &v)
    441453      : x(v.ptr[I]), y(v.ptr[J]), z(v.ptr[K]), w(v.ptr[L]) {}
    442454
     
    794806};
    795807
    796 inline Vec2<float> operator /(Vec2<float> a, Vec2<float> b)
    797 {
    798     return Vec2<float>(a.x / b.x, a.y / b.y);
    799 }
    800 
    801808/*
    802809 * 4-element quaternions
     
    853860 */
    854861
    855 #define VECTOR_OP(tname, op) \
    856     template<typename T> \
    857     inline tname<T> operator op(tname<T> const &a, tname<T> const &b) \
     862#define VECTOR_OP(tname, op, tprefix, T) \
     863    tprefix \
     864    static inline tname<T> operator op(tname<T> const &a, tname<T> const &b) \
    858865    { \
    859866        tname<T> ret; \
     
    863870    } \
    864871    \
    865     template<typename T> \
    866     inline tname<T> operator op##=(tname<T> &a, tname<T> const &b) \
     872    tprefix \
     873    static inline tname<T> operator op##=(tname<T> &a, tname<T> const &b) \
    867874    { \
    868875        return a = a op b; \
    869876    }
    870877
    871 #define BOOL_OP(tname, op, op2, ret) \
    872     template<typename T> \
    873     inline bool operator op(tname<T> const &a, tname<T> const &b) \
     878#define BOOL_OP(tname, op, op2, ret, tprefix, T) \
     879    tprefix \
     880    static inline bool operator op(tname<T> const &a, tname<T> const &b) \
    874881    { \
    875882        for (size_t n = 0; n < sizeof(a) / sizeof(T); n++) \
     
    879886    }
    880887
    881 #define SCALAR_OP(tname, op) \
    882     template<typename T> \
    883     inline tname<T> operator op##=(tname<T> &a, T const &val) \
    884     { \
    885         return a = a op val; \
    886     } \
    887     \
    888     template<typename T> \
     888#define SCALAR_OP(tname, op, tprefix, T) \
     889    tprefix \
    889890    static inline tname<T> operator op(tname<T> const &a, T const &val) \
    890891    { \
     
    893894            ret[n] = a[n] op val; \
    894895        return ret; \
     896    } \
     897    \
     898    tprefix \
     899    static inline tname<T> operator op(T const &val, tname<T> const &a) \
     900    { \
     901        tname<T> ret; \
     902        for (size_t n = 0; n < sizeof(a) / sizeof(T); n++) \
     903            ret[n] = a[n] op val; \
     904        return ret; \
     905    } \
     906    \
     907    tprefix \
     908    static inline tname<T> operator op##=(tname<T> &a, T const &val) \
     909    { \
     910        return a = a op val; \
    895911    }
    896912
     
    905921    }
    906922
    907 #define GLOBALS(tname) \
    908     SCALAR_OP(tname, *) \
    909     SCALAR_OP(tname, /) \
    910     \
    911     SCALAR_PROMOTE_OP(tname, *, int) \
    912     SCALAR_PROMOTE_OP(tname, *, float) \
    913     SCALAR_PROMOTE_OP(tname, *, double) \
    914     \
    915     VECTOR_OP(tname, -) \
    916     VECTOR_OP(tname, +) \
    917     \
    918     BOOL_OP(tname, ==, ==, true) \
    919     BOOL_OP(tname, !=, ==, false) \
    920     \
    921     template<typename T> \
    922     inline tname<T> operator -(tname<T> const &a) \
     923#define GLOBAL_OPS(tname, tprefix, T) \
     924    SCALAR_OP(tname, *, tprefix, T) \
     925    SCALAR_OP(tname, /, tprefix, T) \
     926    \
     927    VECTOR_OP(tname, -, tprefix, T) \
     928    VECTOR_OP(tname, +, tprefix, T) \
     929    \
     930    BOOL_OP(tname, ==, ==, true, tprefix, T) \
     931    BOOL_OP(tname, !=, ==, false, tprefix, T) \
     932    \
     933    tprefix \
     934    static inline tname<T> operator -(tname<T> const &a) \
    923935    { \
    924936        tname<T> ret; \
     
    928940    } \
    929941    \
    930     template<typename T> \
    931     inline T sqlen(tname<T> const &a) \
     942    tprefix \
     943    static inline T sqlen(tname<T> const &a) \
    932944    { \
    933945        T acc = 0; \
     
    937949    } \
    938950    \
    939     template<typename T> \
    940     inline double len(tname<T> const &a) \
     951    tprefix \
     952    static inline double len(tname<T> const &a) \
    941953    { \
    942954        using namespace std; \
     
    944956    } \
    945957    \
    946     /* dot() does not take const refs because the function is not inlined, \
    947      * so we try to avoid carrying a shitty pointer around. */ \
    948     template<typename T> \
    949     T dot(tname<T> a, tname<T> b); \
    950     \
    951     template<typename T> \
     958    tprefix \
     959    static inline T dot(tname<T> const &a, tname<T> const &b) \
     960    { \
     961        T ret = 0; \
     962        for (size_t n = 0; n < sizeof(a) / sizeof(T); n++) \
     963            ret += a[n] * b[n]; \
     964        return ret; \
     965    } \
     966    \
     967    tprefix \
    952968    static inline tname<T> normalize(tname<T> const &val) \
    953969    { \
     
    956972    }
    957973
    958 GLOBALS(Vec2)
    959 GLOBALS(Cmplx)
    960 GLOBALS(Vec3)
    961 GLOBALS(Vec4)
    962 GLOBALS(Quat)
    963 
    964 #define OTHER_OPS(tname) \
    965     VECTOR_OP(tname, *) \
    966     VECTOR_OP(tname, /) \
    967     \
    968     BOOL_OP(tname, <=, <=, true) \
    969     BOOL_OP(tname, >=, >=, true) \
    970     BOOL_OP(tname, <, <, true) \
    971     BOOL_OP(tname, >, >, true)
    972 
    973 OTHER_OPS(Vec2)
    974 OTHER_OPS(Vec3)
    975 OTHER_OPS(Vec4)
     974#define GLOBAL_TEMPLATE_OPS(tname) \
     975    GLOBAL_OPS(tname, template<typename T>, T)
     976
     977#define ALL_GLOBAL_OPS(tname, tprefix, T) \
     978    VECTOR_OP(tname, *, tprefix, T) \
     979    VECTOR_OP(tname, /, tprefix, T) \
     980    \
     981    GLOBAL_OPS(tname, tprefix, T) \
     982    \
     983    BOOL_OP(tname, <=, <=, true, tprefix, T) \
     984    BOOL_OP(tname, >=, >=, true, tprefix, T) \
     985    BOOL_OP(tname, <, <, true, tprefix, T) \
     986    BOOL_OP(tname, >, >, true, tprefix, T)
     987
     988/* FIXME: a few problems need to be fixed before we can use "half" here. It
     989 * will probably never work until we switch to C++11 because it's not really
     990 * a POD class. */
     991#define GLOBAL_TYPED_OPS(tname) \
     992    ALL_GLOBAL_OPS(tname, , float) \
     993    ALL_GLOBAL_OPS(tname, , double) \
     994    ALL_GLOBAL_OPS(tname, , int8_t) \
     995    ALL_GLOBAL_OPS(tname, , uint8_t) \
     996    ALL_GLOBAL_OPS(tname, , int16_t) \
     997    ALL_GLOBAL_OPS(tname, , uint16_t) \
     998    ALL_GLOBAL_OPS(tname, , int32_t) \
     999    ALL_GLOBAL_OPS(tname, , uint32_t) \
     1000    ALL_GLOBAL_OPS(tname, , int64_t) \
     1001    ALL_GLOBAL_OPS(tname, , uint64_t)
     1002
     1003GLOBAL_TEMPLATE_OPS(Cmplx)
     1004GLOBAL_TEMPLATE_OPS(Quat)
     1005
     1006GLOBAL_TYPED_OPS(Vec2)
     1007GLOBAL_TYPED_OPS(Vec3)
     1008GLOBAL_TYPED_OPS(Vec4)
    9761009
    9771010/*
  • trunk/src/matrix.cpp

    r1135 r1137  
    3232{
    3333
    34 template<> float dot(vec2 v1, vec2 v2)
     34float dot(vec2 v1, vec2 v2)
    3535{
    3636    return v1.x * v2.x + v1.y * v2.y;
    3737}
    3838
    39 template<> float dot(vec3 v1, vec3 v2)
     39float dot(vec3 v1, vec3 v2)
    4040{
    4141    return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
    4242}
    4343
    44 template<> float dot(vec4 v1, vec4 v2)
     44float dot(vec4 v1, vec4 v2)
    4545{
    4646    return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z + v1.w * v2.w;
Note: See TracChangeset for help on using the changeset viewer.