Skip to content

Commit

Permalink
Updates for the projection matrix integration
Browse files Browse the repository at this point in the history
  • Loading branch information
“Malcolm committed Dec 14, 2024
1 parent 5abcfef commit 6a9cdef
Show file tree
Hide file tree
Showing 9 changed files with 335 additions and 125 deletions.
10 changes: 5 additions & 5 deletions platform/darwin/src/gltf/Camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ Camera::Camera() {

}

simd_float4x4 Camera::projectionMatrix() {
simd_double4x4 Camera::projectionMatrix() {
float fov = M_PI / 3; // original
fov = 0.6435011087932844; // Constant that i found in ML
simd_float4x4 matrix = GLTFPerspectiveProjectionMatrixAspectFovRH(fov, 1.0, 0.01, 250);
simd_double4x4 matrix = GLTFPerspectiveProjectionMatrixAspectFovRH(fov, 1.0, 0.01, 250);
return matrix;
}

Expand All @@ -53,9 +53,9 @@ void Camera::updateWithTimestep(double timestep) {
// _distance += _velocity * timestep;
_velocity *= GLTFViewerOrbitCameraZoomDrag;

simd_float4x4 pitchRotation = GLTFRotationMatrixFromAxisAngle(GLTFAxisX, -_rotationAngles.y);
simd_float4x4 yawRotation = GLTFRotationMatrixFromAxisAngle(GLTFAxisY, -_rotationAngles.x);
simd_float4x4 translation = GLTFMatrixFromTranslation((simd_float3){ 0, 0, _distance });
simd_double4x4 pitchRotation = GLTFRotationMatrixFromAxisAngleD(GLTFAxisXD, -_rotationAngles.y);
simd_double4x4 yawRotation = GLTFRotationMatrixFromAxisAngleD(GLTFAxisYD, -_rotationAngles.x);
simd_double4x4 translation = GLTFMatrixFromTranslationD((simd_double3){ 0, 0, _distance });
_viewMatrix = matrix_invert(matrix_multiply(matrix_multiply(yawRotation, pitchRotation), translation));


Expand Down
4 changes: 2 additions & 2 deletions platform/darwin/src/gltf/Camera.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ class Camera {
double _velocity;
float _distance;
Camera();
simd_float4x4 projectionMatrix();
simd_float4x4 _viewMatrix;
simd_double4x4 projectionMatrix();
simd_double4x4 _viewMatrix;
void updateWithTimestep(double timestep);

};
Expand Down
5 changes: 4 additions & 1 deletion platform/darwin/src/gltf/GLTFManagerRenderingEnvironment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ namespace maplibre { namespace gltf {
double _currentFOVDEG = 50;

// Environment projection matrix
simd_float4x4 _currentProjectionMatrix;
simd_double4x4 _currentProjectionMatrix;

// Current zoom level
double _currentZoomLevel = 1;

};

Expand Down
73 changes: 63 additions & 10 deletions platform/darwin/src/gltf/GLTFMath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ simd_float4x4 GLTFMatrixFromScale(const simd_float3 s) {
return m;
}

simd_double4x4 GLTFMatrixFromScaleD(const simd_double3 s) {
simd_double4x4 m = matrix_identity_double4x4;
m.columns[0].x = s.x;
m.columns[1].y = s.y;
m.columns[2].z = s.z;
return m;
}


simd_float4x4 GLTFMatrixFromUniformScale(float s) {
simd_float4x4 m = matrix_identity_float4x4;
m.columns[0].x = s;
Expand All @@ -24,6 +33,22 @@ simd_float4x4 GLTFMatrixFromUniformScale(float s) {
return m;
}

simd_double4x4 GLTFMatrixFromUniformScaleD(double s) {
simd_double4x4 m = matrix_identity_double4x4;
m.columns[0].x = s;
m.columns[1].y = s;
m.columns[2].z = s;
return m;
}


simd_double4x4 GLTFMatrixFromTranslationD(simd_double3 t) {
simd_double4x4 m = matrix_identity_double4x4;
m.columns[3] = (simd_double4) { t.x, t.y, t.z, 1.0 };
return m;
}


simd_float4x4 GLTFMatrixFromTranslation(simd_float3 t) {
simd_float4x4 m = matrix_identity_float4x4;
m.columns[3] = (simd_float4) { t.x, t.y, t.z, 1.0 };
Expand All @@ -32,13 +57,13 @@ simd_float4x4 GLTFMatrixFromTranslation(simd_float3 t) {

GLTFBoundingSphere GLTFBoundingSphereFromBox(const GLTFBoundingBox b) {
GLTFBoundingSphere s;
float midX = (b.maxPoint.x + b.minPoint.x) * 0.5;
float midY = (b.maxPoint.y + b.minPoint.y) * 0.5;
float midZ = (b.maxPoint.z + b.minPoint.z) * 0.5;
double midX = (b.maxPoint.x + b.minPoint.x) * 0.5;
double midY = (b.maxPoint.y + b.minPoint.y) * 0.5;
double midZ = (b.maxPoint.z + b.minPoint.z) * 0.5;

float r = sqrt(pow(b.maxPoint.x - midX, 2) + pow(b.maxPoint.y - midY, 2) + pow(b.maxPoint.z - midZ, 2));
double r = sqrt(pow(b.maxPoint.x - midX, 2) + pow(b.maxPoint.y - midY, 2) + pow(b.maxPoint.z - midZ, 2));

s.center = (simd_float3){ midX, midY, midZ };
s.center = (simd_double3){ midX, midY, midZ };
s.radius = r;
return s;
}
Expand All @@ -57,17 +82,36 @@ simd_float4x4 GLTFRotationMatrixFromAxisAngle(simd_float3 axis, float angle) {
return (simd_float4x4){ c0, c1, c2, c3 };
}

simd_double4x4 GLTFRotationMatrixFromAxisAngleD(simd_double3 axis, double angle) {
double x = axis.x, y = axis.y, z = axis.z;
double c = cos(angle);
double s = sin(angle);
double t = 1 - c;

simd_double4 c0 = { t * x * x + c, t * x * y + z * s, t * x * z - y * s, 0 };
simd_double4 c1 = { t * x * y - z * s, t * y * y + c, t * y * z + x * s, 0 };
simd_double4 c2 = { t * x * z + y * s, t * y * z - x * s, t * z * z + c, 0 };
simd_double4 c3 = { 0, 0, 0, 1 };

return (simd_double4x4){ c0, c1, c2, c3 };
}


simd_float3 GLTFAxisX = (simd_float3){ 1, 0, 0 };
simd_float3 GLTFAxisY = (simd_float3){ 0, 1, 0 };
simd_float3 GLTFAxisZ = (simd_float3){ 0, 0, 1 };

simd_float4x4 GLTFPerspectiveProjectionMatrixAspectFovRH(const float fovY, const float aspect, const float nearZ, const float farZ)
simd_double3 GLTFAxisXD = (simd_double3){ 1, 0, 0 };
simd_double3 GLTFAxisYD = (simd_double3){ 0, 1, 0 };
simd_double3 GLTFAxisZD = (simd_double3){ 0, 0, 1 };

simd_double4x4 GLTFPerspectiveProjectionMatrixAspectFovRH(const double fovY, const double aspect, const double nearZ, const double farZ)
{
float yscale = 1 / tanf(fovY * 0.5f); // 1 / tan == cot
float xscale = yscale / aspect;
float q = -farZ / (farZ - nearZ);
double yscale = 1 / tanf(fovY * 0.5f); // 1 / tan == cot
double xscale = yscale / aspect;
double q = -farZ / (farZ - nearZ);

simd_float4x4 m = {
simd_double4x4 m = {
.columns[0] = { xscale, 0, 0, 0 },
.columns[1] = { 0, yscale, 0, 0 },
.columns[2] = { 0, 0, q, -1 },
Expand Down Expand Up @@ -106,6 +150,15 @@ simd_float3x3 GLTFMatrixUpperLeft3x3(simd_float4x4 m) {
return mout;
}

simd_double3x3 GLTFMatrixUpperLeft3x3D(simd_double4x4 m) {
simd_double3x3 mout = { {
{ m.columns[0][0], m.columns[0][1], m.columns[0][2] },
{ m.columns[1][0], m.columns[1][1], m.columns[1][2] },
{ m.columns[2][0], m.columns[2][1], m.columns[2][2] }
} };
return mout;
}


GLTFTextureTransform GLTFTextureTransformMakeIdentity(void) {
GLTFTextureTransform t = {
Expand Down
27 changes: 22 additions & 5 deletions platform/darwin/src/gltf/GLTFMath.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ typedef enum {
GLTFTextureBindIndexBRDFLookup,
} GLTFMTLTextureBindIndex;

#define DEG_RAD 0.0174533
#define RAD_DEG 57.2958
#define DEG_RAD (M_PI/180.0)
#define RAD_DEG (180.0/M_PI)

// 0.0174533
// 57.2958

#define GLTFMTLMaximumLightCount 3
#define GLTFMTLMaximumTextureCount (GLTFTextureBindIndexEmissive + 1)
Expand All @@ -35,7 +38,7 @@ typedef struct {
} GLTFBoundingBox;

typedef struct {
simd_float3 center;
simd_double3 center;
float radius;
} GLTFBoundingSphere;

Expand Down Expand Up @@ -84,17 +87,29 @@ extern simd_float3 GLTFAxisX;
extern simd_float3 GLTFAxisY;
extern simd_float3 GLTFAxisZ;

extern simd_double3 GLTFAxisXD;
extern simd_double3 GLTFAxisYD;
extern simd_double3 GLTFAxisZD;

simd_float4x4 GLTFMatrixFromScale(const simd_float3 s);

simd_double4x4 GLTFMatrixFromScaleD(const simd_double3 s);

simd_float4x4 GLTFMatrixFromUniformScale(float s);

GLTFBoundingSphere GLTFBoundingSphereFromBox(const GLTFBoundingBox b);
simd_double4x4 GLTFMatrixFromUniformScaleD(double s);

GLTFBoundingSphere GLTFBoundingSphereFromBox(GLTFBoundingBox b);

simd_float4x4 GLTFMatrixFromTranslation(simd_float3 t);

simd_double4x4 GLTFMatrixFromTranslationD(simd_double3 t);

simd_float4x4 GLTFRotationMatrixFromAxisAngle(simd_float3 axis, float angle);

simd_float4x4 GLTFPerspectiveProjectionMatrixAspectFovRH(const float fovY, const float aspect, const float nearZ, const float farZ);
simd_double4x4 GLTFRotationMatrixFromAxisAngleD(simd_double3 axis, double angle);

simd_double4x4 GLTFPerspectiveProjectionMatrixAspectFovRH(const double fovY, const double aspect, const double nearZ, const double farZ);

simd_float4x4 GLTFOrthoProjectionMatrix(const float left,
const float right,
Expand All @@ -105,6 +120,8 @@ simd_float4x4 GLTFOrthoProjectionMatrix(const float left,

simd_float3x3 GLTFMatrixUpperLeft3x3(simd_float4x4 m);

simd_double3x3 GLTFMatrixUpperLeft3x3D(simd_double4x4 m);

GLTFTextureTransform GLTFTextureTransformMakeIdentity(void);

GLTFTextureTransform GLTFTextureTransformMakeSRT(simd_float2 scale, float rotation, simd_float2 offset);
Expand Down
Loading

0 comments on commit 6a9cdef

Please sign in to comment.