# Additions
- libpicasso (shader compiling) - linearAllocator template (for std::vector) - palladium vec and mat api - iron (2d renderer)
This commit is contained in:
@@ -4,6 +4,8 @@
|
||||
#include <citro3d.h>
|
||||
|
||||
#include <amethyst/asset.hpp>
|
||||
#include <amethyst/maths/mat.hpp>
|
||||
#include <amethyst/types.hpp>
|
||||
#include <string>
|
||||
|
||||
namespace amy {
|
||||
@@ -36,13 +38,18 @@ class c3d {
|
||||
class shader : public asset {
|
||||
public:
|
||||
shader(const std::string& path);
|
||||
shader() {}
|
||||
~shader();
|
||||
|
||||
void load(const std::string& path);
|
||||
void load(const std::vector<uc>& data);
|
||||
void compile(const std::string& code);
|
||||
void use();
|
||||
void input(int reg, GPU_FORMATS f, int num);
|
||||
void input(GPU_FORMATS f, int num) { input(m_reg++, f, num); }
|
||||
void setMat4(int loc, C3D_Mtx* m);
|
||||
void setMat4(int loc, const mat4& m);
|
||||
int loc(const std::string& name);
|
||||
|
||||
private:
|
||||
C3D_AttrInfo m_info;
|
||||
@@ -72,5 +79,9 @@ class c3d {
|
||||
static void endFrame();
|
||||
static screen* createScreen(gfxScreen_t screen, gfx3dSide_t side = GFX_LEFT);
|
||||
static void deleteScreen(screen* screen);
|
||||
static void drawArrays(int start, int count,
|
||||
GPU_Primitive_t prim = GPU_TRIANGLES);
|
||||
static void drawElements(int count, const void* idx_ptr, int type = GPU_SHORT,
|
||||
GPU_Primitive_t prim = GPU_TRIANGLES);
|
||||
};
|
||||
} // namespace amy
|
||||
44
include/amethyst/iron.hpp
Normal file
44
include/amethyst/iron.hpp
Normal file
@@ -0,0 +1,44 @@
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <amethyst/c3d.hpp>
|
||||
#include <amethyst/linearAlloc.hpp>
|
||||
#include <amethyst/maths/mat.hpp>
|
||||
#include <amethyst/maths/vec.hpp>
|
||||
|
||||
namespace amy {
|
||||
class iron {
|
||||
public:
|
||||
struct vertex {
|
||||
vertex(float x, float y, float u, float v, u32 clr) {
|
||||
pos.x = x;
|
||||
pos.y = y;
|
||||
uv.x = x;
|
||||
uv.y = y;
|
||||
color = clr;
|
||||
}
|
||||
vertex() {}
|
||||
|
||||
amy::fvec2 pos;
|
||||
amy::fvec2 uv;
|
||||
u32 color = 0;
|
||||
};
|
||||
iron() = default;
|
||||
~iron() = default;
|
||||
|
||||
static void init();
|
||||
static void newFrame();
|
||||
static void drawOn(c3d::screen* screen);
|
||||
|
||||
private:
|
||||
static void setupShader();
|
||||
static void fragConfig();
|
||||
|
||||
static std::vector<vertex, linearAllocator<vertex>> m_vbuf;
|
||||
static std::vector<u16, linearAllocator<u16>> m_ibuf;
|
||||
static int uLocProj;
|
||||
static c3d::shader* m_shader;
|
||||
static mat4 m_mtx;
|
||||
static int m_idx, m_vtx;
|
||||
};
|
||||
} // namespace amy
|
||||
36
include/amethyst/linearAlloc.hpp
Normal file
36
include/amethyst/linearAlloc.hpp
Normal file
@@ -0,0 +1,36 @@
|
||||
#pragma once
|
||||
|
||||
#include <3ds.h>
|
||||
|
||||
#include <memory>
|
||||
#include <stdexcept>
|
||||
|
||||
// Custom C++ Allocator class to interface with libctru linear heap memory
|
||||
// based on this guide:
|
||||
// https://johnfarrier.com/custom-allocators-in-c-high-performance-memory-management/
|
||||
|
||||
namespace amy {
|
||||
template <typename T>
|
||||
class linearAllocator {
|
||||
public:
|
||||
using value_type = T;
|
||||
linearAllocator() = default;
|
||||
template <typename U>
|
||||
constexpr linearAllocator(const linearAllocator<U>&) noexcept {}
|
||||
|
||||
T* allocate(std::size_t n) {
|
||||
if (n > std::allocator_traits<linearAllocator>::max_size(*this)) {
|
||||
throw std::bad_alloc();
|
||||
}
|
||||
return static_cast<T*>(linearAlloc(n * sizeof(T)));
|
||||
}
|
||||
void deallocate(T* p, std::size_t) noexcept { linearFree(p); }
|
||||
|
||||
friend bool operator==(const linearAllocator, const linearAllocator) {
|
||||
return true;
|
||||
}
|
||||
friend bool operator!=(const linearAllocator, const linearAllocator) {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
} // namespace amy
|
||||
144
include/amethyst/maths/mat.hpp
Normal file
144
include/amethyst/maths/mat.hpp
Normal file
@@ -0,0 +1,144 @@
|
||||
#pragma once
|
||||
|
||||
/*
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2024 - 2025 tobid7
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <amethyst/maths/vec3.hpp>
|
||||
#include <array>
|
||||
#include <cmath>
|
||||
#include <numbers>
|
||||
|
||||
namespace amy {
|
||||
namespace numbers {
|
||||
constexpr float Tau = std::numbers::pi * 2.f;
|
||||
}
|
||||
constexpr float Radians(float v) { return v * (numbers::Tau / 360.0f); }
|
||||
/**
|
||||
* Minimal Mtx4 Lib that precomputes
|
||||
* basic stuff stuff at compiletime
|
||||
*
|
||||
* This Lib includes Patches for work with Citro3D as well
|
||||
*
|
||||
* @note That this is not a full Matrix Library
|
||||
*/
|
||||
|
||||
struct mat4 {
|
||||
std::array<float, 16> m;
|
||||
constexpr mat4() : m{} {}
|
||||
constexpr static mat4 diagonal(float x, float y, float z, float w) {
|
||||
mat4 ret;
|
||||
ret(0, 0) = x;
|
||||
ret(1, 1) = y;
|
||||
ret(2, 2) = z;
|
||||
ret(3, 3) = w;
|
||||
return ret;
|
||||
}
|
||||
constexpr static mat4 identity() { return diagonal(1, 1, 1, 1); }
|
||||
|
||||
constexpr float* ptr() { return m.data(); }
|
||||
constexpr const float* ptr() const { return m.data(); }
|
||||
|
||||
constexpr float& operator()(int row, int col) {
|
||||
#ifdef __3DS__
|
||||
// 3ds is full reverse order iirc
|
||||
return m[row * 4 + (3 - col)];
|
||||
#else
|
||||
return m[col * 4 + row];
|
||||
#endif
|
||||
}
|
||||
constexpr float operator()(int row, int col) const {
|
||||
#ifdef __3DS__
|
||||
// 3ds is full reverse order iirc
|
||||
return m[row * 4 + (3 - col)];
|
||||
#else
|
||||
return m[col * 4 + row];
|
||||
#endif
|
||||
}
|
||||
|
||||
constexpr mat4 operator*(const mat4& v) const {
|
||||
mat4 ret;
|
||||
for (int i = 0; i < 4; i++) {
|
||||
for (int j = 0; j < 4; j++) {
|
||||
float t = 0.f;
|
||||
for (int k = 0; k < 4; k++) {
|
||||
t += (*this)(i, k) * v(k, j);
|
||||
}
|
||||
ret(i, j) = t;
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
constexpr mat4& operator*=(const mat4& v) {
|
||||
*this = *this * v;
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr static mat4 translate(float x, float y, float z) {
|
||||
mat4 ret = identity();
|
||||
ret(0, 3) = x;
|
||||
ret(1, 3) = y;
|
||||
ret(2, 3) = z;
|
||||
return ret;
|
||||
}
|
||||
|
||||
constexpr static mat4 scale(float x, float y, float z) {
|
||||
mat4 ret;
|
||||
ret(0, 0) = x;
|
||||
ret(1, 1) = y;
|
||||
ret(2, 2) = z;
|
||||
ret(3, 3) = 1.f;
|
||||
return ret;
|
||||
}
|
||||
|
||||
constexpr static mat4 ortho(float l, float r, float b, float t, float n,
|
||||
float f) {
|
||||
mat4 ret;
|
||||
#ifdef __3DS__ // Patch to rotate the Matrix correctly
|
||||
ret(0, 1) = 2.f / (t - b);
|
||||
ret(0, 3) = (b + t) / (b - t);
|
||||
ret(1, 0) = 2.f / (l - r);
|
||||
ret(1, 3) = (l + r) / (r - l);
|
||||
ret(2, 2) = 1.f / (n - f);
|
||||
ret(2, 3) = 0.5f * (n + f) / (n - f) - 0.5f;
|
||||
#else
|
||||
ret(0, 0) = 2.0f / (r - l);
|
||||
ret(0, 3) = -(r + l) / (r - l);
|
||||
ret(1, 1) = 2.0f / (t - b);
|
||||
ret(1, 3) = -(t + b) / (t - b);
|
||||
ret(2, 2) = -2.0f / (f - n);
|
||||
ret(2, 3) = -(f + n) / (f - n);
|
||||
#endif
|
||||
ret(3, 3) = 1.f;
|
||||
return ret;
|
||||
}
|
||||
|
||||
static mat4 rotate(fvec3 axis, float a);
|
||||
static mat4 rotateX(float a);
|
||||
static mat4 rotateY(float a);
|
||||
static mat4 rotateZ(float a);
|
||||
static mat4 perspective(float fov, float aspect, float n, float f);
|
||||
static mat4 lookAt(const fvec3& pos, const fvec3& center, const fvec3& up);
|
||||
};
|
||||
} // namespace amy
|
||||
60
include/amethyst/maths/vec.hpp
Normal file
60
include/amethyst/maths/vec.hpp
Normal file
@@ -0,0 +1,60 @@
|
||||
#pragma once
|
||||
|
||||
/*
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2024 - 2025 tobid7
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <amethyst/maths/vec2.hpp>
|
||||
#include <amethyst/maths/vec3.hpp>
|
||||
#include <amethyst/maths/vec4.hpp>
|
||||
#include <format>
|
||||
|
||||
/** Define Formatters for C++ 20 */
|
||||
|
||||
/**
|
||||
* WHY DOES MSVC ALWAYS NEED THESE EXTRA THINGS
|
||||
*/
|
||||
|
||||
template <typename T, typename CharT>
|
||||
struct std::formatter<amy::vec2<T>, CharT> : std::formatter<T, CharT> {
|
||||
template <typename FormatContext>
|
||||
auto format(const amy::vec2<T> &v, FormatContext &ctx) const {
|
||||
return std::format_to(ctx.out(), "{}, {}", v.x, v.y);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T, typename CharT>
|
||||
struct std::formatter<amy::vec3<T>, CharT> : std::formatter<T, CharT> {
|
||||
template <typename FormatContext>
|
||||
auto format(const amy::vec3<T> &v, FormatContext &ctx) const {
|
||||
return std::format_to(ctx.out(), "{}, {}, {}", v.x, v.y, v.z);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T, typename CharT>
|
||||
struct std::formatter<amy::vec4<T>, CharT> : std::formatter<T, CharT> {
|
||||
template <typename FormatContext>
|
||||
auto format(const amy::vec4<T> &v, FormatContext &ctx) const {
|
||||
return std::format_to(ctx.out(), "{}, {}, {}, {}", v.x, v.y, v.z, v.w);
|
||||
}
|
||||
};
|
||||
174
include/amethyst/maths/vec2.hpp
Normal file
174
include/amethyst/maths/vec2.hpp
Normal file
@@ -0,0 +1,174 @@
|
||||
#pragma once
|
||||
|
||||
/*
|
||||
MIT License
|
||||
Copyright (c) 2024 - 2025 tobid7
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
// This file is generated by lazyvec 2.0.0
|
||||
|
||||
#include <cmath>
|
||||
|
||||
namespace amy {
|
||||
template <typename T> class vec2 {
|
||||
public:
|
||||
T x;
|
||||
T y;
|
||||
|
||||
// Constructors
|
||||
|
||||
constexpr vec2() : x(0), y(0) {}
|
||||
template <typename T1> constexpr vec2(T1 v) {
|
||||
x = (T)v;
|
||||
y = (T)v;
|
||||
}
|
||||
|
||||
template <typename T1> constexpr vec2(const vec2<T1> &v) {
|
||||
x = (T)v.x;
|
||||
y = (T)v.y;
|
||||
}
|
||||
|
||||
constexpr explicit vec2(T x, T y) : x(x), y(y) {}
|
||||
|
||||
// Operations
|
||||
|
||||
template <typename T1> vec2<T> &operator+=(T1 v) {
|
||||
x += (T)v;
|
||||
y += (T)v;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename T1> vec2<T> &operator+=(const vec2<T1> &v) {
|
||||
x += (T)v.x;
|
||||
y += (T)v.y;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename T1> vec2<T> operator+(T1 v) const {
|
||||
return vec2<T>(x + (T)v, y + (T)v);
|
||||
}
|
||||
|
||||
template <typename T1> vec2<T> operator+(const vec2<T1> &v) const {
|
||||
return vec2<T>(x + (T)v.x, y + (T)v.y);
|
||||
}
|
||||
|
||||
template <typename T1> vec2<T> &operator-=(T1 v) {
|
||||
x -= (T)v;
|
||||
y -= (T)v;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename T1> vec2<T> &operator-=(const vec2<T1> &v) {
|
||||
x -= (T)v.x;
|
||||
y -= (T)v.y;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename T1> vec2<T> operator-(T1 v) const {
|
||||
return vec2<T>(x - (T)v, y - (T)v);
|
||||
}
|
||||
|
||||
template <typename T1> vec2<T> operator-(const vec2<T1> &v) const {
|
||||
return vec2<T>(x - (T)v.x, y - (T)v.y);
|
||||
}
|
||||
|
||||
template <typename T1> vec2<T> &operator*=(T1 v) {
|
||||
x *= (T)v;
|
||||
y *= (T)v;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename T1> vec2<T> &operator*=(const vec2<T1> &v) {
|
||||
x *= (T)v.x;
|
||||
y *= (T)v.y;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename T1> vec2<T> operator*(T1 v) const {
|
||||
return vec2<T>(x * (T)v, y * (T)v);
|
||||
}
|
||||
|
||||
template <typename T1> vec2<T> operator*(const vec2<T1> &v) const {
|
||||
return vec2<T>(x * (T)v.x, y * (T)v.y);
|
||||
}
|
||||
|
||||
template <typename T1> vec2<T> &operator/=(T1 v) {
|
||||
x /= (T)v;
|
||||
y /= (T)v;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename T1> vec2<T> &operator/=(const vec2<T1> &v) {
|
||||
x /= (T)v.x;
|
||||
y /= (T)v.y;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename T1> vec2<T> operator/(T1 v) const {
|
||||
return vec2<T>(x / (T)v, y / (T)v);
|
||||
}
|
||||
|
||||
template <typename T1> vec2<T> operator/(const vec2<T1> &v) const {
|
||||
return vec2<T>(x / (T)v.x, y / (T)v.y);
|
||||
}
|
||||
|
||||
// Generic Operations
|
||||
|
||||
vec2 operator-() const { return vec2(-x, -y); }
|
||||
template <typename T1> bool operator==(const vec2<T1> &v) const {
|
||||
return x == (T)v.x && y == (T)v.y;
|
||||
}
|
||||
template <typename T1> bool operator!=(const vec2<T1> &v) const {
|
||||
return !(*this == v);
|
||||
}
|
||||
|
||||
// Functions
|
||||
|
||||
double Len() const { return std::sqrt(SqLen()); }
|
||||
double SqLen() const { return x * x + y * y; }
|
||||
|
||||
template <typename T1> double Distance(const vec2<T1> &v) const {
|
||||
return (*this - v).Len();
|
||||
}
|
||||
|
||||
vec2<T> Normalize() const {
|
||||
double l = Len();
|
||||
if (l == 0) {
|
||||
return *this;
|
||||
}
|
||||
return *this / (T)l;
|
||||
}
|
||||
|
||||
template <typename T1> T Dot(const vec2<T1> &v) const {
|
||||
return x * (T)v.x + y * (T)v.y;
|
||||
}
|
||||
|
||||
// Swap Functions
|
||||
void SwapXY() {
|
||||
T t = x;
|
||||
x = y;
|
||||
y = t;
|
||||
}
|
||||
};
|
||||
using fvec2 = vec2<float>;
|
||||
using ivec2 = vec2<int>;
|
||||
using dvec2 = vec2<double>;
|
||||
} // namespace amy
|
||||
210
include/amethyst/maths/vec3.hpp
Normal file
210
include/amethyst/maths/vec3.hpp
Normal file
@@ -0,0 +1,210 @@
|
||||
#pragma once
|
||||
|
||||
/*
|
||||
MIT License
|
||||
Copyright (c) 2024 - 2025 tobid7
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
// This file is generated by lazyvec 2.0.0
|
||||
|
||||
#include <cmath>
|
||||
// Extended includes (rename if you use other filenames/paths)
|
||||
#include <amethyst/maths/vec2.hpp>
|
||||
|
||||
namespace amy {
|
||||
template <typename T> class vec3 {
|
||||
public:
|
||||
T x;
|
||||
T y;
|
||||
T z;
|
||||
|
||||
// Constructors
|
||||
|
||||
constexpr vec3() : x(0), y(0), z(0) {}
|
||||
template <typename T1> constexpr vec3(T1 v) {
|
||||
x = (T)v;
|
||||
y = (T)v;
|
||||
z = (T)v;
|
||||
}
|
||||
|
||||
template <typename T1> constexpr vec3(const vec3<T1> &v) {
|
||||
x = (T)v.x;
|
||||
y = (T)v.y;
|
||||
z = (T)v.z;
|
||||
}
|
||||
|
||||
constexpr explicit vec3(T x, T y, T z) : x(x), y(y), z(z) {}
|
||||
|
||||
// Extended Constructors
|
||||
template <typename T1> constexpr explicit vec3(const vec2<T1> &xy, T1 z) {
|
||||
{
|
||||
x = (T)xy.x;
|
||||
y = (T)xy.y;
|
||||
this->z = (T)z;
|
||||
}
|
||||
}
|
||||
|
||||
// Operations
|
||||
|
||||
template <typename T1> vec3<T> &operator+=(T1 v) {
|
||||
x += (T)v;
|
||||
y += (T)v;
|
||||
z += (T)v;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename T1> vec3<T> &operator+=(const vec3<T1> &v) {
|
||||
x += (T)v.x;
|
||||
y += (T)v.y;
|
||||
z += (T)v.z;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename T1> vec3<T> operator+(T1 v) const {
|
||||
return vec3<T>(x + (T)v, y + (T)v, z + (T)v);
|
||||
}
|
||||
|
||||
template <typename T1> vec3<T> operator+(const vec3<T1> &v) const {
|
||||
return vec3<T>(x + (T)v.x, y + (T)v.y, z + (T)v.z);
|
||||
}
|
||||
|
||||
template <typename T1> vec3<T> &operator-=(T1 v) {
|
||||
x -= (T)v;
|
||||
y -= (T)v;
|
||||
z -= (T)v;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename T1> vec3<T> &operator-=(const vec3<T1> &v) {
|
||||
x -= (T)v.x;
|
||||
y -= (T)v.y;
|
||||
z -= (T)v.z;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename T1> vec3<T> operator-(T1 v) const {
|
||||
return vec3<T>(x - (T)v, y - (T)v, z - (T)v);
|
||||
}
|
||||
|
||||
template <typename T1> vec3<T> operator-(const vec3<T1> &v) const {
|
||||
return vec3<T>(x - (T)v.x, y - (T)v.y, z - (T)v.z);
|
||||
}
|
||||
|
||||
template <typename T1> vec3<T> &operator*=(T1 v) {
|
||||
x *= (T)v;
|
||||
y *= (T)v;
|
||||
z *= (T)v;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename T1> vec3<T> &operator*=(const vec3<T1> &v) {
|
||||
x *= (T)v.x;
|
||||
y *= (T)v.y;
|
||||
z *= (T)v.z;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename T1> vec3<T> operator*(T1 v) const {
|
||||
return vec3<T>(x * (T)v, y * (T)v, z * (T)v);
|
||||
}
|
||||
|
||||
template <typename T1> vec3<T> operator*(const vec3<T1> &v) const {
|
||||
return vec3<T>(x * (T)v.x, y * (T)v.y, z * (T)v.z);
|
||||
}
|
||||
|
||||
template <typename T1> vec3<T> &operator/=(T1 v) {
|
||||
x /= (T)v;
|
||||
y /= (T)v;
|
||||
z /= (T)v;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename T1> vec3<T> &operator/=(const vec3<T1> &v) {
|
||||
x /= (T)v.x;
|
||||
y /= (T)v.y;
|
||||
z /= (T)v.z;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename T1> vec3<T> operator/(T1 v) const {
|
||||
return vec3<T>(x / (T)v, y / (T)v, z / (T)v);
|
||||
}
|
||||
|
||||
template <typename T1> vec3<T> operator/(const vec3<T1> &v) const {
|
||||
return vec3<T>(x / (T)v.x, y / (T)v.y, z / (T)v.z);
|
||||
}
|
||||
|
||||
// Generic Operations
|
||||
|
||||
vec3 operator-() const { return vec3(-x, -y, -z); }
|
||||
template <typename T1> bool operator==(const vec3<T1> &v) const {
|
||||
return x == (T)v.x && y == (T)v.y && z == (T)v.z;
|
||||
}
|
||||
template <typename T1> bool operator!=(const vec3<T1> &v) const {
|
||||
return !(*this == v);
|
||||
}
|
||||
|
||||
// Functions
|
||||
|
||||
double Len() const { return std::sqrt(SqLen()); }
|
||||
double SqLen() const { return x * x + y * y + z * z; }
|
||||
|
||||
template <typename T1> double Distance(const vec3<T1> &v) const {
|
||||
return (*this - v).Len();
|
||||
}
|
||||
|
||||
vec3<T> Normalize() const {
|
||||
double l = Len();
|
||||
if (l == 0) {
|
||||
return *this;
|
||||
}
|
||||
return *this / (T)l;
|
||||
}
|
||||
|
||||
template <typename T1> T Dot(const vec3<T1> &v) const {
|
||||
return x * (T)v.x + y * (T)v.y + z * (T)v.z;
|
||||
}
|
||||
|
||||
template <typename T1> vec3<T> Cross(const vec3<T1> &v) const {
|
||||
return vec3<T>(y * v.z - z * v.y, z * v.x - x * v.z, x * v.y - y * v.x);
|
||||
}
|
||||
|
||||
// Swap Functions
|
||||
void SwapXY() {
|
||||
T t = x;
|
||||
x = y;
|
||||
y = t;
|
||||
}
|
||||
void SwapXZ() {
|
||||
T t = x;
|
||||
x = z;
|
||||
z = t;
|
||||
}
|
||||
void SwapYZ() {
|
||||
T t = y;
|
||||
y = z;
|
||||
z = t;
|
||||
}
|
||||
};
|
||||
using fvec3 = vec3<float>;
|
||||
using ivec3 = vec3<int>;
|
||||
using dvec3 = vec3<double>;
|
||||
} // namespace amy
|
||||
244
include/amethyst/maths/vec4.hpp
Normal file
244
include/amethyst/maths/vec4.hpp
Normal file
@@ -0,0 +1,244 @@
|
||||
#pragma once
|
||||
|
||||
/*
|
||||
MIT License
|
||||
Copyright (c) 2024 - 2025 tobid7
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
// This file is generated by lazyvec 2.0.0
|
||||
|
||||
#include <cmath>
|
||||
// Extended includes (rename if you use other filenames/paths)
|
||||
#include <amethyst/maths/vec2.hpp>
|
||||
#include <amethyst/maths/vec3.hpp>
|
||||
|
||||
namespace amy {
|
||||
template <typename T> class vec4 {
|
||||
public:
|
||||
T x;
|
||||
T y;
|
||||
T z;
|
||||
T w;
|
||||
|
||||
// Constructors
|
||||
|
||||
constexpr vec4() : x(0), y(0), z(0), w(0) {}
|
||||
template <typename T1> constexpr vec4(T1 v) {
|
||||
x = (T)v;
|
||||
y = (T)v;
|
||||
z = (T)v;
|
||||
w = (T)v;
|
||||
}
|
||||
|
||||
template <typename T1> constexpr vec4(const vec4<T1> &v) {
|
||||
x = (T)v.x;
|
||||
y = (T)v.y;
|
||||
z = (T)v.z;
|
||||
w = (T)v.w;
|
||||
}
|
||||
|
||||
constexpr explicit vec4(T x, T y, T z, T w) : x(x), y(y), z(z), w(w) {}
|
||||
|
||||
// Extended Constructors
|
||||
template <typename T1>
|
||||
constexpr explicit vec4(const vec2<T1> &xy, const vec2<T1> &zw) {
|
||||
{
|
||||
x = (T)xy.x;
|
||||
y = (T)xy.y;
|
||||
z = (T)zw.x;
|
||||
w = (T)zw.y;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T1> constexpr explicit vec4(const vec3<T1> &xyz, T1 w) {
|
||||
{
|
||||
x = (T)xyz.x;
|
||||
y = (T)xyz.y;
|
||||
z = (T)xyz.z;
|
||||
this->w = (T)w;
|
||||
}
|
||||
}
|
||||
|
||||
// Operations
|
||||
|
||||
template <typename T1> vec4<T> &operator+=(T1 v) {
|
||||
x += (T)v;
|
||||
y += (T)v;
|
||||
z += (T)v;
|
||||
w += (T)v;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename T1> vec4<T> &operator+=(const vec4<T1> &v) {
|
||||
x += (T)v.x;
|
||||
y += (T)v.y;
|
||||
z += (T)v.z;
|
||||
w += (T)v.w;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename T1> vec4<T> operator+(T1 v) const {
|
||||
return vec4<T>(x + (T)v, y + (T)v, z + (T)v, w + (T)v);
|
||||
}
|
||||
|
||||
template <typename T1> vec4<T> operator+(const vec4<T1> &v) const {
|
||||
return vec4<T>(x + (T)v.x, y + (T)v.y, z + (T)v.z, w + (T)v.w);
|
||||
}
|
||||
|
||||
template <typename T1> vec4<T> &operator-=(T1 v) {
|
||||
x -= (T)v;
|
||||
y -= (T)v;
|
||||
z -= (T)v;
|
||||
w -= (T)v;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename T1> vec4<T> &operator-=(const vec4<T1> &v) {
|
||||
x -= (T)v.x;
|
||||
y -= (T)v.y;
|
||||
z -= (T)v.z;
|
||||
w -= (T)v.w;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename T1> vec4<T> operator-(T1 v) const {
|
||||
return vec4<T>(x - (T)v, y - (T)v, z - (T)v, w - (T)v);
|
||||
}
|
||||
|
||||
template <typename T1> vec4<T> operator-(const vec4<T1> &v) const {
|
||||
return vec4<T>(x - (T)v.x, y - (T)v.y, z - (T)v.z, w - (T)v.w);
|
||||
}
|
||||
|
||||
template <typename T1> vec4<T> &operator*=(T1 v) {
|
||||
x *= (T)v;
|
||||
y *= (T)v;
|
||||
z *= (T)v;
|
||||
w *= (T)v;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename T1> vec4<T> &operator*=(const vec4<T1> &v) {
|
||||
x *= (T)v.x;
|
||||
y *= (T)v.y;
|
||||
z *= (T)v.z;
|
||||
w *= (T)v.w;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename T1> vec4<T> operator*(T1 v) const {
|
||||
return vec4<T>(x * (T)v, y * (T)v, z * (T)v, w * (T)v);
|
||||
}
|
||||
|
||||
template <typename T1> vec4<T> operator*(const vec4<T1> &v) const {
|
||||
return vec4<T>(x * (T)v.x, y * (T)v.y, z * (T)v.z, w * (T)v.w);
|
||||
}
|
||||
|
||||
template <typename T1> vec4<T> &operator/=(T1 v) {
|
||||
x /= (T)v;
|
||||
y /= (T)v;
|
||||
z /= (T)v;
|
||||
w /= (T)v;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename T1> vec4<T> &operator/=(const vec4<T1> &v) {
|
||||
x /= (T)v.x;
|
||||
y /= (T)v.y;
|
||||
z /= (T)v.z;
|
||||
w /= (T)v.w;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename T1> vec4<T> operator/(T1 v) const {
|
||||
return vec4<T>(x / (T)v, y / (T)v, z / (T)v, w / (T)v);
|
||||
}
|
||||
|
||||
template <typename T1> vec4<T> operator/(const vec4<T1> &v) const {
|
||||
return vec4<T>(x / (T)v.x, y / (T)v.y, z / (T)v.z, w / (T)v.w);
|
||||
}
|
||||
|
||||
// Generic Operations
|
||||
|
||||
vec4 operator-() const { return vec4(-x, -y, -z, -w); }
|
||||
template <typename T1> bool operator==(const vec4<T1> &v) const {
|
||||
return x == (T)v.x && y == (T)v.y && z == (T)v.z && w == (T)v.w;
|
||||
}
|
||||
template <typename T1> bool operator!=(const vec4<T1> &v) const {
|
||||
return !(*this == v);
|
||||
}
|
||||
|
||||
// Functions
|
||||
|
||||
double Len() const { return std::sqrt(SqLen()); }
|
||||
double SqLen() const { return x * x + y * y + z * z + w * w; }
|
||||
|
||||
template <typename T1> double Distance(const vec4<T1> &v) const {
|
||||
return (*this - v).Len();
|
||||
}
|
||||
|
||||
vec4<T> Normalize() const {
|
||||
double l = Len();
|
||||
if (l == 0) {
|
||||
return *this;
|
||||
}
|
||||
return *this / (T)l;
|
||||
}
|
||||
|
||||
template <typename T1> T Dot(const vec4<T1> &v) const {
|
||||
return x * (T)v.x + y * (T)v.y + z * (T)v.z + w * (T)v.w;
|
||||
}
|
||||
|
||||
// Swap Functions
|
||||
void SwapXY() {
|
||||
T t = x;
|
||||
x = y;
|
||||
y = t;
|
||||
}
|
||||
void SwapXZ() {
|
||||
T t = x;
|
||||
x = z;
|
||||
z = t;
|
||||
}
|
||||
void SwapXW() {
|
||||
T t = x;
|
||||
x = w;
|
||||
w = t;
|
||||
}
|
||||
void SwapYZ() {
|
||||
T t = y;
|
||||
y = z;
|
||||
z = t;
|
||||
}
|
||||
void SwapYW() {
|
||||
T t = y;
|
||||
y = w;
|
||||
w = t;
|
||||
}
|
||||
void SwapZW() {
|
||||
T t = z;
|
||||
z = w;
|
||||
w = t;
|
||||
}
|
||||
};
|
||||
using fvec4 = vec4<float>;
|
||||
using ivec4 = vec4<int>;
|
||||
using dvec4 = vec4<double>;
|
||||
} // namespace amy
|
||||
Reference in New Issue
Block a user