Esempio n. 1
0
        private void RenderMesh(int meshIndex, int diffuseSlot, int normalSlot, int specularSlot)
        {
            if (meshIndex < 0 || meshIndex >= this.Meshes.Count)
            {
                return;
            }

            SdkMeshMesh mesh = this.Meshes[meshIndex];

            if (mesh.VertexBuffers.Length > D3D11Constants.InputAssemblerVertexInputResourceSlotCount)
            {
                return;
            }

            D3D11Buffer[] vb      = new D3D11Buffer[mesh.VertexBuffers.Length];
            uint[]        strides = new uint[mesh.VertexBuffers.Length];
            uint[]        offsets = new uint[mesh.VertexBuffers.Length];

            for (int i = 0; i < mesh.VertexBuffers.Length; i++)
            {
                vb[i]      = mesh.VertexBuffers[i].Buffer;
                strides[i] = mesh.VertexBuffers[i].StrideBytes;
                offsets[i] = 0;
            }

            D3D11Buffer ib       = mesh.IndexBuffer.Buffer;
            DxgiFormat  ibFormat = mesh.IndexBuffer.IndexFormat;

            this._d3dDeviceContext.InputAssemblerSetVertexBuffers(0, vb, strides, offsets);
            this._d3dDeviceContext.InputAssemblerSetIndexBuffer(ib, ibFormat, 0);


            foreach (SdkMeshSubset subset in mesh.Subsets)
            {
                this._d3dDeviceContext.InputAssemblerSetPrimitiveTopology(subset.PrimitiveTopology);

                SdkMeshMaterial material = this.Materials[subset.MaterialIndex];

                if (diffuseSlot != -1 && material.DiffuseTextureView != null)
                {
                    this._d3dDeviceContext.PixelShaderSetShaderResources((uint)diffuseSlot, new[] { material.DiffuseTextureView });
                }

                if (normalSlot != -1 && material.NormalTextureView != null)
                {
                    this._d3dDeviceContext.PixelShaderSetShaderResources((uint)normalSlot, new[] { material.NormalTextureView });
                }

                if (specularSlot != -1 && material.SpecularTextureView != null)
                {
                    this._d3dDeviceContext.PixelShaderSetShaderResources((uint)specularSlot, new[] { material.SpecularTextureView });
                }

                this._d3dDeviceContext.DrawIndexed((uint)subset.IndexCount, (uint)subset.IndexStart, subset.VertexStart);
            }
        }
Esempio n. 2
0
        public static SdkMeshFile FromFile(D3D11Device device, D3D11DeviceContext deviceContext, string fileName)
        {
            if (device == null)
            {
                throw new ArgumentNullException(nameof(device));
            }

            if (deviceContext == null)
            {
                throw new ArgumentNullException(nameof(deviceContext));
            }

            if (string.IsNullOrEmpty(fileName))
            {
                throw new ArgumentNullException(nameof(fileName));
            }

            var file = new SdkMeshFile
            {
                _d3dDevice        = device,
                _d3dDeviceContext = deviceContext,
                FilePath          = fileName,
                FileDirectory     = Path.GetDirectoryName(fileName)
            };

            SdkMeshRawFile rawFile = SdkMeshRawFile.FromFile(file.FilePath);

            foreach (SdkMeshRawMaterial rawMaterial in rawFile.Materials)
            {
                var material = new SdkMeshMaterial(file._d3dDevice, file._d3dDeviceContext, file.FileDirectory, rawMaterial);
                file.Materials.Add(material);
            }

            foreach (SdkMeshRawMesh rawMesh in rawFile.Meshes)
            {
                var mesh = new SdkMeshMesh(file._d3dDevice, rawFile, rawMesh);
                file.Meshes.Add(mesh);
            }

            foreach (SdkMeshRawFrame rawFrame in rawFile.Frames)
            {
                var frame = new SdkMeshFrame(rawFrame);
                file.Frames.Add(frame);
            }

            string animFilePath = file.FilePath + "_anim";

            if (File.Exists(animFilePath))
            {
                SdkMeshRawAnimFile rawAnimFile = SdkMeshRawAnimFile.FromFile(animFilePath);

                file.AnimationFrameTransformType = rawAnimFile.Header.FrameTransformType;
                file.AnimationKeysCount          = rawAnimFile.Header.NumAnimationKeys;
                file.AnimationFPS = rawAnimFile.Header.AnimationFPS;

                foreach (SdkMeshRawAnimFrameData rawFrame in rawAnimFile.AnimationFrames)
                {
                    var frame = new SdkMeshAnimationFrame(rawFrame);
                    file.AnimationFrames.Add(frame);
                }

                for (int index = 0; index < file.AnimationFrames.Count; index++)
                {
                    SdkMeshFrame frame = file.FindFrame(file.AnimationFrames[index].FrameName);

                    if (frame != null)
                    {
                        frame.UpdateAnimationFrameIndex(index);
                    }
                }
            }

            return(file);
        }