public override void InitCBuffersFrame(DeviceContext deviceContext, Camera camera, LightClass light) { DataStream mappedResource; #region Constant Camera Buffer deviceContext.MapSubresource(ConstantCameraBuffer, MapMode.WriteDiscard, MapFlags.None, out mappedResource); var cameraBuffer = new DCameraBuffer() { cameraPosition = camera.Position, padding = 0.0f }; mappedResource.Write(cameraBuffer); deviceContext.UnmapSubresource(ConstantCameraBuffer, 0); int bufferSlotNumber = 1; deviceContext.VertexShader.SetConstantBuffer(bufferSlotNumber, ConstantCameraBuffer); #endregion #region Constant Light Buffer deviceContext.MapSubresource(ConstantLightBuffer, MapMode.WriteDiscard, MapFlags.None, out mappedResource); LightBuffer lightbuffer = new LightBuffer() { ambientColor = light.AmbientColor, diffuseColor = light.DiffuseColour, LightDirection = light.Direction, specularColor = light.SpecularColor, specularPower = light.SpecularPower }; mappedResource.Write(lightbuffer); deviceContext.UnmapSubresource(ConstantLightBuffer, 0); bufferSlotNumber = 0; deviceContext.PixelShader.SetConstantBuffer(bufferSlotNumber, ConstantLightBuffer); #endregion }
public virtual void InitCBuffersFrame(DeviceContext context, Camera camera, WorldSettings settings) { var cameraBuffer = new DCameraBuffer() { cameraPosition = camera.Position, padding = 0.0f }; ConstantBufferFactory.UpdateVertexBuffer(context, ConstantCameraBuffer, 1, cameraBuffer); if (previousLighting == null || !previousLighting.Equals(settings.Lighting)) { LightBuffer lightbuffer = new LightBuffer() { ambientColor = settings.Lighting.AmbientColor, diffuseColor = settings.Lighting.DiffuseColour, LightDirection = settings.Lighting.Direction, specularColor = settings.Lighting.SpecularColor, specularPower = settings.Lighting.SpecularPower }; previousLighting = settings.Lighting; ConstantBufferFactory.UpdatePixelBuffer(context, ConstantLightBuffer, 0, lightbuffer); } }
private bool SetShaderParameters(DeviceContext deviceContext, Matrix worldMatrix, Matrix viewMatrix, Matrix projectionMatrix, ShaderResourceView[] textures, Vector3 lightDirection, Vector4 diffuseColor, Vector3 cameraPosition, Vector4 specularColor, float specularPower) { try { #region Constant Matrix Buffer // 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 passed in matrices into the constant buffer. DMatrixBuffer matrixBuffer = new DMatrixBuffer() { 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. int bufferPositionNumber = 0; // Finally set the constant buffer in the vertex shader with the updated values. deviceContext.VertexShader.SetConstantBuffer(bufferPositionNumber, ConstantMatrixBuffer); // Set shader resource in the pixel shader. deviceContext.PixelShader.SetShaderResources(0, textures); #endregion #region Constant Light Buffer // Lock the light constant buffer so it can be written to. deviceContext.MapSubresource(ConstantLightBuffer, MapMode.WriteDiscard, SharpDX.Direct3D11.MapFlags.None, out mappedResource); // Copy the lighting variables into the constant buffer. DLightBuffer lightBuffer = new DLightBuffer() { diffuseColor = diffuseColor, lightDirection = lightDirection, specularColor = specularColor, specularPower = specularPower, }; mappedResource.Write(lightBuffer); // Unlock the constant buffer. deviceContext.UnmapSubresource(ConstantLightBuffer, 0); // Set the position of the light constant buffer in the pixel shader. bufferPositionNumber = 0; // Finally set the light constant buffer in the pixel shader with the updated values. deviceContext.PixelShader.SetConstantBuffer(bufferPositionNumber, ConstantLightBuffer); #endregion #region Constant Camera Buffer // Lock the camera constant buffer so it can be written to. deviceContext.MapSubresource(ConstantCameraBuffer, MapMode.WriteDiscard, SharpDX.Direct3D11.MapFlags.None, out mappedResource); // Copy the lighting variables into the constant buffer. var cameraBuffer = new DCameraBuffer() { cameraPosition = cameraPosition, padding = 0.0f }; mappedResource.Write(cameraBuffer); // Unlock the constant buffer. deviceContext.UnmapSubresource(ConstantCameraBuffer, 0); // Set the position of the light constant buffer in the pixel shader. bufferPositionNumber = 1; // Now set the camera constant buffer in the vertex shader with the updated values. deviceContext.VertexShader.SetConstantBuffer(bufferPositionNumber, ConstantCameraBuffer); #endregion return(true); } catch { return(false); } }