public static void Go() { int screen_w = BitmapEcran.GetWidth(), screen_h = BitmapEcran.GetHeight(); V3 camera_position = new V3(screen_w / 2, -400, screen_h / 2); /* Lights */ // Light sources V3 key_light_dir = new V3(-1, 1, -1); Couleur key_light_color = new Couleur(0.8f, 0.8f, 0.8f); //Lights.add_light(key_light_dir, key_light_color); V3 fill_light_dir = new V3(1, 1, -1); Couleur fill_light_color = new Couleur(0.5f, 0.5f, 0.5f); //Lights.add_light(fill_light_dir, fill_light_color); V3 point_light_position = new V3(screen_w * 0.5f, Scene.max_y_position / 2, screen_h - 43); Couleur point_light_color = new Couleur(1f, 1f, 1f); Lights.add_light(point_light_position, point_light_color, true); /* Objects */ // list of all objects appearing on screen List <Object> objects_on_screen = new List <Object>(); Scene.Room(ref objects_on_screen); Scene.Boxes(ref objects_on_screen); Scene.Mirror(ref objects_on_screen); Scene.Bed(ref objects_on_screen); Scene.Ball(ref objects_on_screen); Scene.Shelf(ref objects_on_screen); // Creates and initialize Z_buffer/ray casting activate + determines the camera position Elements.inicialize_Elements(camera_position, true, objects_on_screen); // Plot all objects on the screen with the ray casting method /*for (int i = 0; i < screen_w; i++) * { * for(int j = 0; j < screen_h; j++) * { * Elements.RayCasting(new V3(i, 0, j), objects_on_screen); * } * }*/ V3 ray_direction; Couleur c; for (int i = 0; i < screen_w; i++) { for (int j = 0; j < screen_h; j++) { ray_direction = new V3(i, 0, j) - Elements.camera_position; ray_direction.Normalize(); c = Elements.RayTracer(Elements.camera_position, ray_direction, 0); BitmapEcran.DrawPixel(i, j, c); } } }
/* Prints the pixel of point P on the screen: * * receives the color of the pixel without the lights * and the normal vector. * * if P is set outside of the screen bounds, doesn't print anything * * compare its position on the Z_Buffer * * draws the pixel with its final color (adding the lights effects) */ public int printPixel(V3 P, Couleur color) { int x, y, z; x = (int)P.x; y = (int)P.y; z = (int)P.z; if (x < 0 || x >= BitmapEcran.GetWidth() || z < 0 || z >= BitmapEcran.GetHeight()) { return(-1); } if (Elements.raycasting) // using ray casting { BitmapEcran.DrawPixel(x, z, color); } else { //using Z-Buffer if (y < Elements.Z_Buffer[x, z]) { Elements.Z_Buffer[x, z] = y; BitmapEcran.DrawPixel(x, z, color); } } return(0); }
//dessine tous les objets de la scène public void DrawScene() { for (int x_ecran = 0; x_ecran <= BitmapEcran.GetWidth(); x_ecran++) { for (int y_ecran = 0; y_ecran <= BitmapEcran.GetHeight(); y_ecran++) { V3 PosPixScene = new V3(x_ecran, 0, y_ecran); V3 DirRayon = PosPixScene - PosCamera; DirRayon.Normalize(); Couleur C = RayCast(PosCamera, DirRayon, Objets); BitmapEcran.DrawPixel(x_ecran, y_ecran, C); } } }
//récupère la couleur du pixel en prenant compte des lumières de la scène public Couleur CouleurEclairee(V3 position, V3 normal, Couleur couleurDeBase, Materiel mat, Objet3D ObjetToDraw) { //initialisation de la couleur Couleur couleurFinale = new Couleur(0, 0, 0); //---------------------------Lumiere AMBIANTE----------------------------------// if (LumAmb != null) { couleurFinale += new Couleur(couleurDeBase * LumAmb.Couleur); } if (Lumieres.Count > 0) { foreach (Lumiere l in Lumieres) { V3 DirectionLumiere = l.getDirection(position); DirectionLumiere.Normalize(); if (!CheckIfObjectBetweenLightSource(DirectionLumiere, position, ObjetToDraw)) { //Console.WriteLine("Je dessine la lumière"); //-------------------------------Lumiere DIFFUSE------------------------------------// float diff = Math.Max((DirectionLumiere * normal), 0); couleurFinale += new Couleur((couleurDeBase * l.getCouleur()) * diff); //--------------------------------Reflet SPECULAIRE--------------------------------// float forceSeculaire = mat.GetForceSpeculaire(); V3 positionOeil = new V3(BitmapEcran.GetHeight() / 2, -3000, BitmapEcran.GetWidth() / 2); V3 directionDuRayonReflechis = new V3(2 * normal - DirectionLumiere); directionDuRayonReflechis.Normalize(); V3 directionObservateur = new V3(positionOeil - position); directionObservateur.Normalize(); couleurFinale += new Couleur(l.getCouleur() * (float)Math.Pow((directionDuRayonReflechis * directionObservateur), forceSeculaire)); } } } return(couleurFinale); }
static public void inicialize_Elements(V3 camera_position, bool raycasting, List <Object> objects_on_screen) { Elements.raycasting = raycasting; Elements.camera_position = camera_position; Elements.objects_on_screen = objects_on_screen; if (!raycasting) { int x_ecran = BitmapEcran.GetWidth(); int z_ecran = BitmapEcran.GetHeight(); Z_Buffer = new float[x_ecran, z_ecran]; //Initializer le Z_Buffer for (int i = 0; i < x_ecran; i++) { for (int j = 0; j < z_ecran; j++) { Z_Buffer[i, j] = int.MaxValue; } } } }
public void DessineRaycast() { V3 camera = new V3((float)BitmapEcran.GetWidth() / 2, (float)BitmapEcran.GetWidth() * -1.5f, (float)BitmapEcran.GetHeight() / 2); int xmax = BitmapEcran.GetWidth(); int ymax = BitmapEcran.GetHeight(); Couleur[,] colorbuffer = new Couleur[xmax, ymax]; for (int x_ecran = 0; x_ecran < BitmapEcran.GetWidth(); x_ecran++) { for (int y_ecran = 0; y_ecran < BitmapEcran.GetHeight(); y_ecran++) { V3 pixel = new V3((float)x_ecran, 0, (float)y_ecran); V3 rayon = pixel - camera; rayon.Normalize(); colorbuffer[x_ecran, y_ecran] = Raycast(camera, rayon); BitmapEcran.DrawPixel(x_ecran, y_ecran, colorbuffer[x_ecran, y_ecran]); } } }
public static void Start() { //création de la scène qui va contenir les objets Scene mainScene = new Scene(); //couleurs Couleur blanc = new Couleur(1, 1, 1); Couleur rouge = new Couleur(0.9f, 0, 0); Couleur jaune = new Couleur(1, 1, 0); Couleur vert = new Couleur(0, 1, 0); Couleur cyan = new Couleur(0, 1, 1); Couleur bleu = new Couleur(0, 0, 1); Couleur violet = new Couleur(1, 0, 1); Couleur gris = new Couleur(0.5f, 0.5f, 0.5f); Couleur noir = new Couleur(0, 0, 0); //========== Lumières ========== //lumière ambiante //lumières directionnelles V3 directionLumièreDirectionnelle; V3 positionLumièrePoint; LumiereDirectionnelle lumiereDirerectionnelle; LumierePoint lumierePoint; LumiereAmbiante LumAmb = new LumiereAmbiante(new Couleur(0.3f, 0.3f, 0.3f)); mainScene.SetLumAmb(LumAmb); directionLumièreDirectionnelle = new V3(0, -1, 0); lumiereDirerectionnelle = new LumiereDirectionnelle(new Couleur(0.5f, 0.5f, 0.5f), directionLumièreDirectionnelle); //mainScene.AddLumDir(lumiereDirerectionnelle); positionLumièrePoint = new V3(400, 1, 400); lumierePoint = new LumierePoint(new Couleur(0.5f, 0.5f, 0.5f), positionLumièrePoint); mainScene.AddLum(lumierePoint); positionLumièrePoint = new V3(BitmapEcran.GetWidth() / 2, 0, BitmapEcran.GetHeight() / 2); lumierePoint = new LumierePoint(new Couleur(0.5f, 0.5f, 0.5f), positionLumièrePoint); mainScene.AddLum(lumierePoint); //directionLumièreDirectionnelle = new V3(-1, -1, -1); //lumiereDirerectionnelle = new LumiereDirectionnelle(new Couleur(0,0, 0.7f), directionLumièreDirectionnelle); //mainScene.AddLumDir(lumiereDirerectionnelle); //========== Matériaux ========== Materiel Blanc = new Materiel(blanc / 1.2f, new Texture("bump38.jpg"), 50, 1); Materiel test = new Materiel(new Texture("test.jpg"), new Texture("test.jpg"), 200, 3); Materiel or = new Materiel(new Texture("gold.jpg"), new Texture("gold_Bump.jpg"), 200, 0.7f); Materiel plomb = new Materiel(new Texture("lead.jpg"), new Texture("lead_bump.jpg"), 50, 1); Materiel brique = new Materiel(new Texture("brickwork-texture.jpg"), new Texture("brickwork-bump-map.jpg"), 500, 1); Materiel rock = new Materiel(new Texture("rock.jpg"), new Texture("rock.jpg"), 500, 1); Materiel stone = new Materiel(new Texture("rock.jpg"), new Texture("rock.jpg"), 500, 1); Materiel fibre = new Materiel(new Texture("rock.jpg"), new Texture("rock.jpg"), 500, 1); Materiel Blob = new Materiel(blanc / 1.2f, new Texture("bump.jpg"), 50, 1); //========== Objets ========== V3 center; Sphere s; Rectangle r; //Boule Or center = new V3(600, 300f, 200); s = new Sphere(center, 50, or); mainScene.AddObjet3D(s); //Boule Or center = new V3(200, 200, 100); s = new Sphere(center, 150, Blob); mainScene.AddObjet3D(s); //Boule Plomb center = new V3(800, 1000, 400); s = new Sphere(center, 100, plomb); mainScene.AddObjet3D(s); //Boule Brique center = new V3(300, 1000, 400); s = new Sphere(center, 150, Blanc); mainScene.AddObjet3D(s); //Rectangle brique Gauche V3 A = new V3(0, 0, 0); V3 B = new V3(0, 2000, 0); V3 C = new V3(0, 0, 800); r = new Rectangle(A, B, C, brique); mainScene.AddObjet3D(r); //Rectangle brique Droit A = new V3(1000, 2000, 0); B = new V3(1000, 0, 0); C = new V3(1000, 2000, 800); r = new Rectangle(A, B, C, brique); mainScene.AddObjet3D(r); //Rectangle brique Fond A = new V3(0, 2000, 0); B = new V3(1000, 2000, 0); C = new V3(0, 2000, 800); r = new Rectangle(A, B, C, brique); mainScene.AddObjet3D(r); //Rectangle brique Bas A = new V3(0, 0, 0); B = new V3(1000, 0, 0); C = new V3(0, 2000, 0); r = new Rectangle(A, B, C, brique); mainScene.AddObjet3D(r); A = new V3(400, 0, 0); B = new V3(190, -50, 0); C = new V3(0, 0, 190); r = new Rectangle(A, B, C, brique); //mainScene.AddObjet3D(r); //Affichage de la scène mainScene.DrawScene(); }