示例#1
0
        bool SetShaderParameters(DeviceContext deviceContext, Matrix world, Matrix view, Matrix projection, ShaderResourceView texture, Vector3 lightDirection, Color4 ambientColor, Color4 diffuseColor)
        {
            try {
                var matrixBufferType = new MatrixBufferType {
                    World      = Matrix.Transpose(world),
                    View       = Matrix.Transpose(view),
                    Projection = Matrix.Transpose(projection)
                };

                deviceContext.UpdateSubresource(ref matrixBufferType, matrixBuffer);
                deviceContext.VertexShader.SetConstantBuffer(0, matrixBuffer);
                deviceContext.PixelShader.SetShaderResource(0, texture);

                var lightBufferType = new LightBufferType {
                    AmbientColor   = ambientColor,
                    DiffuseColor   = diffuseColor,
                    LightDirection = lightDirection,
                    Padding        = 0.0f
                };

                deviceContext.UpdateSubresource(ref lightBufferType, lightBuffer);
                deviceContext.PixelShader.SetConstantBuffer(0, lightBuffer);
            } catch { return(false); }
            return(true);
        }
        bool SetShaderParameters(DeviceContext deviceContext, Matrix world, Matrix view, Matrix projection, ShaderResourceView[] textureArray, Vector3 lightDirection, Color4 diffuseColor, Vector3 cameraPosition, Color4 specularColor, float specularPower)
        {
            try {
                var matrixBufferType = new MatrixBufferType {
                    World      = Matrix.Transpose(world),
                    View       = Matrix.Transpose(view),
                    Projection = Matrix.Transpose(projection)
                };

                deviceContext.UpdateSubresource(ref matrixBufferType, matrixBuffer);
                deviceContext.VertexShader.SetConstantBuffer(0, matrixBuffer);
                deviceContext.PixelShader.SetShaderResources(0, textureArray);

                var lightBufferType = new LightBufferType {
                    DiffuseColor   = diffuseColor,
                    LightDirection = lightDirection,
                    SpecularColor  = specularColor,
                    SpecularPower  = specularPower
                };

                deviceContext.UpdateSubresource(ref lightBufferType, lightBuffer);
                deviceContext.PixelShader.SetConstantBuffer(0, lightBuffer);

                var cameraBufferType = new CameraBufferType {
                    CameraPosition = cameraPosition
                };

                deviceContext.UpdateSubresource(ref cameraBufferType, cameraBuffer);
                deviceContext.VertexShader.SetConstantBuffer(1, cameraBuffer);
            } catch { return(false); }
            return(true);
        }