コード例 #1
0
        public void DrawTriangle(Vertex v1, Vertex v2, Vertex v3, MeshLightParameters lightParameters)
        {
            // Sorting the points in order to always have this order on screen p1, p2 & p3
            // with p1 always up (thus having the Y the lowest possible to be near the top screen)
            // then p2 between p1 & p3
            if (v1.Coordinates.Y > v2.Coordinates.Y)
            {
                var temp = v2;
                v2 = v1;
                v1 = temp;
            }

            if (v2.Coordinates.Y > v3.Coordinates.Y)
            {
                var temp = v2;
                v2 = v3;
                v3 = temp;
            }

            if (v1.Coordinates.Y > v2.Coordinates.Y)
            {
                var temp = v2;
                v2 = v1;
                v1 = temp;
            }

            Vector3 p1 = v1.Coordinates;
            Vector3 p2 = v2.Coordinates;
            Vector3 p3 = v3.Coordinates;

            DetailsForShading details = shadingMachine.GetDetailsForShading(v1, v2, v3);

            // computing lines' directions
            float dP1P2, dP1P3;

            // http://en.wikipedia.org/wiki/Slope
            // Computing slopes
            if (p2.Y - p1.Y > 0)
            {
                dP1P2 = (p2.X - p1.X) / (p2.Y - p1.Y);
            }
            else
            {
                dP1P2 = 0;
            }

            if (p3.Y - p1.Y > 0)
            {
                dP1P3 = (p3.X - p1.X) / (p3.Y - p1.Y);
            }
            else
            {
                dP1P3 = 0;
            }

            // First case where triangles are like that:
            // P1
            // -
            // --
            // - -
            // -  -
            // -   - P2
            // -  -
            // - -
            // -
            // P3
            if (dP1P2 > dP1P3)
            {
                for (var y = (int)p1.Y; y <= (int)p3.Y; y++)
                {
                    //data.currentY = y;
                    details.currentY = y;
                    if (y < p2.Y)
                    {
                        details.pa = v1.Coordinates;
                        details.pb = v3.Coordinates;
                        details.pc = v1.Coordinates;
                        details.pd = v2.Coordinates;
                        //ProcessScanLine(data, v1, v3, v1, v2, details.globalColor);
                        ProcessScanLine(details);
                    }
                    else
                    {
                        details.pa = v1.Coordinates;
                        details.pb = v3.Coordinates;
                        details.pc = v2.Coordinates;
                        details.pd = v3.Coordinates;
                        // ProcessScanLine(data, v1, v3, v2, v3, details.globalColor);
                        ProcessScanLine(details);
                    }
                }
            }
            // First case where triangles are like that:
            //       P1
            //        -
            //       --
            //      - -
            //     -  -
            // P2 -   -
            //     -  -
            //      - -
            //        -
            //       P3
            else
            {
                for (var y = (int)p1.Y; y <= (int)p3.Y; y++)
                {
                    //data.currentY = y;
                    details.currentY = y;
                    if (y < p2.Y)
                    {
                        details.pa = v1.Coordinates;
                        details.pb = v2.Coordinates;
                        details.pc = v1.Coordinates;
                        details.pd = v3.Coordinates;
                        //ProcessScanLine(data, v1, v2, v1, v3, details.globalColor);
                        ProcessScanLine(details);
                    }
                    else
                    {
                        details.pa = v2.Coordinates;
                        details.pb = v3.Coordinates;
                        details.pc = v1.Coordinates;
                        details.pd = v3.Coordinates;
                        // ProcessScanLine(data, v2, v3, v1, v3, details.globalColor);
                        ProcessScanLine(details);
                    }
                }
            }
        }
コード例 #2
0
        public Form1()
        {
            InitializeComponent();

            //Main window settings
            this.MinimumSize   = this.Size;
            this.MaximumSize   = this.Size;
            this.MaximizeBox   = false;
            this.StartPosition = FormStartPosition.CenterScreen;

            //render device initialize
            device = new Device(pictureBox1.Width, pictureBox1.Height);

            //camera section
            staticCamera.Position = new Vector3(3, 6, 15);
            staticCamera.Target   = Vector3.Zero;

            followingCamera.Position = new Vector3(-5, 7, 20);
            followingCamera.Target   = Vector3.Zero;

            dynamicCamera.Position = new Vector3(0, 0, 0);
            dynamicCamera.Target   = new Vector3(0, 0, -6);

            //initialize objects on the scene
            //monkey
            Mesh monkey = device.LoadJSONFileAsync("monkey.babylon");
            MeshLightParameters monkeyLightParameters = new MeshLightParameters(Color.Red, 0.4f, 0.7f, 0.9f, 32);

            monkey.lightParameters = monkeyLightParameters;
            monkey.Rotation        = new Vector3(0, 0, 0);
            monkey.RotationStep    = new Vector3(0, 0.05f, 0);
            PositionCalculator monkeyCalculator = new PositionCalculator();
            Calculator         xc = new Calculator(-3, 2.5f, 0, 0.05f);
            Calculator         bc = new Calculator();

            monkeyCalculator.XCalculator = xc;
            monkeyCalculator.YCalculator = bc;
            monkeyCalculator.ZCalculator = bc;
            monkey.positionCalculator    = monkeyCalculator;
            monkey.Scale = Vector3.One;
            meshes.Add(monkey);

            //cube
            Mesh cube = device.LoadJSONFileAsync("cube1.babylon");
            MeshLightParameters cube1LightParameters = new MeshLightParameters(Color.Gray, 0.9f, 0.8f, 0.9f, 60);

            cube.lightParameters = cube1LightParameters;
            cube.Rotation        = new Vector3(0, 0.4f, 0);
            cube.RotationStep    = new Vector3(0, 0, 0);
            PositionCalculator cubeCalculator = new PositionCalculator();
            Calculator         xc2            = new Calculator(0, 5.1f, 5, 0);

            cubeCalculator.XCalculator = xc2;
            cubeCalculator.YCalculator = bc;
            cubeCalculator.ZCalculator = bc;
            cube.positionCalculator    = cubeCalculator;
            cube.Scale = Vector3.One;
            meshes.Add(cube);

            //sphere
            Mesh sphere = device.LoadJSONFileAsync("sphere.babylon");
            MeshLightParameters sphereLightParameters = new MeshLightParameters(Color.Gray, 0.9f, 0.8f, 0.9f, 32);

            sphere.lightParameters = sphereLightParameters;
            sphere.Rotation        = new Vector3(0, 0.4f, 0);
            sphere.RotationStep    = new Vector3(0, 0, 0);
            PositionCalculator sphereCalculator = new PositionCalculator();
            Calculator         xc3 = new Calculator(-6.1f, 0, -6, 0);

            sphereCalculator.XCalculator = xc3;
            sphereCalculator.YCalculator = bc;
            sphereCalculator.ZCalculator = bc;
            sphere.positionCalculator    = sphereCalculator;
            sphere.Scale = Vector3.One;
            meshes.Add(sphere);

            //cylinder
            Mesh cylinder = device.LoadJSONFileAsync("cylinder.babylon");
            MeshLightParameters cylinderLightParameters = new MeshLightParameters(Color.Green, 0.9f, 0.8f, 0.9f, 32);

            cylinder.lightParameters = cylinderLightParameters;
            cylinder.Rotation        = new Vector3(0, 0, 0);
            cylinder.RotationStep    = new Vector3(0, 0, 0);
            PositionCalculator cylinderCalculator = new PositionCalculator();
            Calculator         zc = new Calculator(0, -5.1f, -5, 0);

            cylinderCalculator.XCalculator = bc;
            cylinderCalculator.YCalculator = bc;
            cylinderCalculator.ZCalculator = zc;
            cylinder.positionCalculator    = cylinderCalculator;
            cylinder.Scale = Vector3.One;
            meshes.Add(cylinder);

            //floor
            floor = device.LoadJSONFileAsync("floor.babylon");
            MeshLightParameters floorLightParameters = new MeshLightParameters(Color.Black, 0.9f, 0.8f, 0.9f, 32);

            floor.lightParameters = floorLightParameters;
            floor.Rotation        = new Vector3(0, 0, 0);
            floor.RotationStep    = new Vector3(0, 0, 0);
            PositionCalculator floorCalculator = new PositionCalculator();

            floorCalculator.XCalculator = bc;
            floorCalculator.YCalculator = bc;
            floorCalculator.ZCalculator = bc;
            floor.positionCalculator    = floorCalculator;
            floor.Scale = new Vector3(1.1f, 1.5f, 1);
            floor.ProjectCoordinatesTransform = new Vector3(0, 0, 1);
            meshes.Add(floor);

            //light sources initialize
            LightSource reflector1 = new LightSource()
            {
                color         = Color.Yellow,
                Position      = new Vector3(-6, 4, 2),
                Direction     = new Vector3(0, -1, 0),
                CosLightAngle = 0.5f
            };

            LightSource reflector2 = new LightSource()
            {
                color         = Color.Red,
                Position      = new Vector3(5.5f, 3, 0),
                Direction     = new Vector3(0, -1, 0),
                CosLightAngle = 0.2f
            };

            LightSource globalLight = new LightSource()
            {
                color         = Color.White,
                Position      = new Vector3(0, 100000, 0),
                Direction     = new Vector3(0, -1, 0),
                CosLightAngle = -1f
            };

            LightsManager lightsManager = new LightsManager(0.5f);

            lightsManager.AddLightSource(reflector1);
            lightsManager.AddLightSource(reflector2);
            lightsManager.AddLightSource(globalLight);
            //lightsManager.AddLightSource(new LightSource() { color = Color.Yellow, Position = new Vector3(-5, 3, 0), Direction = new Vector3(-0.6f, -0.8f, 0), CosLightAngle = 0.5f });
            device.lightsManager = lightsManager;

            //default settings
            radioButtonCameraStatic.Checked  = true;
            device.shadingMachine            = new FlatShadingMachine();
            device.shadingMachine.fogMachine = new NoFogMachine();
            radioButtonFogDisabled.Checked   = true;

            //start rendering
            SetTimer();
        }