Graphics Reference
In-Depth Information
perFrameBuffer = ToDispose(new Buffer(device,
Utilities.SizeOf< ConstantBuffers.PerFrame >(),
ResourceUsage.Default, BindFlags.ConstantBuffer,
CpuAccessFlags.None, ResourceOptionFlags.None, 0));
20. In the same method, we bind them to the shaders (note that we now bind a
buffer to the pixel shader as well).
// Set our vertex constant buffers
context.VertexShader.SetConstantBuffer(0, perObjectBuffer );
context.VertexShader.SetConstantBuffer(1, perFrameBuffer);
// Set our pixel constant buffers
context.PixelShader.SetConstantBuffer(1, perFrameBuffer);
21. Within our render loop, we can now change the value of the buffers to an instance
of one of the structures we have defined. First is the PerFrame constant buffer.
// Extract camera position from view matrix
var camPosition = Matrix.Transpose(
Matrix.Invert(viewMatrix)).Column4;
cameraPosition = new Vector3(
camPosition.X, camPosition.Y, camPosition.Z);
// Update the per frame constant buffer
var perFrame = new ConstantBuffers.PerFrame();
perFrame.CameraPosition = cameraPosition;
context.UpdateSubresource(ref perFrame, perFrameBuffer);
22. Before each call to the renderer's Render method, change the code to use our
PerObject structure. Here we show the code for the quad:
var perObject = new ConstantBuffers.PerObject();
perObject.World = quad.World * worldMatrix;
perObject.WorldInverseTranspose = Matrix.Transpose(Matrix.
Invert(perObject.World));
perObject.WorldViewProjection = perObject.World * viewProjection;
perObject.Transpose();
context.UpdateSubresource(ref perObject, perObjectBuffer);
 
Search WWH ::




Custom Search