Guest User

Untitled

a guest
Mar 18th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. // Example program
  2. #include <iostream>
  3. #include <string>
  4. #include <cmath>
  5.  
  6. struct vec3 {
  7. vec3(float x) : x(x), y(x), z(x) {}
  8. vec3(float x, float y, float z) : x(x), y(y), z(z) {}
  9. vec3 operator +(const vec3& v) const { return vec3(x + v.x, y + v.y, z + v.z); }
  10. vec3 operator -(const vec3& v) const { return vec3(x - v.x, y - v.y, z - v.z); }
  11. vec3 operator *(const vec3& v) const { return vec3(x * v.x, y * v.y, z * v.z); }
  12. vec3 operator *(const float v) const { return vec3(x * v, y * v, z * v); }
  13. vec3 operator /(const vec3& v) const { return vec3(x / v.x, y / v.y, z / v.z); }
  14. float x,y,z;
  15. };
  16. struct Ray {
  17. vec3 origin;
  18. vec3 direction;
  19. };
  20. struct Sphere {
  21. vec3 position;
  22. float radius;
  23. };
  24. struct Box {
  25. vec3 min;
  26. vec3 max;
  27. };
  28.  
  29. float dot(const vec3& v1, const vec3& v2)
  30. {
  31. return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
  32. }
  33. vec3 min(const vec3& v1, const vec3& v2)
  34. {
  35. return vec3(
  36. (v1.x < v2.x) ? v1.x : v2.x,
  37. (v1.y < v2.y) ? v1.y : v2.y,
  38. (v1.z < v2.z) ? v1.z : v2.z
  39. );
  40. }
  41. vec3 max(const vec3& v1, const vec3& v2)
  42. {
  43. return vec3(
  44. (v1.x > v2.x) ? v1.x : v2.x,
  45. (v1.y > v2.y) ? v1.y : v2.y,
  46. (v1.z > v2.z) ? v1.z : v2.z
  47. );
  48. }
  49. float min(float x1, float x2)
  50. {
  51. return (x1 < x2) ? x1 : x2;
  52. }
  53. float max(float x1, float x2)
  54. {
  55. return (x1 > x2) ? x1 : x2;
  56. }
  57. bool IntersectSphere(const Sphere& sphere, const Ray& ray, float& t)
  58. {
  59. std::cout << "sphere" << "\n";
  60. vec3 op = sphere.position - ray.origin;
  61. float b = dot(op, ray.direction);
  62. float det = b * b - dot(op, op) + sphere.radius * sphere.radius;
  63. if (det < 0.0f) return false;
  64.  
  65. det = sqrt(det);
  66. t = b - det;
  67. if (t < 0.0f) t = b + det;
  68. if (t < 0.0f) return false;
  69. return true;
  70. }
  71. bool IntersectBox(const Box& box, const Ray& ray, float& t)
  72. {
  73. std::cout << "box" << "\n";
  74. vec3 tMin = (box.min - ray.origin) / ray.direction;
  75. vec3 tMax = (box.max - ray.origin) / ray.direction;
  76. vec3 t1 = min(tMin, tMax);
  77. vec3 t2 = max(tMin, tMax);
  78. float tNear = max(max(t1.x, t1.y), t1.z);
  79. float tFar = min(min(t2.x, t2.y), t2.z);
  80. std::cout << "tNear = " << tNear << "\n";
  81. std::cout << "tFar = " << tFar << "\n";
  82. if (tNear < tFar)
  83. {
  84. t = (tNear > 0.0f) ? tNear : tFar;
  85. return true;
  86. }
  87. else
  88. return false;
  89. }
  90.  
  91. int main()
  92. {
  93. Sphere sphere = {vec3(0.0f), 1.0f};
  94. Box box = {vec3(-1.0f), vec3(1.0f)};
  95. Ray ray = {vec3(0.0f, 0.5f, 0.0f), vec3(1.0f, 0.0f, 0.0f)};
  96. float t = -1000.0f;
  97. //bool hit = IntersectBox(box, ray, t);
  98. bool hit = IntersectSphere(sphere, ray, t);
  99. std::cout << "hit = " << hit << "\n";
  100. std::cout << "t = " << t << "\n";
  101. }
Add Comment
Please, Sign In to add comment