diff options
| author | 2015-05-31 09:55:29 -0700 | |
|---|---|---|
| committer | 2015-05-31 09:55:29 -0700 | |
| commit | 0414ad20cb82ef0565d08c4102b64ccbc89d5012 (patch) | |
| tree | 3ec018dd373582c3f460ae0441f600ba5c445e48 /src/common | |
| parent | Merge pull request #832 from yuriks/refresh-rate-option (diff) | |
| parent | Move video_core/color.h to common/color.h (diff) | |
| download | yuzu-0414ad20cb82ef0565d08c4102b64ccbc89d5012.tar.gz yuzu-0414ad20cb82ef0565d08c4102b64ccbc89d5012.tar.xz yuzu-0414ad20cb82ef0565d08c4102b64ccbc89d5012.zip | |
Merge pull request #811 from archshift/commonify
Commonify video_core utility headers
Diffstat (limited to 'src/common')
| -rw-r--r-- | src/common/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | src/common/color.h | 214 | ||||
| -rw-r--r-- | src/common/vector_math.h | 640 |
3 files changed, 856 insertions, 0 deletions
diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index dbaaac77b..e78f4f144 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt | |||
| @@ -24,6 +24,7 @@ set(HEADERS | |||
| 24 | bit_field.h | 24 | bit_field.h |
| 25 | break_points.h | 25 | break_points.h |
| 26 | chunk_file.h | 26 | chunk_file.h |
| 27 | color.h | ||
| 27 | common_funcs.h | 28 | common_funcs.h |
| 28 | common_paths.h | 29 | common_paths.h |
| 29 | common_types.h | 30 | common_types.h |
| @@ -54,6 +55,7 @@ set(HEADERS | |||
| 54 | thread_queue_list.h | 55 | thread_queue_list.h |
| 55 | thunk.h | 56 | thunk.h |
| 56 | timer.h | 57 | timer.h |
| 58 | vector_math.h | ||
| 57 | ) | 59 | ) |
| 58 | 60 | ||
| 59 | create_directory_groups(${SRCS} ${HEADERS}) | 61 | create_directory_groups(${SRCS} ${HEADERS}) |
diff --git a/src/common/color.h b/src/common/color.h new file mode 100644 index 000000000..422fdc8af --- /dev/null +++ b/src/common/color.h | |||
| @@ -0,0 +1,214 @@ | |||
| 1 | // Copyright 2014 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include "common/common_types.h" | ||
| 8 | #include "common/swap.h" | ||
| 9 | #include "common/vector_math.h" | ||
| 10 | |||
| 11 | namespace Color { | ||
| 12 | |||
| 13 | /// Convert a 1-bit color component to 8 bit | ||
| 14 | inline u8 Convert1To8(u8 value) { | ||
| 15 | return value * 255; | ||
| 16 | } | ||
| 17 | |||
| 18 | /// Convert a 4-bit color component to 8 bit | ||
| 19 | inline u8 Convert4To8(u8 value) { | ||
| 20 | return (value << 4) | value; | ||
| 21 | } | ||
| 22 | |||
| 23 | /// Convert a 5-bit color component to 8 bit | ||
| 24 | inline u8 Convert5To8(u8 value) { | ||
| 25 | return (value << 3) | (value >> 2); | ||
| 26 | } | ||
| 27 | |||
| 28 | /// Convert a 6-bit color component to 8 bit | ||
| 29 | inline u8 Convert6To8(u8 value) { | ||
| 30 | return (value << 2) | (value >> 4); | ||
| 31 | } | ||
| 32 | |||
| 33 | /// Convert a 8-bit color component to 1 bit | ||
| 34 | inline u8 Convert8To1(u8 value) { | ||
| 35 | return value >> 7; | ||
| 36 | } | ||
| 37 | |||
| 38 | /// Convert a 8-bit color component to 4 bit | ||
| 39 | inline u8 Convert8To4(u8 value) { | ||
| 40 | return value >> 4; | ||
| 41 | } | ||
| 42 | |||
| 43 | /// Convert a 8-bit color component to 5 bit | ||
| 44 | inline u8 Convert8To5(u8 value) { | ||
| 45 | return value >> 3; | ||
| 46 | } | ||
| 47 | |||
| 48 | /// Convert a 8-bit color component to 6 bit | ||
| 49 | inline u8 Convert8To6(u8 value) { | ||
| 50 | return value >> 2; | ||
| 51 | } | ||
| 52 | |||
| 53 | /** | ||
| 54 | * Decode a color stored in RGBA8 format | ||
| 55 | * @param bytes Pointer to encoded source color | ||
| 56 | * @return Result color decoded as Math::Vec4<u8> | ||
| 57 | */ | ||
| 58 | inline const Math::Vec4<u8> DecodeRGBA8(const u8* bytes) { | ||
| 59 | return { bytes[3], bytes[2], bytes[1], bytes[0] }; | ||
| 60 | } | ||
| 61 | |||
| 62 | /** | ||
| 63 | * Decode a color stored in RGB8 format | ||
| 64 | * @param bytes Pointer to encoded source color | ||
| 65 | * @return Result color decoded as Math::Vec4<u8> | ||
| 66 | */ | ||
| 67 | inline const Math::Vec4<u8> DecodeRGB8(const u8* bytes) { | ||
| 68 | return { bytes[2], bytes[1], bytes[0], 255 }; | ||
| 69 | } | ||
| 70 | |||
| 71 | /** | ||
| 72 | * Decode a color stored in RGB565 format | ||
| 73 | * @param bytes Pointer to encoded source color | ||
| 74 | * @return Result color decoded as Math::Vec4<u8> | ||
| 75 | */ | ||
| 76 | inline const Math::Vec4<u8> DecodeRGB565(const u8* bytes) { | ||
| 77 | const u16_le pixel = *reinterpret_cast<const u16_le*>(bytes); | ||
| 78 | return { Convert5To8((pixel >> 11) & 0x1F), Convert6To8((pixel >> 5) & 0x3F), | ||
| 79 | Convert5To8(pixel & 0x1F), 255 }; | ||
| 80 | } | ||
| 81 | |||
| 82 | /** | ||
| 83 | * Decode a color stored in RGB5A1 format | ||
| 84 | * @param bytes Pointer to encoded source color | ||
| 85 | * @return Result color decoded as Math::Vec4<u8> | ||
| 86 | */ | ||
| 87 | inline const Math::Vec4<u8> DecodeRGB5A1(const u8* bytes) { | ||
| 88 | const u16_le pixel = *reinterpret_cast<const u16_le*>(bytes); | ||
| 89 | return { Convert5To8((pixel >> 11) & 0x1F), Convert5To8((pixel >> 6) & 0x1F), | ||
| 90 | Convert5To8((pixel >> 1) & 0x1F), Convert1To8(pixel & 0x1) }; | ||
| 91 | } | ||
| 92 | |||
| 93 | /** | ||
| 94 | * Decode a color stored in RGBA4 format | ||
| 95 | * @param bytes Pointer to encoded source color | ||
| 96 | * @return Result color decoded as Math::Vec4<u8> | ||
| 97 | */ | ||
| 98 | inline const Math::Vec4<u8> DecodeRGBA4(const u8* bytes) { | ||
| 99 | const u16_le pixel = *reinterpret_cast<const u16_le*>(bytes); | ||
| 100 | return { Convert4To8((pixel >> 12) & 0xF), Convert4To8((pixel >> 8) & 0xF), | ||
| 101 | Convert4To8((pixel >> 4) & 0xF), Convert4To8(pixel & 0xF) }; | ||
| 102 | } | ||
| 103 | |||
| 104 | /** | ||
| 105 | * Decode a depth value stored in D16 format | ||
| 106 | * @param bytes Pointer to encoded source value | ||
| 107 | * @return Depth value as an u32 | ||
| 108 | */ | ||
| 109 | inline u32 DecodeD16(const u8* bytes) { | ||
| 110 | return *reinterpret_cast<const u16_le*>(bytes); | ||
| 111 | } | ||
| 112 | |||
| 113 | /** | ||
| 114 | * Decode a depth value stored in D24 format | ||
| 115 | * @param bytes Pointer to encoded source value | ||
| 116 | * @return Depth value as an u32 | ||
| 117 | */ | ||
| 118 | inline u32 DecodeD24(const u8* bytes) { | ||
| 119 | return (bytes[2] << 16) | (bytes[1] << 8) | bytes[0]; | ||
| 120 | } | ||
| 121 | |||
| 122 | /** | ||
| 123 | * Decode a depth value and a stencil value stored in D24S8 format | ||
| 124 | * @param bytes Pointer to encoded source values | ||
| 125 | * @return Resulting values stored as a Math::Vec2 | ||
| 126 | */ | ||
| 127 | inline const Math::Vec2<u32> DecodeD24S8(const u8* bytes) { | ||
| 128 | return { static_cast<u32>((bytes[2] << 16) | (bytes[1] << 8) | bytes[0]), bytes[3] }; | ||
| 129 | } | ||
| 130 | |||
| 131 | /** | ||
| 132 | * Encode a color as RGBA8 format | ||
| 133 | * @param color Source color to encode | ||
| 134 | * @param bytes Destination pointer to store encoded color | ||
| 135 | */ | ||
| 136 | inline void EncodeRGBA8(const Math::Vec4<u8>& color, u8* bytes) { | ||
| 137 | bytes[3] = color.r(); | ||
| 138 | bytes[2] = color.g(); | ||
| 139 | bytes[1] = color.b(); | ||
| 140 | bytes[0] = color.a(); | ||
| 141 | } | ||
| 142 | |||
| 143 | /** | ||
| 144 | * Encode a color as RGB8 format | ||
| 145 | * @param color Source color to encode | ||
| 146 | * @param bytes Destination pointer to store encoded color | ||
| 147 | */ | ||
| 148 | inline void EncodeRGB8(const Math::Vec4<u8>& color, u8* bytes) { | ||
| 149 | bytes[2] = color.r(); | ||
| 150 | bytes[1] = color.g(); | ||
| 151 | bytes[0] = color.b(); | ||
| 152 | } | ||
| 153 | |||
| 154 | /** | ||
| 155 | * Encode a color as RGB565 format | ||
| 156 | * @param color Source color to encode | ||
| 157 | * @param bytes Destination pointer to store encoded color | ||
| 158 | */ | ||
| 159 | inline void EncodeRGB565(const Math::Vec4<u8>& color, u8* bytes) { | ||
| 160 | *reinterpret_cast<u16_le*>(bytes) = (Convert8To5(color.r()) << 11) | | ||
| 161 | (Convert8To6(color.g()) << 5) | Convert8To5(color.b()); | ||
| 162 | } | ||
| 163 | |||
| 164 | /** | ||
| 165 | * Encode a color as RGB5A1 format | ||
| 166 | * @param color Source color to encode | ||
| 167 | * @param bytes Destination pointer to store encoded color | ||
| 168 | */ | ||
| 169 | inline void EncodeRGB5A1(const Math::Vec4<u8>& color, u8* bytes) { | ||
| 170 | *reinterpret_cast<u16_le*>(bytes) = (Convert8To5(color.r()) << 11) | | ||
| 171 | (Convert8To5(color.g()) << 6) | (Convert8To5(color.b()) << 1) | Convert8To1(color.a()); | ||
| 172 | } | ||
| 173 | |||
| 174 | /** | ||
| 175 | * Encode a color as RGBA4 format | ||
| 176 | * @param color Source color to encode | ||
| 177 | * @param bytes Destination pointer to store encoded color | ||
| 178 | */ | ||
| 179 | inline void EncodeRGBA4(const Math::Vec4<u8>& color, u8* bytes) { | ||
| 180 | *reinterpret_cast<u16_le*>(bytes) = (Convert8To4(color.r()) << 12) | | ||
| 181 | (Convert8To4(color.g()) << 8) | (Convert8To4(color.b()) << 4) | Convert8To4(color.a()); | ||
| 182 | } | ||
| 183 | |||
| 184 | /** | ||
| 185 | * Encode a 16 bit depth value as D16 format | ||
| 186 | * @param value 16 bit source depth value to encode | ||
| 187 | * @param bytes Pointer where to store the encoded value | ||
| 188 | */ | ||
| 189 | inline void EncodeD16(u32 value, u8* bytes) { | ||
| 190 | *reinterpret_cast<u16_le*>(bytes) = value & 0xFFFF; | ||
| 191 | } | ||
| 192 | |||
| 193 | /** | ||
| 194 | * Encode a 24 bit depth value as D24 format | ||
| 195 | * @param value 24 bit source depth value to encode | ||
| 196 | * @param bytes Pointer where to store the encoded value | ||
| 197 | */ | ||
| 198 | inline void EncodeD24(u32 value, u8* bytes) { | ||
| 199 | bytes[0] = value & 0xFF; | ||
| 200 | bytes[1] = (value >> 8) & 0xFF; | ||
| 201 | bytes[2] = (value >> 16) & 0xFF; | ||
| 202 | } | ||
| 203 | |||
| 204 | /** | ||
| 205 | * Encode a 24 bit depth and 8 bit stencil values as D24S8 format | ||
| 206 | * @param depth 24 bit source depth value to encode | ||
| 207 | * @param stencil 8 bit source stencil value to encode | ||
| 208 | * @param bytes Pointer where to store the encoded value | ||
| 209 | */ | ||
| 210 | inline void EncodeD24S8(u32 depth, u8 stencil, u8* bytes) { | ||
| 211 | *reinterpret_cast<u32_le*>(bytes) = (stencil << 24) | depth; | ||
| 212 | } | ||
| 213 | |||
| 214 | } // namespace | ||
diff --git a/src/common/vector_math.h b/src/common/vector_math.h new file mode 100644 index 000000000..4928c9bf2 --- /dev/null +++ b/src/common/vector_math.h | |||
| @@ -0,0 +1,640 @@ | |||
| 1 | // Licensed under GPLv2 or any later version | ||
| 2 | // Refer to the license.txt file included. | ||
| 3 | |||
| 4 | |||
| 5 | // Copyright 2014 Tony Wasserka | ||
| 6 | // All rights reserved. | ||
| 7 | // | ||
| 8 | // Redistribution and use in source and binary forms, with or without | ||
| 9 | // modification, are permitted provided that the following conditions are met: | ||
| 10 | // | ||
| 11 | // * Redistributions of source code must retain the above copyright | ||
| 12 | // notice, this list of conditions and the following disclaimer. | ||
| 13 | // * Redistributions in binary form must reproduce the above copyright | ||
| 14 | // notice, this list of conditions and the following disclaimer in the | ||
| 15 | // documentation and/or other materials provided with the distribution. | ||
| 16 | // * Neither the name of the owner nor the names of its contributors may | ||
| 17 | // be used to endorse or promote products derived from this software | ||
| 18 | // without specific prior written permission. | ||
| 19 | // | ||
| 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
| 21 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
| 22 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
| 23 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
| 24 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
| 26 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
| 27 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
| 28 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
| 29 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
| 30 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| 31 | |||
| 32 | #pragma once | ||
| 33 | |||
| 34 | #include <cmath> | ||
| 35 | |||
| 36 | namespace Math { | ||
| 37 | |||
| 38 | template<typename T> class Vec2; | ||
| 39 | template<typename T> class Vec3; | ||
| 40 | template<typename T> class Vec4; | ||
| 41 | |||
| 42 | template<typename T> | ||
| 43 | static inline Vec2<T> MakeVec(const T& x, const T& y); | ||
| 44 | template<typename T> | ||
| 45 | static inline Vec3<T> MakeVec(const T& x, const T& y, const T& z); | ||
| 46 | template<typename T> | ||
| 47 | static inline Vec4<T> MakeVec(const T& x, const T& y, const T& z, const T& w); | ||
| 48 | |||
| 49 | |||
| 50 | template<typename T> | ||
| 51 | class Vec2 { | ||
| 52 | public: | ||
| 53 | T x; | ||
| 54 | T y; | ||
| 55 | |||
| 56 | T* AsArray() { return &x; } | ||
| 57 | |||
| 58 | Vec2() = default; | ||
| 59 | Vec2(const T a[2]) : x(a[0]), y(a[1]) {} | ||
| 60 | Vec2(const T& _x, const T& _y) : x(_x), y(_y) {} | ||
| 61 | |||
| 62 | template<typename T2> | ||
| 63 | Vec2<T2> Cast() const { | ||
| 64 | return Vec2<T2>((T2)x, (T2)y); | ||
| 65 | } | ||
| 66 | |||
| 67 | static Vec2 AssignToAll(const T& f) | ||
| 68 | { | ||
| 69 | return Vec2<T>(f, f); | ||
| 70 | } | ||
| 71 | |||
| 72 | void Write(T a[2]) | ||
| 73 | { | ||
| 74 | a[0] = x; a[1] = y; | ||
| 75 | } | ||
| 76 | |||
| 77 | Vec2<decltype(T{}+T{})> operator +(const Vec2& other) const | ||
| 78 | { | ||
| 79 | return MakeVec(x+other.x, y+other.y); | ||
| 80 | } | ||
| 81 | void operator += (const Vec2 &other) | ||
| 82 | { | ||
| 83 | x+=other.x; y+=other.y; | ||
| 84 | } | ||
| 85 | Vec2<decltype(T{}-T{})> operator -(const Vec2& other) const | ||
| 86 | { | ||
| 87 | return MakeVec(x-other.x, y-other.y); | ||
| 88 | } | ||
| 89 | void operator -= (const Vec2& other) | ||
| 90 | { | ||
| 91 | x-=other.x; y-=other.y; | ||
| 92 | } | ||
| 93 | Vec2<decltype(-T{})> operator -() const | ||
| 94 | { | ||
| 95 | return MakeVec(-x,-y); | ||
| 96 | } | ||
| 97 | Vec2<decltype(T{}*T{})> operator * (const Vec2& other) const | ||
| 98 | { | ||
| 99 | return MakeVec(x*other.x, y*other.y); | ||
| 100 | } | ||
| 101 | template<typename V> | ||
| 102 | Vec2<decltype(T{}*V{})> operator * (const V& f) const | ||
| 103 | { | ||
| 104 | return MakeVec(x*f,y*f); | ||
| 105 | } | ||
| 106 | template<typename V> | ||
| 107 | void operator *= (const V& f) | ||
| 108 | { | ||
| 109 | x*=f; y*=f; | ||
| 110 | } | ||
| 111 | template<typename V> | ||
| 112 | Vec2<decltype(T{}/V{})> operator / (const V& f) const | ||
| 113 | { | ||
| 114 | return MakeVec(x/f,y/f); | ||
| 115 | } | ||
| 116 | template<typename V> | ||
| 117 | void operator /= (const V& f) | ||
| 118 | { | ||
| 119 | *this = *this / f; | ||
| 120 | } | ||
| 121 | |||
| 122 | T Length2() const | ||
| 123 | { | ||
| 124 | return x*x + y*y; | ||
| 125 | } | ||
| 126 | |||
| 127 | // Only implemented for T=float | ||
| 128 | float Length() const; | ||
| 129 | void SetLength(const float l); | ||
| 130 | Vec2 WithLength(const float l) const; | ||
| 131 | float Distance2To(Vec2 &other); | ||
| 132 | Vec2 Normalized() const; | ||
| 133 | float Normalize(); // returns the previous length, which is often useful | ||
| 134 | |||
| 135 | T& operator [] (int i) //allow vector[1] = 3 (vector.y=3) | ||
| 136 | { | ||
| 137 | return *((&x) + i); | ||
| 138 | } | ||
| 139 | T operator [] (const int i) const | ||
| 140 | { | ||
| 141 | return *((&x) + i); | ||
| 142 | } | ||
| 143 | |||
| 144 | void SetZero() | ||
| 145 | { | ||
| 146 | x=0; y=0; | ||
| 147 | } | ||
| 148 | |||
| 149 | // Common aliases: UV (texel coordinates), ST (texture coordinates) | ||
| 150 | T& u() { return x; } | ||
| 151 | T& v() { return y; } | ||
| 152 | T& s() { return x; } | ||
| 153 | T& t() { return y; } | ||
| 154 | |||
| 155 | const T& u() const { return x; } | ||
| 156 | const T& v() const { return y; } | ||
| 157 | const T& s() const { return x; } | ||
| 158 | const T& t() const { return y; } | ||
| 159 | |||
| 160 | // swizzlers - create a subvector of specific components | ||
| 161 | const Vec2 yx() const { return Vec2(y, x); } | ||
| 162 | const Vec2 vu() const { return Vec2(y, x); } | ||
| 163 | const Vec2 ts() const { return Vec2(y, x); } | ||
| 164 | }; | ||
| 165 | |||
| 166 | template<typename T, typename V> | ||
| 167 | Vec2<T> operator * (const V& f, const Vec2<T>& vec) | ||
| 168 | { | ||
| 169 | return Vec2<T>(f*vec.x,f*vec.y); | ||
| 170 | } | ||
| 171 | |||
| 172 | typedef Vec2<float> Vec2f; | ||
| 173 | |||
| 174 | template<typename T> | ||
| 175 | class Vec3 | ||
| 176 | { | ||
| 177 | public: | ||
| 178 | T x; | ||
| 179 | T y; | ||
| 180 | T z; | ||
| 181 | |||
| 182 | T* AsArray() { return &x; } | ||
| 183 | |||
| 184 | Vec3() = default; | ||
| 185 | Vec3(const T a[3]) : x(a[0]), y(a[1]), z(a[2]) {} | ||
| 186 | Vec3(const T& _x, const T& _y, const T& _z) : x(_x), y(_y), z(_z) {} | ||
| 187 | |||
| 188 | template<typename T2> | ||
| 189 | Vec3<T2> Cast() const { | ||
| 190 | return MakeVec<T2>((T2)x, (T2)y, (T2)z); | ||
| 191 | } | ||
| 192 | |||
| 193 | // Only implemented for T=int and T=float | ||
| 194 | static Vec3 FromRGB(unsigned int rgb); | ||
| 195 | unsigned int ToRGB() const; // alpha bits set to zero | ||
| 196 | |||
| 197 | static Vec3 AssignToAll(const T& f) | ||
| 198 | { | ||
| 199 | return MakeVec(f, f, f); | ||
| 200 | } | ||
| 201 | |||
| 202 | void Write(T a[3]) | ||
| 203 | { | ||
| 204 | a[0] = x; a[1] = y; a[2] = z; | ||
| 205 | } | ||
| 206 | |||
| 207 | Vec3<decltype(T{}+T{})> operator +(const Vec3 &other) const | ||
| 208 | { | ||
| 209 | return MakeVec(x+other.x, y+other.y, z+other.z); | ||
| 210 | } | ||
| 211 | void operator += (const Vec3 &other) | ||
| 212 | { | ||
| 213 | x+=other.x; y+=other.y; z+=other.z; | ||
| 214 | } | ||
| 215 | Vec3<decltype(T{}-T{})> operator -(const Vec3 &other) const | ||
| 216 | { | ||
| 217 | return MakeVec(x-other.x, y-other.y, z-other.z); | ||
| 218 | } | ||
| 219 | void operator -= (const Vec3 &other) | ||
| 220 | { | ||
| 221 | x-=other.x; y-=other.y; z-=other.z; | ||
| 222 | } | ||
| 223 | Vec3<decltype(-T{})> operator -() const | ||
| 224 | { | ||
| 225 | return MakeVec(-x,-y,-z); | ||
| 226 | } | ||
| 227 | Vec3<decltype(T{}*T{})> operator * (const Vec3 &other) const | ||
| 228 | { | ||
| 229 | return MakeVec(x*other.x, y*other.y, z*other.z); | ||
| 230 | } | ||
| 231 | template<typename V> | ||
| 232 | Vec3<decltype(T{}*V{})> operator * (const V& f) const | ||
| 233 | { | ||
| 234 | return MakeVec(x*f,y*f,z*f); | ||
| 235 | } | ||
| 236 | template<typename V> | ||
| 237 | void operator *= (const V& f) | ||
| 238 | { | ||
| 239 | x*=f; y*=f; z*=f; | ||
| 240 | } | ||
| 241 | template<typename V> | ||
| 242 | Vec3<decltype(T{}/V{})> operator / (const V& f) const | ||
| 243 | { | ||
| 244 | return MakeVec(x/f,y/f,z/f); | ||
| 245 | } | ||
| 246 | template<typename V> | ||
| 247 | void operator /= (const V& f) | ||
| 248 | { | ||
| 249 | *this = *this / f; | ||
| 250 | } | ||
| 251 | |||
| 252 | T Length2() const | ||
| 253 | { | ||
| 254 | return x*x + y*y + z*z; | ||
| 255 | } | ||
| 256 | |||
| 257 | // Only implemented for T=float | ||
| 258 | float Length() const; | ||
| 259 | void SetLength(const float l); | ||
| 260 | Vec3 WithLength(const float l) const; | ||
| 261 | float Distance2To(Vec3 &other); | ||
| 262 | Vec3 Normalized() const; | ||
| 263 | float Normalize(); // returns the previous length, which is often useful | ||
| 264 | |||
| 265 | T& operator [] (int i) //allow vector[2] = 3 (vector.z=3) | ||
| 266 | { | ||
| 267 | return *((&x) + i); | ||
| 268 | } | ||
| 269 | T operator [] (const int i) const | ||
| 270 | { | ||
| 271 | return *((&x) + i); | ||
| 272 | } | ||
| 273 | |||
| 274 | void SetZero() | ||
| 275 | { | ||
| 276 | x=0; y=0; z=0; | ||
| 277 | } | ||
| 278 | |||
| 279 | // Common aliases: UVW (texel coordinates), RGB (colors), STQ (texture coordinates) | ||
| 280 | T& u() { return x; } | ||
| 281 | T& v() { return y; } | ||
| 282 | T& w() { return z; } | ||
| 283 | |||
| 284 | T& r() { return x; } | ||
| 285 | T& g() { return y; } | ||
| 286 | T& b() { return z; } | ||
| 287 | |||
| 288 | T& s() { return x; } | ||
| 289 | T& t() { return y; } | ||
| 290 | T& q() { return z; } | ||
| 291 | |||
| 292 | const T& u() const { return x; } | ||
| 293 | const T& v() const { return y; } | ||
| 294 | const T& w() const { return z; } | ||
| 295 | |||
| 296 | const T& r() const { return x; } | ||
| 297 | const T& g() const { return y; } | ||
| 298 | const T& b() const { return z; } | ||
| 299 | |||
| 300 | const T& s() const { return x; } | ||
| 301 | const T& t() const { return y; } | ||
| 302 | const T& q() const { return z; } | ||
| 303 | |||
| 304 | // swizzlers - create a subvector of specific components | ||
| 305 | // e.g. Vec2 uv() { return Vec2(x,y); } | ||
| 306 | // _DEFINE_SWIZZLER2 defines a single such function, DEFINE_SWIZZLER2 defines all of them for all component names (x<->r) and permutations (xy<->yx) | ||
| 307 | #define _DEFINE_SWIZZLER2(a, b, name) const Vec2<T> name() const { return Vec2<T>(a, b); } | ||
| 308 | #define DEFINE_SWIZZLER2(a, b, a2, b2, a3, b3, a4, b4) \ | ||
| 309 | _DEFINE_SWIZZLER2(a, b, a##b); \ | ||
| 310 | _DEFINE_SWIZZLER2(a, b, a2##b2); \ | ||
| 311 | _DEFINE_SWIZZLER2(a, b, a3##b3); \ | ||
| 312 | _DEFINE_SWIZZLER2(a, b, a4##b4); \ | ||
| 313 | _DEFINE_SWIZZLER2(b, a, b##a); \ | ||
| 314 | _DEFINE_SWIZZLER2(b, a, b2##a2); \ | ||
| 315 | _DEFINE_SWIZZLER2(b, a, b3##a3); \ | ||
| 316 | _DEFINE_SWIZZLER2(b, a, b4##a4) | ||
| 317 | |||
| 318 | DEFINE_SWIZZLER2(x, y, r, g, u, v, s, t); | ||
| 319 | DEFINE_SWIZZLER2(x, z, r, b, u, w, s, q); | ||
| 320 | DEFINE_SWIZZLER2(y, z, g, b, v, w, t, q); | ||
| 321 | #undef DEFINE_SWIZZLER2 | ||
| 322 | #undef _DEFINE_SWIZZLER2 | ||
| 323 | }; | ||
| 324 | |||
| 325 | template<typename T, typename V> | ||
| 326 | Vec3<T> operator * (const V& f, const Vec3<T>& vec) | ||
| 327 | { | ||
| 328 | return Vec3<T>(f*vec.x,f*vec.y,f*vec.z); | ||
| 329 | } | ||
| 330 | |||
| 331 | template<> | ||
| 332 | inline float Vec3<float>::Length() const { | ||
| 333 | return std::sqrt(x * x + y * y + z * z); | ||
| 334 | } | ||
| 335 | |||
| 336 | template<> | ||
| 337 | inline Vec3<float> Vec3<float>::Normalized() const { | ||
| 338 | return *this / Length(); | ||
| 339 | } | ||
| 340 | |||
| 341 | |||
| 342 | typedef Vec3<float> Vec3f; | ||
| 343 | |||
| 344 | template<typename T> | ||
| 345 | class Vec4 | ||
| 346 | { | ||
| 347 | public: | ||
| 348 | T x; | ||
| 349 | T y; | ||
| 350 | T z; | ||
| 351 | T w; | ||
| 352 | |||
| 353 | T* AsArray() { return &x; } | ||
| 354 | |||
| 355 | Vec4() = default; | ||
| 356 | Vec4(const T a[4]) : x(a[0]), y(a[1]), z(a[2]), w(a[3]) {} | ||
| 357 | Vec4(const T& _x, const T& _y, const T& _z, const T& _w) : x(_x), y(_y), z(_z), w(_w) {} | ||
| 358 | |||
| 359 | template<typename T2> | ||
| 360 | Vec4<T2> Cast() const { | ||
| 361 | return Vec4<T2>((T2)x, (T2)y, (T2)z, (T2)w); | ||
| 362 | } | ||
| 363 | |||
| 364 | // Only implemented for T=int and T=float | ||
| 365 | static Vec4 FromRGBA(unsigned int rgba); | ||
| 366 | unsigned int ToRGBA() const; | ||
| 367 | |||
| 368 | static Vec4 AssignToAll(const T& f) { | ||
| 369 | return Vec4<T>(f, f, f, f); | ||
| 370 | } | ||
| 371 | |||
| 372 | void Write(T a[4]) | ||
| 373 | { | ||
| 374 | a[0] = x; a[1] = y; a[2] = z; a[3] = w; | ||
| 375 | } | ||
| 376 | |||
| 377 | Vec4<decltype(T{}+T{})> operator +(const Vec4& other) const | ||
| 378 | { | ||
| 379 | return MakeVec(x+other.x, y+other.y, z+other.z, w+other.w); | ||
| 380 | } | ||
| 381 | void operator += (const Vec4& other) | ||
| 382 | { | ||
| 383 | x+=other.x; y+=other.y; z+=other.z; w+=other.w; | ||
| 384 | } | ||
| 385 | Vec4<decltype(T{}-T{})> operator -(const Vec4 &other) const | ||
| 386 | { | ||
| 387 | return MakeVec(x-other.x, y-other.y, z-other.z, w-other.w); | ||
| 388 | } | ||
| 389 | void operator -= (const Vec4 &other) | ||
| 390 | { | ||
| 391 | x-=other.x; y-=other.y; z-=other.z; w-=other.w; | ||
| 392 | } | ||
| 393 | Vec4<decltype(-T{})> operator -() const | ||
| 394 | { | ||
| 395 | return MakeVec(-x,-y,-z,-w); | ||
| 396 | } | ||
| 397 | Vec4<decltype(T{}*T{})> operator * (const Vec4 &other) const | ||
| 398 | { | ||
| 399 | return MakeVec(x*other.x, y*other.y, z*other.z, w*other.w); | ||
| 400 | } | ||
| 401 | template<typename V> | ||
| 402 | Vec4<decltype(T{}*V{})> operator * (const V& f) const | ||
| 403 | { | ||
| 404 | return MakeVec(x*f,y*f,z*f,w*f); | ||
| 405 | } | ||
| 406 | template<typename V> | ||
| 407 | void operator *= (const V& f) | ||
| 408 | { | ||
| 409 | x*=f; y*=f; z*=f; w*=f; | ||
| 410 | } | ||
| 411 | template<typename V> | ||
| 412 | Vec4<decltype(T{}/V{})> operator / (const V& f) const | ||
| 413 | { | ||
| 414 | return MakeVec(x/f,y/f,z/f,w/f); | ||
| 415 | } | ||
| 416 | template<typename V> | ||
| 417 | void operator /= (const V& f) | ||
| 418 | { | ||
| 419 | *this = *this / f; | ||
| 420 | } | ||
| 421 | |||
| 422 | T Length2() const | ||
| 423 | { | ||
| 424 | return x*x + y*y + z*z + w*w; | ||
| 425 | } | ||
| 426 | |||
| 427 | // Only implemented for T=float | ||
| 428 | float Length() const; | ||
| 429 | void SetLength(const float l); | ||
| 430 | Vec4 WithLength(const float l) const; | ||
| 431 | float Distance2To(Vec4 &other); | ||
| 432 | Vec4 Normalized() const; | ||
| 433 | float Normalize(); // returns the previous length, which is often useful | ||
| 434 | |||
| 435 | T& operator [] (int i) //allow vector[2] = 3 (vector.z=3) | ||
| 436 | { | ||
| 437 | return *((&x) + i); | ||
| 438 | } | ||
| 439 | T operator [] (const int i) const | ||
| 440 | { | ||
| 441 | return *((&x) + i); | ||
| 442 | } | ||
| 443 | |||
| 444 | void SetZero() | ||
| 445 | { | ||
| 446 | x=0; y=0; z=0; | ||
| 447 | } | ||
| 448 | |||
| 449 | // Common alias: RGBA (colors) | ||
| 450 | T& r() { return x; } | ||
| 451 | T& g() { return y; } | ||
| 452 | T& b() { return z; } | ||
| 453 | T& a() { return w; } | ||
| 454 | |||
| 455 | const T& r() const { return x; } | ||
| 456 | const T& g() const { return y; } | ||
| 457 | const T& b() const { return z; } | ||
| 458 | const T& a() const { return w; } | ||
| 459 | |||
| 460 | // Swizzlers - Create a subvector of specific components | ||
| 461 | // e.g. Vec2 uv() { return Vec2(x,y); } | ||
| 462 | |||
| 463 | // _DEFINE_SWIZZLER2 defines a single such function | ||
| 464 | // DEFINE_SWIZZLER2_COMP1 defines one-component functions for all component names (x<->r) | ||
| 465 | // DEFINE_SWIZZLER2_COMP2 defines two component functions for all component names (x<->r) and permutations (xy<->yx) | ||
| 466 | #define _DEFINE_SWIZZLER2(a, b, name) const Vec2<T> name() const { return Vec2<T>(a, b); } | ||
| 467 | #define DEFINE_SWIZZLER2_COMP1(a, a2) \ | ||
| 468 | _DEFINE_SWIZZLER2(a, a, a##a); \ | ||
| 469 | _DEFINE_SWIZZLER2(a, a, a2##a2) | ||
| 470 | #define DEFINE_SWIZZLER2_COMP2(a, b, a2, b2) \ | ||
| 471 | _DEFINE_SWIZZLER2(a, b, a##b); \ | ||
| 472 | _DEFINE_SWIZZLER2(a, b, a2##b2); \ | ||
| 473 | _DEFINE_SWIZZLER2(b, a, b##a); \ | ||
| 474 | _DEFINE_SWIZZLER2(b, a, b2##a2) | ||
| 475 | |||
| 476 | DEFINE_SWIZZLER2_COMP2(x, y, r, g); | ||
| 477 | DEFINE_SWIZZLER2_COMP2(x, z, r, b); | ||
| 478 | DEFINE_SWIZZLER2_COMP2(x, w, r, a); | ||
| 479 | DEFINE_SWIZZLER2_COMP2(y, z, g, b); | ||
| 480 | DEFINE_SWIZZLER2_COMP2(y, w, g, a); | ||
| 481 | DEFINE_SWIZZLER2_COMP2(z, w, b, a); | ||
| 482 | DEFINE_SWIZZLER2_COMP1(x, r); | ||
| 483 | DEFINE_SWIZZLER2_COMP1(y, g); | ||
| 484 | DEFINE_SWIZZLER2_COMP1(z, b); | ||
| 485 | DEFINE_SWIZZLER2_COMP1(w, a); | ||
| 486 | #undef DEFINE_SWIZZLER2_COMP1 | ||
| 487 | #undef DEFINE_SWIZZLER2_COMP2 | ||
| 488 | #undef _DEFINE_SWIZZLER2 | ||
| 489 | |||
| 490 | #define _DEFINE_SWIZZLER3(a, b, c, name) const Vec3<T> name() const { return Vec3<T>(a, b, c); } | ||
| 491 | #define DEFINE_SWIZZLER3_COMP1(a, a2) \ | ||
| 492 | _DEFINE_SWIZZLER3(a, a, a, a##a##a); \ | ||
| 493 | _DEFINE_SWIZZLER3(a, a, a, a2##a2##a2) | ||
| 494 | #define DEFINE_SWIZZLER3_COMP3(a, b, c, a2, b2, c2) \ | ||
| 495 | _DEFINE_SWIZZLER3(a, b, c, a##b##c); \ | ||
| 496 | _DEFINE_SWIZZLER3(a, c, b, a##c##b); \ | ||
| 497 | _DEFINE_SWIZZLER3(b, a, c, b##a##c); \ | ||
| 498 | _DEFINE_SWIZZLER3(b, c, a, b##c##a); \ | ||
| 499 | _DEFINE_SWIZZLER3(c, a, b, c##a##b); \ | ||
| 500 | _DEFINE_SWIZZLER3(c, b, a, c##b##a); \ | ||
| 501 | _DEFINE_SWIZZLER3(a, b, c, a2##b2##c2); \ | ||
| 502 | _DEFINE_SWIZZLER3(a, c, b, a2##c2##b2); \ | ||
| 503 | _DEFINE_SWIZZLER3(b, a, c, b2##a2##c2); \ | ||
| 504 | _DEFINE_SWIZZLER3(b, c, a, b2##c2##a2); \ | ||
| 505 | _DEFINE_SWIZZLER3(c, a, b, c2##a2##b2); \ | ||
| 506 | _DEFINE_SWIZZLER3(c, b, a, c2##b2##a2) | ||
| 507 | |||
| 508 | DEFINE_SWIZZLER3_COMP3(x, y, z, r, g, b); | ||
| 509 | DEFINE_SWIZZLER3_COMP3(x, y, w, r, g, a); | ||
| 510 | DEFINE_SWIZZLER3_COMP3(x, z, w, r, b, a); | ||
| 511 | DEFINE_SWIZZLER3_COMP3(y, z, w, g, b, a); | ||
| 512 | DEFINE_SWIZZLER3_COMP1(x, r); | ||
| 513 | DEFINE_SWIZZLER3_COMP1(y, g); | ||
| 514 | DEFINE_SWIZZLER3_COMP1(z, b); | ||
| 515 | DEFINE_SWIZZLER3_COMP1(w, a); | ||
| 516 | #undef DEFINE_SWIZZLER3_COMP1 | ||
| 517 | #undef DEFINE_SWIZZLER3_COMP3 | ||
| 518 | #undef _DEFINE_SWIZZLER3 | ||
| 519 | }; | ||
| 520 | |||
| 521 | |||
| 522 | template<typename T, typename V> | ||
| 523 | Vec4<decltype(V{}*T{})> operator * (const V& f, const Vec4<T>& vec) | ||
| 524 | { | ||
| 525 | return MakeVec(f*vec.x,f*vec.y,f*vec.z,f*vec.w); | ||
| 526 | } | ||
| 527 | |||
| 528 | typedef Vec4<float> Vec4f; | ||
| 529 | |||
| 530 | |||
| 531 | template<typename T> | ||
| 532 | static inline decltype(T{}*T{}+T{}*T{}) Dot(const Vec2<T>& a, const Vec2<T>& b) | ||
| 533 | { | ||
| 534 | return a.x*b.x + a.y*b.y; | ||
| 535 | } | ||
| 536 | |||
| 537 | template<typename T> | ||
| 538 | static inline decltype(T{}*T{}+T{}*T{}) Dot(const Vec3<T>& a, const Vec3<T>& b) | ||
| 539 | { | ||
| 540 | return a.x*b.x + a.y*b.y + a.z*b.z; | ||
| 541 | } | ||
| 542 | |||
| 543 | template<typename T> | ||
| 544 | static inline decltype(T{}*T{}+T{}*T{}) Dot(const Vec4<T>& a, const Vec4<T>& b) | ||
| 545 | { | ||
| 546 | return a.x*b.x + a.y*b.y + a.z*b.z + a.w*b.w; | ||
| 547 | } | ||
| 548 | |||
| 549 | template<typename T> | ||
| 550 | static inline Vec3<decltype(T{}*T{}-T{}*T{})> Cross(const Vec3<T>& a, const Vec3<T>& b) | ||
| 551 | { | ||
| 552 | return MakeVec(a.y*b.z-a.z*b.y, a.z*b.x-a.x*b.z, a.x*b.y-a.y*b.x); | ||
| 553 | } | ||
| 554 | |||
| 555 | // linear interpolation via float: 0.0=begin, 1.0=end | ||
| 556 | template<typename X> | ||
| 557 | static inline decltype(X{}*float{}+X{}*float{}) Lerp(const X& begin, const X& end, const float t) | ||
| 558 | { | ||
| 559 | return begin*(1.f-t) + end*t; | ||
| 560 | } | ||
| 561 | |||
| 562 | // linear interpolation via int: 0=begin, base=end | ||
| 563 | template<typename X, int base> | ||
| 564 | static inline decltype((X{}*int{}+X{}*int{}) / base) LerpInt(const X& begin, const X& end, const int t) | ||
| 565 | { | ||
| 566 | return (begin*(base-t) + end*t) / base; | ||
| 567 | } | ||
| 568 | |||
| 569 | // Utility vector factories | ||
| 570 | template<typename T> | ||
| 571 | static inline Vec2<T> MakeVec(const T& x, const T& y) | ||
| 572 | { | ||
| 573 | return Vec2<T>{x, y}; | ||
| 574 | } | ||
| 575 | |||
| 576 | template<typename T> | ||
| 577 | static inline Vec3<T> MakeVec(const T& x, const T& y, const T& z) | ||
| 578 | { | ||
| 579 | return Vec3<T>{x, y, z}; | ||
| 580 | } | ||
| 581 | |||
| 582 | template<typename T> | ||
| 583 | static inline Vec4<T> MakeVec(const T& x, const T& y, const Vec2<T>& zw) | ||
| 584 | { | ||
| 585 | return MakeVec(x, y, zw[0], zw[1]); | ||
| 586 | } | ||
| 587 | |||
| 588 | template<typename T> | ||
| 589 | static inline Vec3<T> MakeVec(const Vec2<T>& xy, const T& z) | ||
| 590 | { | ||
| 591 | return MakeVec(xy[0], xy[1], z); | ||
| 592 | } | ||
| 593 | |||
| 594 | template<typename T> | ||
| 595 | static inline Vec3<T> MakeVec(const T& x, const Vec2<T>& yz) | ||
| 596 | { | ||
| 597 | return MakeVec(x, yz[0], yz[1]); | ||
| 598 | } | ||
| 599 | |||
| 600 | template<typename T> | ||
| 601 | static inline Vec4<T> MakeVec(const T& x, const T& y, const T& z, const T& w) | ||
| 602 | { | ||
| 603 | return Vec4<T>{x, y, z, w}; | ||
| 604 | } | ||
| 605 | |||
| 606 | template<typename T> | ||
| 607 | static inline Vec4<T> MakeVec(const Vec2<T>& xy, const T& z, const T& w) | ||
| 608 | { | ||
| 609 | return MakeVec(xy[0], xy[1], z, w); | ||
| 610 | } | ||
| 611 | |||
| 612 | template<typename T> | ||
| 613 | static inline Vec4<T> MakeVec(const T& x, const Vec2<T>& yz, const T& w) | ||
| 614 | { | ||
| 615 | return MakeVec(x, yz[0], yz[1], w); | ||
| 616 | } | ||
| 617 | |||
| 618 | // NOTE: This has priority over "Vec2<Vec2<T>> MakeVec(const Vec2<T>& x, const Vec2<T>& y)". | ||
| 619 | // Even if someone wanted to use an odd object like Vec2<Vec2<T>>, the compiler would error | ||
| 620 | // out soon enough due to misuse of the returned structure. | ||
| 621 | template<typename T> | ||
| 622 | static inline Vec4<T> MakeVec(const Vec2<T>& xy, const Vec2<T>& zw) | ||
| 623 | { | ||
| 624 | return MakeVec(xy[0], xy[1], zw[0], zw[1]); | ||
| 625 | } | ||
| 626 | |||
| 627 | template<typename T> | ||
| 628 | static inline Vec4<T> MakeVec(const Vec3<T>& xyz, const T& w) | ||
| 629 | { | ||
| 630 | return MakeVec(xyz[0], xyz[1], xyz[2], w); | ||
| 631 | } | ||
| 632 | |||
| 633 | template<typename T> | ||
| 634 | static inline Vec4<T> MakeVec(const T& x, const Vec3<T>& yzw) | ||
| 635 | { | ||
| 636 | return MakeVec(x, yzw[0], yzw[1], yzw[2]); | ||
| 637 | } | ||
| 638 | |||
| 639 | |||
| 640 | } // namespace | ||