Пример #1
0
        public V3 getNormal()
        {
            V3 normal = new V3((B - A) ^ (C - A));

            normal.Normalize();
            return(normal);
        }
Пример #2
0
        private Couleur processLumiere(V3 rayon, PointColore point)
        {
            V3 normalPoint = point.GetNormale();
            //V3 directionOculaire = new V3(camera - point.GetLoc());
            //directionOculaire.Normalize();

            Couleur lumiereTotale = new Couleur(0, 0, 0);

            lumiereTotale += point.GetCouleur() * couleurAmbiance * intensiteAmbiance;
            foreach (Lumiere lampe in lampes)
            {
                if (!(lampe.isOccluded(point, objets)))
                {
                    V3 directionLampeNormale = new V3(lampe.GetDirection(point.GetLoc()));
                    directionLampeNormale.Normalize();

                    float cosAlpha      = normalPoint * directionLampeNormale;
                    float facteurDiffus = Math.Max(0, cosAlpha);
                    lumiereTotale += point.GetCouleur() * lampe.GetCouleur() * facteurDiffus * lampe.GetIntensite(point.GetLoc());

                    V3    reflet            = (2 * cosAlpha * normalPoint) - directionLampeNormale;
                    float produitSpeculaire = Math.Max(0, (reflet * (-rayon)) / (reflet.Norm() * rayon.Norm()));
                    float facteurSpeculaire = (float)Math.Pow(produitSpeculaire, puissanceSpeculaire);
                    lumiereTotale += lampe.GetCouleur() * lampe.GetIntensite(point.GetLoc()) * facteurSpeculaire;
                }
            }
            return(lumiereTotale);
        }
Пример #3
0
        public override float IntersectRayon(V3 camera, V3 directionOculaire)
        {
            // A + alpha AB + beta AC
            V3 AB          = new V3(pointB - position);
            V3 ABNormalise = new V3(AB);

            ABNormalise.Normalize();

            V3 AC          = new V3(pointC - position);
            V3 ACNormalise = new V3(AC);

            ACNormalise.Normalize();

            V3 normal = new V3(AB ^ AC);

            normal.Normalize();

            float t  = ((position - camera) * normal) / (directionOculaire * normal);
            V3    I  = new V3(camera + t * directionOculaire);
            V3    AI = new V3(I - position);

            float alpha = (AI * AB) / (AB * AB);
            float beta  = (AI * AC) / (AC * AC);

            if (alpha >= 0 && alpha <= 1 && beta >= 0 && beta <= 1)
            {
                return(t);
            }
            return(-1);
        }
Пример #4
0
        public override V3 getDirection(V3 PositionObjet)
        {
            V3 DirectionLumiere = Position - PositionObjet;

            DirectionLumiere.Normalize();
            return(DirectionLumiere);
        }
        public override V3 GetNormal(V3 intersection = null)
        {
            V3 normal = Normal;

            if (!HasBump())
            {
                normal.Normalize();
                return(normal);
            }

            V3    AB = PointB - PointA;
            V3    AC = PointC - PointA;
            V3    AI = intersection - PointA;
            float u  = ((AC ^ Normal) * AI) / (AB ^ AC).Norm();
            float v  = ((Normal ^ AB) * AI) / (AC ^ AB).Norm();

            BumpTexture.Bump(u, v, out float dhdu, out float dhdv);
            V3 T2 = AB ^ (dhdv * normal);
            V3 T3 = (dhdu * normal) ^ AC;

            V3 normalBump = normal + (BumpIntensity * (T2 + T3));

            normalBump.Normalize();
            return(normalBump);
        }
Пример #6
0
        public override PointColore GetCouleurIntersect(V3 camera, V3 directionOculaire, float intersection)
        {
            V3 point = new V3(camera + intersection * directionOculaire);
            V3 AI    = new V3(point - position);

            float beta  = (AI * AC) / (AC.Norm() * AC.Norm());
            float alpha = (AI * AB) / (AB.Norm() * AB.Norm());


            V3 dMdu = AB;
            V3 dMdv = AC;

            float dhdu, dhdv;

            BumpMap.Bump(alpha, beta, out dhdu, out dhdv);

            V3 T2          = dMdu ^ (dhdv * normal);
            V3 T3          = (dhdu * normal) ^ dMdv;
            V3 normaleBump = normal + 0.08f * (T2 + T3);

            normaleBump.Normalize();

            V3 positionCouleur = textureA + (alpha * (textureB - textureA) + beta * (textureC - textureA));

            return(new PointColore(point, normaleBump, texture.LireCouleur(positionCouleur.x, positionCouleur.y), this));
        }
Пример #7
0
        public V3 getNormal(float u, float v)
        {
            V3 normal = getPixel3DPosition(u, v);

            normal.Normalize();
            return(normal);
        }
Пример #8
0
        public override PointColore GetCouleurIntersect(V3 camera, V3 directionOculaire, float intersection)
        {
            V3 point = camera + (intersection * directionOculaire);

            IMA.Invert_Coord_Spherique(point - this.position, this.rayon, out float u, out float v);

            float offsetU = u / (float)(Math.PI * 2);
            float offsetV = (v + (float)(Math.PI / 2)) / (float)(Math.PI);

            V3    dMdu = new V3((float)(-Math.Sin(u) * Math.Cos(v) * rayon), (float)(rayon * (float)(Math.Cos(u) * Math.Cos(v))), 0);
            V3    dMdv = new V3((float)(-Math.Sin(v) * Math.Cos(v) * rayon), (float)(rayon * (float)(-Math.Sin(u) * Math.Sin(v))), rayon * (float)Math.Cos(v));
            float dhdu, dhdv;

            BumpMap.Bump(offsetU, offsetV, out dhdu, out dhdv);

            V3 normalPoint = new V3(point - position);

            normalPoint.Normalize();

            V3 T2          = dMdu ^ (dhdv * normalPoint);
            V3 T3          = (dhdu * normalPoint) ^ dMdv;
            V3 normaleBump = normalPoint + 0.008f * (T2 + T3);

            normaleBump.Normalize();
            return(new PointColore(point, normaleBump, texture.LireCouleur(offsetU, offsetV), this));
        }
Пример #9
0
        public static Couleur Color_Diffused(Light_Source light, Couleur px_color, V3 P, V3 N)
        {
            V3 L = -light.direction;

            if (light.point_light)
            {
                L = light.position - P;
                //L = P-light.position;
                L.Normalize();
            }
            float cos_theta = N * L;


            if (cos_theta < 0)
            {
                cos_theta = 0;
            }



            float   on_shadow = getShadow(P, light);
            Couleur c         = cos_theta * (light.color * px_color);

            return(c * on_shadow);
        }
Пример #10
0
        public static Couleur Color_Specular(Light_Source light, V3 P, V3 N, int k)
        {
            V3 L = -light.direction;

            if (light.point_light)
            {
                L = light.position - P;
                L.Normalize();
            }
            float cos_alpha = (N * L);

            if (light.point_light && cos_alpha < 0)
            {
                cos_alpha = -cos_alpha;
            }

            V3 R = (N * 2 * cos_alpha) - L;

            R.Normalize();

            V3 D = Elements.camera_position - P;

            D.Normalize();

            float f = (R * D);

            if (f < 0)
            {
                f = 0;        // if cos is negative there won't be any specularity
            }
            Couleur c = (float)Math.Pow(f, k) * (light.color);

            return(c);
        }
Пример #11
0
        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);
                }
            }
        }
Пример #12
0
        public override V3 GetDirection(V3 point)
        {
            V3 sens = this.position - point;

            sens.Normalize();
            return(sens);
        }
Пример #13
0
        public Couleur getPixelColor(V3 P)
        {
            float u, v;
            V3    d = new V3(Center - P);

            d.Normalize();

            u = (float)(0.5f + Math.Atan2(d.x, d.y) / (2 * Math.PI));
            v = (float)(0.5f - Math.Asin(d.z) / Math.PI);

            return(Lights.getPixelColor(P, getColor(u, v), getPixelSphereNormal(P), object_index));
        }
Пример #14
0
 private void resetVectors()
 {
     this.AB          = new V3(pointB - this.position);
     this.AC          = new V3(pointC - this.position);
     this.ABNormalise = new V3(AB);
     ABNormalise.Normalize();
     this.ACNormalise = new V3(AC);
     ACNormalise.Normalize();
     this.ABprojector = AC ^ normal;
     this.ACprojector = normal ^ AB;
     this.normal      = new V3(AB ^ AC);
     normal.Normalize();
 }
Пример #15
0
        public V3 getPixelSphereNormal(V3 P)
        {
            V3 N = P - Center;

            IMA.Invert_Coord_Spherique(N, R, out float u, out float v);
            N.Normalize();
            // calculate the new normal vector for the bump mapping effect
            if (bump_activate)
            {
                N = bumpN(N, u, v);
            }
            return(N);
        }
        private static MyColor Illumination(List <Light> lights, List <IShape> sceneObjects, IShape currentObject, V3 intersection, V3 rayDirection, int reflexionNumber, int refractionNumber)
        {
            var shapeColor = currentObject.GetColor(intersection);
            var pixelColor = shapeColor * new MyColor(.1f, .1f, .1f); // Modele de réflexion ambiant

            rayDirection.Normalize();

            V3    normal             = currentObject.GetNormal(intersection);
            float coeffDiffuseLight1 = normal * lights[0].Orientation;
            float coeffDiffuseLight2 = normal * lights[1].Orientation;

            if (coeffDiffuseLight1 >= 0 && !IsIntersect(intersection, lights[0].Orientation, sceneObjects, currentObject))
            {
                pixelColor += coeffDiffuseLight1 * (shapeColor * lights[0].Color);                        // Modele diffus key lamp
                V3 rayReflected = -lights[0].Orientation + 2 * (normal * lights[0].Orientation) * normal; //Rayon réfléchi
                rayReflected.Normalize();
                float coeffSpecular = (float)Math.Pow(rayReflected * (-rayDirection), 70);
                pixelColor += coeffSpecular * lights[0].Color; // Modele speculaire
            }

            if (coeffDiffuseLight2 >= 0 && !IsIntersect(intersection, lights[1].Orientation, sceneObjects, currentObject))
            {
                pixelColor += coeffDiffuseLight2 * (shapeColor * lights[1].Color); // Modele diffus fill lamp
            }

            if (currentObject.GetCoefReflexion() > 0 && reflexionNumber > 0)
            {
                V3 rayReflectedCamera = rayDirection + 2 * (normal * -rayDirection) * normal; //Rayon réfléchi MAIS DEPUIS LA CAMERA !!!!!
                rayReflectedCamera.Normalize();
                pixelColor += currentObject.GetCoefReflexion() * RayCast(intersection, rayReflectedCamera, sceneObjects, lights, reflexionNumber - 1, refractionNumber, currentObject);
            }



            if (currentObject.GetCoefRefraction() > 0 && refractionNumber > 0)
            {
                float angle = lights[0].Orientation * normal;
                //if (angle > 0)
                //{
                float sin2 = ((lights[0].Orientation ^ normal).Norm() * Fresnel.AIR) / currentObject.GetIndiceFresnel();
                if (sin2 > 0 && sin2 < 1)
                {
                    V3 tangente      = lights[0].Orientation - (normal * (lights[0].Orientation)) * normal;
                    V3 rayRefraction = sin2 * (-tangente) + SinToCos(sin2) * (-normal);
                    rayRefraction.Normalize();
                    pixelColor += currentObject.GetCoefRefraction() * RayCast(intersection, rayRefraction, sceneObjects, lights, reflexionNumber, refractionNumber - 1, currentObject);
                }
                // }
            }
            return(pixelColor);
        }
Пример #17
0
        public override Couleur drawPixel(V3 positionInScene)
        {
            V3 posionForSphere = positionInScene - this.getPosition();

            float v;
            float u;

            IMA.Invert_Coord_Spherique(posionForSphere, this.rayon, out u, out v);

            V3 vectSphere = new V3((float)(this.rayon * Math.Cos(v) * Math.Cos(u)),
                                   (float)(this.rayon * Math.Cos(v) * Math.Sin(u)),
                                   (float)(this.rayon * Math.Sin(v)));

            V3 copyvectSphere = new V3(vectSphere);

            copyvectSphere.Normalize();

            float dhsdu;
            float dhsdv;

            this.getBump(u / (Math.PI * 2), v / Math.PI + Math.PI / 2, out dhsdu, out dhsdv);


            V3 dmsdu = new V3((float)(-1 * Math.Cos(v) * Math.Sin(u)),
                              (float)(1 * Math.Cos(v) * Math.Cos(u)),
                              0.0f);

            V3 dmsdv = new V3((float)(-1 * Math.Sin(v) * Math.Cos(u)),
                              (float)(-1 * Math.Sin(v) * Math.Sin(u)),
                              (float)(1 * Math.Cos(v)));

            V3 dmpsdu = dmsdu + dhsdu * copyvectSphere;
            V3 dmpsdv = dmsdv + dhsdv * copyvectSphere;


            V3 Np = ((dmpsdu ^ dmpsdv) / (dmpsdu ^ dmpsdv).Norm());

            V3 vecteurBump = Np;

            V3 vect = this.getPosition() + vectSphere;

            Couleur vcolor = new Couleur();

            foreach (Light light in RenderSing.getCurrentRender().getLight())
            {
                vcolor += light.applyLight(this, vect, vecteurBump, this.getColor(u / (2 * Math.PI), v / Math.PI + Math.PI / 2));
            }

            return(vcolor);
        }
Пример #18
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);
         }
     }
 }
Пример #19
0
        public void Partial_derivative(float u, float v, out V3 dMdu, out V3 dMdv)
        {
            float dxdu, dydu, dzdu, dxdv, dydv, dzdv;

            dxdu = (float)(B.x);
            dydu = (float)(B.y);
            dzdu = (float)(B.z);
            dMdu = new V3(dxdu, dydu, dzdu);
            dMdu.Normalize();

            dxdv = (float)(C.x);
            dydv = (float)(C.y);
            dzdv = (float)(C.z);
            dMdv = new V3(dxdv, dydv, dzdv);
            dMdv.Normalize();
        }
Пример #20
0
        public void Partial_derivative(float u, float v, out V3 dMdu, out V3 dMdv)
        {
            float dxdu, dydu, dzdu, dxdv, dydv, dzdv;

            dxdu = (float)(Math.Cos(v) * -Math.Sin(u));
            dydu = (float)(Math.Cos(v) * Math.Cos(u));
            dzdu = (float)0;
            dMdu = new V3(dxdu, dydu, dzdu);

            dxdv = (float)(-Math.Sin(v) * (Math.Cos(u)));
            dydv = (float)(-Math.Sin(v) * (Math.Sin(u)));
            dzdv = (float)(Math.Cos(v));
            dMdv = new V3(dxdv, dydv, dzdv);

            dMdu.Normalize(); dMdv.Normalize();
        }
Пример #21
0
        public bool intersection_RayObject(V3 R0, V3 Rd, out V3 P, out V3 N, out float t)
        {
            V3 R0A;

            P = new V3(0, 0, 0);
            float aux1;

            V3 AB = B - A;
            V3 AC = C - A;

            N = AB ^ AC;
            N.Normalize();

            R0A = A - R0;

            t    = V3.prod_scal(ref R0A, ref N);
            aux1 = V3.prod_scal(ref Rd, ref N);

            if (aux1 == 0)
            {
                t = -1;
                return(false);
            }

            t /= aux1;

            if (t < MIN_DISTANCE)
            {
                return(false);
            }

            P = R0 + t * Rd;

            getUV(P, out float u, out float v);
            if (u < 0 || u > 1 || v < 0 || v > 1)
            {
                P = new V3(0, 0, 0);
                t = -1;
                return(false);
            }
            return(true);
        }
Пример #22
0
        //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);
        }
Пример #23
0
        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]);
                }
            }
        }
        /// <summary>
        /// Calcul la normale a partir d une intersection
        /// A partir de cette intersection on va calculer u et v
        /// </summary>
        /// <param name="intersection">L intersection</param>
        /// <returns>La normal par rapport a l intersection</returns>
        public override V3 GetNormal(V3 intersection = null)
        {
            Tools.InvertCoordSpherique(FindSpherePoint(intersection), Radius, out float u, out float v);
            V3 normal = GetNormal(u, v);

            if (!HasBump())
            {
                normal.Normalize();
                return(normal);
            }

            float uTexture = u / Tools.TAU;
            float vTexture = -(v + Tools.PI2) / (Tools.PI2 + Tools.PI2);

            BumpTexture.Bump(uTexture, vTexture, out float dhdu, out float dhdv);
            V3 T2         = FindPointDerU(u, v) ^ (dhdv * normal);
            V3 T3         = (dhdu * normal) ^ FindPointDerV(u, v);
            V3 normalBump = normal + (BumpIntensity * (T2 + T3));

            normalBump.Normalize();
            return(normalBump);
        }
Пример #25
0
        public V3 getRectangleNormal(V3 P)
        {
            V3 N  = new V3(0, 0, 0);
            V3 AB = B - A;
            V3 AC = C - A;

            getUV(P, out float u, out float v);
            if (u < 0 || u > 1 || v < 0 || v > 1)
            {
                return(N);
            }
            // get the normal vector
            N = AB * u ^ AC * v;
            N.Normalize();
            // calculate the new normal vector for the bump mapping effect
            if (bump_activate)
            {
                N = bumpN(N, u, v);
            }

            return(N);
        }
Пример #26
0
        public static V3 computeRefractionRay(float refractive_index, V3 ray_direction, V3 N)
        {
            V3    Nrefr      = N;
            float cos_theta1 = N * ray_direction;

            float n_aux, n1 = 1, n2 = refractive_index; // n1 is the index of refraction of the medium and n2 is in before entering the second medium

            if (cos_theta1 < 0)
            {
                // we are outside the surface, we want cos(theta) to be positive
                cos_theta1 = -cos_theta1;
            }
            else
            {
                // we are inside the surface, cos(theta) is already positive but reverse normal direction
                //Nrefr = -N;
                // swap the refraction indices
                n_aux = n1;
                n1    = n2;
                n2    = n_aux;
            }
            float n = n1 / n2;

            float cos_theta2 = 1 - n * n * (1 - cos_theta1 * cos_theta1);

            if (cos_theta2 < 0)
            {
                V3 r = new V3(0, 0, 0);

                return(r);
            }
            else
            {
                V3 r = n * (ray_direction + cos_theta1 * N) - (float)Math.Sqrt(cos_theta2) * N;
                r.Normalize();
                return(r);
            }
        }
Пример #27
0
        public override PointColore GetCouleurIntersect(V3 camera, V3 directionOculaire, float intersection)
        {
            V3 point = new V3(camera + intersection * directionOculaire);
            V3 AI    = new V3(point - position);

            V3 AB          = new V3(pointB - position);
            V3 ABNormalise = new V3(AB);

            ABNormalise.Normalize();

            V3 AC          = new V3(pointC - position);
            V3 ACNormalise = new V3(AC);

            ACNormalise.Normalize();

            V3 normal = new V3(AB ^ AC);

            normal.Normalize();

            float alpha = (AI * AB) / (AB * AB);
            float beta  = (AI * AC) / (AC * AC);

            V3 dMdu = AB;
            V3 dMdv = AC;

            float dhdu, dhdv;

            BumpMap.Bump(alpha, beta, out dhdu, out dhdv);

            V3 T2          = dMdu ^ (dhdv * normal);
            V3 T3          = (dhdu * normal) ^ dMdv;
            V3 normaleBump = normal + 0.008f * (T2 + T3);

            normaleBump.Normalize();

            return(new PointColore(point, normaleBump, texture.LireCouleur(alpha, beta), this));
        }
Пример #28
0
        static public void RayCasting(V3 pixel_screen_pos, List <Object> objects_on_screen)
        {
            float t, t_min = float.MaxValue, y_min = 0;
            V3    ray_direction = pixel_screen_pos - camera_position;

            ray_direction.Normalize();
            int i_min = 0;

            for (int i = 0; i < objects_on_screen.Count; i++)
            {
                objects_on_screen[i].intersection_RayObject(camera_position, ray_direction, out V3 P, out V3 N, out t);
                if (t < t_min && t > 0)
                {
                    t_min = t;
                    y_min = P.y;
                    i_min = i;
                }
            }
            pixel_screen_pos.y = y_min;
            if (t_min < float.MaxValue)
            {
                objects_on_screen[i_min].draw_pixel(pixel_screen_pos);
            }
        }
Пример #29
0
        public static float getShadow(V3 P, Light_Source light)
        {
            float t, t_max = 200;
            V3    direction = -light.direction;

            if (light.point_light)
            {
                direction = light.position - P;
                direction.Normalize();
            }

            foreach (Object obj in Elements.objects_on_screen)
            {
                obj.intersection_RayObject(P, direction, out V3 P1, out V3 N1, out t);
                if (t > MIN_DISTANCE && t < t_max)
                {
                    if (-direction * N1 > 0.1f)
                    {
                        return(0.3f);
                    }
                }
            }
            return(1);
        }
Пример #30
0
        public static void Go()
        {
            int width = BitmapEcran.GetWidth();
            int height = BitmapEcran.GetHeight()+1;

            Couleur C_ambiant = new Couleur(0.2f, 0.2f, 0.2f);
            V3 camera = new V3(width / 2, -1000, height / 2);

            // LAMPES
            List<Lampe> lampList = new List<Lampe>();
            lampList.Add(new Lumiere(0, new V3(1f, -1f, 1f), new V3(0f, 0f, 0f), new Couleur(0.4f, 0.4f, 0.4f)));
            lampList.Add(new Lumiere(0, new V3(-1f, -1f, 1f), new V3(0f, 0f, 0f), new Couleur(0.4f, 0.4f, 0.4f)));

            // TEXTURES
            Texture T_carreau = new Texture("carreau.jpg");
            Texture T_lead = new Texture("lead.jpg");
            Texture T_Aymeric = new Texture("aymeric.jpg");
            Texture T_stone = new Texture("stone2.jpg");
            Texture T_fibre = new Texture("fibre.jpg");
            Texture T_brick = new Texture("brick01.jpg");
            Texture T_gold = new Texture("gold.jpg");
            // BUMP TEXTURES
            Texture T_bump = new Texture("bump38.jpg");
            Texture T_leadbump = new Texture("lead_bump.jpg");

            // OBJETS
            List<Objet3D> objList = new List<Objet3D>();

            //SPHERES
            objList.Add(new Sphere(new V3(width / 2, 500, height - 50), 150, T_carreau, T_bump, 0.008f));
            objList.Add(new Sphere(new V3(50, 500, height / 2), 150, T_lead, T_leadbump, 0.01f));
            objList.Add(new Sphere(new V3(175, 300, height / 2 + 75), 50, T_gold, T_bump, 0.1f));
            //RECTS
            //fond
            objList.Add(new Rect(new V3(50, 1000, 50), new V3(width - 100, 0, 0), new V3(0, 0, height - 100), T_brick, null));
            //bas
            objList.Add(new Rect(new V3(50, 0, 50), new V3(width - 100, 0, 0), new V3(0, 1000, 0), T_stone, null));
            //haut
            //objList.Add(new Rect(new V3(width-50, 0, height-50), new V3(-(width-100), 0, 0), new V3(0, 1000, 0), T_fibre, null));
            //gauche
            objList.Add(new Rect(new V3(50, 0, 50), new V3(0, 1000, 0), new V3(0, 0, height - 100), T_brick, null));
            //droite
            objList.Add(new Rect(new V3(width - 50, 1000, 50), new V3(0, -1000, 0), new V3(0, 0, height - 100), T_brick, null));
            //milieu
            objList.Add(new Rect(new V3(width / 2 + 100, 750, 50), new V3(0, -500, 0), new V3(0, 0, height / 3), T_brick, null));

            // RAY CASTING

            V3 Rd, R;
            float t, tnew;
            bool[] occs = new bool[lampList.Count];

            //parcourir les pixels en x
            for (int pxx = 0; pxx < width; pxx++) {
                //parcourir les pixels en z
                for (int pxz = 0; pxz < height; pxz++) {
                    t = float.MaxValue;
                    // pour chaque pixel générer un rayon Rd
                    Rd = new V3(pxx - camera.x, -camera.y, pxz - camera.z);
                    Rd.Normalize();
                    // parcourir la liste des objets
                    foreach (Objet3D obj in objList) {
                        // appeler l'intersection et récupérer le point
                        tnew = obj.getIntersect(Rd, camera);
                        if (tnew != -1)
                        {
                            if (tnew <= t)
                            {
                                t = tnew;
                                // R est le point d'intersection de Rd et de l'objet
                                R = camera + t * Rd;
                                occs = obj.occultation(R, objList, lampList);
                                //Console.WriteLine(occs[0]+" "+occs[1]);
                                Couleur finalColor = obj.DrawPoint(C_ambiant, lampList, camera, R, occs);
                                BitmapEcran.DrawPixel(pxx, pxz, finalColor);
                            }
                        }

                    }
                }
            }

            //s1.Draw(C_ambiant, L, camera);
            //s2.Draw(C_ambiant, L, camera);

            //r1.Draw(C_ambiant, L, camera);
            //r2.Draw(C_ambiant, L, camera);
        }
Пример #31
0
        public static void Go()
        {
            /*
             * RenderSing.getCurrentRender()
             *    .addObject(new Rectangle(   new V3(-50f, 0f, -10f),
             *                                new V3(100f, 0f, 0f),
             *                                new V3(0, 100f, 0f),
             *                                new Texture("brick01.jpg"),
             *                                new Texture("lead_bump.jpg")))
             *
             *    .addObject(new Rectangle(   new V3(-20.0f, 50f, 10f),
             *                                new V3(20f, 20f,10f),
             *                                new V3(0,0,20f),
             *                                new Texture("lead.jpg"),
             *                                new Texture("lead_bump.jpg")))
             *
             *    .addObject(new Sphere(      new V3(0.0f, 50f, 0.0f),
             *                                15f,
             *                                new Texture("carreau.jpg"),
             *                                new Texture("bump38.jpg")))
             *
             *    .addObject(new Sphere(      new V3(20.0f, 75f, 0.0f),
             *                                10f,
             *                                new Couleur(1f,0,0),
             *                                new Texture("bump38.jpg")))
             *    ;*/

            /*
             *
             *      RenderSing.getCurrentRender()
             *          .addLight(new DirectionalLight( new V3(1f, -1f, 1f),
             *                                          new Couleur(.8f, .8f, .8f)))
             *
             *          .addLight(new AmbiantLight(     new Couleur(.3f, 0.3f, 0.3f)));
             */


            RenderSing rnd = RenderSing.getCurrentRender();

            for (double x = rnd.getMinX();
                 x < rnd.getMaxX();
                 x += rnd.stepX)
            {
                for (double z = rnd.getMinY();
                     z < rnd.getMaxY();
                     z += rnd.stepY)
                {
                    V3 rayProjection = new V3((float)x, 0.0f, (float)z) - RenderSing.getCurrentRender().getEyesPosition();
                    rayProjection.Normalize();


                    Double   positionColition = 0;
                    Double   minPosition      = Double.MaxValue;
                    Object3D elementprox      = null;

                    foreach (Object3D one in RenderSing.getCurrentRender().getObject())
                    {
                        if (one.testColition(
                                RenderSing.getCurrentRender().getEyesPosition(),
                                rayProjection,
                                out positionColition) && positionColition < minPosition)
                        {
                            minPosition = positionColition;
                            elementprox = one;
                        }
                    }

                    Couleur color_pixel;

                    if (elementprox != null)
                    {
                        color_pixel = elementprox.drawPixel((float)minPosition * rayProjection + RenderSing.getCurrentRender().getEyesPosition());
                    }
                    else
                    {
                        //TODO sky map inplements
                        color_pixel = new Couleur(0, 0, 0);
                    }

                    RenderSing.getCurrentRender().Draw(x, z, color_pixel);
                }
            }
        }