Пример #1
0
        /// <summary/>
        public static Camera MakeCamera(Variation v)
        {
            Camera camera = null;

            if (v["Camera"] != null)
            {
                v.AssertAbsenceOf("CameraType", "CameraPosition", "CameraLookDirection", "CameraUp", "CameraNearPlaneDistance", "CameraFarPlaneDistance", "CameraWidth", "CameraFieldOfView", "CameraViewMatrix", "CameraProjectionMatrix");
                camera = CameraFactory.MakeCamera(v["Camera"]);
            }
            else
            {
                v.AssertExistenceOf("CameraType");
                switch (v["CameraType"])
                {
                case "Orthographic":
                    v.AssertExistenceOf("CameraPosition", "CameraLookDirection", "CameraUp", "CameraNearPlaneDistance", "CameraFarPlaneDistance", "CameraWidth");
                    camera = new OrthographicCamera();
                    SetProjectionCameraParameters(camera, v);
                    ((OrthographicCamera)camera).Width = StringConverter.ToDouble(v["CameraWidth"]);
                    break;

                case "Perspective":
                    v.AssertExistenceOf("CameraPosition", "CameraLookDirection", "CameraUp", "CameraNearPlaneDistance", "CameraFarPlaneDistance", "CameraFieldOfView");
                    camera = new PerspectiveCamera();
                    SetProjectionCameraParameters(camera, v);
                    ((PerspectiveCamera)camera).FieldOfView = StringConverter.ToDouble(v["CameraFieldOfView"]);
                    break;

                case "Matrix":
                    v.AssertExistenceOf("CameraViewMatrix", "CameraProjectionMatrix");
                    camera = new MatrixCamera();
                    ((MatrixCamera)camera).ViewMatrix       = StringConverter.ToMatrix3D(v["CameraViewMatrix"]);
                    ((MatrixCamera)camera).ProjectionMatrix = StringConverter.ToMatrix3D(v["CameraProjectionMatrix"]);
                    break;

                case "MatrixOrtho":
                    v.AssertExistenceOf("CameraPosition", "CameraLookDirection", "CameraUp", "CameraNearPlaneDistance", "CameraFarPlaneDistance", "CameraWidth", "CameraHeight");
                    camera = new MatrixCamera();
                    ((MatrixCamera)camera).ViewMatrix       = MakeViewMatrix(v);
                    ((MatrixCamera)camera).ProjectionMatrix = MakeOrthoMatrix(v);
                    break;

                case "MatrixPersp":
                    v.AssertExistenceOf("CameraPosition", "CameraLookDirection", "CameraUp", "CameraNearPlaneDistance", "CameraFarPlaneDistance", "CameraFieldOfViewX", "CameraFieldOfViewY");
                    camera = new MatrixCamera();
                    ((MatrixCamera)camera).ViewMatrix       = MakeViewMatrix(v);
                    ((MatrixCamera)camera).ProjectionMatrix = MakePerspMatrix(v);
                    break;

                default:
                    throw new ApplicationException("Invalid camera type: " + v["CameraType"]);
                }
            }

            return(camera);
        }
Пример #2
0
        /// <summary/>
        public static Light MakeLight(Variation v)
        {
            Light light = null;

            if (v["Light"] != null)
            {
                v.AssertAbsenceOf("LightType", "LightColor", "LightPosition", "LightDirection", "LightRange", "LightConstantAttenuation", "LightLinearAttenuation", "LightQuadraticAttenuation", "LightInnerConeAngle", "LightOuterConeAngle");
                light = LightFactory.MakeLight(v["Light"]);
            }
            else
            {
                v.AssertExistenceOf("LightType", "LightColor");
                switch (v["LightType"])
                {
                case "Ambient":
                    light = new AmbientLight();
                    break;

                case "Directional":
                    v.AssertExistenceOf("LightDirection");
                    light = new DirectionalLight();
                    ((DirectionalLight)light).Direction = StringConverter.ToVector3D(v["LightDirection"]);
                    break;

                case "Point":
                    v.AssertExistenceOf("LightPosition", "LightRange", "LightConstantAttenuation", "LightLinearAttenuation", "LightQuadraticAttenuation");
                    light = new PointLight();
                    SetLocalLightParameters((PointLight)light, v);
                    break;

                case "Spot":
                    v.AssertExistenceOf("LightPosition", "LightDirection", "LightRange", "LightConstantAttenuation", "LightLinearAttenuation", "LightQuadraticAttenuation", "LightInnerConeAngle", "LightOuterConeAngle");
                    light = new SpotLight();
                    SetLocalLightParameters((SpotLight)light, v);
                    ((SpotLight)light).Direction      = StringConverter.ToVector3D(v["LightDirection"]);
                    ((SpotLight)light).InnerConeAngle = StringConverter.ToDouble(v["LightInnerConeAngle"]);
                    ((SpotLight)light).OuterConeAngle = StringConverter.ToDouble(v["LightOuterConeAngle"]);
                    break;

                default:
                    throw new ApplicationException("Invalid light type: " + v["LightType"]);
                }
                light.Color = StringConverter.ToColor(v["LightColor"]);
            }

            return(light);
        }
Пример #3
0
        /// <summary/>
        public static Model3D MakeModel(Variation v)
        {
            if (v["Lines"] != null)
            {
#if SSL
                v.AssertAbsenceOf("Mesh", "Material");
                return(MakeScreenSpaceLines(v));
#else
                throw new ArgumentException("Can't create ScreenSpaceLines3D anymore");
#endif
            }
            else
            {
                Material        mat  = MakeFrontMaterial(v);
                MeshGeometry3D  mesh = MakeMesh(v);
                GeometryModel3D gm   = new GeometryModel3D(mesh, mat);
                gm.BackMaterial = MakeBackMaterial(v);
                return(gm);
            }
        }
Пример #4
0
        /// <summary/>
        public static Transform3D MakeTransform3D(Variation v)
        {
            Transform3D tx;

            if (v["TransformType"] == null)
            {
                v.AssertAbsenceOf("TranslateOffset", "RotateAngle", "RotateAxis", "RotateQuaternion", "RotateCenter", "ScaleVector", "ScaleCenter", "MatrixValue");
                tx = Transform3D.Identity;
            }
            else
            {
                switch (v["TransformType"])
                {
                case "Translate":
                    v.AssertExistenceOf("TranslateOffset");
                    tx = new TranslateTransform3D(StringConverter.ToVector3D(v["TranslateOffset"]));
                    break;

                case "RotateAxisAngle":
                    v.AssertExistenceOf("RotateAngle", "RotateAxis");
                    tx = new RotateTransform3D(new AxisAngleRotation3D(StringConverter.ToVector3D(v["RotateAxis"]), StringConverter.ToDouble(v["RotateAngle"])));
                    break;

                case "RotateAxisAngleCenter":
                    v.AssertExistenceOf("RotateAngle", "RotateAxis", "RotateCenter");
                    tx = new RotateTransform3D(new AxisAngleRotation3D(StringConverter.ToVector3D(v["RotateAxis"]), StringConverter.ToDouble(v["RotateAngle"])), StringConverter.ToPoint3D(v["RotateCenter"]));
                    break;

                case "RotateQuaternion":
                    v.AssertExistenceOf("RotateQuaternion");
                    tx = new RotateTransform3D(new QuaternionRotation3D(StringConverter.ToQuaternion(v["RotateQuaternion"])));
                    break;

                case "RotateQuaternionCenter":
                    v.AssertExistenceOf("RotateQuaternion", "RotateCenter");
                    tx = new RotateTransform3D(new QuaternionRotation3D(StringConverter.ToQuaternion(v["RotateQuaternion"])), StringConverter.ToPoint3D(v["RotateCenter"]));
                    break;

                case "RotateNullRotation":
                    tx = new RotateTransform3D();
                    ((RotateTransform3D)tx).Rotation = null;
                    break;

                case "Scale":
                    v.AssertExistenceOf("ScaleVector");
                    tx = new ScaleTransform3D(StringConverter.ToVector3D(v["ScaleVector"]));
                    break;

                case "ScaleCenter":
                    v.AssertExistenceOf("ScaleVector", "ScaleCenter");
                    tx = new ScaleTransform3D(StringConverter.ToVector3D(v["ScaleVector"]), StringConverter.ToPoint3D(v["ScaleCenter"]));
                    break;

                case "Matrix":
                    v.AssertExistenceOf("MatrixValue");
                    tx = new MatrixTransform3D(StringConverter.ToMatrix3D(v["MatrixValue"]));
                    break;

                case "Group":
                    tx = new Transform3DGroup();
                    break;

                case "GroupNullChildren":
                    tx = new Transform3DGroup();
                    ((Transform3DGroup)tx).Children = null;
                    break;

                case "Null":
                    tx = null;
                    break;

                default:
                    throw new ApplicationException("Invalid TransformType specified: " + v["TransformType"]);
                }
            }

            return(tx);
        }
Пример #5
0
        private static Material MakeMaterial(Variation v, string namePrefix)
        {
            Material material      = null;
            string   type          = namePrefix + "Type";
            string   specularPower = namePrefix + "SpecularPower";
            string   ambientColor  = namePrefix + "AmbientColor";
            string   color         = namePrefix + "Color";

            // Material CAN be null

            if (v[namePrefix] == "Group")
            {
                v.AssertAbsenceOf(ambientColor, color, specularPower);
                if (v[type] != null && v[type] != "Group")
                {
                    throw new ArgumentException("Can't have " + namePrefix + "=Group and type=" + v[type]);
                }
                material = MakeMaterialGroup(v, namePrefix);
            }
            else if (v[namePrefix] != null)
            {
                if (v[type] != null)
                {
                    switch (v[type])
                    {
                    case "Group":
                        throw new ArgumentException("Can't have 'Group' type without " + namePrefix + "='Group'");

                    case "Specular":
                        v.AssertExistenceOf(specularPower);
                        material = MaterialFactory.MakeMaterial(
                            v[namePrefix],
                            v[type],
                            StringConverter.ToDouble(v[specularPower]));
                        break;

                    default:
                        v.AssertAbsenceOf(specularPower);
                        material = MaterialFactory.MakeMaterial(v[namePrefix], v[type]);
                        break;
                    }
                }
                else
                {
                    v.AssertAbsenceOf(specularPower);
                    material = MaterialFactory.MakeMaterial(v[namePrefix]);
                }

                // Parse Color Knobs
                if (v[ambientColor] != null)
                {
                    if (material is DiffuseMaterial)
                    {
                        ((DiffuseMaterial)material).AmbientColor = StringConverter.ToColor(v[ambientColor]);
                    }
                    else
                    {
                        throw new ArgumentException("Can't set " + namePrefix + "AmbientColor on non-DiffuseMaterial");
                    }
                }
                if (v[color] != null)
                {
                    if (material is DiffuseMaterial)
                    {
                        ((DiffuseMaterial)material).Color = StringConverter.ToColor(v[color]);
                    }
                    else if (material is EmissiveMaterial)
                    {
                        ((EmissiveMaterial)material).Color = StringConverter.ToColor(v[color]);
                    }
                    else if (material is SpecularMaterial)
                    {
                        ((SpecularMaterial)material).Color = StringConverter.ToColor(v[color]);
                    }
                    else
                    {
                        throw new ArgumentException("Can't set " + namePrefix + "Color on MaterialGroup");
                    }
                }
            }
            return(material);
        }