Skip to content
Extraits de code Groupes Projets
Valider 619e01de rédigé par DBA_3's avatar DBA_3
Parcourir les fichiers

correction sphere hit reflection

parent 6d15730a
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
main 100644 → 100755
Aucun aperçu pour ce type de fichier
......@@ -167,13 +167,27 @@ int main()
shapes[2] = new Quad(white, Vector3f(width/2, height, depth/2), width, 0, depth); //plafond
shapes[3] = new Quad(cyan, Vector3f(0, height/2, depth/2), 0, height, depth); //mur droit
shapes[4] = new Quad(grey, Vector3f(width/2, 0, depth/2), width, 0, depth); //sol
shapes[5] = new Quad(red, Vector3f(50, 50, 50), 5, 5, 5); //cube
shapes[6] = new Sphere(blue, Vector3f(3/4*width, 20, depth/2), 40); //sphere
shapes[5] = new Quad(red, Vector3f(10, 10, 10), 5, 5, 5); //cube
shapes[6] = new Sphere(blue, Vector3f(50, 50, 50), 5); //sphere
Camera camera(Vector3f(width/2, height/2, -30), Vector3f(0, 0, 1)); //on met la caméra un peu en recul
Ray3f source(Vector3f(width/2, height, depth/2), Vector3f(0, -1, 0)); //lumière au plafonc
Scene scene(camera, shapes, source);
/* reflexion et hit test */
Ray3f light(Vector3f(71,70,70),Vector3f(-1,-1,-1));
std::cout<<"is hit "<<shapes[6]->is_hit(light)<<std::endl;
if(shapes[6]->is_hit(light))
{
std::cout<<"origin "<<shapes[6]->reflect(light).origin()<<std::endl;
std::cout<<"direction "<<shapes[6]->reflect(light).direction()<<std::endl;
}
//----------CALCUL ET SAUVEGARDE DE L'IMAGE----------
std::cout << "Création de l'image..." << std::endl;
......
......@@ -140,10 +140,6 @@ Ray3f Quad::reflect(const Ray3f ray) const
Vector3f I = ray.direction();
//std::cout << "a=" <<a << std::endl;
//std::cout << "b=" <<a << std::endl;
//std::cout << "c=" <<a << std::endl;
//Face 1
if(a!=0 && Vector3f(1,0,0)*ray.direction()<0)
......@@ -156,15 +152,10 @@ Ray3f Quad::reflect(const Ray3f ray) const
if (y5<=y && y<=y2 && z6<=z && z<=z3)
{
std::cout<<"face 1.2"<<std::endl;
Vector3f hit = ray.origin() + t*ray.direction();
Vector3f N = Vector3f(1,0,0);
N.normalize();
std::cout<< "vecteur (I*N) " <<I*N<< std::endl;
std::cout<< "vecteur (I*N)*N " <<(I*N)*N<< std::endl;
std::cout<< "vecteur I " <<I<< std::endl;
std::cout<< "vecteur N " <<N<< std::endl;
return Ray3f(hit, I-2*(I*N)*N);
}
......@@ -181,7 +172,6 @@ Ray3f Quad::reflect(const Ray3f ray) const
z = Z + c*t;
if (x4<=x && x<=x1 && z6<=z && z<=z3)
{
std::cout<<"face 2.2"<<std::endl;
Vector3f hit = ray.origin() + t*ray.direction();
Vector3f N = Vector3f(0,1,0);
N.normalize();
......@@ -201,7 +191,6 @@ Ray3f Quad::reflect(const Ray3f ray) const
//z = z3;
if (x4<=x && x<=x1 && y5<=y && y<=y2)
{
std::cout<<"face 3.2"<<std::endl;
Vector3f hit = ray.origin() + t*ray.direction();
Vector3f N = Vector3f(0,0,1);
N.normalize();
......@@ -213,14 +202,12 @@ Ray3f Quad::reflect(const Ray3f ray) const
//Face 4
if(a!=0 && Vector3f(-1,0,0)*ray.direction()<0)
{
std::cout<<"face 4"<<std::endl;
t = (x4 - X) / a ;
//x = X;
y = Y + b*t;
z = Z + c*t;
if (y5<=y && y<=y2 && z6<=z && z<=z3)
{
std::cout<<"face 4.2"<<std::endl;
Vector3f hit = ray.origin() + t*ray.direction();
Vector3f N = Vector3f(-1,0,0);
N.normalize();
......@@ -240,7 +227,6 @@ Ray3f Quad::reflect(const Ray3f ray) const
z = Z + c*t;
if (x4<=x && x<=x1 && z6<=z && z<=z3)
{
std::cout<<"face 5.2"<<std::endl;
Vector3f hit = ray.origin() + t*ray.direction();
Vector3f N = Vector3f(0,-1,0);
N.normalize();
......@@ -259,7 +245,6 @@ Ray3f Quad::reflect(const Ray3f ray) const
//z = z6;
if (x4<=x && x<=x1 && y5<=y && y<=y2)
{
std::cout<<"face 6.2"<<std::endl;
Vector3f hit = ray.origin() + t*ray.direction();
Vector3f N = Vector3f(0,0,-1);
N.normalize();
......
#include "sphere.h"
#include <math.h>
#include <iostream>
Sphere::Sphere(Material matter, Vector3f origin, float radius) : Shape(matter)
......@@ -16,11 +17,14 @@ bool Sphere::is_hit(const Ray3f ray)
Vector3f D = ray.origin()-origin_;
Vector3f u = ray.direction();
if (D.norm() < radius_) return false;
if (D*u < 0) return false;
if( (D^u).norm()/u.norm() < radius_ ) return false;
if (D*u > 0) return false;
if( (D^u).norm()/u.norm() > radius_ ) return false;
return true;
}
......@@ -30,9 +34,9 @@ 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 u = ray.direction().x();
double v = ray.direction().y();
double w = ray.direction().z();
double X = ray.origin().x()-origin_.x();
double Y = ray.origin().y()-origin_.y();
......@@ -54,7 +58,7 @@ Ray3f Sphere::reflect(const Ray3f ray) const
Vector3f I = ray.direction();
return Ray3f(hit, I-(I*N)*N);
return Ray3f(hit, I-2*(I*N)*N);
}
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter