From e3257c07510701218185e3c5307ce9a2afbb8e22 Mon Sep 17 00:00:00 2001 From: Acheroufkebir Yacine <yacine.acheroufkebir@gmail.com> Date: Tue, 7 Jan 2020 17:19:26 +0100 Subject: [PATCH] hit and reflection --- quad.cpp | 209 ++++++++++++++++++++++++++++++++++++++++++----------- sphere.cpp | 28 ++++++- 2 files changed, 195 insertions(+), 42 deletions(-) diff --git a/quad.cpp b/quad.cpp index 521c3fb..dbbc493 100644 --- a/quad.cpp +++ b/quad.cpp @@ -31,67 +31,79 @@ bool Quad::is_hit(const Ray3f ray) float Z = ray.origin().z(); - //Cas particuliers - if (a==0) //le rayon ne se déplace pas selon l'axe X - return (x4<=X && X<=x1); - - if (b==0) //le rayon ne se déplace pas selon l'axe Y - return (y5<=Y && Y<=y2); - - if (c==0) //le rayon ne se déplace pas selon l'axe Z - return (z6<=Z && Z<=z3); - //Calculs des coordonnées d'intersection avec les faces et vérification float t, x, y, z; //Face 1 - t = (x1 - X) / a ; - //x = x1; - y = Y + b*t; - z = Z + b*t; - if (y5<=y && y<=y2 && z6<=z && z3<=z) + if(a!=0) + { + t = (x1 - X) / a ; + //x = x1; + y = Y + b*t; + z = Z + c*t; + if (t>0 && y5<=y && y<=y2 && z6<=z && z3<=z) return true; + } //Face 2 - t = (y2 - Y) / b ; - x = X + b*t; - //y = y2; - z = Z + b*t; - if (x4<=x && x<=x1 && z6<=z && z3<=z) + if(b!=0) + { + t = (y2 - Y) / b ; + x = X + b*t; + //y = y2; + z = Z + c*t; + if (t>0 && x4<=x && x<=x1 && z6<=z && z3<=z) return true; + } + //Face 3 - t = (z3 - Z) / c ; - x = X + b*t; - y = Y + b*t; - //z = z3; - if (x4<=x && x<=x1 && y5<=y && y<=y2) + if(c!=0) + { + t = (z3 - Z) / c ; + x = X + a*t; + y = Y + b*t; + //z = z3; + if (t>0 && x4<=x && x<=x1 && y5<=y && y<=y2) return true; + } + //Face 4 - t = (x4 - X) / a ; - //x = x4; - y = Y + b*t; - z = Z + b*t; - if (y5<=y && y<= y2 && z6<=z && z3<=z) + if(a!=0) + { + t = (x4 - X) / a ; + //x = x4; + y = Y + b*t; + z = Z + c*t; + if (t>0 && y5<=y && y<= y2 && z6<=z && z3<=z) return true; + } + //Face 5 - t = (y5 - Y) / b ; - x = X + b*t; - //y = y5; - z = Z + b*t; - if (x4<=x && x<=x1 && z6<=z && z3<=z) + if(b!=0) + { + t = (y5 - Y) / b ; + x = X + a*t; + //y = y5; + z = Z + c*t; + if (t>0 && x4<=x && x<=x1 && z6<=z && z3<=z) return true; + } + //Face 6 - t = (z6 - Z) / c ; - x = X + b*t; - y = Y + b*t; - //z = z6; - if (x4<=x && x<=x1 && y5<=y && y<=y2) + if(c!=0) + { + t = (z6 - Z) / c ; + x = X + a*t; + y = Y + b*t; + //z = z6; + if (t>0 && x4<=x && x<=x1 && y5<=y && y<=y2) return true; + } return false; } @@ -100,7 +112,122 @@ bool Quad::is_hit(const Ray3f ray) Ray3f Quad::reflect(const Ray3f ray) const { + //Equations des faces + float x1 = origin_.x() + width_/2; //après + float y2 = origin_.y() + height_/2; //dessus + float z3 = origin_.z() + depth_/2; //devant + float x4 = origin_.x() - width_/2; //avant + float y5 = origin_.y() - height_/2; //dessous + float z6 = origin_.z() - depth_/2; //derrière + + //Informations du rayon + float a = ray.direction().x(); + float b = ray.direction().y(); + float c = ray.direction().z(); + float X = ray.origin().x(); + float Y = ray.origin().y(); + float Z = ray.origin().z(); + + + + //Calculs des coordonnées d'intersection avec les faces et vérification + float t, x, y, z; + + + Vector3f hit; + Vector3f N; + Vector3f I=ray.direction(); + + + //Face 1 + if(a!=0) + { + t = (x1 - X) / a ; + //x = x1; + y = Y + b*t; + z = Z + c*t; + if (t>0 && y5<=y && y<=y2 && z6<=z && z3<=z) + { + Vector3f hit=ray.origin()+t*ray.direction(); + Vector3f N=(hit-origin).normalize(); + } + } + + //Face 2 + if(b!=0) + { + t = (y2 - Y) / b ; + x = X + b*t; + //y = y2; + z = Z + c*t; + if (t>0 && x4<=x && x<=x1 && z6<=z && z3<=z) + { + Vector3f hit=ray.origin()+t*ray.direction(); + Vector3f N=(hit-origin).normalize(); + } + } + + + //Face 3 + if(c!=0) + { + t = (z3 - Z) / c ; + x = X + a*t; + y = Y + b*t; + //z = z3; + if (t>0 && x4<=x && x<=x1 && y5<=y && y<=y2) + { + Vector3f hit=ray.origin()+t*ray.direction(); + Vector3f N=(hit-origin).normalize(); + } + } + + //Face 4 + if(a!=0) + { + t = (x4 - X) / a ; + //x = x4; + y = Y + b*t; + z = Z + c*t; + if (t>0 && y5<=y && y<= y2 && z6<=z && z3<=z) + { + Vector3f hit=ray.origin()+t*ray.direction(); + Vector3f N=(hit-origin).normalize(); + } + } + + + //Face 5 + if(b!=0) + { + t = (y5 - Y) / b ; + x = X + a*t; + //y = y5; + z = Z + c*t; + if (t>0 && x4<=x && x<=x1 && z6<=z && z3<=z) + { + Vector3f hit=ray.origin()+t*ray.direction(); + Vector3f N=(hit-origin).normalize(); + } + } + + + //Face 6 + if(c!=0) + { + t = (z6 - Z) / c ; + x = X + a*t; + y = Y + b*t; + //z = z6; + if (t>0 && x4<=x && x<=x1 && y5<=y && y<=y2) + { + Vector3f hit=ray.origin()+t*ray.direction(); + Vector3f N=(hit-origin).normalize(); + } + } + + return(new Ray3f(hit,I-(I*N)*N)); } @@ -142,4 +269,4 @@ std::ostream & operator<<(std::ostream & st, const Quad & q) st << "depth : " << q.depth(); return st; -} \ No newline at end of file +} diff --git a/sphere.cpp b/sphere.cpp index df40249..887089c 100644 --- a/sphere.cpp +++ b/sphere.cpp @@ -1,5 +1,6 @@ #include "sphere.h" +#include <math.h> Sphere::Sphere(Material matter, Vector3f origin, float radius) : Shape(matter) @@ -28,6 +29,31 @@ bool Sphere::is_hit(const Ray3f ray) Ray3f Sphere::reflect(const Ray3f ray) const { + + double u= ray.origin().x(); + double v= ray.origin().y(); + double w= ray.origin().z(); + + double X= ray.origin().x()-origin_.x(); + double Y= ray.origin().y()-origin_.y(); + double Z= ray.origin().z()-origin_.z(); + + double a=u*u+v*v+w*w; + double b=2*(X*u+Y*v+Z*w); + double c=X*X+Y*Y+Z*Z-radius_*radius_; + + double delta=b*b-4*a*c; + + if(delta<0) {throw "object is not hit";} + + double k=(-b-sqrt(delta))/(2*a); + + Vector3f hit=ray.origin()+k*ray.direction(); + Vector3f N=(hit-origin).normalize(); + + Vector3f I=ray.direction(); + + return(new Ray3f(hit,I-(I*N)*N)); } @@ -54,4 +80,4 @@ std::ostream & operator<<(std::ostream & st, const Sphere & s) st << "radius : " << s.radius() << std::endl; return st; -} \ No newline at end of file +} -- GitLab