From a75b08b76f91451bb586b154fdca872955d8a57a Mon Sep 17 00:00:00 2001 From: KT Date: Tue, 21 Sep 2021 10:05:57 -0500 Subject: publish a2 --- dev/MinGfx/src/gfxmath.cc | 55 +++++++++++++++++++++++++++++++++++++ dev/MinGfx/src/gfxmath.h | 16 +++++++++++ dev/MinGfx/src/quaternion.cc | 19 +++++++------ dev/MinGfx/src/quick_shapes.cc | 62 +++++++++++++++++++++++------------------- dev/MinGfx/src/ray.h | 9 +++--- 5 files changed, 119 insertions(+), 42 deletions(-) (limited to 'dev/MinGfx/src') diff --git a/dev/MinGfx/src/gfxmath.cc b/dev/MinGfx/src/gfxmath.cc index 11180e6..28cfedf 100644 --- a/dev/MinGfx/src/gfxmath.cc +++ b/dev/MinGfx/src/gfxmath.cc @@ -19,6 +19,61 @@ const float GfxMath::PI = 3.14159265359f; const float GfxMath::TWO_PI = 6.28318530718f; const float GfxMath::HALF_PI = 1.57079632679f; +float GfxMath::sin(float a) { +#ifdef WIN32 + return std::sinf(a); +#else + return std::sin(a); +#endif +} + +float GfxMath::cos(float a) { +#ifdef WIN32 + return std::cosf(a); +#else + return std::cos(a); +#endif +} + +float GfxMath::tan(float a) { +#ifdef WIN32 + return std::tanf(a); +#else + return std::tan(a); +#endif +} + +float GfxMath::asin(float a) { +#ifdef WIN32 + return std::asinf(a); +#else + return std::asin(a); +#endif +} + +float GfxMath::acos(float a) { +#ifdef WIN32 + return std::acosf(a); +#else + return std::acos(a); +#endif +} + +float GfxMath::atan(float a) { +#ifdef WIN32 + return std::atanf(a); +#else + return std::atan(a); +#endif +} + +float GfxMath::atan2(float a, float b) { +#ifdef WIN32 + return std::atan2f(a, b); +#else + return std::atan2(a, b); +#endif +} float GfxMath::Clamp(float x, float a, float b) { return std::min(std::max(x, a), b); diff --git a/dev/MinGfx/src/gfxmath.h b/dev/MinGfx/src/gfxmath.h index 09a3a1d..86c1061 100644 --- a/dev/MinGfx/src/gfxmath.h +++ b/dev/MinGfx/src/gfxmath.h @@ -28,6 +28,22 @@ namespace mingfx { class GfxMath { public: + /// MinGfx specific implementations of trigonometric functions included to + /// solve compilation issues between different platforms. + static float sin(float a); + + static float cos(float a); + + static float tan(float a); + + static float asin(float a); + + static float acos(float a); + + static float atan(float a); + + static float atan2(float a, float b); + /// Returns a if x is less than a and b if x is greater than b. static float Clamp(float x, float a, float b); diff --git a/dev/MinGfx/src/quaternion.cc b/dev/MinGfx/src/quaternion.cc index 4f2998f..24830c8 100644 --- a/dev/MinGfx/src/quaternion.cc +++ b/dev/MinGfx/src/quaternion.cc @@ -116,12 +116,13 @@ Quaternion Quaternion::Slerp(const Quaternion &other, float alpha) const { return result; } - GfxMath::Clamp(dot, -1, 1); // Robustness: Stay within domain of acos() - float theta_0 = acos(dot); // theta_0 = angle between input vectors - float theta = theta_0 * alpha; // theta = angle between v0 and result + GfxMath::Clamp(dot, -1, 1); // Robustness: Stay within domain of acos() + float theta_0 = GfxMath::acos(dot); // theta_0 = angle between input vectors + float theta = theta_0 * alpha; // theta = angle between v0 and result - float s0 = cos(theta) - dot * sin(theta) / sin(theta_0); // == sin(theta_0 - theta) / sin(theta_0) - float s1 = sin(theta) / sin(theta_0); + float s0 = GfxMath::cos(theta) - dot + * GfxMath::sin(theta) / GfxMath::sin(theta_0); // == sin(theta_0 - theta) / sin(theta_0) + float s1 = GfxMath::sin(theta) / GfxMath::sin(theta_0); return (s0 * v0) + (s1 * v1); } @@ -177,10 +178,10 @@ Quaternion Quaternion::Conjugate() const { Quaternion Quaternion::FromAxisAngle(const Vector3 &axis, float angle) { // [qx, qy, qz, qw] = [sin(a/2) * vx, sin(a/2)* vy, sin(a/2) * vz, cos(a/2)] - float x = sin(angle/2.0f) * axis[0]; - float y = sin(angle/2.0f) * axis[1]; - float z = sin(angle/2.0f) * axis[2]; - float w = cos(angle/2.0f); + float x = GfxMath::sin(angle/2.0) * axis[0]; + float y = GfxMath::sin(angle/2.0) * axis[1]; + float z = GfxMath::sin(angle/2.0) * axis[2]; + float w = GfxMath::cos(angle/2.0); return Quaternion(x,y,z,w); } diff --git a/dev/MinGfx/src/quick_shapes.cc b/dev/MinGfx/src/quick_shapes.cc index 388aa3d..01f187a 100644 --- a/dev/MinGfx/src/quick_shapes.cc +++ b/dev/MinGfx/src/quick_shapes.cc @@ -11,6 +11,7 @@ #include #include +#include "gfxmath.h" namespace mingfx { @@ -180,11 +181,10 @@ void QuickShapes::initCyl() { const int nslices = 20; for (int s=1; s vertices; std::vector normals; for (int i = 0; i < verts.size(); i++) { diff --git a/dev/MinGfx/src/ray.h b/dev/MinGfx/src/ray.h index 8e84546..d1b41b6 100644 --- a/dev/MinGfx/src/ray.h +++ b/dev/MinGfx/src/ray.h @@ -33,14 +33,13 @@ namespace mingfx { Example: ~~~ // Create a pick ray from the mouse position - void MyGraphicsApp::OnLeftMouseDown(const Point2 &pos) { - Point2 mouse_xy = PixelsToNormalizedDeviceCoords(pos); - float mouse_z = ReadZValueAtPixel(pos); - Point3 mouse_3d = GfxMath::ScreenToNearPlane(view_matrix, proj_matrix, mouse_xy, mouse_z); + void MyGraphicsApp::OnLeftMouseDown(const Point2 &mouse_in_2d_pixels) { + Point2 mouse_in_2d_ndc = PixelsToNormalizedDeviceCoords(mouse_in_2d_pixels); + Point3 mouse_in_3d = GfxMath::ScreenToNearPlane(view_matrix, proj_matrix, mouse_in_2d_ndc); Matrix4 camera_matrix = view_matrix.Inverse(); Point3 eye = camera_matrix.ColumnToPoint3(3); - Ray pick_ray(eye, mouse_3d - eye); + Ray pick_ray(eye, mouse_in_3d - eye); // check to see if the ray intersects a sphere float t; -- cgit v1.2.3