private bool SetShaderParameters(DeviceContext deviceContext, Matrix worldMatrix, Matrix viewMatrix, Matrix projectionMatrix, ShaderResourceView[] textures, float translation)
        {
            try
            {
                #region Set Matrix Shader Resources
                // Transpose the matrices to prepare them for shader.
                worldMatrix.Transpose();
                viewMatrix.Transpose();
                projectionMatrix.Transpose();

                // Lock the constant buffer so it can be written to.
                DataStream mappedResource;
                deviceContext.MapSubresource(ConstantMatrixBuffer, MapMode.WriteDiscard, SharpDX.Direct3D11.MapFlags.None, out mappedResource);

                // Copy the matrices into the constant buffer.
                var matrixBuffer = new MatrixBuffer()
                {
                    world = worldMatrix,
                    view = viewMatrix,
                    projection = projectionMatrix
                };

                mappedResource.Write(matrixBuffer);

                // Unlock the constant buffer.
                deviceContext.UnmapSubresource(ConstantMatrixBuffer, 0);

                // Set the position of the constant buffer in the vertex shader.
                var bufferNumber = 0;

                // Finally set the constant buffer in the vertex shader with the updated values.
                deviceContext.VertexShader.SetConstantBuffer(bufferNumber, ConstantMatrixBuffer);

                // Set shader resource in the pixel shader.
                deviceContext.PixelShader.SetShaderResources(0, textures);
                #endregion

                #region Set Translate Shader Resources
                // Lock the constant buffer so it can be written to.
                deviceContext.MapSubresource(ConstantTranslateBuffer, MapMode.WriteDiscard, SharpDX.Direct3D11.MapFlags.None, out mappedResource);

                // Copy the matrices into the constant buffer.
                var TranslateBuffer = new TranslateBuffer()
                {
                    translation = translation
                };

                mappedResource.Write(TranslateBuffer);

                // Unlock the constant buffer.
                deviceContext.UnmapSubresource(ConstantTranslateBuffer, 0);

                // Set the position of the constant buffer in the vertex shader.
                bufferNumber = 0;

                // Finally set the constant buffer in the vertex shader with the updated values.
                deviceContext.PixelShader.SetConstantBuffer(bufferNumber, ConstantTranslateBuffer);
                #endregion

                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
Example #2
0
        private bool SetShaderParameters(DeviceContext deviceContext, Matrix worldMatrix, Matrix viewMatrix, Matrix projectionMatrix, ShaderResourceView[] textures, float translation)
        {
            try
            {
                #region Set Matrix Shader Resources
                // Transpose the matrices to prepare them for shader.
                worldMatrix.Transpose();
                viewMatrix.Transpose();
                projectionMatrix.Transpose();

                // Lock the constant buffer so it can be written to.
                DataStream mappedResource;
                deviceContext.MapSubresource(ConstantMatrixBuffer, MapMode.WriteDiscard, SharpDX.Direct3D11.MapFlags.None, out mappedResource);

                // Copy the matrices into the constant buffer.
                var matrixBuffer = new MatrixBuffer()
                {
                    world      = worldMatrix,
                    view       = viewMatrix,
                    projection = projectionMatrix
                };

                mappedResource.Write(matrixBuffer);

                // Unlock the constant buffer.
                deviceContext.UnmapSubresource(ConstantMatrixBuffer, 0);

                // Set the position of the constant buffer in the vertex shader.
                var bufferNumber = 0;

                // Finally set the constant buffer in the vertex shader with the updated values.
                deviceContext.VertexShader.SetConstantBuffer(bufferNumber, ConstantMatrixBuffer);

                // Set shader resource in the pixel shader.
                deviceContext.PixelShader.SetShaderResources(0, textures);
                #endregion

                #region Set Translate Shader Resources
                // Lock the constant buffer so it can be written to.
                deviceContext.MapSubresource(ConstantTranslateBuffer, MapMode.WriteDiscard, SharpDX.Direct3D11.MapFlags.None, out mappedResource);

                // Copy the matrices into the constant buffer.
                var TranslateBuffer = new TranslateBuffer()
                {
                    translation = translation
                };

                mappedResource.Write(TranslateBuffer);

                // Unlock the constant buffer.
                deviceContext.UnmapSubresource(ConstantTranslateBuffer, 0);

                // Set the position of the constant buffer in the vertex shader.
                bufferNumber = 0;

                // Finally set the constant buffer in the vertex shader with the updated values.
                deviceContext.PixelShader.SetConstantBuffer(bufferNumber, ConstantTranslateBuffer);
                #endregion

                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }