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

merge

parents dce4c08b 69ca13a3
Branches
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
0.png

9,57 ko

...@@ -13,7 +13,7 @@ sphere.o: sphere.h ...@@ -13,7 +13,7 @@ sphere.o: sphere.h
scene.o: scene.h scene.o: scene.h
main: $(OBJ) main: $(OBJ)
$(CC) -o $@ $^ $(CC) -o $@ $^ -lpng
clean: clean:
rm *.o main rm *.o main
\ No newline at end of file
Aucun aperçu pour ce type de fichier
...@@ -144,6 +144,9 @@ int main() ...@@ -144,6 +144,9 @@ int main()
Material red(255, 0, 0, 0); Material red(255, 0, 0, 0);
Material green(0, 255, 0, 0); Material green(0, 255, 0, 0);
Material blue(0, 0, 255, 0); Material blue(0, 0, 255, 0);
Material yellow(255, 255, 0, 0);
Material magenta(255, 0, 255, 0);
Material cyan(0, 255, 255, 0);
Material white(255, 255, 255, 0); Material white(255, 255, 255, 0);
Material grey(122, 122, 122, 0); Material grey(122, 122, 122, 0);
Material black(0, 0, 0, 0); Material black(0, 0, 0, 0);
...@@ -159,24 +162,24 @@ int main() ...@@ -159,24 +162,24 @@ int main()
| |
x <----X z*/ x <----X z*/
Shape *shapes[nb_shapes]; Shape *shapes[nb_shapes];
shapes[0] = new Quad(grey, Vector3f(width/2, height/2, depth), width, height, 0); //mur du fond shapes[0] = new Quad(yellow, Vector3f(width/2, height/2, depth), width, height, 0); //mur du fond
shapes[1] = new Quad(grey, Vector3f(width, height/2, depth/2), 0, height, depth); //mur gauche shapes[1] = new Quad(magenta, Vector3f(width, height/2, depth/2), 0, height, depth); //mur gauche
shapes[2] = new Quad(grey, Vector3f(width/2, height, depth/2), width, 0, depth); //plafond shapes[2] = new Quad(white, Vector3f(width/2, height, depth/2), width, 0, depth); //plafond
shapes[3] = new Quad(grey, 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(grey, Vector3f(width/2, 0, depth/2), width, 0, depth); //sol 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[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[6] = new Sphere(blue, Vector3f(3/4*width, 20, depth/2), 40); //sphere
Camera camera(Vector3f(width/2, height/2, 0), Vector3f(0, 0, 1)); 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)); Ray3f source(Vector3f(width/2, height, depth/2), Vector3f(0, -1, 0)); //lumière au plafonc
Scene scene(camera, shapes, source); Scene scene(camera, shapes, source);
//----------CALCUL ET SAUVEGARDE DE L'IMAGE---------- //----------CALCUL ET SAUVEGARDE DE L'IMAGE----------
std::cout << "Création de l'image..." ; std::cout << "Création de l'image..." << std::endl;
std::string filename = "0.png"; std::string filename = "0.png";
scene.render(640, 480, filename.c_str()); scene.render(width, height, 640, 480, nb_shapes, (char *) filename.c_str());
std::cout << " image sauvegardée" << std::endl; std::cout << "Image sauvegardée" << std::endl;
//----------LIBERATION DE LA MEMOIRE---------- //----------LIBERATION DE LA MEMOIRE----------
for (int i=0; i<nb_shapes; i++) for (int i=0; i<nb_shapes; i++)
......
Fichier déplacé
Fichier déplacé
#include "quad.h" #include "quad.h"
#include <iostream> //#include <iostream>
...@@ -131,7 +131,6 @@ Ray3f Quad::reflect(const Ray3f ray) const ...@@ -131,7 +131,6 @@ Ray3f Quad::reflect(const Ray3f ray) const
float Z = ray.origin().z(); float Z = ray.origin().z();
//Calculs des coordonnées d'intersection avec les faces et vérification //Calculs des coordonnées d'intersection avec les faces et vérification
float t, x, y ,z; float t, x, y ,z;
...@@ -141,12 +140,15 @@ Ray3f Quad::reflect(const Ray3f ray) const ...@@ -141,12 +140,15 @@ Ray3f Quad::reflect(const Ray3f ray) const
Vector3f I = ray.direction(); Vector3f I = ray.direction();
//std::cout << "a=" <<a << std::endl;
//std::cout << "b=" <<a << std::endl;
//std::cout << "c=" <<a << std::endl;
//Face 1 //Face 1
if(a!=0 && Vector3f(1,0,0)*ray.direction()<0) if(a!=0 && Vector3f(1,0,0)*ray.direction()<0)
{ {
std::cout<<"face 1"<<std::endl;
t = (x1 - X) / a ; t = (x1 - X) / a ;
//x = X; //x = X;
y = Y + b*t; y = Y + b*t;
...@@ -172,7 +174,6 @@ Ray3f Quad::reflect(const Ray3f ray) const ...@@ -172,7 +174,6 @@ Ray3f Quad::reflect(const Ray3f ray) const
//Face 2 //Face 2
if(b!=0 && Vector3f(0,1,0)*ray.direction()<0) if(b!=0 && Vector3f(0,1,0)*ray.direction()<0)
{ {
std::cout<<"face 2"<<std::endl;
t = (y2 - Y) / b ; t = (y2 - Y) / b ;
x = X + a*t; x = X + a*t;
...@@ -194,8 +195,6 @@ Ray3f Quad::reflect(const Ray3f ray) const ...@@ -194,8 +195,6 @@ Ray3f Quad::reflect(const Ray3f ray) const
//Face 3 //Face 3
if(c!=0 && Vector3f(0,0,1)*ray.direction()<0) if(c!=0 && Vector3f(0,0,1)*ray.direction()<0)
{ {
std::cout<<"face 3"<<std::endl;
t = (z3 - Z) / c ; t = (z3 - Z) / c ;
x = X + a*t; x = X + a*t;
y = Y + b*t; y = Y + b*t;
...@@ -235,7 +234,6 @@ Ray3f Quad::reflect(const Ray3f ray) const ...@@ -235,7 +234,6 @@ Ray3f Quad::reflect(const Ray3f ray) const
//Face 5 //Face 5
if(b!=0 && Vector3f(0,-1,0)*ray.direction()<0) if(b!=0 && Vector3f(0,-1,0)*ray.direction()<0)
{ {
std::cout<<"face 5"<<std::endl;
t = (y5 - Y) / b ; t = (y5 - Y) / b ;
x = X + a*t; x = X + a*t;
//y = y5; //y = y5;
...@@ -254,7 +252,6 @@ Ray3f Quad::reflect(const Ray3f ray) const ...@@ -254,7 +252,6 @@ Ray3f Quad::reflect(const Ray3f ray) const
//Face 6 //Face 6
if(c!=0 && Vector3f(0,0,-1)*ray.direction()<0) if(c!=0 && Vector3f(0,0,-1)*ray.direction()<0)
{ {
std::cout<<"face 6"<<std::endl;
t = (z6 - Z) / c ; t = (z6 - Z) / c ;
x = X + a*t; x = X + a*t;
......
#include "scene.h" #include "scene.h"
#include <png.h>
...@@ -46,9 +47,84 @@ Ray3f Scene::source() const ...@@ -46,9 +47,84 @@ Ray3f Scene::source() const
} }
void Scene::render(int width, int height, const char* filename)
void Scene::render(int width, int height, int nb_pixel_row, int nb_pixel_col, int nb_shapes, char* filename)
{
FILE *f = fopen(filename, "wb");
if (!f) throw "Echec de la création du fichier";
png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png_ptr)
{
fclose(f);
throw "Echec allocation png_ptr";
}
png_infop info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr)
{ {
fclose(f);
png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
throw "Echec allocation info_ptr";
}
png_init_io(png_ptr, f);
//Set header
png_set_IHDR(png_ptr, info_ptr, nb_pixel_row, nb_pixel_col, 8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
//Set text
png_text title_text;
title_text.compression = PNG_TEXT_COMPRESSION_NONE;
title_text.text = filename;
png_set_text(png_ptr, info_ptr, &title_text, 0);
png_write_info(png_ptr, info_ptr);
//Mémoire allouée pour une ligne de pixels (rgb => 3 bytes par pixel)
png_bytep row = (png_bytep) png_malloc(png_ptr, 3*nb_pixel_row*sizeof(png_byte));
float pw = width / nb_pixel_row;
float ph = height / nb_pixel_col;
float min_dist;
float hit_dist;
for (int i=0; i<nb_pixel_row; i++)
{
for (int j=0; j<nb_pixel_col; j++)
{
min_dist = 1000;
Vector3f Pij(i*pw, j*ph, 0); //point de la grille par laquelle on regarde
Ray3f camera_to_grid(camera_.position(), Pij-camera_.position());
row[3*i] = 0;
row[3*i + 1] = 0;
row[3*i + 2] = 0;
for (int k=0; k<nb_shapes; k++)
{
if (shapes_[k]->is_hit(camera_to_grid))
{
hit_dist = dist(shapes_[k]->reflect(camera_to_grid).origin() , camera_.position());
//printf("hit_dist : %f\n", hit_dist);
if ( hit_dist < min_dist )
{
min_dist = hit_dist;
row[3*i] = shapes_[k]->matter().r();
row[3*i + 1] = shapes_[k]->matter().g();
row[3*i + 2] = shapes_[k]->matter().b();
}
}
}
}
png_write_row(png_ptr, row);
}
png_write_end(png_ptr, NULL);
fclose(f);
} }
......
...@@ -21,7 +21,7 @@ class Scene ...@@ -21,7 +21,7 @@ class Scene
Shape* *shapes() const; Shape* *shapes() const;
Ray3f source() const; Ray3f source() const;
void render(int width, int height, const char* filename); void render(int width, int height, int nb_pixel_row, int nb_pixel_col, int nb_shapes, char* filename);
}; };
......
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