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

il a abondonné le projet pour ça zouz

parent ec017656
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
2.png

121 ko | W: | H:

2.png

137 ko | W: | H:

2.png
2.png
2.png
2.png
  • 2-up
  • Swipe
  • Onion skin
basic_ray_tracing.png

84,7 ko

Aucun aperçu pour ce type de fichier
...@@ -8,137 +8,10 @@ ...@@ -8,137 +8,10 @@
void test_Vector3f()
{
std::cout << "----- TEST Vector3f -----" << std::endl;
Vector3f a(1, 3, 1);
std::cout << "a=" << a << std::endl;
Vector3f b(a);
std::cout << "b=" << b << std::endl;
b = Vector3f(2, 2, 2);
std::cout << "b=" << b << std::endl;
Vector3f k=a^b;
std::cout << "a+b=" << a+b << std::endl;
std::cout << "a-b=" << a-b << std::endl;
std::cout << "a*b=" << a*b << std::endl;
std::cout << "a^b=" << k << std::endl;
//std::cout << "b*2=" << b*2 << std::endl;
std::cout << "2*b=" << 2*b << std::endl;
std::cout << "b/2=" << b/2 << std::endl;
std::cout << "norme de b = " << b.norm() << std::endl;
std::cout << std::endl << std::endl;
}
void test_Ray3f()
{
std::cout << "----- TEST Ray3f -----" << std::endl;
Ray3f r(Vector3f(0,0,0), Vector3f(1,-2,0));
std::cout << "r" << std::endl << r << std::endl;
Ray3f u = Ray3f(r);
std::cout << "u" << std::endl << u << std::endl;
std::cout << std::endl << std::endl;
}
void test_Camera()
{
std::cout << "----- TEST Camera -----" << std::endl;
Camera c(Vector3f(0,0,0), Vector3f(1,-2,0));
std::cout << "c" << std::endl << c << std::endl;
Camera u = Camera(c);
std::cout << "u" << std::endl << u << std::endl;
std::cout << std::endl << std::endl;
}
void test_Material()
{
std::cout << "----- TEST Material -----" << std::endl;
Material a(-10,15,205,0.5);
std::cout << "a" << std::endl << a << std::endl;
std::cout << std::endl << std::endl;
}
void test_Quad()
{
std::cout << "----- TEST Quad -----" << std::endl;
Material matter(-10, 15, 205, 0.5);
Quad q(matter, Vector3f(), 5, 5, 5);
std::cout << "q" << std::endl << q << std::endl;
std::cout << std::endl << std::endl;
}
void test_Sphere()
{
std::cout << "----- TEST Sphere -----" << std::endl;
Material matter(-10, 15, 205, 0.7);
Sphere s(matter, Vector3f(), 5);
std::cout << "s" << std::endl << s << std::endl;
std::cout << std::endl << std::endl;
}
void test_Scene()
{
std::cout << "----- TEST Scene -----" << std::endl;
Camera c(Vector3f(0,0,0), Vector3f(1,5,0));
Material matter(-10, 15, 205, 0.7);
Shape *tab[3];
tab[0] = new Quad(matter, Vector3f(), 5, 5, 5);
tab[1] = new Sphere(matter, Vector3f(), 5);
tab[2] = new Sphere(matter, Vector3f(), 5);
Ray3f source(Vector3f(10,10,10), Vector3f(1,-2,0));
Scene scene(c, tab, source);
//std::cout << "scene" << std::endl << scene << std::endl;
std::cout << std::endl << std::endl;
}
int main() int main()
{ {
try try
{ {
//----------TESTS----------
/*test_Vector3f();
test_Ray3f();
test_Camera();
test_Material();
test_Quad();
test_Sphere();
test_Scene();*/
//----------MISE EN PLACE DE LA SCENE---------- //----------MISE EN PLACE DE LA SCENE----------
Material red(255, 0, 0, 0); Material red(255, 0, 0, 0);
...@@ -152,7 +25,7 @@ int main() ...@@ -152,7 +25,7 @@ int main()
Material black(0, 0, 0, 0); Material black(0, 0, 0, 0);
Material mirror(0, 0, 0, 0.9); Material mirror(0, 0, 0, 0.9);
int nb_shapes = 7; int nb_shapes = 8;
float width = 100; //x float width = 100; //x
float height = 100; //y float height = 100; //y
float depth = 100; //z float depth = 100; //z
...@@ -169,28 +42,23 @@ int main() ...@@ -169,28 +42,23 @@ int main()
shapes[3] = new Quad(cyan, Vector3f(0, height/2, depth/2), 0, height, depth); //mur droit shapes[3] = new Quad(cyan, Vector3f(0, height/2, depth/2), 0, height, depth); //mur droit
shapes[4] = new Quad(green, Vector3f(width/2, 0, depth/2), width, 0, depth); //sol shapes[4] = new Quad(green, Vector3f(width/2, 0, depth/2), width, 0, depth); //sol
shapes[5] = new Quad(red, Vector3f(22, 15, 20), 10, 10, 10); //cube shapes[5] = new Quad(red, Vector3f(22, 15, 20), 10, 10, 10); //cube
shapes[6] = new Sphere(mirror, Vector3f(75, 20, 70), 20); //sphere shapes[6] = new Sphere(red, Vector3f(75, 20, 70), 20); //sphere
shapes[7] = new Sphere(mirror, Vector3f(25, 20, 70), 20); //sphere
Camera camera(Vector3f(width/2, height/2, -50), Vector3f(0, 0, 1)); //on met la caméra un peu en recul Camera camera(Vector3f(width/2, height/2, -50), Vector3f(0, 0, 1)); //on met la caméra un peu en recul
Ray3f source(Vector3f(width/2, height-1, depth/3), Vector3f(0, -1, 0)); //lumière au plafond Ray3f source(Vector3f(width/2, height-10, depth/3), Vector3f(0, -1, 0)); //lumière au plafond
Scene scene(camera, shapes, source); Scene scene(camera, shapes, source);
/* reflexion et hit test */
/*Ray3f light(Vector3f(71,70,70),Vector3f(-1,-1,-1));
std::cout<<"is hit "<<shapes[0]->is_hit(light)<<std::endl;
if(shapes[0]->is_hit(light))
{
std::cout<<"origin "<<shapes[0]->reflect(light).origin()<<std::endl;
std::cout<<"direction "<<shapes[0]->reflect(light).direction()<<std::endl;
}*/
//----------CALCUL ET SAUVEGARDE DE L'IMAGE---------- //----------CALCUL ET SAUVEGARDE DE L'IMAGE----------
std::cout << "Création de l'image..." << std::endl; std::cout << "Création de l'image..." << std::endl;
std::string filename = "2.png"; std::string filename = "basic_ray_tracing.png";
scene.render(width, height, 1080, 1080, nb_shapes, (char *) filename.c_str()); std::string filename2 = "ray_tracing_reflexion.png";
std::string filename3 = "ray_tracing_shading.png";
scene.render(width, height, 1080, 1080, nb_shapes, (char *) filename.c_str(),2,false);
scene.render(width, height, 1080, 1080, nb_shapes, (char *) filename2.c_str(),6,false);
scene.render(width, height, 1080, 1080, nb_shapes, (char *) filename3.c_str(),6,true);
std::cout << "Image sauvegardée" << std::endl; std::cout << "Image sauvegardée" << std::endl;
//----------LIBERATION DE LA MEMOIRE---------- //----------LIBERATION DE LA MEMOIRE----------
......
...@@ -49,6 +49,10 @@ class Ray3f ...@@ -49,6 +49,10 @@ class Ray3f
Vector3f direction() const; Vector3f direction() const;
}; };
/*!
* \brief Opérateur d'affichage des coordonnées du vecteur origine et direction
*/
std::ostream & operator<<(std::ostream & st, const Ray3f & v); std::ostream & operator<<(std::ostream & st, const Ray3f & v);
......
ray_tracing_reflexion.png

117 ko

ray_tracing_shading.png

162 ko

...@@ -180,7 +180,7 @@ void Scene::render(int width, int height, int nb_pixel_row, int nb_pixel_col, in ...@@ -180,7 +180,7 @@ void Scene::render(int width, int height, int nb_pixel_row, int nb_pixel_col, in
}*/ }*/
void Scene::render(int width, int height, int nb_pixel_row, int nb_pixel_col, int nb_shapes, char* filename) void Scene::render(int width, int height, int nb_pixel_row, int nb_pixel_col, int nb_shapes, char* filename, int ref_max, bool shading)
{ {
/*--------------------------------Copié de la doc de libpng-------------------------------------*/ /*--------------------------------Copié de la doc de libpng-------------------------------------*/
FILE *f = fopen(filename, "wb"); FILE *f = fopen(filename, "wb");
...@@ -226,11 +226,14 @@ void Scene::render(int width, int height, int nb_pixel_row, int nb_pixel_col, in ...@@ -226,11 +226,14 @@ void Scene::render(int width, int height, int nb_pixel_row, int nb_pixel_col, in
float min_dist; float min_dist;
float hit_dist; float hit_dist;
int ref_max = 6;
Ray3f *ray[ref_max]; Ray3f *ray[ref_max];
float shine[ref_max-1]; float shine[ref_max-1];
float coef[ref_max-1]; float coef[ref_max-1];
float incidence;
Vector3f S;
Vector3f N;
for (int i=nb_pixel_col-1; i>=0; i--) //on remplit l'image par le bas à droite for (int i=nb_pixel_col-1; i>=0; i--) //on remplit l'image par le bas à droite
{ {
...@@ -259,6 +262,19 @@ void Scene::render(int width, int height, int nb_pixel_row, int nb_pixel_col, in ...@@ -259,6 +262,19 @@ void Scene::render(int width, int height, int nb_pixel_row, int nb_pixel_col, in
bool shadow = false; bool shadow = false;
Ray3f reflect_to_source(reflected.origin(), source_.origin()-reflected.origin()); Ray3f reflect_to_source(reflected.origin(), source_.origin()-reflected.origin());
if(shading)
{
S=reflect_to_source.direction();
N=(ray[r+1]->direction()-ray[r]->direction());
incidence=fmax(0.5,(fmin(1, (N*S)/(N.norm()*S.norm()))));
}
else
{
incidence=1;
}
for (int l=0; l<nb_shapes; l++) for (int l=0; l<nb_shapes; l++)
{ {
...@@ -278,9 +294,9 @@ void Scene::render(int width, int height, int nb_pixel_row, int nb_pixel_col, in ...@@ -278,9 +294,9 @@ void Scene::render(int width, int height, int nb_pixel_row, int nb_pixel_col, in
if (shadow) if (shadow)
{ {
row[3*(nb_pixel_row-1-j)] = row[3*(nb_pixel_row-1-j)] + coef[r]*fmin(50, 50*intensity); row[3*(nb_pixel_row-1-j)] = row[3*(nb_pixel_row-1-j)] + incidence*coef[r]*fmin(50, 50*intensity);
row[3*(nb_pixel_row-1-j) + 1] = row[3*(nb_pixel_row-1-j) + 1] + coef[r]*fmin(50, 50*intensity); row[3*(nb_pixel_row-1-j) + 1] = row[3*(nb_pixel_row-1-j) + 1] + incidence*coef[r]*fmin(50, 50*intensity);
row[3*(nb_pixel_row-1-j) + 2] = row[3*(nb_pixel_row-1-j) + 2] + coef[r]*fmin(50, 50*intensity); row[3*(nb_pixel_row-1-j) + 2] = row[3*(nb_pixel_row-1-j) + 2] + incidence*coef[r]*fmin(50, 50*intensity);
} }
...@@ -292,17 +308,17 @@ void Scene::render(int width, int height, int nb_pixel_row, int nb_pixel_col, in ...@@ -292,17 +308,17 @@ void Scene::render(int width, int height, int nb_pixel_row, int nb_pixel_col, in
//au lieu de diviser par une constante (2.) diviser par l'angle ? //au lieu de diviser par une constante (2.) diviser par l'angle ?
if ( dist(reflect_to_source.origin() , shapes_[k]->reflect(source_to_reflect).origin()) > 0.05 ) if ( dist(reflect_to_source.origin() , shapes_[k]->reflect(source_to_reflect).origin()) > 0.05 )
{ {
row[3*(nb_pixel_row-1-j)] = row[3*(nb_pixel_row-1-j)] + coef[r]*fmin(shapes_[k]->matter().r()/2. * intensity, shapes_[k]->matter().r()/2.); row[3*(nb_pixel_row-1-j)] = row[3*(nb_pixel_row-1-j)] + incidence*coef[r]*fmin(shapes_[k]->matter().r()/2. * intensity, shapes_[k]->matter().r()/2.);
row[3*(nb_pixel_row-1-j) + 1] =row[3*(nb_pixel_row-1-j) + 1] + coef[r]*fmin(shapes_[k]->matter().g()/2. * intensity, shapes_[k]->matter().g()/2.); row[3*(nb_pixel_row-1-j) + 1] =row[3*(nb_pixel_row-1-j) + 1] + incidence*coef[r]*fmin(shapes_[k]->matter().g()/2. * intensity, shapes_[k]->matter().g()/2.);
row[3*(nb_pixel_row-1-j) + 2] =row[3*(nb_pixel_row-1-j) + 2] + coef[r]*fmin(shapes_[k]->matter().b()/2. * intensity, shapes_[k]->matter().b()/2.); row[3*(nb_pixel_row-1-j) + 2] =row[3*(nb_pixel_row-1-j) + 2] + incidence*coef[r]*fmin(shapes_[k]->matter().b()/2. * intensity, shapes_[k]->matter().b()/2.);
} }
else //il faut rajouter la réflexion et l'angle else //il faut rajouter la réflexion et l'angle
{ {
row[3*(nb_pixel_row-1-j)] = row[3*(nb_pixel_row-1-j)] + coef[r]*fmin(shapes_[k]->matter().r() * intensity, 255.); row[3*(nb_pixel_row-1-j)] = row[3*(nb_pixel_row-1-j)] + incidence*coef[r]*fmin(shapes_[k]->matter().r() * intensity, 255.);
row[3*(nb_pixel_row-1-j) + 1] = row[3*(nb_pixel_row-1-j) + 1] + coef[r]*fmin(shapes_[k]->matter().g() * intensity, 255.); row[3*(nb_pixel_row-1-j) + 1] = row[3*(nb_pixel_row-1-j) + 1] + incidence*coef[r]*fmin(shapes_[k]->matter().g() * intensity, 255.);
row[3*(nb_pixel_row-1-j) + 2] = row[3*(nb_pixel_row-1-j) + 2] + coef[r]*fmin(shapes_[k]->matter().b() * intensity, 255.); row[3*(nb_pixel_row-1-j) + 2] = row[3*(nb_pixel_row-1-j) + 2] + incidence*coef[r]*fmin(shapes_[k]->matter().b() * intensity, 255.);
} }
} }
} }
...@@ -310,12 +326,12 @@ void Scene::render(int width, int height, int nb_pixel_row, int nb_pixel_col, in ...@@ -310,12 +326,12 @@ void Scene::render(int width, int height, int nb_pixel_row, int nb_pixel_col, in
} }
png_write_row(png_ptr, row); png_write_row(png_ptr, row);
}
png_write_end(png_ptr, NULL);
for (int r=0; r<ref_max;r++) for (int r=0; r<ref_max;r++)
delete[] ray[r]; delete[] ray[r];
}
png_write_end(png_ptr, NULL);
fclose(f); fclose(f);
......
...@@ -19,22 +19,84 @@ ...@@ -19,22 +19,84 @@
class Scene class Scene
{ {
Camera camera_; Camera camera_; /*!< La camera */
Shape* *shapes_; Shape* *shapes_; /*!< Tableau de pointeurs des formes */
Ray3f source_; Ray3f source_; /*!< Source de lumiere */
public: public:
/*!
* \brief Constructeur
*
* Constructeur de la classe Scene
* \param camera la camera
* \param Shape tableau abstrait contenant des pointeurs vers des shapes
* \param source Ray3f qui représente la source de lumiere
*/
Scene(Camera camera, Shape* *shapes, Ray3f source); Scene(Camera camera, Shape* *shapes, Ray3f source);
/*!
* \brief Constructeur de Copie
*
* \param s La scene dont les paramètre sont copiées
*/
Scene(const Scene & s); Scene(const Scene & s);
//~Scene(); //~Scene();
/*!
* \brief Getter pour la camera de la scene
*/
Camera camera() const; Camera camera() const;
/*!
* \brief Getter pour le pointeur des shapes
*/
Shape* *shapes() const; Shape* *shapes() const;
/*!
* \brief Getter pour la source de lumière de la scene
*/
Ray3f source() const; Ray3f source() const;
void render(int width, int height, int nb_pixel_row, int nb_pixel_col, int nb_shapes, char* filename); /*!
* \brief Rendu de la scene
*
* La fonction principale du projet. Cree un rendu de la scene vu depuis la camera, au nom de filename. nb_shapes doit contenir le nombre de formes dans shapes_ à afficher. ref_max est le nombre de reflections maximales à réaliser pour chaque rayon. Shading est un bool qui indique si on doit prendre en compte l'angle d'incidence sur les objet. Plus cet angle est grand plus la lumière est atténuée.
* L'image est creee dans le dossier contenant l'executable
* \param width largeur de l'image > 0
* \param height hauteur de l'image > 0
* \param nb_pixel_rox nombre de pixels sur les lignes
* \param nb_pixel_col nombre de pixels sur les colonnes
* \param nb_shapes le nombre de formes à afficher
* \param filename string non vide
* \param ref_max nombre de reflections maximales à réaliser
* \param shading booléen, si vrai prend en compte l'incidence de la lumière sur les objets pour le rendu
*/
void render(int width, int height, int nb_pixel_row, int nb_pixel_col, int nb_shapes, char* filename, int ref_max, bool shading);
/*!
* \brief renvoie l'indice de shapes du premier objet touché par le rayon, si aucun objet n'est touché, renvoie -1
*
* \param nb_shapes nombre de shapes
* \param start Rayon dont on cherche le premier objet touché
*/
int closer(int nb_shapes, Ray3f start); int closer(int nb_shapes, Ray3f start);
/*!
* \brief modifie ray et shine
*
* modifie les tableaux pour qu'ils contiennent respectivement les rayon réfléchis et les coeffiscients de shiness des objets touchés en prenant en compte un nombre maximal de reflexions.
* \param ray doit contenir un premier rayon représentant la direction de la camera
* \param nb_shapes nombre des formes
* \param ref_max nombre maximal de reflections
* \param shines tableau de flotant
*/
void reflected_rays(Ray3f **ray, int nb_shapes, int ref_max, float* shine); void reflected_rays(Ray3f **ray, int nb_shapes, int ref_max, float* shine);
/*!
* \brief récupère le tableau de shiness et modifie les coeffiscients de reflexion
*
* En prenant en compte les coeffiscients de shiness des objets successifs touchés par un rayon, modifie le tableau coef pour qu'il contienne les coeffiscients de reflection des objets successifs touchés.
* \param shine tableau de flotant
* \param coef tableau de flotant
* \param ref_max nombre maximal de reflections
*/
void coeff(float* shine, float* coef, int ref_max); void coeff(float* shine, float* coef, int ref_max);
}; };
......
Ce diff est replié.
...@@ -41,7 +41,7 @@ class Vector3f ...@@ -41,7 +41,7 @@ class Vector3f
* \brief Constructeur de copie * \brief Constructeur de copie
* *
* Constructeur de copie de la classe Vector3f * Constructeur de copie de la classe Vector3f
* \param v le vecteur à copier dans this * \param v le vecteur à copier dans this
*/ */
Vector3f(const Vector3f & v); Vector3f(const Vector3f & v);
Vector3f(); Vector3f();
...@@ -72,22 +72,66 @@ class Vector3f ...@@ -72,22 +72,66 @@ class Vector3f
void normalize(); void normalize();
}; };
/*!
* \brief Opérateur d'affichage des paramètres du vecteur
* \param v Le vecteur à afficher
*/
std::ostream & operator<<(std::ostream & st, const Vector3f & v); std::ostream & operator<<(std::ostream & st, const Vector3f & v);
/*!
* \brief Opérateur d'addition de vecteurs
* \param v1 Le premier vecteur
* \parma v2 Le deuxième vecteur
*/
Vector3f operator+(const Vector3f & v1, const Vector3f & v2); Vector3f operator+(const Vector3f & v1, const Vector3f & v2);
/*!
* \brief Opérateur de soustracton de vecteurs
* \param v1 Le premier vecteur
* \parma v2 Le deuxième vecteur
*/
Vector3f operator-(const Vector3f & v1, const Vector3f & v2); Vector3f operator-(const Vector3f & v1, const Vector3f & v2);
/*!
* \brief Renvoie le produit scalaire entre deux vecteurs
* \param v1 Le premier vecteur
* \parma v2 Le deuxième vecteur
*/
float operator*(const Vector3f & v1, const Vector3f & v2); float operator*(const Vector3f & v1, const Vector3f & v2);
/*!
* \brief Opérateur de multiplication de vecteurs par des sclaires
* \param v Le premier vecteur
* \pram d flotant
*/
Vector3f operator*(const Vector3f & v, float d); Vector3f operator*(const Vector3f & v, float d);
/*!
* \brief Opérateur de multiplication de vecteurs par des sclaires
* \param d flotant
* \param v Le premier vecteur
*/
Vector3f operator*(float d, const Vector3f & v); Vector3f operator*(float d, const Vector3f & v);
/*!
* \brief Opérateur de division de vecteurs par des sclaires
* \param v1 Le premier vecteur
* \param d flotant
*/
Vector3f operator/(const Vector3f & v, float d); Vector3f operator/(const Vector3f & v, float d);
/*!
* \brief Opérateur de preoduit vectoriel entre deux vecteurs
* \param v1 Le premier vecteur
* \parma v2 Le deuxième vecteur
*/
Vector3f operator^(const Vector3f & v1, const Vector3f & v2); Vector3f operator^(const Vector3f & v1, const Vector3f & v2);
/*!
* \brief Renvoie la distance entre deux vecteur de l'espace
* \param v1 Le premier vecteur
* \parma v2 Le deuxième vecteur
*/
float dist(const Vector3f & v1, const Vector3f & v2); float dist(const Vector3f & v1, const Vector3f & v2);
......
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