Ejemplo n.º 1
0
        public void Render(DeviceContext deviceContext, int indexCount, Matrix worldMatrix, Matrix viewMatrix, Matrix projectionMatrix, Matrix reflexionMatrix,
            ShaderResourceView reflectionMap, ShaderResourceView refractionMap, ShaderResourceView bumpMap, Vector2 translation, Vector3 cameraPosition)
        {
            worldMatrix.Transpose();
            viewMatrix.Transpose();
            projectionMatrix.Transpose();
            reflexionMatrix.Transpose();
            // Lock the constant memory buffer so it can be written to.
            DataStream mappedResource;
            deviceContext.MapSubresource(ConstantMatrixBuffer, MapMode.WriteDiscard, SharpDX.Direct3D11.MapFlags.None, out mappedResource);

            // Copy the transposed matrices (because they are stored in column-major order on the GPU by default) into the constant buffer.
            var matrixBuffer = new MatrixBuffer()
            {
                world = worldMatrix,
                view = viewMatrix,
                projection = projectionMatrix,
                reflection = reflexionMatrix
            };
            mappedResource.Write(matrixBuffer);

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

            deviceContext.MapSubresource(ConstantTranslationBuffer, MapMode.WriteDiscard, SharpDX.Direct3D11.MapFlags.None, out mappedResource);

            // Copy the transposed matrices (because they are stored in column-major order on the GPU by default) into the constant buffer.
            var translationBuffer = new TranslationBuffer()
            {
                translation = translation
            };
            mappedResource.Write(translationBuffer);

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

            deviceContext.MapSubresource(ConstantCameraPositionBuffer, MapMode.WriteDiscard, SharpDX.Direct3D11.MapFlags.None, out mappedResource);

            // Copy the transposed matrices (because they are stored in column-major order on the GPU by default) into the constant buffer.
            var cameraPositionBuffer = new CameraPositionBuffer()
            {
                position = cameraPosition
            };
            mappedResource.Write(cameraPositionBuffer);

            // Unlock the constant buffer.
            deviceContext.UnmapSubresource(ConstantCameraPositionBuffer, 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 the vertex input layout.
            deviceContext.InputAssembler.InputLayout = Layout;

            // Set the vertex and pixel shaders that will be used to render this triangle.
            deviceContext.VertexShader.Set(VertexShader);
            deviceContext.PixelShader.Set(PixelShader);
            deviceContext.PixelShader.SetConstantBuffer(0, ConstantTranslationBuffer);
            deviceContext.PixelShader.SetConstantBuffer(1, ConstantCameraPositionBuffer);
            deviceContext.PixelShader.SetSampler(0, SamplerState);
            deviceContext.PixelShader.SetShaderResource(0, reflectionMap);
            deviceContext.PixelShader.SetShaderResource(1, refractionMap);
            deviceContext.PixelShader.SetShaderResource(2, bumpMap);

            // Render the triangle.
            deviceContext.DrawIndexed(indexCount, 0, 0);
        }
Ejemplo n.º 2
0
        public void Render(DeviceContext deviceContext, int indexCount, Matrix worldMatrix, Matrix viewMatrix, Matrix projectionMatrix, Matrix reflexionMatrix,
                           ShaderResourceView reflectionMap, ShaderResourceView refractionMap, ShaderResourceView bumpMap, Vector2 translation, Vector3 cameraPosition)
        {
            worldMatrix.Transpose();
            viewMatrix.Transpose();
            projectionMatrix.Transpose();
            reflexionMatrix.Transpose();
            // Lock the constant memory buffer so it can be written to.
            DataStream mappedResource;

            deviceContext.MapSubresource(ConstantMatrixBuffer, MapMode.WriteDiscard, SharpDX.Direct3D11.MapFlags.None, out mappedResource);

            // Copy the transposed matrices (because they are stored in column-major order on the GPU by default) into the constant buffer.
            var matrixBuffer = new MatrixBuffer()
            {
                world      = worldMatrix,
                view       = viewMatrix,
                projection = projectionMatrix,
                reflection = reflexionMatrix
            };

            mappedResource.Write(matrixBuffer);

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


            deviceContext.MapSubresource(ConstantTranslationBuffer, MapMode.WriteDiscard, SharpDX.Direct3D11.MapFlags.None, out mappedResource);

            // Copy the transposed matrices (because they are stored in column-major order on the GPU by default) into the constant buffer.
            var translationBuffer = new TranslationBuffer()
            {
                translation = translation
            };

            mappedResource.Write(translationBuffer);

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


            deviceContext.MapSubresource(ConstantCameraPositionBuffer, MapMode.WriteDiscard, SharpDX.Direct3D11.MapFlags.None, out mappedResource);

            // Copy the transposed matrices (because they are stored in column-major order on the GPU by default) into the constant buffer.
            var cameraPositionBuffer = new CameraPositionBuffer()
            {
                position = cameraPosition
            };

            mappedResource.Write(cameraPositionBuffer);

            // Unlock the constant buffer.
            deviceContext.UnmapSubresource(ConstantCameraPositionBuffer, 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 the vertex input layout.
            deviceContext.InputAssembler.InputLayout = Layout;

            // Set the vertex and pixel shaders that will be used to render this triangle.
            deviceContext.VertexShader.Set(VertexShader);
            deviceContext.PixelShader.Set(PixelShader);
            deviceContext.PixelShader.SetConstantBuffer(0, ConstantTranslationBuffer);
            deviceContext.PixelShader.SetConstantBuffer(1, ConstantCameraPositionBuffer);
            deviceContext.PixelShader.SetSampler(0, SamplerState);
            deviceContext.PixelShader.SetShaderResource(0, reflectionMap);
            deviceContext.PixelShader.SetShaderResource(1, refractionMap);
            deviceContext.PixelShader.SetShaderResource(2, bumpMap);

            // Render the triangle.
            deviceContext.DrawIndexed(indexCount, 0, 0);
        }