Exemple #1
0
        public ColorIntensity RayIntensity(SDScene scene, RayData data)
        {
            ColorIntensity color = new ColorIntensity();

            float lightPass = LightPassValue(scene.ClosestDistance, data.Pos);

            if (lightPass == 0f)
            {
                return(color);
            }

            Vec3f dir = (Pos - data.Pos).Normalized();

            float diffuseDot = dir.Dot(data.Normal);

            if (diffuseDot > 0f)
            {
                color += data.Mat.DiffuseIntensity * DiffuseIntensity * diffuseDot * lightPass;

                Vec3f reflectionDir = 2f * dir.Dot(data.Normal) * data.Normal - dir;
                Vec3f viewerDir     = (data.RayOrigin - data.Pos).Normalized();
                float specularDot   = reflectionDir.Dot(viewerDir);
                if (specularDot > 0f)
                {
                    color += SpecularIntensity * data.Mat.Specular
                             * MathF.Pow(specularDot, data.Mat.Shininess) * lightPass;
                }
            }

            return(color.AtDistance((Pos - data.Pos).Magnitude));
        }
Exemple #2
0
        static void Main(string[] args)
        {
            ILight light = new PointLight(new Vec3f(4, -4, 2), // Light position
                                          Color.White,         // Light color
                                          0.3f,                // Diffuse intensity
                                          0.3f,                // Specular intensity
                                          0.2f);               // Softness

            IBackground bg = new SolidBackground();

            SDLighting lighting = new SDLighting(bg,          // Lighting background
                                                 Color.White, // Ambient color
                                                 0.0001f,     // Ambient color brightness
                                                 light);      // Lights

            Material ballMat = new Material(Color.Blue,
                                            0.3f, // Diffuse constant
                                            0.9f, // Specular constant
                                            80f,  // Shininess
                                            1f);  // Ambient constant

            Material floorMat = new Material(Color.White,
                                             0.1f,    // Diffuse constant
                                             0.05f,   // Specular constant
                                             30f,     // Shininess
                                             0.4f);   // Ambient constant

            SDSphere ball = new SDSphere(new Vec3f(), // Postion
                                         1f,          // Radius
                                         ballMat);    // Material

            // Repeats ball infinitely in the X direction
            var ballField = SDObject.RepeatX(ball, 0f, 5f);

            // Repeats ball infinitely in the Y direction
            ballField = SDObject.RepeatY(ballField, 0f, 5f);

            SDObject floor = new SDDeepPlane(new Vec3f(0, 0, -1), // Position
                                             new Vec3f(0, 0, 1),  // Outward plane normal
                                             floorMat);           // Material

            Camera cam = new Camera(1920, 1080);

            cam.Pos = new Vec3f(0, -5, 2);
            cam.LookAt(new Vec3f());

            SDScene scene = new SDScene(cam, lighting, ballField, floor);

            Renderer renderer = new Renderer(scene);

            renderer.Render().Save(ImgPath + "example_img.png");
        }