Exemple #1
0
        public bool UpdateInput()
        {
            bool  bCameraUpdated = false;
            float Multiplier     = ToolkitSettings.CameraSpeed;

            if (Input.IsKeyDown(Keys.ShiftKey))
            {
                Multiplier *= 2.0f;
            }

            float speed = Profile.DeltaTime * Multiplier;

            if (Input.IsKeyDown(Keys.A))
            {
                Camera.Position -= Vector3Extenders.FromVector4(Vector4.Multiply(Camera.ViewMatrix.Column1, speed));
                bCameraUpdated   = true;
            }

            if (Input.IsKeyDown(Keys.D))
            {
                Camera.Position += Vector3Extenders.FromVector4(Vector4.Multiply(Camera.ViewMatrix.Column1, speed));
                bCameraUpdated   = true;
            }

            if (Input.IsKeyDown(Keys.W))
            {
                Camera.Position -= Vector3Extenders.FromVector4(Vector4.Multiply(Camera.ViewMatrix.Column3, speed));
                bCameraUpdated   = true;
            }

            if (Input.IsKeyDown(Keys.S))
            {
                Camera.Position += Vector3Extenders.FromVector4(Vector4.Multiply(Camera.ViewMatrix.Column3, speed));
                bCameraUpdated   = true;
            }

            if (Input.IsKeyDown(Keys.Q))
            {
                Camera.Position.Z += speed;
                bCameraUpdated     = true;
            }

            if (Input.IsKeyDown(Keys.E))
            {
                Camera.Position.Z -= speed;
                bCameraUpdated     = true;
            }

            return(bCameraUpdated);
        }
        /// <summary>
        /// Build Lods from retrieved data.
        /// </summary>
        public void BuildLods(FrameGeometry frameGeometry, FrameMaterial frameMaterial, VertexBuffer[] vertexBuffers, IndexBuffer[] indexBuffers)
        {
            lods = new Lod[frameGeometry.NumLods];
            for (int i = 0; i != lods.Length; i++)
            {
                FrameLOD frameLod = frameGeometry.LOD[i];
                lods[i] = new Lod
                {
                    VertexDeclaration = frameGeometry.LOD[i].VertexDeclaration
                };
                IndexBuffer  indexBuffer  = indexBuffers[i];
                VertexBuffer vertexBuffer = vertexBuffers[i];

                int vertexSize;
                Dictionary <VertexFlags, FrameLOD.VertexOffset> vertexOffsets = frameLod.GetVertexOffsets(out vertexSize);
                lods[i].Vertices = new Vertex[frameLod.NumVertsPr];

                if (vertexSize * frameLod.NumVertsPr != vertexBuffer.Data.Length)
                {
                    throw new System.Exception();
                }
                for (int v = 0; v != lods[i].Vertices.Length; v++)
                {
                    Vertex vertex = new Vertex();
                    vertex.UVs = new Half2[4];

                    if (lods[i].VertexDeclaration.HasFlag(VertexFlags.Position))
                    {
                        int startIndex = v * vertexSize + vertexOffsets[VertexFlags.Position].Offset;
                        var output     = VertexTranslator.ReadPositionDataFromVB(vertexBuffer.Data, startIndex, frameGeometry.DecompressionFactor, frameGeometry.DecompressionOffset);
                        vertex.Position = Vector3Extenders.FromVector4(output);
                        vertex.Binormal = new Vector3(output.X);
                    }

                    if (lods[i].VertexDeclaration.HasFlag(VertexFlags.Tangent))
                    {
                        int startIndex = v * vertexSize + vertexOffsets[VertexFlags.Position].Offset;
                        vertex.Tangent = VertexTranslator.ReadTangentDataFromVB(vertexBuffer.Data, startIndex);
                    }

                    if (lods[i].VertexDeclaration.HasFlag(VertexFlags.Normals))
                    {
                        int startIndex = v * vertexSize + vertexOffsets[VertexFlags.Normals].Offset;
                        vertex.Normal = VertexTranslator.ReadNormalDataFromVB(vertexBuffer.Data, startIndex);
                    }

                    if (lods[i].VertexDeclaration.HasFlag(VertexFlags.Skin))
                    {
                        int startIndex = v * vertexSize + vertexOffsets[VertexFlags.Skin].Offset;
                        vertex.BoneWeights = VertexTranslator.ReadWeightsFromVB(vertexBuffer.Data, startIndex);
                        vertex.BoneIDs     = VertexTranslator.ReadBonesFromVB(vertexBuffer.Data, startIndex + 4);
                    }

                    if (lods[i].VertexDeclaration.HasFlag(VertexFlags.Color))
                    {
                        int startIndex = v * vertexSize + vertexOffsets[VertexFlags.Color].Offset;
                        vertex.Color0 = VertexTranslator.ReadColorFromVB(vertexBuffer.Data, startIndex);
                    }

                    if (lods[i].VertexDeclaration.HasFlag(VertexFlags.Color1))
                    {
                        int startIndex = v * vertexSize + vertexOffsets[VertexFlags.Color1].Offset;
                        vertex.Color1 = VertexTranslator.ReadColorFromVB(vertexBuffer.Data, startIndex);
                    }

                    if (lods[i].VertexDeclaration.HasFlag(VertexFlags.TexCoords0))
                    {
                        int startIndex = v * vertexSize + vertexOffsets[VertexFlags.TexCoords0].Offset;
                        vertex.UVs[0] = VertexTranslator.ReadTexcoordFromVB(vertexBuffer.Data, startIndex);
                    }

                    if (lods[i].VertexDeclaration.HasFlag(VertexFlags.TexCoords1))
                    {
                        int startIndex = v * vertexSize + vertexOffsets[VertexFlags.TexCoords1].Offset;
                        vertex.UVs[1] = VertexTranslator.ReadTexcoordFromVB(vertexBuffer.Data, startIndex);
                    }

                    if (lods[i].VertexDeclaration.HasFlag(VertexFlags.TexCoords2))
                    {
                        int startIndex = v * vertexSize + vertexOffsets[VertexFlags.TexCoords2].Offset;
                        vertex.UVs[2] = VertexTranslator.ReadTexcoordFromVB(vertexBuffer.Data, startIndex);
                    }

                    if (lods[i].VertexDeclaration.HasFlag(VertexFlags.ShadowTexture))
                    {
                        int startIndex = v * vertexSize + vertexOffsets[VertexFlags.ShadowTexture].Offset;
                        vertex.UVs[3] = VertexTranslator.ReadTexcoordFromVB(vertexBuffer.Data, startIndex);
                    }

                    if (lods[i].VertexDeclaration.HasFlag(VertexFlags.BBCoeffs))
                    {
                        int startIndex = v * vertexSize + vertexOffsets[VertexFlags.BBCoeffs].Offset;
                        vertex.BBCoeffs = VertexTranslator.ReadBBCoeffsVB(vertexBuffer.Data, startIndex);
                    }

                    if (lods[i].VertexDeclaration.HasFlag(VertexFlags.DamageGroup))
                    {
                        int startIndex = v * vertexSize + vertexOffsets[VertexFlags.DamageGroup].Offset;
                        vertex.DamageGroup = VertexTranslator.ReadDamageGroupFromVB(vertexBuffer.Data, startIndex);
                    }

                    lods[i].Vertices[v] = vertex;
                }

                lods[i].Indices = indexBuffer.Data;
                MaterialStruct[] materials = frameMaterial.Materials[i];
                lods[i].Parts = new ModelPart[materials.Length];
                for (int x = 0; x != materials.Length; x++)
                {
                    if (string.IsNullOrEmpty(materials[x].MaterialName))
                    {
                        var material = MaterialsManager.LookupMaterialByHash(materials[x].MaterialHash);
                        materials[x].MaterialName = material.MaterialName;
                    }

                    ModelPart modelPart = new ModelPart();
                    modelPart.Material   = materials[x].MaterialName;
                    modelPart.StartIndex = (uint)materials[x].StartIndex;
                    modelPart.NumFaces   = (uint)materials[x].NumFaces;
                    lods[i].Parts[x]     = modelPart;
                }
            }
        }
        /*
         * Position.X = 2 Bytes / Half;
         * Position.Y = 2 Bytes / Half;
         * Position.Z = 2 Bytes / Half;
         * Position.W = 2 Bytes / Half;
         *  Also
         *      Tangent.X = 1 Bytes (1st Bytes of Position.W)
         *      Tangent.Y = 1 Bytes (2nd Bytes of Position.W)
         * Normal.X = 1 Bytes / Byte.
         * Normal.Y = 1 Bytes / Byte.
         * Normal.Z = 1 Bytes / Byte.
         * Normal.W = 1 Bytes / Bytes.
         *
         *  Also
         *      Tangent.W = 1 Bytes (1st Bytes of Normal.W)
         *
         *
         * */
        public static Vertex DecompressVertex(byte[] data, VertexFlags declaration, Vector3 offset, float scale, Dictionary <VertexFlags, ResourceTypes.FrameResource.FrameLOD.VertexOffset> offsets)
        {
            Vertex vertex = new Vertex();

            if (declaration.HasFlag(VertexFlags.Position))
            {
                int startIndex = offsets[VertexFlags.Position].Offset;
                var output     = ReadPositionDataFromVB(data, startIndex, scale, offset);
                vertex.Position = Vector3Extenders.FromVector4(output);
                vertex.Binormal = new Vector3(output.W);
            }

            if (declaration.HasFlag(VertexFlags.Tangent))
            {
                int startIndex = offsets[VertexFlags.Position].Offset;
                vertex.Tangent = ReadTangentDataFromVB(data, startIndex);
            }

            if (declaration.HasFlag(VertexFlags.Normals))
            {
                int startIndex = offsets[VertexFlags.Normals].Offset;
                vertex.Normal = ReadNormalDataFromVB(data, startIndex);
            }

            if (declaration.HasFlag(VertexFlags.Skin))
            {
                int startIndex = offsets[VertexFlags.Skin].Offset;
                vertex.BoneWeights = ReadWeightsFromVB(data, startIndex);
                vertex.BoneIDs     = ReadBonesFromVB(data, startIndex + 4);
            }

            if (declaration.HasFlag(VertexFlags.Color))
            {
                int startIndex = offsets[VertexFlags.Color].Offset;
                vertex.Color0 = ReadColorFromVB(data, startIndex);
            }

            if (declaration.HasFlag(VertexFlags.Color1))
            {
                int startIndex = offsets[VertexFlags.Color1].Offset;
                vertex.Color1 = ReadColorFromVB(data, startIndex);
            }

            if (declaration.HasFlag(VertexFlags.TexCoords0))
            {
                int startIndex = offsets[VertexFlags.TexCoords0].Offset;
                vertex.UVs[0] = ReadTexcoordFromVB(data, startIndex);
            }

            if (declaration.HasFlag(VertexFlags.TexCoords1))
            {
                int startIndex = offsets[VertexFlags.TexCoords1].Offset;
                vertex.UVs[1] = ReadTexcoordFromVB(data, startIndex);
            }

            if (declaration.HasFlag(VertexFlags.TexCoords2))
            {
                int startIndex = offsets[VertexFlags.TexCoords2].Offset;
                vertex.UVs[2] = ReadTexcoordFromVB(data, startIndex);
            }

            if (declaration.HasFlag(VertexFlags.ShadowTexture))
            {
                int startIndex = offsets[VertexFlags.ShadowTexture].Offset;
                vertex.UVs[3] = ReadTexcoordFromVB(data, startIndex);
            }

            if (declaration.HasFlag(VertexFlags.BBCoeffs))
            {
                int startIndex = offsets[VertexFlags.BBCoeffs].Offset;
                vertex.BBCoeffs = ReadBBCoeffsVB(data, startIndex);
            }

            if (declaration.HasFlag(VertexFlags.DamageGroup))
            {
                int startIndex = offsets[VertexFlags.DamageGroup].Offset;
                vertex.DamageGroup = ReadDamageGroupFromVB(data, startIndex);
            }

            // We only try to calculate binormal vector if we have the correct tangent space data so far..
            if (declaration.HasFlag(VertexFlags.Normals) && declaration.HasFlag(VertexFlags.Tangent))
            {
                Vector4 positionW = new Vector4(vertex.Position, vertex.Binormal.X);
                vertex.Binormal = CalculateBinormal(positionW, vertex.Tangent, vertex.Normal);
            }
            return(vertex);
        }
        public bool ConvertFrameToRenderModel(FrameObjectSingleMesh mesh, FrameGeometry geom, FrameMaterial mats, IndexBuffer[] indexBuffers, VertexBuffer[] vertexBuffers)
        {
            if (mesh == null || geom == null || mats == null || indexBuffers[0] == null || vertexBuffers[0] == null)
            {
                return(false);
            }

            aoHash = mesh.OMTextureHash;
            SetTransform(mesh.WorldTransform);
            //DoRender = (mesh.SecondaryFlags == 4097 ? true : false);
            BoundingBox = new RenderBoundingBox();
            BoundingBox.Init(mesh.Boundings);
            BoundingBox.SetTransform(Transform);
            BoundingBox.DoRender = false;
            LODs = new LOD[geom.NumLods];

            for (int i = 0; i != geom.NumLods; i++)
            {
                LOD lod = new LOD();
                lod.Indices    = indexBuffers[i].Data;
                lod.ModelParts = new ModelPart[mats.LodMatCount[i]];

                for (int z = 0; z != mats.Materials[i].Length; z++)
                {
                    lod.ModelParts[z]              = new ModelPart();
                    lod.ModelParts[z].NumFaces     = (uint)mats.Materials[i][z].NumFaces;
                    lod.ModelParts[z].StartIndex   = (uint)mats.Materials[i][z].StartIndex;
                    lod.ModelParts[z].MaterialHash = mats.Materials[i][z].MaterialHash;
                    lod.ModelParts[z].Material     = MaterialsManager.LookupMaterialByHash(lod.ModelParts[z].MaterialHash);
                }

                lod.Vertices = new VertexLayouts.NormalLayout.Vertex[geom.LOD[i].NumVertsPr];
                int vertexSize;
                Dictionary <VertexFlags, FrameLOD.VertexOffset> vertexOffsets = geom.LOD[i].GetVertexOffsets(out vertexSize);
                try
                {
                    for (int x = 0; x != lod.Vertices.Length; x++)
                    {
                        VertexLayouts.NormalLayout.Vertex vertex = new VertexLayouts.NormalLayout.Vertex();

                        if (geom.LOD[i].VertexDeclaration.HasFlag(VertexFlags.Position))
                        {
                            int startIndex = x * vertexSize + vertexOffsets[VertexFlags.Position].Offset;
                            var output     = VertexTranslator.ReadPositionDataFromVB(vertexBuffers[i].Data, startIndex, geom.DecompressionFactor, geom.DecompressionOffset);
                            vertex.Position = Vector3Extenders.FromVector4(output);
                            //vertex.Binormal = new Vector3(output.X); this is for later
                        }

                        if (geom.LOD[i].VertexDeclaration.HasFlag(VertexFlags.Tangent))
                        {
                            int startIndex = x * vertexSize + vertexOffsets[VertexFlags.Position].Offset;
                            vertex.Tangent = VertexTranslator.ReadTangentDataFromVB(vertexBuffers[i].Data, startIndex);
                        }

                        if (geom.LOD[i].VertexDeclaration.HasFlag(VertexFlags.Normals))
                        {
                            int startIndex = x * vertexSize + vertexOffsets[VertexFlags.Normals].Offset;
                            vertex.Normal = VertexTranslator.ReadNormalDataFromVB(vertexBuffers[i].Data, startIndex);
                        }

                        if (geom.LOD[i].VertexDeclaration.HasFlag(VertexFlags.Skin))
                        {
                            //int startIndex = v * vertexSize + vertexOffsets[VertexFlags.BlendData].Offset;
                            // vertex.BlendWeight = VertexTranslator.ReadBlendWeightFromVB(vertexBuffer.Data, startIndex);
                            // vertex.BoneID = VertexTranslator.ReadBlendIDFromVB(vertexBuffer.Data, startIndex);
                        }

                        if (geom.LOD[i].VertexDeclaration.HasFlag(VertexFlags.Color))
                        {
                            //unknown
                        }

                        if (geom.LOD[i].VertexDeclaration.HasFlag(VertexFlags.TexCoords0))
                        {
                            int startIndex = x * vertexSize + vertexOffsets[VertexFlags.TexCoords0].Offset;
                            vertex.TexCoord0 = VertexTranslator.ReadTexcoordFromVB(vertexBuffers[i].Data, startIndex);
                        }

                        if (geom.LOD[i].VertexDeclaration.HasFlag(VertexFlags.TexCoords1))
                        {
                            //int startIndex = v * vertexSize + vertexOffsets[VertexFlags.TexCoords1].Offset;
                            //vertex.UVs[1] = VertexTranslator.ReadTexcoordFromVB(vertexBuffer.Data, startIndex);
                        }

                        if (geom.LOD[i].VertexDeclaration.HasFlag(VertexFlags.TexCoords2))
                        {
                            //int startIndex = v * vertexSize + vertexOffsets[VertexFlags.TexCoords2].Offset;
                            //vertex.UVs[2] = VertexTranslator.ReadTexcoordFromVB(vertexBuffer.Data, startIndex);
                        }

                        if (geom.LOD[i].VertexDeclaration.HasFlag(VertexFlags.ShadowTexture))
                        {
                            int startIndex = x * vertexSize + vertexOffsets[VertexFlags.ShadowTexture].Offset;
                            vertex.TexCoord7 = VertexTranslator.ReadTexcoordFromVB(vertexBuffers[i].Data, startIndex);
                        }

                        if (geom.LOD[i].VertexDeclaration.HasFlag(VertexFlags.Color1))
                        {
                            //unknown
                        }

                        if (geom.LOD[i].VertexDeclaration.HasFlag(VertexFlags.BBCoeffs))
                        {
                            //unknown
                        }

                        if (geom.LOD[i].VertexDeclaration.HasFlag(VertexFlags.DamageGroup))
                        {
                            //int startIndex = v * vertexSize + vertexOffsets[VertexFlags.DamageGroup].Offset;
                            //vertex.DamageGroup = VertexTranslator.ReadDamageGroupFromVB(vertexBuffer.Data, startIndex);
                        }

                        lod.Vertices[x] = vertex;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(string.Format("Error when creating renderable {1}!: \n{0}", ex.Message, mesh.Name.ToString()), "Toolkit");
                    return(false);
                }
                LODs[i] = lod;
            }

            SetupShaders();
            return(true);
        }