Skip to content
Extraits de code Groupes Projets
Valider f6351720 rédigé par Lénaïc DURAND's avatar Lénaïc DURAND
Parcourir les fichiers

pas de fautes dans les .h

parents 36745241 346ea2a2
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
CC=g++
OBJ=vector3f.o material.o ray3f.o camera.o quad.o sphere.o scene.o main.cpp
#OBJ=vector3f.o material.o ray3f.o camera.o quad.o sphere.o main.cpp
all: main
......
basic_ray_tracing.png

84,7 ko

Aucun aperçu pour ce type de fichier
......@@ -8,135 +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 << std::endl << std::endl;
}
int main()
{
try
{
//----------TESTS----------
/*test_Vector3f();
test_Ray3f();
test_Camera();
test_Material();
test_Quad();
test_Sphere();
test_Scene();*/
//----------MISE EN PLACE DE LA SCENE----------
Material red(255, 0, 0, 0);
......@@ -150,7 +25,7 @@ int main()
Material black(0, 0, 0, 0);
Material mirror(0, 0, 0, 0.9);
int nb_shapes = 7;
int nb_shapes = 8;
float width = 100; //x
float height = 100; //y
float depth = 100; //z
......@@ -167,17 +42,22 @@ int main()
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[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
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);
//----------CALCUL ET SAUVEGARDE DE L'IMAGE----------
std::cout << "Création de l'image..." << std::endl;
std::string filename = "2.png";
scene.render(width, height, 1080, 1080, nb_shapes, (char *) filename.c_str());
std::string filename = "basic_ray_tracing.png";
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;
//----------LIBERATION DE LA MEMOIRE----------
......
......@@ -49,6 +49,10 @@ class Ray3f
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);
......
ray_tracing_reflexion.png

117 ko

ray_tracing_shading.png

163 ko

......@@ -38,7 +38,7 @@ Ray3f Scene::source() const
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-------------------------------------*/
FILE *f = fopen(filename, "wb");
......@@ -84,13 +84,15 @@ void Scene::render(int width, int height, int nb_pixel_row, int nb_pixel_col, in
float min_dist;
float hit_dist;
int ref_max = 4;
Ray3f *ray[ref_max];
float shine[ref_max-1];
float coef[ref_max-1];
bool shadow;
float intensity;
float incidence;
Vector3f S;
Vector3f N;
for (int i=nb_pixel_col-1; i>=0; i--) //on remplit l'image par le bas à droite
......@@ -120,6 +122,19 @@ void Scene::render(int width, int height, int nb_pixel_row, int nb_pixel_col, in
shadow = false;
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++)
{
......@@ -139,9 +154,9 @@ void Scene::render(int width, int height, int nb_pixel_row, int nb_pixel_col, in
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) + 1] = row[3*(nb_pixel_row-1-j) + 1] + 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)] = 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] + incidence*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);
}
......@@ -153,17 +168,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 ?
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) + 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) + 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)] = 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] + 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] + 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
{
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) + 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) + 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)] = 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] + 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] + incidence*coef[r]*fmin(shapes_[k]->matter().b() * intensity, 255.);
}
}
}
......@@ -171,12 +186,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_end(png_ptr, NULL);
for (int r=0; r<ref_max;r++)
delete[] ray[r];
}
png_write_end(png_ptr, NULL);
fclose(f);
......
......@@ -19,21 +19,83 @@
class Scene
{
Camera camera_;
Shape* *shapes_;
Ray3f source_;
Camera camera_; /*!< La camera */
Shape* *shapes_; /*!< Tableau de pointeurs des formes */
Ray3f source_; /*!< Source de lumiere */
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);
/*!
* \brief Constructeur de Copie
*
* \param s La scene dont les paramètre sont copiées
*/
Scene(const Scene & s);
/*!
* \brief Getter pour la camera de la scene
*/
Camera camera() const;
/*!
* \brief Getter pour le pointeur des shapes
*/
Shape* *shapes() const;
/*!
* \brief Getter pour la source de lumière de la scene
*/
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);
/*!
* \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);
/*!
* \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);
};
......
Ce diff est replié.
......@@ -72,22 +72,66 @@ class Vector3f
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);
/*!
* \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);
/*!
* \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);
/*!
* \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);
/*!
* \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);
/*!
* \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);
/*!
* \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);
/*!
* \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);
/*!
* \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);
......
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