Skip to content

Commit 5771dcc

Browse files
committed
Some more TVector implementation
Some fixes to TSpriteShader
1 parent dac0740 commit 5771dcc

14 files changed

Lines changed: 473 additions & 22 deletions

File tree

Toshi/Include/TKernel/TMath.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,52 @@
11
#pragma once
22

3+
#include "Defines.h"
4+
#include <math.h>
5+
36
#define TMIN(a, b) (((a) < (b)) ? (a) : (b))
47
#define TMAX(a, b) (((a) > (b)) ? (a) : (b))
8+
9+
TOSHI_NAMESPACE_BEGIN
10+
11+
class TKERNELINTERFACE_EXPORTS TMath
12+
{
13+
public:
14+
static TFLOAT Abs(TFLOAT m_fValue)
15+
{
16+
return fabsf(m_fValue);
17+
}
18+
static TFLOAT Sqrt(TFLOAT m_fValue)
19+
{
20+
return sqrtf(m_fValue);
21+
}
22+
// $TKernelInterface: FUNCTION 10011820
23+
static TFLOAT Sin(TFLOAT m_fValue)
24+
{
25+
return sinf(m_fValue);
26+
}
27+
// $TKernelInterface: FUNCTION 10021560
28+
static void SinCos(TFLOAT fVal, TFLOAT &a_rSin, TFLOAT &a_rCos)
29+
{
30+
a_rSin = Sin(fVal);
31+
a_rCos = Sin(fVal);
32+
}
33+
// $TKernelInterface: FUNCTION 100212a0
34+
static void Clip(TFLOAT &m_rfRes, TFLOAT m_fVal1, TFLOAT m_fVal2)
35+
{
36+
if (m_fVal2 < m_rfRes) {
37+
m_rfRes = m_fVal2;
38+
return;
39+
}
40+
if (m_fVal1 >= m_rfRes) {
41+
m_rfRes = m_fVal2;
42+
}
43+
}
44+
45+
static const TFLOAT PI;
46+
static const TFLOAT TWO_PI;
47+
static const TFLOAT HALF_PI;
48+
static const TFLOAT ONEOVER_SQRT_TWO;
49+
static const TFLOAT ONEOVERTWO_PI;
50+
};
51+
52+
TOSHI_NAMESPACE_END

Toshi/Include/TKernel/TPlane.h

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
#pragma once
2+
#include "TVector4.h"
3+
4+
TOSHI_NAMESPACE_BEGIN
5+
6+
class TPlane
7+
{
8+
public:
9+
enum PlaneComparison
10+
{
11+
PlaneComparison_InFront = -1,
12+
PlaneComparison_Intersects,
13+
PlaneComparison_InBack,
14+
};
15+
16+
public:
17+
TPlane() = default;
18+
19+
TPlane(const TVector4 &a_PlaneData)
20+
: m_Normal(a_PlaneData.AsVector3()), m_fDistance(a_PlaneData.w)
21+
{}
22+
23+
TPlane(const TVector3 &a_Normal, TFLOAT a_fDistance)
24+
: m_Normal(a_Normal), m_fDistance(a_fDistance)
25+
{}
26+
27+
TPlane(const TVector3 &a_Normal, const TVector3 &a_Point)
28+
: m_Normal(a_Normal), m_fDistance(TVector3::DotProduct(a_Normal, a_Point))
29+
{}
30+
31+
TPlane(TFLOAT a_fNormalX, TFLOAT a_fNormalY, TFLOAT a_fNormalZ, TFLOAT a_fDistance)
32+
: m_Normal(a_fNormalX, a_fNormalY, a_fNormalZ), m_fDistance(a_fDistance)
33+
{}
34+
35+
TPlane(const TPlane &a_Other)
36+
: m_Normal(a_Other.m_Normal), m_fDistance(a_Other.m_fDistance)
37+
{}
38+
39+
void Set(const TVector4 &a_PlaneData)
40+
{
41+
m_Normal = a_PlaneData.AsVector3();
42+
m_fDistance = a_PlaneData.w;
43+
}
44+
45+
void Set(const TVector3 &a_Normal, TFLOAT a_fDistance)
46+
{
47+
m_Normal = a_Normal;
48+
m_fDistance = a_fDistance;
49+
}
50+
51+
void Set(const TVector3 &a_Normal, const TVector3 &a_Point)
52+
{
53+
m_Normal = a_Normal;
54+
m_fDistance = TVector3::DotProduct(a_Normal, a_Point);
55+
}
56+
57+
void Set(TFLOAT a_fNormalX, TFLOAT a_fNormalY, TFLOAT a_fNormalZ, TFLOAT a_fDistance)
58+
{
59+
m_Normal.Set(a_fNormalX, a_fNormalY, a_fNormalZ);
60+
m_fDistance = a_fDistance;
61+
}
62+
63+
void Set(const TPlane &a_Other)
64+
{
65+
m_Normal = a_Other.m_Normal;
66+
m_fDistance = a_Other.m_fDistance;
67+
}
68+
69+
TFLOAT GetD() const
70+
{
71+
return m_fDistance;
72+
}
73+
74+
void SetD(TFLOAT a_fDistance)
75+
{
76+
m_fDistance = a_fDistance;
77+
}
78+
79+
const TVector3 &GetNormal() const
80+
{
81+
return m_Normal;
82+
}
83+
84+
void SetNormal(const TVector3 &a_rNormal)
85+
{
86+
m_Normal = a_rNormal;
87+
}
88+
89+
void SetNormal(TFLOAT a_fNormalX, TFLOAT a_fNormalY, TFLOAT a_fNormalZ)
90+
{
91+
m_Normal.Set(a_fNormalX, a_fNormalY, a_fNormalZ);
92+
}
93+
94+
void GetPoint(TVector3 &a_rPoint)
95+
{
96+
a_rPoint.Multiply(m_Normal, m_fDistance);
97+
}
98+
99+
void SetPoint(const TVector3 &a_rPoint)
100+
{
101+
m_fDistance = TVector3::DotProduct(m_Normal, a_rPoint);
102+
}
103+
104+
void Negate(const TPlane &a_rPlane)
105+
{
106+
AsVector4().Negate4(a_rPlane.AsVector4());
107+
}
108+
109+
void Negate()
110+
{
111+
AsVector4().Negate4();
112+
}
113+
114+
TPlane &operator=(const TPlane &a_rPlane)
115+
{
116+
m_Normal = a_rPlane.m_Normal;
117+
m_fDistance = a_rPlane.m_fDistance;
118+
return *this;
119+
}
120+
121+
TVector3 &AsNormal() { return m_Normal; }
122+
const TVector3 &AsNormal() const { return m_Normal; }
123+
124+
TVector4 &AsVector4() { return *TREINTERPRETCAST(TVector4 *, this); }
125+
const TVector4 &AsVector4() const { return *TREINTERPRETCAST(const TVector4 *, this); }
126+
127+
private:
128+
TVector3 m_Normal;
129+
TFLOAT m_fDistance;
130+
};
131+
132+
TOSHI_NAMESPACE_END

Toshi/Include/TKernel/TStack.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,12 @@ class TStack
7777
}
7878

7979
private:
80-
TFORCEINLINE constexpr T &AtUnsafe(TUINT a_uiIndex)
80+
TFORCEINLINE T &AtUnsafe(TUINT a_uiIndex)
8181
{
8282
return *((T *)(m_aBuffer) + a_uiIndex);
8383
}
8484

85-
TFORCEINLINE constexpr const T &AtUnsafe(TUINT a_uiIndex) const
85+
TFORCEINLINE const T &AtUnsafe(TUINT a_uiIndex) const
8686
{
8787
return *((const T *)(m_aBuffer) + a_uiIndex);
8888
}

Toshi/Include/TKernel/TVector3.h

Lines changed: 139 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,141 @@
11
#pragma once
22

33
#include "TDebug.h"
4+
#include "TMath.h"
45

56
TOSHI_NAMESPACE_BEGIN
67

78
class TKERNELINTERFACE_EXPORTS TVector3
89
{
910
public:
10-
TVector3()
11+
TVector3() = default;
12+
TVector3(TFLOAT a_fX, TFLOAT a_fY, TFLOAT a_fZ) { Set(a_fX, a_fY, a_fZ); }
13+
TVector3(TFLOAT floats[3]) { Set(floats); }
14+
TVector3(const TVector3 &other) { Set(other.m_fX, other.m_fY, other.m_fZ); }
15+
16+
void Clip(TFLOAT fVal, TFLOAT fVal2);
17+
void CrossProduct(const TVector3 &, const TVector3 &);
18+
19+
void RotateX(TFLOAT a_fRotation);
20+
void RotateY(TFLOAT a_fRotation);
21+
void RotateZ(TFLOAT a_fRotation);
22+
23+
void Normalize();
24+
25+
void Set(const TVector3 &vec)
26+
{
27+
TVector3::m_fX = vec.m_fX;
28+
TVector3::m_fY = vec.m_fY;
29+
TVector3::m_fZ = vec.m_fZ;
30+
}
31+
32+
void Set(TFLOAT floats[3])
33+
{
34+
TVector3::m_fX = floats[0];
35+
TVector3::m_fY = floats[1];
36+
TVector3::m_fZ = floats[2];
37+
}
38+
39+
void Set(TFLOAT m_fX, TFLOAT m_fY, TFLOAT m_fZ)
40+
{
41+
TVector3::m_fX = m_fX;
42+
TVector3::m_fY = m_fY;
43+
TVector3::m_fZ = m_fZ;
44+
}
45+
46+
void Lerp(const TVector3 &finish, float t) { Lerp(*this, finish, t); }
47+
48+
void Lerp(const TVector3 &start, const TVector3 &finish, float t)
1149
{
50+
TVector3 progress = finish - start;
51+
progress.Multiply(t);
1252

53+
Set(start + progress);
1354
}
14-
// $TKernelInterface: FUNCTION 10009820
15-
TVector3(TFLOAT a_fX, TFLOAT a_fY, TFLOAT a_fZ)
55+
56+
void Add(const TVector3 &vec)
1657
{
17-
Set(a_fX, a_fY, a_fZ);
58+
m_fX += vec.m_fX;
59+
m_fY += vec.m_fY;
60+
m_fZ += vec.m_fZ;
1861
}
19-
// $TKernelInterface: FUNCTION 100096e0
20-
void Set(TFLOAT a_fX, TFLOAT a_fY, TFLOAT a_fZ)
62+
63+
void Add(const TVector3 &a, const TVector3 &b)
2164
{
22-
m_fX = a_fX;
23-
m_fY = a_fY;
24-
m_fZ = a_fZ;
65+
Set(a + b);
2566
}
2667

68+
void Divide(const TVector3 &vec)
69+
{
70+
m_fX /= vec.m_fX;
71+
m_fY /= vec.m_fY;
72+
m_fZ /= vec.m_fZ;
73+
}
74+
75+
void Divide(TFLOAT scalar)
76+
{
77+
TFLOAT ratio = 1.0f / scalar;
78+
m_fX *= ratio;
79+
m_fY *= ratio;
80+
m_fZ *= ratio;
81+
}
82+
83+
void Divide(const TVector3 &vec1, const TVector3 &vec2)
84+
{
85+
Set(vec1 / vec2);
86+
}
87+
88+
void Divide(const TVector3 &vec, TFLOAT scalar)
89+
{
90+
Set(vec);
91+
Divide(scalar);
92+
}
93+
94+
void Multiply(const TVector3 &vec)
95+
{
96+
m_fX *= vec.m_fX;
97+
m_fY *= vec.m_fY;
98+
m_fZ *= vec.m_fZ;
99+
}
100+
101+
void Multiply(TFLOAT scalar)
102+
{
103+
m_fX *= scalar;
104+
m_fY *= scalar;
105+
m_fZ *= scalar;
106+
}
107+
108+
void Multiply(const TVector3 &vec1, const TVector3 &vec2)
109+
{
110+
Set(vec1 * vec2);
111+
}
112+
113+
void Multiply(const TVector3 &vec, TFLOAT scalar)
114+
{
115+
Set(vec);
116+
Multiply(scalar);
117+
}
118+
119+
TBOOL Equals(const TVector3 &vec)
120+
{
121+
return vec.m_fX == m_fX && vec.m_fY == m_fY && vec.m_fZ == m_fZ;
122+
}
123+
124+
void Abs(const TVector3 &vec3) { Set(TMath::Abs(vec3.m_fX), TMath::Abs(vec3.m_fY), TMath::Abs(vec3.m_fZ)); }
125+
void Abs() { Set(TMath::Abs(m_fX), TMath::Abs(m_fY), TMath::Abs(m_fZ)); }
126+
127+
TFLOAT Magnitude() const { return TMath::Sqrt(m_fX * m_fX + m_fY * m_fY + m_fZ * m_fZ); }
128+
TFLOAT MagnitudeSq() const { return m_fX * m_fX + m_fY * m_fY + m_fZ * m_fZ; }
129+
130+
TVector3 operator+(const TVector3 &other) const { return { m_fX + other.m_fX, m_fY + other.m_fY, m_fZ + other.m_fZ }; }
131+
TVector3 operator-(const TVector3 &other) const { return { m_fX - other.m_fX, m_fY - other.m_fY, m_fZ - other.m_fZ }; }
132+
TVector3 operator*(const TVector3 &other) const { return { m_fX * other.m_fX, m_fY * other.m_fY, m_fZ * other.m_fZ }; }
133+
TVector3 operator/(const TVector3 &other) const { return { m_fX / other.m_fX, m_fY / other.m_fY, m_fZ / other.m_fZ }; }
134+
135+
void operator=(const TVector3 &other) { Set(other); }
136+
void operator+=(const TVector3 &other) { Add(other); }
137+
void operator/=(const TVector3 &other) { Divide(other); }
138+
27139
const TFLOAT &operator()(TUINT a_iIndex) const
28140
{
29141
return (&m_fX)[a_iIndex];
@@ -34,6 +146,24 @@ class TKERNELINTERFACE_EXPORTS TVector3
34146
return (&m_fX)[a_iIndex];
35147
}
36148

149+
public:
150+
static TFLOAT Distance(const TVector3 &vec1, const TVector3 &vec2) { return (vec2 - vec1).Magnitude(); }
151+
static TFLOAT DistanceSq(const TVector3 &vec1, const TVector3 &vec2) { return (vec2 - vec1).MagnitudeSq(); }
152+
static TFLOAT DotProduct(const TVector3 &vec1, const TVector3 &vec2) { return vec1.m_fX * vec2.m_fX + vec1.m_fY * vec2.m_fY + vec1.m_fZ * vec2.m_fZ; }
153+
154+
public:
155+
static const TVector3 VEC_ZERO;
156+
static const TVector3 VEC_POSX;
157+
static const TVector3 VEC_POSY;
158+
static const TVector3 VEC_POSZ;
159+
static const TVector3 VEC_NEGX;
160+
static const TVector3 VEC_NEGY;
161+
static const TVector3 VEC_NEGZ;
162+
static const TVector3 VEC_NEGXPOSZ;
163+
static const TVector3 VEC_NEGXNEGZ;
164+
static const TVector3 VEC_POSXPOSZ;
165+
static const TVector3 VEC_POSXNEGZ;
166+
37167
private:
38168
TFLOAT m_fX; // 0x0
39169
TFLOAT m_fY; // 0x4

0 commit comments

Comments
 (0)