Пример #1
0
        /*public ColorRgb Radiance(PointLight light, HitInfo hit)
         * {
         *  Vector3 inDirection = (light.Position - hit.HitPoint).Normalised;
         *  double diffuseFactor = inDirection.Dot(hit.Normal);
         *
         *  if (diffuseFactor < 0) { return ColorRgb.Black; }
         *
         *  ColorRgb result = light.Color * materialColor * diffuseFactor * diffuseCoeff;
         *  double phongFactor = PhongFactor(inDirection, hit.Normal, -hit.Ray.Direction);
         *
         *  if(phongFactor != 0)
         *  { result += materialColor * specular * phongFactor; }
         *
         *  return result;
         * }*/

        public ColorRgb Shade(Raytracer tracer, HitInfo hit)
        {
            ColorRgb totalColor = ColorRgb.Black;

            foreach (var light in hit.World.Lights)
            {
                Vector3 inDirection   = (light.Position - hit.HitPoint).Normalised;
                double  diffuseFactor = inDirection.Dot(hit.Normal);
                if (diffuseFactor < 0)
                {
                    return(ColorRgb.Black);
                }
                if (hit.World.AnyObstacleBetween(hit.HitPoint, light.Position))
                {
                    continue;
                }
                ColorRgb result = light.Color * materialColor * diffuseFactor * diffuseCoeff; double phongFactor = PhongFactor(inDirection, hit.Normal, -hit.Ray.Direction);
                if (phongFactor != 0)
                {
                    result += materialColor * specular * phongFactor;
                }
                totalColor += result;
            }
            return(totalColor);
        }
        public ColorRgb Shade(Raytracer tracer, HitInfo hit)
        {
            Vector3  toCameraDirection   = -hit.Ray.Direction;
            ColorRgb radiance            = direct.Shade(tracer, hit);
            Vector3  reflectionDirection = Vector3.Reflect(toCameraDirection, hit.Normal);
            Ray      reflectedRay        = new Ray(hit.HitPoint, reflectionDirection);
            ColorRgb reflected           = tracer.ShadeRay(hit.World, reflectedRay, hit.Depth) * reflectionColor * reflectivity;

            radiance += tracer.ShadeRay(hit.World, reflectedRay, hit.Depth) * reflectionColor * reflectivity;
            return(radiance);
        }
Пример #3
0
        private void generaturButton_Click(object sender, EventArgs e)
        {
            //double diff = double.Parse(txtDiff.Text);
            //double spec = double.Parse(txtSpec.Text);
            //double exp = double.Parse(txtExp.Text);
            //double refl = double.Parse(txtRef.Text);

            World world = new World(Color.Gray);


            IMaterial redMat    = new Reflective(Color.LightCoral, 0.4, 1, 300, 0.6);
            IMaterial greenMat  = new Reflective(Color.Green, 0.4, 1, 300, 0.6);
            IMaterial blueMat   = new Reflective(Color.LightBlue, 0.4, 1, 300, 0.6);
            IMaterial grayMat   = new Reflective(Color.Gray, 0.4, 1, 300, 0.6);
            IMaterial purpMat   = new PerfectDiffuse(Color.Purple);
            IMaterial pinkMat   = new PerfectDiffuse(Color.Pink);
            IMaterial whitekMat = new PerfectDiffuse(Color.White);


            //world.Add(new Sphere(new Vector3(-4, 0, 0), 2, redMat));
            //world.Add(new Sphere(new Vector3(4, 0, 0), 2, greenMat));
            world.Add(new Sphere(new Vector3(0, 0, 3), 2, blueMat));
            //world.Add(new Sphere(new Vector3(-2.5, 0, 3), 2, purpMat));
            //world.Add(new Sphere(new Vector3(2.5, 0, 3), 2, pinkMat));
            //world.Add(new Sphere(new Vector3(0, 0, 5), 2, purpMat));
            world.Add(new Plane(new Vector3(0, -2, 0), new Vector3(0, 1, 0), whitekMat));
            //world.Add(new Triangle(new Vector3(-2, 0, 1), new Vector3(2, 0, 1), new Vector3(0, 2, 4), purpMat));
            world.AddLight(new PointLight(new Vector3(0, 0, -1), Color.White));
            ICamera camera = new Pinhole(new Vector3(0, 4, -1), new Vector3(0, 0, 0), new Vector3(0, -1, 0), 1);

            //ICamera camera = new Pinhole(new Vector3(0, 0, 4), new Vector3(0, 0, 0), new Vector3(0, 1, 0), 1);
            Vector3 vertex0 = new Vector3(-1, -1, 0);
            Vector3 vertex1 = new Vector3(1, -1, 0);
            Vector3 vertex2 = new Vector3(1, 1, 0);
            Vector3 vertex3 = new Vector3(-1, 1, 0);

            world.Add(new Triangle(vertex0, vertex1, vertex2, redMat));
            world.Add(new Triangle(vertex0, vertex2, vertex3, redMat));

            Raytracer tracer = new Raytracer(5);


            Bitmap image = tracer.Raytrace(world, camera, new Size(1024, 1024));

            pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
            image.Save("raytraced.png");
            pictureBox1.Image = image;
            //pictureBox1.ImageLocation = @"G:\Projekty\Elementy Grafiki\RaytracerTest\RayTracerWinFormsTest — kopia\RayTracerWinFormsTest\bin\Debug\raytraced.png";
        }
Пример #4
0
        /*public ColorRgb Radiance(PointLight light, HitInfo hit) {
         *  Vector3 inDirection = (light.Position - hit.HitPoint).Normalised;
         *  double diffuseFactor = inDirection.Dot(hit.Normal);
         *  if (diffuseFactor < 0) { return ColorRgb.Black; }
         *  return light.Color * materialColor * diffuseFactor;
         * }*/

        public ColorRgb Shade(Raytracer tracer, HitInfo hit)
        {
            ColorRgb totalColor = ColorRgb.Black;

            foreach (var light in hit.World.Lights)
            {
                Vector3 inDirection   = (light.Position - hit.HitPoint).Normalised;
                double  diffuseFactor = inDirection.Dot(hit.Normal);
                if (diffuseFactor < 0)
                {
                    continue;
                }
                if (hit.World.AnyObstacleBetween(hit.HitPoint, light.Position))
                {
                    continue;
                }
                totalColor += light.Color * materialColor * diffuseFactor;
            }
            return(totalColor);
        }