Beispiel #1
0
        private void SavePose(string filename)
        {
            Armature      armature  = selectedItem.Model.Armature;
            List <string> boneNames = new List <string>();

            foreach (Armature.Bone bone in armature.Bones)
            {
                if (!bone.name.StartsWith("unused"))
                {
                    boneNames.Add(bone.name);
                }
            }

            PoseFilterDialog dialog = new PoseFilterDialog(game, "Save pose", "Select bones to save:", boneNames, true);

            if (dialog.ShowDialog(this) != DialogResult.OK)
            {
                return;
            }

            try {
                DataSet      dataSet = dataSetDict[selectedItem];
                StreamWriter file    = new StreamWriter(filename);
                foreach (Armature.Bone bone in armature.Bones)
                {
                    if (!dialog.IsBoneSelected(bone.name))
                    {
                        continue;
                    }
                    BoneTransform boneTransform = dataSet.boneTransforms[bone.id];
                    file.WriteLine("{0}: {1} {2} {3} {4} {5} {6}", bone.name,
                                   boneTransform.rotX, boneTransform.rotY, boneTransform.rotZ,
                                   boneTransform.moveX, boneTransform.moveY, boneTransform.moveZ);
                }
                file.Close();
            }
            catch (Exception ex) {
                MessageBox.Show(this, "Could not save the pose file.\n" + ex.Message,
                                "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
        }
Beispiel #2
0
        public void AddModel(Model model)
        {
            models.Add(model);
            Armature armature = model.Armature;

            foreach (Armature.Bone bone in armature.Bones)
            {
                if (bone.name.StartsWith("unused"))
                {
                    continue;
                }
                activeBones.Add(bone);
                if (model.IsVisible)
                {
                    visibleBones.Add(bone);
                    visibleBonesProjections.Add(Vector3.Zero);
                }
            }
            armature.ArmatureEvent += HandleArmatureEvent;
            RecalculateBonePositions();
        }
Beispiel #3
0
        private Armature LoadArmature(BinaryReader file, Model model)
        {
            int boneCount = file.ReadInt32();

            if (boneCount == 0)
            {
                return(null);
            }
            Armature armature = new Armature(model);

            Armature.Bone[] bones     = new Armature.Bone[boneCount];
            int[]           parentIDs = new int[boneCount];
            for (int boneID = 0; boneID < boneCount; boneID++)
            {
                Armature.Bone bone = new Armature.Bone();
                bone.armature     = armature;
                bone.id           = boneID;
                bone.name         = file.ReadString();
                parentIDs[boneID] = file.ReadInt16();
                float absPosX = file.ReadSingle();
                float absPosY = file.ReadSingle();
                float absPosZ = file.ReadSingle();
                bone.absPosition = new Vector3(absPosX, absPosY, absPosZ);
                bones[boneID]    = bone;
            }
            for (int boneID = 0; boneID < boneCount; boneID++)
            {
                Armature.Bone bone     = bones[boneID];
                int           parentID = parentIDs[boneID];
                if (parentID >= 0)
                {
                    Armature.Bone parent = bones[parentID];
                    bone.parent = parent;
                    parent.children.Add(bone);
                }
            }
            armature.Bones = bones;
            return(armature);
        }
Beispiel #4
0
        public static void ExportToMeshAscii(Model model, string filename, bool applyPose)
        {
            StreamWriter file     = new StreamWriter(filename);
            Armature     armature = model.Armature;

            file.WriteLine("{0} # bones", model.Armature.Bones.Length);
            foreach (Armature.Bone bone in model.Armature.Bones)
            {
                file.WriteLine("{0}", bone.name);
                file.WriteLine("{0}", bone.parent != null ? bone.parent.id : -1);
                Vector3 position;
                if (!applyPose)
                {
                    position = bone.absPosition * armature.WorldScale;
                }
                else
                {
                    position = Utils.GetTransformedBone(bone, armature.WorldMatrix);
                }
                file.WriteLine("{0} {1} {2}", position.X, position.Y, position.Z);
            }
            file.WriteLine("{0} # meshes", model.Meshes.Count);
            foreach (MeshDesc meshDesc in model.MeshDescs)
            {
                file.WriteLine("{0}", meshDesc.name);
                file.WriteLine("{0} # uv layers", meshDesc.uvLayerCount);
                file.WriteLine("{0} # textures", meshDesc.textures.Length);
                foreach (MeshDesc.Texture texture in meshDesc.textures)
                {
                    file.WriteLine("{0}", ExtractFilename(texture.filename));
                    file.WriteLine("{0} # uv layer index", texture.uvLayerIndex);
                }
                file.WriteLine("{0} # vertices", meshDesc.vertices.Length);
                foreach (MeshDesc.Vertex vertex in meshDesc.vertices)
                {
                    Vector3 position;
                    Vector3 normal;
                    if (!applyPose)
                    {
                        position = vertex.position * armature.WorldScale;
                        normal   = vertex.normal;
                    }
                    else
                    {
                        Utils.TransformVertex(vertex,
                                              armature.WorldMatrix, armature.BoneMatrices, meshDesc.boneIndexMap,
                                              out position, out normal);
                    }
                    file.WriteLine("{0} {1} {2}", position.X, position.Y, position.Z);
                    file.WriteLine("{0} {1} {2}", normal.X, normal.Y, normal.Z);
                    Color color = VertexColorToColor(vertex.color);
                    file.WriteLine("{0} {1} {2} {3}", color.R, color.G, color.B, color.A);
                    for (int uvLayerID = 0; uvLayerID < meshDesc.uvLayerCount; uvLayerID++)
                    {
                        Vector2 uv = vertex.texCoords[uvLayerID];
                        file.WriteLine("{0} {1}", uv.X, uv.Y);
                    }
                    if (model.Armature.Bones.Length > 0)
                    {
                        file.WriteLine("{0} {1} {2} {3}",
                                       vertex.boneIndicesGlobal[0],
                                       vertex.boneIndicesGlobal[1],
                                       vertex.boneIndicesGlobal[2],
                                       vertex.boneIndicesGlobal[3]);
                        file.WriteLine("{0} {1} {2} {3}",
                                       vertex.boneWeights[0],
                                       vertex.boneWeights[1],
                                       vertex.boneWeights[2],
                                       vertex.boneWeights[3]);
                    }
                }
                file.WriteLine("{0} # faces", meshDesc.indices.Length / 3);
                for (int i = 0; i < meshDesc.indices.Length; i += 3)
                {
                    file.WriteLine("{0} {1} {2}",
                                   meshDesc.indices[i + 0],
                                   meshDesc.indices[i + 1],
                                   meshDesc.indices[i + 2]);
                }
            }
            file.Close();
        }
Beispiel #5
0
        private void MirrorPose()
        {
            if (selectedItem == null)
            {
                return;
            }

            Armature armature = selectedItem.Model.Armature;

            List <string> labels = new List <string>();

            foreach (Armature.Bone bone in armature.Bones)
            {
                int    origSide;
                string mirroredName = GetMirroredBoneName(bone.name, out origSide);
                if (mirroredName != null && origSide < 0 && armature.GetBone(mirroredName) != null)
                {
                    labels.Add("L: " + bone.name);
                }
            }
            foreach (Armature.Bone bone in armature.Bones)
            {
                int    origSide;
                string mirroredName = GetMirroredBoneName(bone.name, out origSide);
                if (mirroredName != null && origSide > 0 && armature.GetBone(mirroredName) != null)
                {
                    labels.Add("R: " + bone.name);
                }
            }

            PoseFilterDialog dialog = new PoseFilterDialog(game, "Mirror (copy) pose left/right", "Select bones to mirror pose from:", labels, false);

            if (dialog.ShowDialog(this) != DialogResult.OK)
            {
                return;
            }

            DataSet dataSet = dataSetDict[selectedItem];

            foreach (string label in labels)
            {
                if (!dialog.IsBoneSelected(label))
                {
                    continue;
                }

                string        boneNameFrom      = label.Substring(3);
                Armature.Bone boneFrom          = armature.GetBone(boneNameFrom);
                BoneTransform boneTransformFrom = dataSet.boneTransforms[boneFrom.id];

                string        boneNameTo      = GetMirroredBoneName(boneNameFrom);
                Armature.Bone boneTo          = armature.GetBone(boneNameTo);
                BoneTransform boneTransformTo = dataSet.boneTransforms[boneTo.id];

                boneTransformTo.moveX = -boneTransformFrom.moveX;
                boneTransformTo.moveY = +boneTransformFrom.moveY;
                boneTransformTo.moveZ = +boneTransformFrom.moveZ;

                boneTransformTo.rotX = +boneTransformFrom.rotX;
                boneTransformTo.rotY = -boneTransformFrom.rotY;
                boneTransformTo.rotZ = -boneTransformFrom.rotZ;

                ApplyTransformToBone(boneTo, boneTransformTo);
            }
        }
Beispiel #6
0
        private void FlipPose()
        {
            if (selectedItem == null)
            {
                return;
            }

            Armature armature = selectedItem.Model.Armature;

            List <string> labels = new List <string>();
            Dictionary <string, string> labelsToBoneNames = new Dictionary <string, string>();

            foreach (Armature.Bone bone in armature.Bones)
            {
                int    origSide;
                string flippedName = GetFlippedBoneName(bone.name, out origSide);
                if (flippedName != null && origSide < 0 && armature.GetBone(flippedName) != null)
                {
                    string combinedName = GetCombinedBoneName(bone.name);
                    labels.Add(combinedName);
                    labelsToBoneNames[combinedName] = bone.name;
                }
            }

            PoseFilterDialog dialog = new PoseFilterDialog(game, "Flip (swap) pose left/right", "Select bone pairs whose pose to flip:", labels, false);

            if (dialog.ShowDialog(this) != DialogResult.OK)
            {
                return;
            }

            DataSet dataSet = dataSetDict[selectedItem];

            foreach (string label in labels)
            {
                if (!dialog.IsBoneSelected(label))
                {
                    continue;
                }

                string        boneName1      = labelsToBoneNames[label];
                Armature.Bone bone1          = armature.GetBone(boneName1);
                BoneTransform boneTransform1 = dataSet.boneTransforms[bone1.id];

                string        boneName2      = GetFlippedBoneName(boneName1);
                Armature.Bone bone2          = armature.GetBone(boneName2);
                BoneTransform boneTransform2 = dataSet.boneTransforms[bone2.id];

                float moveX1 = boneTransform1.moveX;
                float moveY1 = boneTransform1.moveY;
                float moveZ1 = boneTransform1.moveZ;

                float rotX1 = boneTransform1.rotX;
                float rotY1 = boneTransform1.rotY;
                float rotZ1 = boneTransform1.rotZ;

                float moveX2 = boneTransform2.moveX;
                float moveY2 = boneTransform2.moveY;
                float moveZ2 = boneTransform2.moveZ;

                float rotX2 = boneTransform2.rotX;
                float rotY2 = boneTransform2.rotY;
                float rotZ2 = boneTransform2.rotZ;

                boneTransform2.moveX = -moveX1;
                boneTransform2.moveY = +moveY1;
                boneTransform2.moveZ = +moveZ1;

                boneTransform2.rotX = +rotX1;
                boneTransform2.rotY = -rotY1;
                boneTransform2.rotZ = -rotZ1;

                boneTransform1.moveX = -moveX2;
                boneTransform1.moveY = +moveY2;
                boneTransform1.moveZ = +moveZ2;

                boneTransform1.rotX = +rotX2;
                boneTransform1.rotY = -rotY2;
                boneTransform1.rotZ = -rotZ2;

                ApplyTransformToBone(bone1, boneTransform1);
                ApplyTransformToBone(bone2, boneTransform2);
            }
        }
Beispiel #7
0
        private Model LoadModel(string filenameObj, Dictionary <string, Material> materials)
        {
            StreamReader file = null;

            try {
                file = new StreamReader(filenameObj);
            }
            catch (Exception) {
                MessageBox.Show("Could not open OBJ file.",
                                "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(null);
            }
            try {
                Model          model           = new Model(game);
                List <Vector3> vertexCoords    = new List <Vector3>();
                List <Vector3> vertexNormals   = new List <Vector3>();
                List <Vector2> vertexTexCoords = new List <Vector2>();
                List <Vector4> vertexColors    = new List <Vector4>();

                List <Vertex>            vertexList = new List <Vertex>();
                Dictionary <Vertex, int> vertexDict = new Dictionary <Vertex, int>();
                List <Face> faces = new List <Face>();

                int      meshID   = 0;
                Material material = null;
                lineCounter = 0;

                while (true)
                {
                    lineCounter++;
                    string line = file.ReadLine();
                    if (line != null)
                    {
                        line = line.TrimStart();
                    }

                    if (line == null || line.StartsWith("usemtl "))
                    {
                        if (faces.Count > 0)
                        {
                            if (material == null)
                            {
                                throw new Exception(string.Format("No material assigned (line {0}).", lineCounter));
                            }
                            if (material.textureDiffuse == null)
                            {
                                throw new Exception(string.Format("No diffuse texture defined for material \"{0}\".", material.name));
                            }
                            meshID++;
                            string   meshName = string.Format("Mesh{0}", meshID);
                            MeshDesc meshDesc = BuildMeshDesc(meshName, material, vertexList, faces);
                            model.AddMeshDesc(meshDesc);
                            vertexList.Clear();
                            vertexDict.Clear();
                            faces.Clear();
                        }
                        if (line == null)
                        {
                            break;
                        }
                        string materialName = line.Substring(7).Trim();
                        material = materials[materialName];
                    }

                    if (line.StartsWith("v "))
                    {
                        Vector3 coord = ParseVertexCoord(line.Substring(2));
                        vertexCoords.Add(coord);
                        continue;
                    }
                    if (line.StartsWith("vn "))
                    {
                        Vector3 normal = ParseVertexNormal(line.Substring(3));
                        normal.Normalize();
                        vertexNormals.Add(normal);
                        continue;
                    }
                    if (line.StartsWith("vt "))
                    {
                        Vector2 texCoord = ParseVertexTexCoord(line.Substring(3));
                        vertexTexCoords.Add(texCoord);
                        continue;
                    }
                    if (line.StartsWith("vc "))
                    {
                        Vector4 color = ParseVertexColor(line.Substring(3));
                        vertexColors.Add(color);
                        continue;
                    }
                    if (line.StartsWith("f "))
                    {
                        Face face = ParseFace(line.Substring(2));
                        try {
                            for (int i = 0; i < face.vertexIndices.Length; i++)
                            {
                                Vertex vertex = new Vertex();
                                int    index;
                                index           = face.coordIndices[i];
                                vertex.coord    = index > 0 ? vertexCoords[index - 1] : vertexCoords[vertexCoords.Count + index];
                                index           = face.normalIndices[i];
                                vertex.normal   = index > 0 ? vertexNormals[index - 1] : vertexNormals[vertexNormals.Count + index];
                                index           = face.texCoordIndices[i];
                                vertex.texCoord = index > 0 ? vertexTexCoords[index - 1] : vertexTexCoords[vertexTexCoords.Count + index];
                                if (face.colorIndices != null)
                                {
                                    index        = face.colorIndices[i];
                                    vertex.color = index > 0 ? vertexColors[index - 1] : vertexColors[vertexColors.Count + index];
                                }
                                else
                                {
                                    vertex.color = new Vector4(1, 1, 1, 1);
                                }
                                if (!vertexDict.TryGetValue(vertex, out index))
                                {
                                    index = vertexList.Count;
                                    vertexList.Add(vertex);
                                    vertexDict[vertex] = index;
                                }
                                face.vertexIndices[i] = index;
                            }
                        }
                        catch (Exception) {
                            throw new Exception(string.Format("Invalid face: invalid vertex index (line {0}).", lineCounter));
                        }
                        if (face.vertexIndices.Length == 3)
                        {
                            FlipTriangularFace(face);
                            faces.Add(face);
                        }
                        else
                        {
                            Face[] triFaces = Triangulate(face);
                            FlipTriangularFace(triFaces[0]);
                            FlipTriangularFace(triFaces[1]);
                            faces.Add(triFaces[0]);
                            faces.Add(triFaces[1]);
                        }
                        continue;
                    }
                }

                Armature      armature = new Armature(model);
                Armature.Bone bone     = new Armature.Bone();
                bone.armature    = armature;
                bone.id          = 0;
                bone.name        = "root";
                bone.absPosition = Vector3.Zero;
                bone.parent      = null;
                armature.Bones   = new Armature.Bone[] { bone };
                model.Armature   = armature;

                file.Close();
                return(model);
            }
            catch (Exception ex) {
                MessageBox.Show("Could not load OBJ file.\n" + ex.Message,
                                "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(null);
            }
        }
Beispiel #8
0
        private void ProcessKeyboard(float deltaMilliseconds)
        {
            KeyboardState keyboardState = Keyboard.GetState();

            KeyboardEventHandler.ProcessKeyboardState(keyboardState);

            if (KeyboardEventHandler.HasKeyBeenPressed(Keys.NumPad0))
            {
                Armature.Bone selectedBone = controlGUI.SelectedBone;
                if (selectedBone != null)
                {
                    controlGUI.HandleBoneRotationXChanged(selectedBone, 0);
                    controlGUI.HandleBoneRotationYChanged(selectedBone, 0);
                    controlGUI.HandleBoneRotationZChanged(selectedBone, 0);
                }
            }

            bool ctrlKeyPressed = keyboardState.IsKeyDown(Keys.LeftControl) ||
                                  keyboardState.IsKeyDown(Keys.RightControl);
            bool shiftKeyPressed = keyboardState.IsKeyDown(Keys.LeftShift) ||
                                   keyboardState.IsKeyDown(Keys.RightShift);
            bool altKeyPressed = keyboardState.IsKeyDown(Keys.LeftAlt) ||
                                 keyboardState.IsKeyDown(Keys.RightAlt);

            if (keyboardState.IsKeyDown(Keys.Up) ||
                keyboardState.IsKeyDown(Keys.Down) ||
                keyboardState.IsKeyDown(Keys.Left) ||
                keyboardState.IsKeyDown(Keys.Right))
            {
                Item selectedItem = controlGUI.SelectedItem;
                if (selectedItem != null)
                {
                    Armature armature = selectedItem.Model.Armature;
                    float    speed    = shiftKeyPressed ? 3e-3f : 1e-3f;
                    if (!altKeyPressed)
                    {
                        Vector3 dir = Vector3.Zero;
                        if (keyboardState.IsKeyDown(Keys.Up))
                        {
                            dir += DetermineMovementVector(Keys.Up);
                        }
                        if (keyboardState.IsKeyDown(Keys.Down))
                        {
                            dir += DetermineMovementVector(Keys.Down);
                        }
                        if (keyboardState.IsKeyDown(Keys.Left))
                        {
                            dir += DetermineMovementVector(Keys.Left);
                        }
                        if (keyboardState.IsKeyDown(Keys.Right))
                        {
                            dir += DetermineMovementVector(Keys.Right);
                        }
                        armature.WorldTranslation += dir * speed;
                        controlGUI.HandlePositionChanged(armature.WorldTranslation);
                    }
                    else
                    {
                        float dir = 0;
                        if (keyboardState.IsKeyDown(Keys.Up))
                        {
                            dir += 1;
                        }
                        if (keyboardState.IsKeyDown(Keys.Down))
                        {
                            dir -= 1;
                        }
                        float height = armature.WorldTranslation.Y;
                        height += dir * speed;
                        controlGUI.HandleHeightChanged(height);
                    }
                }
            }

            if (KeyboardEventHandler.HasKeyBeenPressed(Keys.F2))
            {
                cameraSavedState = camera.CameraState;
                hud.Message      = "camera state saved";
            }

            if (KeyboardEventHandler.HasKeyBeenPressed(Keys.F3))
            {
                if (cameraSavedState != null)
                {
                    camera.CameraState = cameraSavedState;
                    hud.Message        = "camera state loaded";
                }
            }

            if (KeyboardEventHandler.HasKeyBeenPressed(Keys.F5))
            {
                controlGUI.QuickSaveImage();
            }

            if (KeyboardEventHandler.HasKeyBeenPressed(Keys.F8))
            {
                controlGUI.ReloadTextures();
            }

            if (KeyboardEventHandler.HasKeyBeenPressed(Keys.F12))
            {
                if (controlGUI.IsDisposed)
                {
                    controlGUI = new ControlGUI(this);
                }
                controlGUI.Show();
            }

            if (!controlGUI.IsCameraLocked)
            {
                if (KeyboardEventHandler.HasKeyBeenPressed(Keys.D1))
                {
                    controlGUI.SetCameraPivot(0);
                }
                if (KeyboardEventHandler.HasKeyBeenPressed(Keys.D2))
                {
                    controlGUI.SetCameraPivot(1);
                }
                if (KeyboardEventHandler.HasKeyBeenPressed(Keys.D3))
                {
                    controlGUI.SetCameraPivot(2);
                }
                if (KeyboardEventHandler.HasKeyBeenPressed(Keys.D4))
                {
                    controlGUI.SetCameraPivot(3);
                }
                if (KeyboardEventHandler.HasKeyBeenPressed(Keys.D5))
                {
                    controlGUI.SetCameraPivot(4);
                }
                if (KeyboardEventHandler.HasKeyBeenPressed(Keys.D6))
                {
                    controlGUI.SetCameraPivot(5);
                }
                if (KeyboardEventHandler.HasKeyBeenPressed(Keys.D7))
                {
                    controlGUI.SetCameraPivot(6);
                }
                if (KeyboardEventHandler.HasKeyBeenPressed(Keys.D8))
                {
                    controlGUI.SetCameraPivot(7);
                }
                if (KeyboardEventHandler.HasKeyBeenPressed(Keys.D9))
                {
                    controlGUI.SetCameraPivot(8);
                }
                if (KeyboardEventHandler.HasKeyBeenPressed(Keys.D0))
                {
                    controlGUI.SetCameraPivot(9);
                }
            }

            if (ctrlKeyPressed && KeyboardEventHandler.HasKeyBeenPressed(Keys.Z) ||
                KeyboardEventHandler.HasKeyBeenPressed(Keys.NumPad5))
            {
                controlGUI.Undo();
            }
        }
Beispiel #9
0
        private void ProcessMouse(float deltaMilliseconds)
        {
            MouseState mouseState = Mouse.GetState();

            KeyboardState keyboardState  = Keyboard.GetState();
            bool          axisKeyPressed = keyboardState.IsKeyDown(Keys.NumPad1) ||
                                           keyboardState.IsKeyDown(Keys.NumPad2) ||
                                           keyboardState.IsKeyDown(Keys.NumPad3) ||
                                           keyboardState.IsKeyDown(Keys.Q) ||
                                           keyboardState.IsKeyDown(Keys.W) ||
                                           keyboardState.IsKeyDown(Keys.E);
            bool ctrlKeyPressed = keyboardState.IsKeyDown(Keys.LeftControl) ||
                                  keyboardState.IsKeyDown(Keys.RightControl);
            bool shiftKeyPressed = keyboardState.IsKeyDown(Keys.LeftShift) ||
                                   keyboardState.IsKeyDown(Keys.RightShift);
            bool altKeyPressed = keyboardState.IsKeyDown(Keys.LeftAlt) ||
                                 keyboardState.IsKeyDown(Keys.RightAlt);

            if (axisKeyPressed ||
                ctrlKeyPressed ||
                shiftKeyPressed ||
                altKeyPressed)
            {
                disableBoneSelection = true;
            }
            else
            {
                if (!mouseLeftDragging)
                {
                    disableBoneSelection = false;
                }
            }

            if (mouseState.LeftButton == ButtonState.Pressed)
            {
                if (displayBones && !disableBoneSelection)
                {
                    Armature.Bone selectedBone = boneSelector.SelectedBone;
                    if (selectedBone != null)
                    {
                        controlGUI.HandleBoneSelectedIn3D(selectedBone);
                    }
                }
                if (!mouseLeftDragging)
                {
                    mouseLeftDragging = true;
                    mouseAnchor       = new Point(mouseState.X, mouseState.Y);
                }
            }
            else
            {
                mouseLeftDragging    = false;
                disableBoneSelection = false;

                if (boneTransformActive)
                {
                    boneTransformActive = false;
                    controlGUI.UndoSaveInterrupt();
                }
            }

            if (mouseState.RightButton == ButtonState.Pressed)
            {
                if (!mouseRightDragging)
                {
                    mouseRightDragging = true;
                    mouseAnchor        = new Point(mouseState.X, mouseState.Y);
                }
            }
            else
            {
                mouseRightDragging = false;
            }

            if (mouseLeftDragging && mouseRightDragging)
            {
                int dx = mouseState.X - mouseAnchor.X;
                int dy = mouseState.Y - mouseAnchor.Y;
                mouseAnchor = new Point(mouseState.X, mouseState.Y);

                if (!controlGUI.IsCameraLocked)
                {
                    camera.Target -= (camera.Left * dx - camera.Up * dy) * 0.005f;
                }
                return;
            }

            if (mouseLeftDragging)
            {
                int dx = mouseState.X - mouseAnchor.X;
                int dy = mouseState.Y - mouseAnchor.Y;
                mouseAnchor = new Point(mouseState.X, mouseState.Y);

                if (axisKeyPressed)
                {
                    Armature.Bone selectedBone = controlGUI.SelectedBone;
                    if (selectedBone != null)
                    {
                        boneTransformActive = true;

                        if (keyboardState.IsKeyDown(Keys.NumPad1) ||
                            keyboardState.IsKeyDown(Keys.Q))
                        {
                            float angle = controlGUI.GetBoneRotationX(selectedBone);
                            controlGUI.HandleBoneRotationXChanged(selectedBone, angle + (dx + dy) * 0.5f);
                        }
                        if (keyboardState.IsKeyDown(Keys.NumPad2) ||
                            keyboardState.IsKeyDown(Keys.W))
                        {
                            float angle = controlGUI.GetBoneRotationY(selectedBone);
                            controlGUI.HandleBoneRotationYChanged(selectedBone, angle + (dx + dy) * 0.5f);
                        }
                        if (keyboardState.IsKeyDown(Keys.NumPad3) ||
                            keyboardState.IsKeyDown(Keys.E))
                        {
                            float angle = controlGUI.GetBoneRotationZ(selectedBone);
                            controlGUI.HandleBoneRotationZChanged(selectedBone, angle + (dx + dy) * 0.5f);
                        }
                    }
                    return;
                }

                if (ctrlKeyPressed)
                {
                    Item selectedItem = controlGUI.SelectedItem;
                    if (selectedItem != null)
                    {
                        Vector3 point;
                        if (ProjectMouseToGround(mouseState.X, mouseState.Y, out point))
                        {
                            Armature armature = selectedItem.Model.Armature;
                            float    height   = armature.WorldTranslation.Y;
                            armature.WorldTranslation = new Vector3(point.X, height, point.Z);
                            controlGUI.HandlePositionChanged(armature.WorldTranslation);
                        }
                    }
                    return;
                }

                if (!controlGUI.IsCameraLocked)
                {
                    if (shiftKeyPressed)
                    {
                        camera.Target -= (camera.Left * dx - camera.Up * dy) * 0.005f;
                        return;
                    }
                    float rotationHorizontal = camera.RotationHorizontal - dx * 0.01f;
                    float rotationVertical   = camera.RotationVertical + dy * 0.01f;
                    camera.SetRotation(rotationHorizontal, rotationVertical);
                }
            }

            if (mouseRightDragging)
            {
                if (!controlGUI.IsCameraLocked)
                {
                    int dy = mouseState.Y - mouseAnchor.Y;
                    mouseAnchor = new Point(mouseState.X, mouseState.Y);
                    if (!shiftKeyPressed)
                    {
                        camera.Distance += dy * 0.01f;
                    }
                    else
                    {
                        camera.Target -= camera.Forward * dy * 0.005f;
                    }
                }
            }

            if (!mouseLeftDragging && !mouseRightDragging)
            {
                boneSelector.HandleMouseMoved(mouseState.X, mouseState.Y);
            }

            if (mouseWheel != mouseState.ScrollWheelValue)
            {
                if (!controlGUI.IsCameraLocked)
                {
                    int delta = mouseWheel - mouseState.ScrollWheelValue;
                    camera.Distance += delta * 0.002f;
                }
                mouseWheel = mouseState.ScrollWheelValue;
            }
        }
Beispiel #10
0
        private bool LoadItems(BinaryReader file, ushort versionMajor, ushort versionMinor, bool loadScene)
        {
            // items
            int itemCount = file.ReadInt32();

            for (int itemID = 0; itemID < itemCount; itemID++)
            {
                // create item
                string itemTypeName = file.ReadString();
                string dirName;
                if (versionMajor >= 1 && versionMinor >= 5)
                {
                    dirName = file.ReadString();
                }
                else
                {
                    dirName = itemTypeName.ToLower();
                }
                bool    isVisible = file.ReadBoolean();
                Vector3 itemScale;
                if (versionMajor >= 1 && versionMinor >= 8)
                {
                    float itemScaleX = file.ReadSingle();
                    float itemScaleY = file.ReadSingle();
                    float itemScaleZ = file.ReadSingle();
                    itemScale = new Vector3(itemScaleX, itemScaleY, itemScaleZ);
                }
                else
                {
                    float itemScaleXYZ = file.ReadSingle();
                    itemScale = new Vector3(itemScaleXYZ, itemScaleXYZ, itemScaleXYZ);
                }

                ItemType itemType = ParseItemType(itemTypeName);
                Item     item     = ItemFactory.GetItem(game, itemType);
                if (!dirName.ToLower().StartsWith("data\\"))
                {
                    dirName = "data\\" + dirName;
                }
                if (!item.LoadAndInitModel(itemType, dirName))
                {
                    return(false);
                }
                item.Model.IsVisible = isVisible;

                if (loadScene)
                {
                    AddItem(item);
                }

                // pose
                Armature armature = item.Model.Armature;
                foreach (Armature.Bone bone in armature.Bones)
                {
                    float rotX  = file.ReadSingle();
                    float rotY  = file.ReadSingle();
                    float rotZ  = file.ReadSingle();
                    float moveX = 0;
                    float moveY = 0;
                    float moveZ = 0;
                    if (versionMajor >= 1 && versionMinor >= 3)
                    {
                        moveX = file.ReadSingle();
                        moveY = file.ReadSingle();
                        moveZ = file.ReadSingle();
                    }

                    if (loadScene)
                    {
                        DataSet       dataSet       = dataSetDict[item];
                        BoneTransform boneTransform = dataSet.boneTransforms[bone.id];
                        boneTransform.rotX  = rotX;
                        boneTransform.rotY  = rotY;
                        boneTransform.rotZ  = rotZ;
                        boneTransform.moveX = moveX;
                        boneTransform.moveY = moveY;
                        boneTransform.moveZ = moveZ;
                        ApplyTransformToBone(bone, boneTransform);
                    }
                }

                // position
                Vector3 position = new Vector3();
                position.X = file.ReadSingle();
                position.Y = file.ReadSingle();
                position.Z = file.ReadSingle();

                if (loadScene)
                {
                    armature.WorldScale = itemScale;
                    HandleScaleChangedInGUI(itemScale);
                    armature.WorldTranslation = position;
                    HandleHeightChanged(position.Y);
                }

                // accessories
                ImportModelParams(file, loadScene ? item.Model : null);
                if (loadScene)
                {
                    UpdateAccessoriesCheckboxes(item.Model);
                }

                // glow colors
                if (versionMajor >= 1 && versionMinor >= 11)
                {
                    float r, g, b;
                    r = file.ReadSingle();
                    g = file.ReadSingle();
                    b = file.ReadSingle();
                    item.ColorGlowLeft = new Vector4(r, g, b, 1);
                    r = file.ReadSingle();
                    g = file.ReadSingle();
                    b = file.ReadSingle();
                    item.ColorGlowRight = new Vector4(r, g, b, 1);
                }
            }

            return(true);
        }
Beispiel #11
0
        private void SaveScene(string filename)
        {
            BinaryWriter file;

            try {
                file = new BinaryWriter(new FileStream(filename, FileMode.Create));
            }
            catch (Exception) {
                MessageBox.Show(this, "Could not save scene.",
                                "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            // file format version
            file.Write((ushort)1);  // major
            file.Write((ushort)11); // minor

            // items
            file.Write(items.Count);
            foreach (Item item in items)
            {
                // type
                file.Write(item.Type.ToString());
                // directory name
                file.Write(item.DirName);
                // visibility
                file.Write(item.Model.IsVisible);
                // scale
                file.Write(item.Model.Armature.WorldScale.X);
                file.Write(item.Model.Armature.WorldScale.Y);
                file.Write(item.Model.Armature.WorldScale.Z);

                // pose
                Armature armature = item.Model.Armature;
                DataSet  dataSet  = dataSetDict[item];
                foreach (Armature.Bone bone in armature.Bones)
                {
                    BoneTransform boneTransform = dataSet.boneTransforms[bone.id];
                    file.Write(boneTransform.rotX);
                    file.Write(boneTransform.rotY);
                    file.Write(boneTransform.rotZ);
                    file.Write(boneTransform.moveX);
                    file.Write(boneTransform.moveY);
                    file.Write(boneTransform.moveZ);
                }

                // position
                Vector3 position = armature.WorldTranslation;
                file.Write(position.X);
                file.Write(position.Y);
                file.Write(position.Z);

                // accessories
                ExportModelParams(file, item.Model);

                // glow colors
                file.Write(item.ColorGlowLeft.X);
                file.Write(item.ColorGlowLeft.Y);
                file.Write(item.ColorGlowLeft.Z);
                file.Write(item.ColorGlowRight.X);
                file.Write(item.ColorGlowRight.Y);
                file.Write(item.ColorGlowRight.Z);
            }

            // camera
            CameraTurnTable camera = game.Camera;

            file.Write(camera.FieldOfViewHorizontal);
            file.Write(camera.Target.X);
            file.Write(camera.Target.Y);
            file.Write(camera.Target.Z);
            file.Write(camera.Distance);
            file.Write(camera.RotationHorizontal);
            file.Write(camera.RotationVertical);

            // light 1
            Vector3 light1Dir = game.Light1Direction;

            file.Write(light1Dir.X);
            file.Write(light1Dir.Y);
            file.Write(light1Dir.Z);
            file.Write(game.Light1Intensity);
            Color light1Color = game.Light1Color;

            file.Write(light1Color.R);
            file.Write(light1Color.G);
            file.Write(light1Color.B);
            file.Write(game.Light1ShadowDepth);

            // light 2
            Vector3 light2Dir = game.Light2Direction;

            file.Write(light2Dir.X);
            file.Write(light2Dir.Y);
            file.Write(light2Dir.Z);
            file.Write(game.Light2Intensity);
            Color light2Color = game.Light2Color;

            file.Write(light2Color.R);
            file.Write(light2Color.G);
            file.Write(light2Color.B);
            file.Write(game.Light2ShadowDepth);

            // light 3
            Vector3 light3Dir = game.Light3Direction;

            file.Write(light3Dir.X);
            file.Write(light3Dir.Y);
            file.Write(light3Dir.Z);
            file.Write(game.Light3Intensity);
            Color light3Color = game.Light3Color;

            file.Write(light3Color.R);
            file.Write(light3Color.G);
            file.Write(light3Color.B);
            file.Write(game.Light3ShadowDepth);

            // post-processing params
            file.Write(game.PostProcessBrightness);
            file.Write(game.PostProcessGamma);
            file.Write(game.PostProcessContrast);
            file.Write(game.PostProcessSaturation);

            // ground texture
            file.Write(game.DisplayGround);
            file.Write("data\\ground.png");

            // background color
            file.Write(game.BackgroundColor.R);
            file.Write(game.BackgroundColor.G);
            file.Write(game.BackgroundColor.B);

            // background image
            file.Write("");

            // skydome
            file.Write(game.DisplaySkyDome);
            file.Write(ItemType.SkyDome_Thailand_Sea.ToString());
            file.Write(game.SkyDomeRotation);
            file.Write(game.SkyDomeElevation);

            // canvas size
            file.Write(game.GameForm.WindowState == FormWindowState.Maximized);
            file.Write(game.GameForm.ClientSize.Width);
            file.Write(game.GameForm.ClientSize.Height);

            file.Close();
        }