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

ajout de libpng

parent ed87d410
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
0.png

9,66 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(width/4, 20, depth-20), 40, 40, 40); //cube shapes[5] = new Quad(red, Vector3f(width/4, 20, depth-20), 40, 40, 40); //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, -1), 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++)
......
#include "scene.h" #include "scene.h"
#include <png.h>
...@@ -46,9 +47,74 @@ Ray3f Scene::source() const ...@@ -46,9 +47,74 @@ 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;
for (int i=0; i<nb_pixel_row; i++)
{
for (int j=0; j<nb_pixel_col; j++)
{
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))
{ {
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