isle-portable/LEGO1/tgl/tglvector.h
Mark Langen 260772e374
Bootstrap decomp of Tgl rendering library (#293)
* Bootstrap decomp of D3DRM rendering code

* This PR kicks off work on decompiling the D3D Retained Mode (D3DRM)
  rendering part of the codebase.

* High level overview:

* There is a base IMxDirect3DRMObject class which all of the D3DRM
  rendering objects inherit from. Its only virtual method is one to get
  the underlying object handle.

* A hierarchy of abstract classes inherits from this base class, which
  I've called "IMxDirect3DRM<class>". These classes only have pure
  virtual methods on them and don't contain any data.

* Each one of the abstract classes has exactly one concrete
  implementation, which I've called "MxDirect3DRM<class>". These classes
  have exactly one piece of data, which is a pointer to the underlying
  D3D Retained Mode object.

* If the classes need to store additional data, they store it in a
  userdata blob which is attached to the D3DRM object rather than the
  additional data being stored in the class itself.

* I've worked out about twice this many classes related to D3DRM
  rendering so far but the PR was getting large enough as is, so I'm
  cutting it here for now.

* I decomped sufficiently many methods of these classe to convince
  myself that the above observations are correct. About 60% of the
  decomped methods here are perfect matches, including at least one
  non-trivial method per class.

* Formatting

* Restructure changes using Tgl naming / details

* Restructure the changes to use the naming that we know from Tgl.

* Fill in some parts of the implementation I couldn't initially figure
  out using the details from Tgl (got more 100% matches).

* Move d3drm link requirement

* Fixups FloatMatrix -> FloatMatrix4

* Fix order

* Full fix for ordering problems

* Put back accidentally removed include.

* Fix call which should have been Release

* Use new and delete for DeepClone

* Missing Tgl:: on CreateRenderer

* Revert change to bool return value.

* Rename Something -> Unk

* Return paramter naming convention to what Tgl used

* Add scalar ddtor to verify inline destructor

* Fix order

* Change malloc/free -> new/delete in Tgl

* Remove duplicate destructor.

* Check all inline destructors

* Fix dtor comments

* Third time's the charm

* Alphabetical sort

* Decomp adjustments

* Add d3drm files to clang-format

---------

Co-authored-by: Christian Semmler <mail@csemmler.com>
2023-12-07 13:10:42 +01:00

279 lines
5.5 KiB
C++

#ifndef _tglVector_h
#define _tglVector_h
// Note: This file is almost an exact copy of the one from
// the leak but using floats instead of doubles, hence the
// strange formatting in some places.
#include "math.h" // sin() in RotateAroundY()
#include <stddef.h> // offsetof()
namespace Tgl
{
namespace Constant
{
const float Pi = 3.14159265358979323846;
};
inline float DegreesToRadians(float degrees)
{
return Constant::Pi * (degrees / 180.0);
}
inline float RadiansToDegrees(float radians)
{
return (radians / Constant::Pi) * 180.0;
}
//////////////////////////////////////////////////////////////////////////////
//
// Array<T, N>
template <class T, int N>
class Array {
public:
Array() {}
Array(const Array& rArray) { *this = rArray; }
~Array() {}
const T& operator[](int i) const { return m_elements[i]; };
T& operator[](int i) { return m_elements[i]; };
Array<T, N>& operator=(const Array<T, N>&);
void operator+=(const Array<T, N>&);
protected:
T m_elements[N];
};
//////////////////////////////////////////////////////////////////////////////
//
// Array<T, N> implementation
template <class T, int N>
inline Array<T, N>& Array<T, N>::operator=(const Array<T, N>& rArray)
{
int i;
for (i = 0; i < N; i++) {
m_elements[i] = rArray.m_elements[i];
}
return *this;
}
template <class T, int N>
inline void Array<T, N>::operator+=(const Array<T, N>& rArray)
{
int i;
for (i = 0; i < N; i++) {
m_elements[i] += rArray.m_elements[i];
}
}
//////////////////////////////////////////////////////////////////////////////
//
// FloatMatrix4
class FloatMatrix4 : public Array<Array<float, 4>, 4> {
public:
FloatMatrix4() {}
FloatMatrix4(const FloatMatrix4& rMatrix) { *this = rMatrix; }
FloatMatrix4(const FloatMatrix4&, const FloatMatrix4&);
void operator*=(const FloatMatrix4&);
};
//////////////////////////////////////////////////////////////////////////////
//
// FloatMatrix4 implementation
inline FloatMatrix4::FloatMatrix4(const FloatMatrix4& rMatrix1, const FloatMatrix4& rMatrix2)
{
for (int row = 0; row < 4; row++) {
for (int column = 0; column < 4; column++) {
float element = 0;
for (int i = 0; i < 4; i++) {
element += rMatrix1[row][i] * rMatrix2[i][column];
}
m_elements[row][column] = element;
}
}
}
inline void FloatMatrix4::operator*=(const FloatMatrix4& rMatrix)
{
FloatMatrix4 temp(*this, rMatrix);
// *this = FloatMatrix4(*this, rMatrix);
*this = temp;
}
//////////////////////////////////////////////////////////////////////////////
//
// Transformation matrices
class Translation : public FloatMatrix4 {
public:
Translation(const float[3]);
Translation(float x, float y, float z);
protected:
void Init(float x, float y, float z);
};
class Scale : public FloatMatrix4 {
public:
Scale(const float[3]);
Scale(float x, float y, float z);
Scale(float);
protected:
void Init(float x, float y, float z);
};
class RotationX : public FloatMatrix4 {
public:
RotationX(float radians);
};
class RotationY : public FloatMatrix4 {
public:
RotationY(float radians);
};
//////////////////////////////////////////////////////////////////////////////
//
// Transformation matrices implementation
inline Translation::Translation(const float vector[3])
{
Init(vector[0], vector[1], vector[2]);
}
inline Translation::Translation(float x, float y, float z)
{
Init(x, y, z);
}
inline void Translation::Init(float x, float y, float z)
{
m_elements[0][0] = 1;
m_elements[0][1] = 0;
m_elements[0][2] = 0;
m_elements[0][3] = 0;
m_elements[1][0] = 0;
m_elements[1][1] = 1;
m_elements[1][2] = 0;
m_elements[1][3] = 0;
m_elements[2][0] = 0;
m_elements[2][1] = 0;
m_elements[2][2] = 1;
m_elements[2][3] = 0;
m_elements[3][0] = x;
m_elements[3][1] = y;
m_elements[3][2] = z;
m_elements[3][3] = 1;
}
inline Scale::Scale(const float vector[3])
{
Init(vector[0], vector[1], vector[2]);
}
inline Scale::Scale(float x, float y, float z)
{
Init(x, y, z);
}
inline Scale::Scale(float scale)
{
Init(scale, scale, scale);
}
inline void Scale::Init(float x, float y, float z)
{
m_elements[0][0] = x;
m_elements[0][1] = 0;
m_elements[0][2] = 0;
m_elements[0][3] = 0;
m_elements[1][0] = 0;
m_elements[1][1] = y;
m_elements[1][2] = 0;
m_elements[1][3] = 0;
m_elements[2][0] = 0;
m_elements[2][1] = 0;
m_elements[2][2] = z;
m_elements[2][3] = 0;
m_elements[3][0] = 0;
m_elements[3][1] = 0;
m_elements[3][2] = 0;
m_elements[3][3] = 1;
}
inline RotationX::RotationX(float radians)
{
float cosRadians = cos(radians);
float sinRadians = sin(radians);
m_elements[0][0] = 1;
m_elements[0][1] = 0;
m_elements[0][2] = 0;
m_elements[0][3] = 0;
m_elements[1][0] = 0;
m_elements[1][1] = cosRadians;
m_elements[1][2] = -sinRadians;
m_elements[1][3] = 0;
m_elements[2][0] = 0;
m_elements[2][1] = sinRadians;
m_elements[2][2] = cosRadians;
m_elements[2][3] = 0;
m_elements[3][0] = 0;
m_elements[3][1] = 0;
m_elements[3][2] = 0;
m_elements[3][3] = 1;
}
inline RotationY::RotationY(float radians)
{
float cosRadians = cos(radians);
float sinRadians = sin(radians);
m_elements[0][0] = cosRadians;
m_elements[0][1] = 0;
m_elements[0][2] = sinRadians;
m_elements[0][3] = 0;
m_elements[1][0] = 0;
m_elements[1][1] = 1;
m_elements[1][2] = 0;
m_elements[1][3] = 0;
m_elements[2][0] = -sinRadians;
m_elements[2][1] = 0;
m_elements[2][2] = cosRadians;
m_elements[2][3] = 0;
m_elements[3][0] = 0;
m_elements[3][1] = 0;
m_elements[3][2] = 0;
m_elements[3][3] = 1;
}
//////////////////////////////////////////////////////////////////////////////
} // namespace Tgl
#endif /* _tglVector_h */