diff options
author | David Doan <daviddoan@Davids-MacBook-Pro-100.local> | 2023-12-11 17:59:14 -0500 |
---|---|---|
committer | David Doan <daviddoan@Davids-MacBook-Pro-100.local> | 2023-12-11 17:59:14 -0500 |
commit | 58c896801817ac67913644ccd9373cd18774232a (patch) | |
tree | 4363b1d82c37f760d8efa56a22749949aa55879a /src/intersect/intersect.cpp | |
parent | 5b67f3470236099e6991ed03e6e80fafbfc20284 (diff) | |
parent | bbe08c295ccfa32de1a9ca7321f36b2c130ad233 (diff) |
render and rotation and merge
Diffstat (limited to 'src/intersect/intersect.cpp')
-rw-r--r-- | src/intersect/intersect.cpp | 100 |
1 files changed, 71 insertions, 29 deletions
diff --git a/src/intersect/intersect.cpp b/src/intersect/intersect.cpp index 3a39a87..cc3143d 100644 --- a/src/intersect/intersect.cpp +++ b/src/intersect/intersect.cpp @@ -7,6 +7,7 @@ // TODO: implement mesh +//updated to handle intersection in 4d glm::vec4 intersectCircle( glm::vec4 p, glm::vec4 d, @@ -14,9 +15,9 @@ glm::vec4 intersectCircle( { // implicit: x^2 + y^2 + z^2 - r^2 = 0, all directions float radius = 0.5f; - float a = d.x*d.x + d.y*d.y + d.z*d.z; - float b = 2.f * (p.x*d.x + p.y*d.y + p.z*d.z); - float c = p.x*p.x + p.y*p.y + p.z*p.z - radius*radius; + float a = d.x*d.x + d.y*d.y + d.z*d.z + d[3] * d[3]; + float b = 2.f * (p.x*d.x + p.y*d.y + p.z*d.z + p[3]*d[3]); + float c = p.x*p.x + p.y*p.y + p.z*p.z + p[3] * p[3] - radius*radius; float discriminant = b*b - 4*a*c; if (discriminant < 0) // no solution @@ -47,12 +48,12 @@ glm::vec4 intersectCone( const RenderShapeData& shape) { float t = FINF; - - // implicit: x^2 + y^2 - z^2 = 0, conic top + // updated to 4d + // x^2 + y^2 - z^2 - w^2= 0, conic top float radius = 0.5f; - float a = d.x*d.x + d.z*d.z - .25f*(d.y*d.y); - float b = 2.f*(p.x*d.x + p.z*d.z) - .5f*(p.y*d.y) + .25f*d.y; - float c = p.x*p.x + p.z*p.z - .25f*(p.y*p.y) + .25f*p.y - 1/16.f; + float a = d.x*d.x + d.z*d.z - .25f*(d.y*d.y) - .25f*(d[3]*d[3]); + float b = 2.f*(p.x*d.x + p.z*d.z) - .5f*(p.y*d.y) + .25f*d.y - .5f*(p[3]*d[3]) + .25f*d[3]; + float c = p.x*p.x + p.z*p.z - .25f*(p.y*p.y) + .25f*p.y - .25f*(p[3]*p[3]) + .25f*p[3] - 1/8.f; float discriminant = b*b - 4*a*c; if (discriminant >= 0) @@ -63,7 +64,8 @@ glm::vec4 intersectCone( auto p1Top = p + t1 * d; if ( t1 > 0 && - p1Top.y >= -.5f && p1Top.y <= .5f) + p1Top.y >= -.5f && p1Top.y <= .5f && + p1Top[3] >= -.5f && p1Top[3] <= .5f) { t = std::min(t1, t); @@ -72,23 +74,34 @@ glm::vec4 intersectCone( auto p2Top = p + t2 * d; if ( t2 > 0 && - p2Top.y >= -.5f && p2Top.y <= .5f) + p2Top.y >= -.5f && p2Top.y <= .5f && + p2Top[3] >= -.5f && p2Top[3] <= .5f) { t = std::min(t2, t); } } - - // implicit p_y + t*d_y = -.5f, top base - float tBase = (- .5f - p.y) / d.y; - auto pBase = p + tBase * d; + // x^2 + y^2 - z^2 = 0, base w.r.t. w axis + float twBase = (- .5f - p[3]) / d[3]; + auto pwBase = p + twBase * d; if ( - tBase > 0 && - pBase.x*pBase.x + pBase.z*pBase.z <= radius*radius + twBase > 0 && + pwBase.x*pwBase.x + pwBase.z*pwBase.z <= pwBase.y*pwBase.y ) { - t = std::min(t, tBase); + t = std::min(t, twBase); + } + + // x^2 + y^2 - z^2 = 0, base w.r.t. y axis + float tyBase = (- .5f - p.y) / d.y; + auto pyBase = p + tyBase * d; + if ( + tyBase > 0 && + pyBase.x*pyBase.x + pyBase.z*pyBase.z <= pyBase[3]*pyBase[3] + ) + { + t = std::min(t, tyBase); } return t == FINF ? glm::vec4(0.f) : p + t*d; @@ -101,7 +114,7 @@ glm::vec4 intersectCylinder( { float t = FINF; - // implicit: x^2 + z^2 = 0, y between -.5, 5 rectuangular side + // implicit: x^2 + z^2 = r^2, y + w between -.5, 5 rectuangular side float radius = 0.5f; float a = d.x*d.x + d.z*d.z; float b = 2.f * (p.x*d.x + p.z*d.z); @@ -116,7 +129,9 @@ glm::vec4 intersectCylinder( auto p1Top = p + t1 * d; if ( t1 > 0 && - p1Top.y >= -.5f && p1Top.y <= .5f) + p1Top.y + p1Top[3] >= -.5f && p1Top.y + p1Top[3] <= .5f && + p1Top.y >= -.5f && p1Top.y <= .5f && + p1Top[3] >= -.5f && p1Top[3] <= .5f) { t = std::min(t1, t); } @@ -124,32 +139,36 @@ glm::vec4 intersectCylinder( auto p2Top = p + t2 * d; if ( t2 > 0 && - p2Top.y >= -.5f && p2Top.y <= .5f) + p2Top.y + p2Top[3] >= -.5f && p2Top.y + p2Top[3] <= .5f && + p2Top.y >= -.5f && p2Top.y <= .5f && + p2Top[3] >= -.5f && p2Top[3] <= .5f) { t = std::min(t2, t); } } - // implicit p_y + t*d_y = -.5f, top base - float tTop = (.5f - p.y) / d.y; + // implicit y + w = .5f, top base + float tTop = (.5f - p.y - p.y) / (d[3] + d.y); auto pTop = p + tTop * d; if ( tTop > 0 && - pTop.x*pTop.x + pTop.z*pTop.z <= radius*radius - ) + pTop.x*pTop.x + pTop.z*pTop.z <= radius*radius && + pTop.y >= -.5f && pTop.y <= .5f && + pTop[3] >= -.5f && pTop[3] <= .5f) { t = std::min(t, tTop); } - // implicit p_y + t*d_y = -.5f, top base - float tBase = (- .5f - p.y) / d.y; + // implicit p_y + t*d_y = -.5f, Bottom base + float tBase = (.5f - p.y - p.y) / (d[3] + d.y); auto pBase = p + tBase * d; if ( tBase > 0 && - pBase.x*pBase.x + pBase.z*pBase.z <= radius*radius - ) + pBase.x*pBase.x + pBase.z*pBase.z <= radius*radius && + pBase.y >= -.5f && pBase.y <= .5f && + pBase[3] >= -.5f && pBase[3] <= .5f) { t = std::min(t, tBase); } @@ -221,6 +240,29 @@ glm::vec4 intersectCube ( tmax = tzmax; } + // w-dir + float twmin = (-apothem - p[3]) / d[3]; + float twmax = (apothem - p[3]) / d[3]; + + if (twmin > twmax) + { + std::swap(twmin, twmax); + } + + if ((tmin > twmax) || (twmin > tmax)) + { // no hit + return glm::vec4(0.f); + } + + if (twmin > tmin) + { + tmin = twmin; + } + if (twmax < tmax) + { + tmax = twmax; + } + if (tmin <= 0 && tmax <= 0) // both behind camera { return glm::vec4(0.f); @@ -262,4 +304,4 @@ glm::vec4 RayTracer::findIntersection( break; } return glm::vec4(0.f); -}
\ No newline at end of file +} |