Advertisement
Guest User

Untitled

a guest
Sep 29th, 2018
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.11 KB | None | 0 0
  1. class float4 {
  2. public:
  3.     float x;
  4.     float y;
  5.     float z;
  6.     float w;
  7.  
  8.     float4() : x(0.0f), y(0.0f), z(0.0f), w(0.0f) {}
  9.     float4(float3 const v, float vw) : x(v.x), y(v.y), z(v.z), w(vw) {}
  10.     float4(float const vx, float const vy, float const vz, float const vw) : x(vx), y(vy), z(vz), w(vw) {}
  11.  
  12.     template <class T>
  13.     float4(T val) : x(val), y(val), z(val), w(val) {}
  14.  
  15.     float4& operator= (float4 other) {
  16.         x = other.x;
  17.         y = other.y;
  18.         z = other.z;
  19.         w = other.w;
  20.         return *this;
  21.     }
  22.  
  23.     template <class T>
  24.     float4& operator=(T other) {
  25.         *this = float4(other);
  26.         return *this;
  27.     }
  28.  
  29.     float4& operator+= (float4 other) {
  30.         x += other.x;
  31.         y += other.y;
  32.         z += other.z;
  33.         w += other.w;
  34.         return *this;
  35.     }
  36.  
  37.     float4& operator-= (float4 other) {
  38.         x -= other.x;
  39.         y -= other.y;
  40.         z -= other.z;
  41.         w -= other.w;
  42.         return *this;
  43.     }
  44.  
  45.     float4& operator*= (float4 other) {
  46.         x *= other.x;
  47.         y *= other.y;
  48.         z *= other.z;
  49.         w *= other.w;
  50.         return *this;
  51.     }
  52.  
  53.     float4& operator/= (float4 other) {
  54.         x /= other.x;
  55.         y /= other.y;
  56.         z /= other.z;
  57.         w /= other.w;
  58.         return *this;
  59.     }
  60.  
  61.     float4 clamp(float4 const &lo, float4 const &hi) {
  62.         return float4(
  63.             std::max(std::min(x, hi.x),lo.x),
  64.             std::max(std::min(y, hi.y),lo.y),
  65.             std::max(std::min(z, hi.z),lo.z),
  66.             std::max(std::min(w, hi.w),lo.w)
  67.         );
  68.     }
  69.  
  70.     friend float4 operator+ (float4 lhs, float4 const &rhs) { lhs += rhs; return lhs; }
  71.     friend float4 operator- (float4 lhs, float4 const &rhs) { lhs -= rhs; return lhs; }
  72.     friend float4 operator* (float4 lhs, float4 const &rhs) { lhs *= rhs; return lhs; }
  73.     friend float4 operator/ (float4 lhs, float4 const &rhs) { lhs /= rhs; return lhs; }
  74.  
  75.     bool operator!= (float4 other) {
  76.         return (x != other.x) || (y != other.y) || (z != other.z) || (w != other.w);
  77.     }
  78.  
  79.     bool operator== (float4 other) {
  80.         return (x == other.x) && (y == other.y) && (z == other.z) && (w == other.w);
  81.     }
  82.  
  83.     float3 toFloat3() {
  84.         return float3(x,y,z);
  85.     }
  86.  
  87.     friend std::ostream & operator << (std::ostream &os, const float4 &v)
  88.     {
  89.         os << "[" << v.x << "," << v.y << "," << v.z << "," << v.w << "]";
  90.         return os;
  91.     }
  92. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement