diff options
author | KT <tran0563@umn.edu> | 2021-09-21 10:05:57 -0500 |
---|---|---|
committer | KT <tran0563@umn.edu> | 2021-09-21 10:05:57 -0500 |
commit | a75b08b76f91451bb586b154fdca872955d8a57a (patch) | |
tree | dd1c30f65162c1e12b8f6481bbd102d69f5c7196 /dev/MinGfx | |
parent | Upload a1 (diff) | |
download | csci4611-a75b08b76f91451bb586b154fdca872955d8a57a.tar csci4611-a75b08b76f91451bb586b154fdca872955d8a57a.tar.gz csci4611-a75b08b76f91451bb586b154fdca872955d8a57a.tar.bz2 csci4611-a75b08b76f91451bb586b154fdca872955d8a57a.tar.lz csci4611-a75b08b76f91451bb586b154fdca872955d8a57a.tar.xz csci4611-a75b08b76f91451bb586b154fdca872955d8a57a.tar.zst csci4611-a75b08b76f91451bb586b154fdca872955d8a57a.zip |
publish a2
Diffstat (limited to 'dev/MinGfx')
-rw-r--r-- | dev/MinGfx/src/gfxmath.cc | 55 | ||||
-rw-r--r-- | dev/MinGfx/src/gfxmath.h | 16 | ||||
-rw-r--r-- | dev/MinGfx/src/quaternion.cc | 19 | ||||
-rw-r--r-- | dev/MinGfx/src/quick_shapes.cc | 62 | ||||
-rw-r--r-- | dev/MinGfx/src/ray.h | 9 |
5 files changed, 119 insertions, 42 deletions
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 <iostream> #include <string> +#include "gfxmath.h" namespace mingfx { @@ -180,11 +181,10 @@ void QuickShapes::initCyl() { const int nslices = 20; for (int s=1; s<nslices+1; s++) { - int slast = s - 1; - GLfloat xlast = std::cosf(-TWOPI * (float)slast / (float)nslices); - GLfloat zlast = std::sinf(-TWOPI * (float)slast/(float)nslices); - GLfloat xnew = std::cosf(-TWOPI * (float)(s)/(float)nslices); - GLfloat znew = std::sinf(-TWOPI * (float)(s)/(float)nslices); + GLfloat xlast = GfxMath::cos(-TWOPI * (float)(s-1)/(float)nslices); + GLfloat zlast = GfxMath::sin(-TWOPI * (float)(s-1)/(float)nslices); + GLfloat xnew = GfxMath::cos(-TWOPI * (float)(s)/(float)nslices); + GLfloat znew = GfxMath::sin(-TWOPI * (float)(s)/(float)nslices); // one triangle on the top verts.push_back(top); @@ -245,11 +245,10 @@ void QuickShapes::initCone() { const int nslices = 20; for (int s=1; s<nslices+1; s++) { - int slast = s - 1; - GLfloat xlast = std::cosf(-TWOPI * (float)slast/(float)nslices); - GLfloat zlast = std::sinf(-TWOPI * (float)slast/(float)nslices); - GLfloat xnew = std::cosf(-TWOPI * (float)(s)/(float)nslices); - GLfloat znew = std::sinf(-TWOPI * (float)(s)/(float)nslices); + GLfloat xlast = GfxMath::cos(-TWOPI * (float)(s-1)/(float)nslices); + GLfloat zlast = GfxMath::sin(-TWOPI * (float)(s-1)/(float)nslices); + GLfloat xnew = GfxMath::cos(-TWOPI * (float)(s)/(float)nslices); + GLfloat znew = GfxMath::sin(-TWOPI * (float)(s)/(float)nslices); // one triangle on the side // normals are a bit more complex than for other shapes... @@ -309,28 +308,30 @@ void QuickShapes::initSph() { const int nslices = 40; const int nstacks = 40; for (int s=1; s<nslices+1; s++) { - int slast = s - 1; - GLfloat xlast = std::cosf(-TWOPI * (float)slast/(float)nslices); - GLfloat zlast = std::sinf(-TWOPI * (float)slast/(float)nslices); - GLfloat xnew = std::cosf(-TWOPI * (float)(s)/(float)nslices); - GLfloat znew = std::sinf(-TWOPI * (float)(s)/(float)nslices); + GLfloat xlast = GfxMath::cos(-TWOPI * (float)(s-1)/(float)nslices); + GLfloat zlast = GfxMath::sin(-TWOPI * (float)(s-1)/(float)nslices); + GLfloat xnew = GfxMath::cos(-TWOPI * (float)(s)/(float)nslices); + GLfloat znew = GfxMath::sin(-TWOPI * (float)(s)/(float)nslices); float stackstep = PI/(float)nstacks; // one triangle on the top verts.push_back(top); - verts.push_back(Vertex(std::sinf(stackstep)*xlast,std::cosf(stackstep),std::sinf(stackstep)*zlast, - std::sinf(stackstep)*xlast,std::cosf(stackstep),std::sinf(stackstep)*zlast)); - verts.push_back(Vertex(std::sinf(stackstep)*xnew,std::cosf(stackstep),std::sinf(stackstep)*znew, - std::sinf(stackstep)*xnew,std::cosf(stackstep),std::sinf(stackstep)*znew)); + verts.push_back(Vertex(GfxMath::sin(stackstep)*xlast,GfxMath::cos(stackstep), + GfxMath::sin(stackstep)*zlast, + GfxMath::sin(stackstep)*xlast,GfxMath::cos(stackstep), + GfxMath::sin(stackstep)*zlast)); + verts.push_back(Vertex(GfxMath::sin(stackstep)*xnew,GfxMath::cos(stackstep), + GfxMath::sin(stackstep)*znew, + GfxMath::sin(stackstep)*xnew,GfxMath::cos(stackstep), + GfxMath::sin(stackstep)*znew)); for (int t=2; t<nstacks; t++) { - int tlast = t - 1; - GLfloat ylast = std::cosf(PI*(float)(tlast)/(float)nstacks); - GLfloat ynew = std::cosf(PI*(float)(t)/(float)nstacks); + GLfloat ylast = GfxMath::cos(PI*(float)(t-1)/(float)nstacks); + GLfloat ynew = GfxMath::cos(PI*(float)(t)/(float)nstacks); - GLfloat rlast = std::sinf(PI * (float)(tlast)/(float)nstacks); - GLfloat rnew = std::sinf(PI * (float)(t)/(float)nstacks); + GLfloat rlast = GfxMath::sin(PI * (float)(t-1)/(float)nstacks); + GLfloat rnew = GfxMath::sin(PI * (float)(t)/(float)nstacks); // two triangles to create a rect on the side verts.push_back(Vertex(rlast*xlast,ylast,rlast*zlast, rlast*xlast,ylast,rlast*zlast)); @@ -344,12 +345,17 @@ void QuickShapes::initSph() { // one triangle on the bottom verts.push_back(bot); - verts.push_back(Vertex(std::sinf(stackstep)*xnew,std::cosf(PI-stackstep),std::sinf(stackstep)*znew, - std::sinf(stackstep)*xnew,std::cosf(PI-stackstep),std::sinf(stackstep)*znew)); - verts.push_back(Vertex(std::sinf(stackstep)*xlast,std::cosf(PI-stackstep),std::sinf(stackstep)*zlast, - std::sinf(stackstep)*xlast,std::cosf(PI-stackstep),std::sinf(stackstep)*zlast)); + verts.push_back(Vertex(GfxMath::sin(stackstep)*xnew,GfxMath::cos(PI-stackstep), + GfxMath::sin(stackstep)*znew, + GfxMath::sin(stackstep)*xnew,GfxMath::cos(PI-stackstep), + GfxMath::sin(stackstep)*znew)); + verts.push_back(Vertex(GfxMath::sin(stackstep)*xlast,GfxMath::cos(PI-stackstep), + GfxMath::sin(stackstep)*zlast, + GfxMath::sin(stackstep)*xlast,GfxMath::cos(PI-stackstep), + GfxMath::sin(stackstep)*zlast)); } + std::vector<Point3> vertices; std::vector<Vector3> 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; |