From 4ed3602c53418d6999800d2b404c2efb05f43c93 Mon Sep 17 00:00:00 2001
From: DBA_3 <yacine.acheroufkebir@gmail.com>
Date: Wed, 8 Jan 2020 16:59:48 +0100
Subject: [PATCH] is_hit

---
 quad.cpp | 74 +++++++++++++++++++++++++++++++++++---------------------
 1 file changed, 47 insertions(+), 27 deletions(-)

diff --git a/quad.cpp b/quad.cpp
index 9c35b44..1a20808 100644
--- a/quad.cpp
+++ b/quad.cpp
@@ -1,5 +1,7 @@
 #include "quad.h"
 
+#include <iostream>
+
 
 
 Quad::Quad(Material matter, Vector3f origin, float width, float height, float depth) : Shape(matter)
@@ -42,7 +44,7 @@ bool Quad::is_hit(const Ray3f ray)
 		//x = x1;
 		y = Y + b*t;
 		z = Z + c*t;
-		if (t>0 && y5<=y && y<=y2 && z6<=z && z3<=z)
+		if (Vector3f(1,0,0)*ray.direction()<0 && t>0 && y5<=y && y<=y2 && z6<=z && z<=z3)
 		return true;
 	}
 
@@ -53,7 +55,7 @@ bool Quad::is_hit(const Ray3f ray)
 		x = X + a*t;
 		//y = y2;
 		z = Z + c*t;
-		if (t>0 && x4<=x && x<=x1 && z6<=z && z3<=z)
+		if (Vector3f(0,1,0)*ray.direction()<0 && t>0 && x4<=x && x<=x1 && z6<=z && z<=z3)
 		return true;
 	}
 
@@ -65,7 +67,7 @@ bool Quad::is_hit(const Ray3f ray)
 		x = X + a*t;
 		y = Y + b*t;
 		//z = z3;
-		if (t>0 && x4<=x && x<=x1 && y5<=y && y<=y2)
+		if (Vector3f(0,0,1)*ray.direction()<0 && t>0 && x4<=x && x<=x1 && y5<=y && y<=y2)
 		return true;
 	}
 
@@ -77,7 +79,7 @@ bool Quad::is_hit(const Ray3f ray)
 		//x = x4;
 		y = Y + b*t;
 		z = Z + c*t;
-		if (t>0 && y5<=y && y<= y2 && z6<=z && z3<=z)
+		if (Vector3f(-1,0,0)*ray.direction()<0 && t>0 && y5<=y && y<= y2 && z6<=z && z<=z3)
 		return true;
 	}
 
@@ -89,7 +91,7 @@ bool Quad::is_hit(const Ray3f ray)
 		x = X + a*t;
 		//y = y5;
 		z = Z + c*t;
-		if (t>0 && x4<=x && x<=x1 && z6<=z && z3<=z)
+		if (Vector3f(0,-1,0)*ray.direction()<0 && t>0 && x4<=x && x<=x1 && z6<=z && z<=z3)
 		return true;
 	}
 
@@ -101,7 +103,7 @@ bool Quad::is_hit(const Ray3f ray)
 		x = X + a*t;
 		y = Y + b*t;
 		//z = z6;
-		if (t>0 && x4<=x && x<=x1 && y5<=y && y<=y2)
+		if (Vector3f(0,0,-1)*ray.direction()<0 && t>0 && x4<=x && x<=x1 && y5<=y && y<=y2)
 		return true;
 	}
 
@@ -131,7 +133,7 @@ Ray3f Quad::reflect(const Ray3f ray) const
 
 
 	//Calculs des coordonnées d'intersection avec les faces et vérification
-	float t, x, y, z;
+	float t, x, y ,z;
 
 
 	Vector3f hit;
@@ -139,40 +141,65 @@ Ray3f Quad::reflect(const Ray3f ray) const
 	Vector3f I = ray.direction();
 
 
-	//Face 1
+	std::cout << "a=" <<a << std::endl;
+	std::cout << "b=" <<a << std::endl;
+	std::cout << "c=" <<a << std::endl;
+
+	//Face 1 et 4
 	if(a!=0)
 	{
+		std::cout << "a!=0 face 1 ou 4" << std::endl;
 		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)
+
+		t = (x4 - X) / a ;
+		//x = x4;
+		y = Y + b*t;
+		z = Z + c*t;
+
+
+		if (y5<=y && y<= y2 && z6<=z && z<=z3)
 		{
 			Vector3f hit = ray.origin() + t*ray.direction();
-			Vector3f N = Vector3f(1,0,0);
+			Vector3f N = Vector3f(-1,0,0);
 			N.normalize();
+			return Ray3f(hit, I-(I*N)*N);
 		}
+
+
 	}
 
+
+
+
 	//Face 2
 	if(b!=0)
 	{
+		std::cout << "b!=0 face 2" << std::endl;
+
 		t = (y2 - Y) / b ;
 		x = X + a*t;
 		//y = y2;
 		z = Z + c*t;
-		if (t>0 && x4<=x && x<=x1 && z6<=z && z3<=z)
+		if (t>0 && x4<=x && x<=x1 && z6<=z && z<=z3)
 		{
 			Vector3f hit = ray.origin() + t*ray.direction();
 			Vector3f N = Vector3f(0,1,0);
 			N.normalize();
+			return Ray3f(hit, I-(I*N)*N);
 		}
+
+
+
 	}
 
 
 	//Face 3
 	if(c!=0)
 	{
+		std::cout << "c!=0 face 3" << std::endl;
 		t = (z3 - Z) / c ;
 		x = X + a*t;
 		y = Y + b*t;
@@ -182,38 +209,29 @@ Ray3f Quad::reflect(const Ray3f ray) const
 			Vector3f hit = ray.origin() + t*ray.direction();
 			Vector3f N = Vector3f(0,0,1);
 			N.normalize();
+			return Ray3f(hit, I-(I*N)*N);
 		}
 	}
 	
 
-	//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 = Vector3f(-1,0,0);
-			N.normalize();
-		}
-	}
+
+
 
 
 	//Face 5
 	if(b!=0)
 	{
+		std::cout << "b!=0 face 5" << std::endl;
 		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)
+		if (t>0 && x4<=x && x<=x1 && z6<=z && z<=z3)
 		{
 			Vector3f hit = ray.origin() + t*ray.direction();
 			Vector3f N = Vector3f(0,-1,0);
 			N.normalize();
+			return Ray3f(hit, I-(I*N)*N);
 		}
 	}
 
@@ -221,6 +239,7 @@ Ray3f Quad::reflect(const Ray3f ray) const
 	//Face 6
 	if(c!=0)
 	{
+		std::cout << "c!=0 face 6" << std::endl;
 		t = (z6 - Z) / c ;
 		x = X + a*t;
 		y = Y + b*t;
@@ -230,10 +249,11 @@ Ray3f Quad::reflect(const Ray3f ray) const
 			Vector3f hit = ray.origin() + t*ray.direction();
 			Vector3f N = Vector3f(0,0,-1);
 			N.normalize();
+			return Ray3f(hit, I-(I*N)*N);
 		}
 	}
 
-	return Ray3f(hit, I-(I*N)*N);
+	
 }
 
 
-- 
GitLab