public static void WpfColorFromColor4(ref Color4 source, ref WpfMedia.Color target)
 {
     target.A = (byte)EngineMath.Clamp(0f, 255f, source.Alpha * 255f);
     target.R = (byte)EngineMath.Clamp(0f, 255f, source.Alpha * 255f);
     target.G = (byte)EngineMath.Clamp(0f, 255f, source.Alpha * 255f);
     target.B = (byte)EngineMath.Clamp(0f, 255f, source.Alpha * 255f);
 }
        public static Color UIColorFromColor4(ref Color4 color)
        {
            var uiColor = new Color
            {
                A = (byte)EngineMath.Clamp(0f, 255f, color.Alpha * 255f),
                R = (byte)EngineMath.Clamp(0f, 255f, color.Red * 255f),
                G = (byte)EngineMath.Clamp(0f, 255f, color.Green * 255f),
                B = (byte)EngineMath.Clamp(0f, 255f, color.Blue * 255f)
            };

            return(uiColor);
        }
示例#3
0
        /// <summary>
        /// Loads a ac file from the given uri
        /// </summary>
        internal static ACFileInfo LoadFile(Stream inStream)
        {
            ACFileInfo?  result = null;
            StreamReader?reader = null;

            try
            {
                reader = new StreamReader(inStream);

                //Check for correct header
                var header = reader.ReadLine();
                if (header == null ||
                    !header.StartsWith("AC3D"))
                {
                    throw new SeeingSharpGraphicsException("Header of AC3D file not found!");
                }

                //Create file information object
                result = new ACFileInfo();

                //Create a loaded objects stack
                var       loadedObjects  = new Stack <ACObjectInfo>();
                var       parentObjects  = new Stack <ACObjectInfo>();
                ACSurface?currentSurface = null;

                //Read the file
                while (!reader.EndOfStream)
                {
                    var actLine = reader.ReadLine() !.Trim();

                    var firstWord  = string.Empty;
                    var spaceIndex = actLine.IndexOf(' ');
                    if (spaceIndex == -1)
                    {
                        firstWord = actLine;
                    }
                    else
                    {
                        firstWord = firstWord = actLine.Substring(0, spaceIndex);
                    }

                    switch (firstWord)
                    {
                    //New Material info
                    case "MATERIAL":
                        var materialInfo = new ACMaterialInfo();
                        {
                            //Get the name of the material
                            var materialData = actLine.Split(' ');

                            if (materialData.Length > 1)
                            {
                                materialInfo.Name = materialData[1].Trim(' ', '"');
                            }

                            //Parse components
                            for (var loop = 0; loop < materialData.Length; loop++)
                            {
                                switch (materialData[loop])
                                {
                                case "rgb":
                                    var diffuseColor = materialInfo.Diffuse;
                                    diffuseColor.Alpha   = 1f;
                                    diffuseColor.Red     = float.Parse(materialData[loop + 1], CultureInfo.InvariantCulture);
                                    diffuseColor.Green   = float.Parse(materialData[loop + 2], CultureInfo.InvariantCulture);
                                    diffuseColor.Blue    = float.Parse(materialData[loop + 3], CultureInfo.InvariantCulture);
                                    materialInfo.Diffuse = diffuseColor;
                                    break;

                                case "amb":
                                    var ambientColor = new Color4();
                                    ambientColor.Red     = float.Parse(materialData[loop + 1], CultureInfo.InvariantCulture);
                                    ambientColor.Green   = float.Parse(materialData[loop + 2], CultureInfo.InvariantCulture);
                                    ambientColor.Blue    = float.Parse(materialData[loop + 3], CultureInfo.InvariantCulture);
                                    materialInfo.Ambient = ambientColor;
                                    break;

                                case "emis":
                                    var emissiveColor = new Color4();
                                    emissiveColor.Red     = float.Parse(materialData[loop + 1], CultureInfo.InvariantCulture);
                                    emissiveColor.Green   = float.Parse(materialData[loop + 2], CultureInfo.InvariantCulture);
                                    emissiveColor.Blue    = float.Parse(materialData[loop + 3], CultureInfo.InvariantCulture);
                                    materialInfo.Emissive = emissiveColor;
                                    break;

                                case "spec":
                                    var specularColor = new Color4();
                                    specularColor.Red     = float.Parse(materialData[loop + 1], CultureInfo.InvariantCulture);
                                    specularColor.Green   = float.Parse(materialData[loop + 2], CultureInfo.InvariantCulture);
                                    specularColor.Blue    = float.Parse(materialData[loop + 3], CultureInfo.InvariantCulture);
                                    materialInfo.Specular = specularColor;
                                    break;

                                case "shi":
                                    materialInfo.Shininess = float.Parse(materialData[loop + 1], CultureInfo.InvariantCulture);
                                    break;

                                case "trans":
                                    diffuseColor         = materialInfo.Diffuse;
                                    diffuseColor.Alpha   = 1f - EngineMath.Clamp(float.Parse(materialData[loop + 1], CultureInfo.InvariantCulture), 0f, 1f);
                                    materialInfo.Diffuse = diffuseColor;
                                    break;
                                }
                            }
                            result.Materials.Add(materialInfo);
                        }
                        break;

                    //New object starts here
                    case "OBJECT":
                    {
                        var newObject = new ACObjectInfo();

                        var lineData = actLine.Split(' ');
                        if (lineData[1] == "poly")
                        {
                            newObject.Type = ACObjectType.Poly;
                        }
                        else if (lineData[1] == "group")
                        {
                            newObject.Type = ACObjectType.Group;
                        }
                        else if (lineData[1] == "world")
                        {
                            newObject.Type = ACObjectType.World;
                        }

                        loadedObjects.Push(newObject);
                    }
                    break;

                    //End of an object, kids following
                    case "kids":
                        if (loadedObjects.Count == 0)
                        {
                            break;
                        }
                        {
                            //Parse kid count
                            var kidCount = 0;
                            var lineData = actLine.Split(' ');

                            if (lineData.Length >= 1)
                            {
                                int.TryParse(lineData[1], out kidCount);
                            }

                            var currentObject = loadedObjects.Peek();

                            if (currentObject != null)
                            {
                                //AddObject object to parent object, if any related
                                var addedToParent = false;

                                if (parentObjects.Count > 0)
                                {
                                    var currentParent = parentObjects.Peek();

                                    if (currentParent.Children.Count < currentParent.KidCount)
                                    {
                                        currentParent.Children.Add(currentObject);
                                        addedToParent = true;
                                    }
                                    else
                                    {
                                        while (parentObjects.Count > 0)
                                        {
                                            parentObjects.Pop();

                                            if (parentObjects.Count == 0)
                                            {
                                                break;
                                            }

                                            currentParent = parentObjects.Peek();

                                            if (currentParent == null)
                                            {
                                                break;
                                            }
                                            if (currentParent.Children.Count < currentParent.KidCount)
                                            {
                                                break;
                                            }
                                        }

                                        if (currentParent != null &&
                                            currentParent.Children.Count < currentParent.KidCount)
                                        {
                                            currentParent.Children.Add(currentObject);
                                            addedToParent = true;
                                        }
                                    }
                                }

                                //Enable this object as parent object
                                currentObject.KidCount = kidCount;

                                if (currentObject.KidCount > 0)
                                {
                                    parentObjects.Push(currentObject);
                                }

                                //AddObject to scene root if this object has no parent
                                loadedObjects.Pop();

                                if (!addedToParent)
                                {
                                    if (loadedObjects.Count == 0)
                                    {
                                        result.Objects.Add(currentObject);
                                    }
                                    else
                                    {
                                        loadedObjects.Peek().Children.Add(currentObject);
                                    }
                                }
                                currentObject = null;
                            }
                        }
                        break;

                    //Current object's name
                    case "name":
                        if (loadedObjects.Count == 0)
                        {
                            break;
                        }
                        {
                            var currentObject = loadedObjects.Peek();
                            if (currentObject != null)
                            {
                                currentObject.Name = actLine.Replace("name ", "").Replace("\"", "");
                            }
                        }
                        break;

                    case "data":
                        break;

                    case "texture":
                        if (loadedObjects.Count == 0)
                        {
                            break;
                        }
                        {
                            var currentObject = loadedObjects.Peek();

                            if (currentObject != null)
                            {
                                var lineData = actLine.Split(' ');
                                currentObject.Texture = lineData[1].Trim('"');
                            }
                        }
                        break;

                    case "texrep":
                        if (loadedObjects.Count == 0)
                        {
                            break;
                        }
                        {
                            var currentObject = loadedObjects.Peek();

                            if (currentObject != null)
                            {
                                var lineData = actLine.Split(' ');

                                var repetition = new Vector2
                                {
                                    X = float.Parse(lineData[1], CultureInfo.InvariantCulture),
                                    Y = float.Parse(lineData[2], CultureInfo.InvariantCulture)
                                };

                                currentObject.TextureRepeat = repetition;
                            }
                        }
                        break;

                    case "texoff":
                        if (loadedObjects.Count == 0)
                        {
                            break;
                        }
                        {
                            var currentObject = loadedObjects.Peek();

                            if (currentObject != null)
                            {
                                var lineData = actLine.Split(' ');

                                var offset = new Vector2
                                {
                                    X = float.Parse(lineData[1], CultureInfo.InvariantCulture),
                                    Y = float.Parse(lineData[2], CultureInfo.InvariantCulture)
                                };

                                currentObject.TextureRepeat = offset;
                            }
                        }
                        break;

                    case "rot":
                        if (loadedObjects.Count == 0)
                        {
                            break;
                        }
                        {
                            var currentObject = loadedObjects.Peek();

                            if (currentObject != null)
                            {
                                var lineData = actLine.Split(' ');

                                var rotation = Matrix4x4.Identity;
                                rotation.M11 = !string.IsNullOrEmpty(lineData[1]) ? float.Parse(lineData[1], CultureInfo.InvariantCulture) : 0f;
                                rotation.M12 = !string.IsNullOrEmpty(lineData[2]) ? float.Parse(lineData[2], CultureInfo.InvariantCulture) : 0f;
                                rotation.M13 = !string.IsNullOrEmpty(lineData[3]) ? float.Parse(lineData[3], CultureInfo.InvariantCulture) : 0f;
                                rotation.M21 = !string.IsNullOrEmpty(lineData[4]) ? float.Parse(lineData[4], CultureInfo.InvariantCulture) : 0f;
                                rotation.M22 = !string.IsNullOrEmpty(lineData[5]) ? float.Parse(lineData[5], CultureInfo.InvariantCulture) : 0f;
                                rotation.M23 = !string.IsNullOrEmpty(lineData[6]) ? float.Parse(lineData[6], CultureInfo.InvariantCulture) : 0f;
                                rotation.M31 = !string.IsNullOrEmpty(lineData[7]) ? float.Parse(lineData[7], CultureInfo.InvariantCulture) : 0f;
                                rotation.M32 = !string.IsNullOrEmpty(lineData[8]) ? float.Parse(lineData[8], CultureInfo.InvariantCulture) : 0f;
                                rotation.M33 = !string.IsNullOrEmpty(lineData[9]) ? float.Parse(lineData[9], CultureInfo.InvariantCulture) : 0f;

                                currentObject.Rotation = rotation;
                            }
                        }
                        break;

                    case "url":
                        if (loadedObjects.Count == 0)
                        {
                            break;
                        }
                        {
                            var currentObject = loadedObjects.Peek();

                            if (currentObject != null)
                            {
                                var lineData = actLine.Split(' ');
                                currentObject.Url = lineData[1].Trim('"');
                            }
                        }
                        break;

                    //Current object's location
                    case "loc":
                        if (loadedObjects.Count == 0)
                        {
                            break;
                        }
                        {
                            var currentObject = loadedObjects.Peek();

                            if (currentObject != null)
                            {
                                var lineData = actLine.Split(' ');

                                var location = new Vector3
                                {
                                    X = float.Parse(lineData[1], CultureInfo.InvariantCulture),
                                    Y = float.Parse(lineData[2], CultureInfo.InvariantCulture),
                                    Z = float.Parse(lineData[3], CultureInfo.InvariantCulture)
                                };

                                currentObject.Translation = location;
                            }
                        }
                        break;

                    case "numvert":
                        if (loadedObjects.Count == 0)
                        {
                            break;
                        }
                        {
                            var currentObject = loadedObjects.Peek();

                            if (currentObject != null)
                            {
                                var lineData         = actLine.Split(' ');
                                var numberOfVertices = int.Parse(lineData[1], CultureInfo.InvariantCulture);

                                for (var loop = 0; loop < numberOfVertices; loop++)
                                {
                                    var actInnerLine   = reader.ReadLine() !.Trim();
                                    var splittedVertex = actInnerLine.Split(' ');

                                    var position = new Vector3
                                    {
                                        X = float.Parse(splittedVertex[0], CultureInfo.InvariantCulture),
                                        Y = float.Parse(splittedVertex[1], CultureInfo.InvariantCulture),
                                        Z = float.Parse(splittedVertex[2], CultureInfo.InvariantCulture)
                                    };

                                    currentObject.Vertices.Add(new ACVertex {
                                        Position = position
                                    });
                                }
                            }
                        }
                        break;

                    //Start of a list of surfaces
                    case "numsurf":
                        break;

                    //New surface starts here
                    case "SURF":
                    {
                        if (currentSurface == null)
                        {
                            currentSurface = new ACSurface();
                        }

                        var lineData = actLine.Split(' ');
                        lineData[1]          = lineData[1].Substring(2);
                        currentSurface.Flags = int.Parse(lineData[1], NumberStyles.HexNumber);
                    }
                    break;

                    //Current surface's material
                    case "mat":
                    {
                        if (currentSurface == null)
                        {
                            currentSurface = new ACSurface();
                        }

                        var lineData = actLine.Split(' ');
                        currentSurface.Material = int.Parse(lineData[1], CultureInfo.InvariantCulture);
                    }
                    break;

                    //Current surface's indices
                    case "refs":
                        if (loadedObjects.Count == 0)
                        {
                            break;
                        }
                        {
                            if (currentSurface == null)
                            {
                                currentSurface = new ACSurface();
                            }

                            var lineData     = actLine.Split(' ');
                            var numberOfRefs = int.Parse(lineData[1], CultureInfo.InvariantCulture);

                            for (var loop = 0; loop < numberOfRefs; loop++)
                            {
                                var actInnerLine = reader.ReadLine() !.Trim();
                                var splittedRef  = actInnerLine.Split(' ');

                                var texCoord        = new Vector2();
                                int vertexReference = ushort.Parse(splittedRef[0], CultureInfo.InvariantCulture);
                                texCoord.X = float.Parse(splittedRef[1], CultureInfo.InvariantCulture);
                                texCoord.Y = float.Parse(splittedRef[2], CultureInfo.InvariantCulture);

                                currentSurface.TextureCoordinates.Add(texCoord);
                                currentSurface.VertexReferences.Add(vertexReference);
                            }

                            var currentObject = loadedObjects.Peek();

                            currentObject?.Surfaces.Add(currentSurface);

                            currentSurface = null;
                        }
                        break;
                    }
                }
            }
            finally
            {
                SeeingSharpUtil.SafeDispose(ref reader);
            }

            return(result);
        }
        /// <summary>
        /// Querries all current input states.
        /// </summary>
        public IEnumerable <InputStateBase> GetInputStates()
        {
            // Update connected states first
            for (int loop = 0; loop < m_controllers.Length; loop++)
            {
                bool isConnected = m_controllers[loop].IsConnected;

                if (!isConnected)
                {
                    m_states[loop].NotifyConnected(false);
                    continue;
                }
                m_states[loop].NotifyConnected(true);

                // Query all state structures
                XI.State   xiState   = m_controllers[loop].GetState();
                XI.Gamepad xiGamepad = xiState.Gamepad;

                // Convert float values
                GamepadReportedState repState = new GamepadReportedState()
                {
                    LeftThumbstickX  = EngineMath.Clamp((float)xiGamepad.LeftThumbX / (float)short.MaxValue, -1f, 1f),
                    LeftThumbstickY  = EngineMath.Clamp((float)xiGamepad.LeftThumbY / (float)short.MaxValue, -1f, 1f),
                    LeftTrigger      = EngineMath.Clamp((float)xiGamepad.LeftTrigger / 255f, 0f, 1f),
                    RightThumbstickX = EngineMath.Clamp((float)xiGamepad.RightThumbX / (float)short.MaxValue, -1f, 1f),
                    RightThumbstickY = EngineMath.Clamp((float)xiGamepad.RightThumbY / (float)short.MaxValue, -1f, 1f),
                    RightTrigger     = EngineMath.Clamp((float)xiGamepad.RightTrigger / 255f, 0, 1f)
                };

                // Convert button states
                GamepadButton pressedButtons = GamepadButton.None;
                if (xiGamepad.Buttons.HasFlag(XI.GamepadButtonFlags.A))
                {
                    pressedButtons |= GamepadButton.A;
                }
                if (xiGamepad.Buttons.HasFlag(XI.GamepadButtonFlags.B))
                {
                    pressedButtons |= GamepadButton.B;
                }
                if (xiGamepad.Buttons.HasFlag(XI.GamepadButtonFlags.Back))
                {
                    pressedButtons |= GamepadButton.View;
                }
                if (xiGamepad.Buttons.HasFlag(XI.GamepadButtonFlags.DPadDown))
                {
                    pressedButtons |= GamepadButton.DPadDown;
                }
                if (xiGamepad.Buttons.HasFlag(XI.GamepadButtonFlags.DPadLeft))
                {
                    pressedButtons |= GamepadButton.DPadLeft;
                }
                if (xiGamepad.Buttons.HasFlag(XI.GamepadButtonFlags.DPadRight))
                {
                    pressedButtons |= GamepadButton.DPadRight;
                }
                if (xiGamepad.Buttons.HasFlag(XI.GamepadButtonFlags.DPadUp))
                {
                    pressedButtons |= GamepadButton.DPadUp;
                }
                if (xiGamepad.Buttons.HasFlag(XI.GamepadButtonFlags.LeftShoulder))
                {
                    pressedButtons |= GamepadButton.LeftShoulder;
                }
                if (xiGamepad.Buttons.HasFlag(XI.GamepadButtonFlags.LeftThumb))
                {
                    pressedButtons |= GamepadButton.LeftThumbstick;
                }
                if (xiGamepad.Buttons.HasFlag(XI.GamepadButtonFlags.RightShoulder))
                {
                    pressedButtons |= GamepadButton.RightShoulder;
                }
                if (xiGamepad.Buttons.HasFlag(XI.GamepadButtonFlags.RightThumb))
                {
                    pressedButtons |= GamepadButton.RightThumbstick;
                }
                if (xiGamepad.Buttons.HasFlag(XI.GamepadButtonFlags.Start))
                {
                    pressedButtons |= GamepadButton.Menu;
                }
                if (xiGamepad.Buttons.HasFlag(XI.GamepadButtonFlags.X))
                {
                    pressedButtons |= GamepadButton.X;
                }
                if (xiGamepad.Buttons.HasFlag(XI.GamepadButtonFlags.Y))
                {
                    pressedButtons |= GamepadButton.Y;
                }
                repState.Buttons = pressedButtons;

                // Report controller state to the system
                m_states[loop].NotifyState(repState);
            }

            // Now return all input states
            for (int loop = 0; loop < m_states.Length; loop++)
            {
                yield return(m_states[loop]);
            }
        }