tyrav2 init
This commit is contained in:
@@ -0,0 +1,194 @@
|
||||
/*
|
||||
# ______ ____ ___
|
||||
# | \/ ____| |___|
|
||||
# | | | \ | |
|
||||
#-----------------------------------------------------------------------
|
||||
# Copyright 2022, tyra - https://github.com/h4570/tyra
|
||||
# Licenced under Apache License 2.0
|
||||
# Sandro Sobczyński <sandro.sobczynski@gmail.com>
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "math/vec4.hpp"
|
||||
#include "math/math.hpp"
|
||||
|
||||
namespace Tyra {
|
||||
|
||||
class M4x4 {
|
||||
public:
|
||||
MATRIX data alignas(sizeof(float) * 16);
|
||||
|
||||
/**
|
||||
* @param identity true = identity matrix, false = random values
|
||||
*/
|
||||
explicit M4x4(const bool& identity = true);
|
||||
M4x4(const M4x4& v) { copy(this, v); }
|
||||
explicit M4x4(const float* v) { copy(this, v); }
|
||||
inline M4x4(const float& m11, const float& m12, const float& m13,
|
||||
const float& m14, const float& m21, const float& m22,
|
||||
const float& m23, const float& m24, const float& m31,
|
||||
const float& m32, const float& m33, const float& m34,
|
||||
const float& m41, const float& m42, const float& m43,
|
||||
const float& m44) {
|
||||
set(m11, m12, m13, m14, m21, m22, m23, m24, m31, m32, m33, m34, m41, m42,
|
||||
m43, m44);
|
||||
}
|
||||
|
||||
static void copy(M4x4* out, const float* in);
|
||||
static void copy(M4x4* out, const M4x4& in) { copy(out, in.data); }
|
||||
|
||||
void operator=(const M4x4& v);
|
||||
Vec4 operator*(const Vec4& v) const;
|
||||
|
||||
M4x4 operator*(const M4x4& v) const {
|
||||
M4x4 result;
|
||||
cross(result.data, this->data, v.data);
|
||||
return result;
|
||||
}
|
||||
|
||||
void operator*=(const M4x4& v) { cross(this->data, this->data, v.data); }
|
||||
|
||||
float& operator[](const u8& index) { return data[index]; }
|
||||
|
||||
/**
|
||||
* 1 0 0 0
|
||||
* 0 1 0 0
|
||||
* 0 0 1 0
|
||||
* 0 0 0 1
|
||||
*/
|
||||
void identity();
|
||||
|
||||
/**
|
||||
* 1 0 0 0
|
||||
* 0 1 0 0
|
||||
* 0 0 1 0
|
||||
* 0 0 0 1
|
||||
*/
|
||||
void unit() { identity(); }
|
||||
|
||||
void set(const float& m11, const float& m12, const float& m13,
|
||||
const float& m14, const float& m21, const float& m22,
|
||||
const float& m23, const float& m24, const float& m31,
|
||||
const float& m32, const float& m33, const float& m34,
|
||||
const float& m41, const float& m42, const float& m43,
|
||||
const float& m44);
|
||||
|
||||
void translate(const Vec4& v) {
|
||||
M4x4 temp = M4x4(true);
|
||||
temp.translationX(v.x);
|
||||
temp.translationY(v.y);
|
||||
temp.translationZ(v.z);
|
||||
cross(this->data, temp.data, this->data);
|
||||
}
|
||||
|
||||
void translateX(const float& v) {
|
||||
M4x4 temp = M4x4(true);
|
||||
temp.translationX(v);
|
||||
cross(this->data, temp.data, this->data);
|
||||
}
|
||||
|
||||
void translateY(const float& v) {
|
||||
M4x4 temp = M4x4(true);
|
||||
temp.translationY(v);
|
||||
cross(this->data, temp.data, this->data);
|
||||
}
|
||||
|
||||
void translateZ(const float& v) {
|
||||
M4x4 temp = M4x4(true);
|
||||
temp.translationZ(v);
|
||||
cross(this->data, temp.data, this->data);
|
||||
}
|
||||
|
||||
/** Rotate M4x4. */
|
||||
void rotate(const Vec4& v) {
|
||||
M4x4 temp = M4x4(true);
|
||||
|
||||
temp.rotationZ(v.z);
|
||||
cross(this->data, temp.data, this->data);
|
||||
|
||||
temp.identity();
|
||||
temp.rotationY(v.y);
|
||||
cross(this->data, temp.data, this->data);
|
||||
|
||||
temp.identity();
|
||||
temp.rotationX(v.x);
|
||||
cross(this->data, temp.data, this->data);
|
||||
}
|
||||
|
||||
/** Rotate M4x4 by X. */
|
||||
void rotateX(const float& radians) {
|
||||
M4x4 temp = M4x4(true);
|
||||
temp.rotationX(radians);
|
||||
cross(this->data, temp.data, this->data);
|
||||
}
|
||||
|
||||
/** Rotate M4x4 by Y. */
|
||||
void rotateY(const float& radians) {
|
||||
M4x4 temp = M4x4(true);
|
||||
temp.rotationY(radians);
|
||||
cross(this->data, temp.data, this->data);
|
||||
}
|
||||
|
||||
/** Rotate M4x4 by Z. */
|
||||
void rotateZ(const float& radians) {
|
||||
M4x4 temp = M4x4(true);
|
||||
temp.rotationZ(radians);
|
||||
cross(this->data, temp.data, this->data);
|
||||
}
|
||||
|
||||
/** Rotate M4x4 by angle. */
|
||||
void rotateByAngle(const float& angle, const Vec4& axis) {
|
||||
M4x4 temp;
|
||||
temp.rotationByAngle(angle, axis);
|
||||
cross(this->data, temp.data, this->data);
|
||||
}
|
||||
|
||||
void scale(const float& v) { scale(Vec4(v, v, v, 1.0F)); }
|
||||
void scaleX(const float& v) { scale(Vec4(v, 1.0F, 1.0F, 1.0F)); }
|
||||
void scaleY(const float& v) { scale(Vec4(1.0F, v, 1.0F, 1.0F)); }
|
||||
void scaleZ(const float& v) { scale(Vec4(1.0F, 1.0F, v, 1.0F)); }
|
||||
|
||||
void scale(const Vec4& v) {
|
||||
M4x4 temp = M4x4(true);
|
||||
temp.setScale(v);
|
||||
cross(this->data, temp.data, this->data);
|
||||
}
|
||||
|
||||
/** Create perspective projection M4x4 (gluPerspective) */
|
||||
static M4x4 perspective(const float& fov, const float& width,
|
||||
const float& height, const float& projectionScale,
|
||||
const float& aspectRatio, const float& near,
|
||||
const float& far);
|
||||
|
||||
/**
|
||||
* Create a view M4x4 that transforms coordinates in
|
||||
* such a way that the user looks at a target vector
|
||||
* direction from a position vector (glLookAt)
|
||||
*/
|
||||
static M4x4 lookAt(const Vec4& position, const Vec4& target);
|
||||
static void lookAt(M4x4* res, const Vec4& position, const Vec4& target);
|
||||
|
||||
void print() const;
|
||||
void print(const char* name) const;
|
||||
void print(const std::string& name) const { print(name.c_str()); }
|
||||
std::string getPrint(const char* name = nullptr) const;
|
||||
|
||||
private:
|
||||
static float upVec[4] alignas(sizeof(float) * 4);
|
||||
static float viewVec[4] alignas(sizeof(float) * 4);
|
||||
|
||||
static void cross(float res[16], const float a[16], const float b[16]);
|
||||
void rotationX(const float& v);
|
||||
void rotationY(const float& v);
|
||||
void rotationZ(const float& v);
|
||||
void rotationByAngle(const float& angle, const Vec4& axis);
|
||||
void translationX(const float& v);
|
||||
void translationY(const float& v);
|
||||
void translationZ(const float& v);
|
||||
void setScale(const Vec4& val);
|
||||
static M4x4 setCamera(const float pos[4], const float vz[4],
|
||||
const float vy[4]);
|
||||
};
|
||||
|
||||
} // namespace Tyra
|
||||
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
# ______ ____ ___
|
||||
# | \/ ____| |___|
|
||||
# | | | \ | |
|
||||
#-----------------------------------------------------------------------
|
||||
# Copyright 2022, tyra - https://github.com/h4570/tyra
|
||||
# Licenced under Apache License 2.0
|
||||
# Sandro Sobczyński <sandro.sobczynski@gmail.com>
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
extern "C" {
|
||||
#include <tamtypes.h>
|
||||
}
|
||||
|
||||
#include <math.h>
|
||||
|
||||
namespace Tyra {
|
||||
|
||||
class Math {
|
||||
public:
|
||||
constexpr static float HALF_ANG2RAD = 3.14159265358979323846F / 360.0F;
|
||||
constexpr static float ANG2RAD = 3.14159265358979323846F / 180.0F;
|
||||
constexpr static float PI = 3.1415926535897932384626433832795F;
|
||||
constexpr static float HALF_PI = 1.5707963267948966192313216916398F;
|
||||
static float cos(float x);
|
||||
static float asin(float x);
|
||||
static float mod(float x, float y);
|
||||
static inline float sin(float x) { return cos(x - HALF_PI); }
|
||||
static inline float tan(float x) { return sin(x) / cos(x); }
|
||||
static float invSqrt(float x);
|
||||
|
||||
private:
|
||||
Math();
|
||||
};
|
||||
|
||||
} // namespace Tyra
|
||||
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
# ______ ____ ___
|
||||
# | \/ ____| |___|
|
||||
# | | | \ | |
|
||||
#-----------------------------------------------------------------------
|
||||
# Copyright 2022, tyra - https://github.com/h4570/tyra
|
||||
# Licenced under Apache License 2.0
|
||||
# Sandro Sobczyński <sandro.sobczynski@gmail.com>
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
|
||||
namespace Tyra {
|
||||
|
||||
class Plane {
|
||||
public:
|
||||
Vec4 normal;
|
||||
float distance;
|
||||
|
||||
Plane();
|
||||
Plane(const Vec4& a, const Vec4& b, const Vec4& c);
|
||||
~Plane();
|
||||
|
||||
void update(const Vec4& a, const Vec4& b, const Vec4& c);
|
||||
inline float distanceTo(const Vec4& t_vec) const {
|
||||
return this->distance + this->normal.innerProduct(t_vec);
|
||||
}
|
||||
|
||||
void print() const;
|
||||
void print(const char* name) const;
|
||||
void print(const std::string& name) const { print(name.c_str()); }
|
||||
std::string getPrint(const char* name = nullptr) const;
|
||||
};
|
||||
|
||||
} // namespace Tyra
|
||||
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
# ______ ____ ___
|
||||
# | \/ ____| |___|
|
||||
# | | | \ | |
|
||||
#-----------------------------------------------------------------------
|
||||
# Copyright 2022, tyra - https://github.com/h4570/tyra
|
||||
# Licenced under Apache License 2.0
|
||||
# Sandro Sobczyński <sandro.sobczynski@gmail.com>
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
extern "C" {
|
||||
#include <stdio.h>
|
||||
#include <tamtypes.h>
|
||||
}
|
||||
|
||||
#include <iomanip>
|
||||
#include <string>
|
||||
#include "math/math.hpp"
|
||||
|
||||
namespace Tyra {
|
||||
|
||||
class Vec2 {
|
||||
public:
|
||||
union {
|
||||
struct {
|
||||
float x;
|
||||
float y;
|
||||
};
|
||||
float xy[2] alignas(sizeof(float) * 2);
|
||||
};
|
||||
|
||||
Vec2(const float& t_x, const float& t_y);
|
||||
Vec2(const Vec2& v);
|
||||
Vec2();
|
||||
~Vec2();
|
||||
|
||||
void set(const float& t_x, const float& t_y);
|
||||
void set(const Vec2& v);
|
||||
void rotate(const float& t_angle, const float& t_x, const float& t_y);
|
||||
|
||||
void print() const;
|
||||
void print(const char* name) const;
|
||||
void print(const std::string& name) const { print(name.c_str()); }
|
||||
std::string getPrint(const char* name = nullptr) const;
|
||||
};
|
||||
|
||||
} // namespace Tyra
|
||||
@@ -0,0 +1,151 @@
|
||||
/*
|
||||
# ______ ____ ___
|
||||
# | \/ ____| |___|
|
||||
# | | | \ | |
|
||||
#-----------------------------------------------------------------------
|
||||
# Copyright 2022, tyra - https://github.com/h4570/tyra
|
||||
# Licenced under Apache License 2.0
|
||||
# Sandro Sobczyński <sandro.sobczynski@gmail.com>
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
extern "C" {
|
||||
#include <stdio.h>
|
||||
#include <tamtypes.h>
|
||||
}
|
||||
|
||||
#include <math3d.h>
|
||||
#include <iomanip>
|
||||
#include <cmath>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include "math/math.hpp"
|
||||
|
||||
namespace Tyra {
|
||||
|
||||
class Vec4 {
|
||||
public:
|
||||
union {
|
||||
struct {
|
||||
float x;
|
||||
float y;
|
||||
float z;
|
||||
float w;
|
||||
};
|
||||
VECTOR xyzw alignas(sizeof(float) * 4);
|
||||
};
|
||||
|
||||
static void copy(Vec4* out, const float* in);
|
||||
static inline void copy(Vec4* out, const Vec4& in) { copy(out, in.xyzw); }
|
||||
|
||||
/** Initialize Vec4 without setting default values */
|
||||
Vec4() {}
|
||||
|
||||
/** Initialize Vec4 with provided values */
|
||||
Vec4(const float& x, const float& y = 0.0F, const float& z = 0.0F,
|
||||
const float& w = 1.0F)
|
||||
: x(x), y(y), z(z), w(w) {}
|
||||
|
||||
/** Initialize Vec4 with other Vec4 values */
|
||||
Vec4(const Vec4& v) { copy(this, v); }
|
||||
|
||||
/** Initialize Vec4 with float[4] values */
|
||||
explicit Vec4(const float* v) { copy(this, v); }
|
||||
|
||||
Vec4 operator+(const Vec4& v) const;
|
||||
Vec4 operator-(const Vec4& v) const;
|
||||
/** Cross product */
|
||||
Vec4 operator*(const Vec4& v) const;
|
||||
Vec4 operator*(const float& v) const;
|
||||
Vec4 operator/(const float& v) const;
|
||||
Vec4 operator-(void) const;
|
||||
void operator=(const Vec4& v);
|
||||
void operator+=(const Vec4& v);
|
||||
void operator*=(const float& v);
|
||||
void operator*=(const Vec4& v);
|
||||
void operator/=(const float& v);
|
||||
|
||||
inline void set(const Vec4& v) { copy(this, v.xyzw); }
|
||||
inline void set(const float* v) { copy(this, v); }
|
||||
void set(const float& x, const float& y = 0.0F, const float& z = 0.0F,
|
||||
const float& w = 1.0F);
|
||||
|
||||
Vec4 cross(const Vec4& v) const;
|
||||
|
||||
void rotateZ(const int& angle);
|
||||
|
||||
int getRelativeCosBetween(const Vec4& v) const;
|
||||
int getRelativeAngleBetween(const Vec4& v) const;
|
||||
|
||||
float innerProduct(const Vec4& v) const;
|
||||
inline float dot3(const Vec4& v) const { return innerProduct(v); }
|
||||
|
||||
/** Get vector length */
|
||||
float length() const;
|
||||
|
||||
/** Normalize vector */
|
||||
void normalize();
|
||||
|
||||
/** Returns distance between two vectors */
|
||||
float distanceTo(const Vec4& v) const;
|
||||
|
||||
/**
|
||||
* Checks intersection with given box
|
||||
* @param min Opposite min vertex (ex. near, left, down vertex of bounding
|
||||
* box)
|
||||
* @param max Opposite max vertex (ex. far, right, up vertex of bounding
|
||||
* box)
|
||||
*/
|
||||
u8 collidesBox(const Vec4& min, const Vec4& max) const {
|
||||
return ((this->x <= max.x && this->x >= min.x) &&
|
||||
(this->y < max.y && this->y >= min.y) &&
|
||||
(this->z <= max.z && this->z >= min.z))
|
||||
? 1
|
||||
: 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if this vector is on given box (this->y >= box.y)
|
||||
* @param min Opposite min vertex (ex. near, left, down vertex of bounding
|
||||
* box)
|
||||
* @param max Opposite max vertex (ex. far, right, up vertex of bounding
|
||||
* box)
|
||||
*/
|
||||
u8 isOnBox(const Vec4& min, const Vec4& max) const {
|
||||
return ((this->x <= max.x && this->x >= min.x) && (this->y >= max.y) &&
|
||||
(this->z <= max.z && this->z >= min.z))
|
||||
? 1
|
||||
: 0;
|
||||
}
|
||||
|
||||
/** Check if given triangle should be backface culled */
|
||||
static u8 shouldBeBackfaceCulled(const Vec4* cameraPos, const Vec4* v0,
|
||||
const Vec4* v1, const Vec4* v2);
|
||||
|
||||
/**
|
||||
* Update this vector by linear interpolation between two vertices
|
||||
* @param v1 Before vertex
|
||||
* @param v2 After vertex
|
||||
* @param interp State of interpolation
|
||||
*/
|
||||
void lerp(const Vec4& v1, const Vec4& v2, const float& interp);
|
||||
|
||||
/**
|
||||
* Get vector by linear interpolation between two vertices
|
||||
* @param v1 Before vertex
|
||||
* @param v2 After vertex
|
||||
* @param interp State of interpolation
|
||||
*/
|
||||
static Vec4 getByLerp(const Vec4& v1, const Vec4& v2, const float& interp);
|
||||
|
||||
void print() const;
|
||||
void print(const char* name) const;
|
||||
void print(const std::string& name) const { print(name.c_str()); }
|
||||
std::string getPrint(const char* name = nullptr) const;
|
||||
|
||||
static void setLerp(Vec4* output, const Vec4& v1, const Vec4& v2,
|
||||
const float& interp);
|
||||
};
|
||||
|
||||
} // namespace Tyra
|
||||
Reference in New Issue
Block a user