本文整理匯總了C#中SharpDX.Direct3D11.RenderTargetView.Dispose方法的典型用法代碼示例。如果您正苦於以下問題:C# RenderTargetView.Dispose方法的具體用法?C# RenderTargetView.Dispose怎麽用?C# RenderTargetView.Dispose使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類SharpDX.Direct3D11.RenderTargetView
的用法示例。
在下文中一共展示了RenderTargetView.Dispose方法的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: Main
//.........這裏部分代碼省略.........
if (device == null)
throw new NotSupportedException("SharpDX.Direct3D11.Device1 is not supported");
}
// Rather than create a new DXGI Factory we should reuse
// the one that has been used internally to create the device
using (var dxgi = device.QueryInterface<SharpDX.DXGI.Device2>())
using (var adapter = dxgi.Adapter)
using (var factory = adapter.GetParent<Factory2>())
{
var desc1 = new SwapChainDescription1()
{
Width = form.ClientSize.Width,
Height = form.ClientSize.Height,
Format = Format.R8G8B8A8_UNorm,
Stereo = false,
SampleDescription = new SampleDescription(1, 0),
Usage = Usage.BackBuffer | Usage.RenderTargetOutput,
BufferCount = 1,
Scaling = Scaling.Stretch,
SwapEffect = SwapEffect.Discard,
};
swapChain = new SwapChain1(factory,
device,
form.Handle,
ref desc1,
new SwapChainFullScreenDescription()
{
RefreshRate = new Rational(60, 1),
Scaling = DisplayModeScaling.Centered,
Windowed = true
},
// Restrict output to specific Output (monitor)
null);
}
// Create references to backBuffer and renderTargetView
var backBuffer = Texture2D.FromSwapChain<Texture2D>(swapChain, 0);
var renderTargetView = new RenderTargetView(device, backBuffer);
#endregion
#region Render loop
// Create Clock and FPS counters
var clock = new System.Diagnostics.Stopwatch();
var clockFrequency = (double)System.Diagnostics.Stopwatch.Frequency;
clock.Start();
var deltaTime = 0.0;
var fpsTimer = new System.Diagnostics.Stopwatch();
fpsTimer.Start();
var fps = 0.0;
int fpsFrames = 0;
// Create and run the render loop
RenderLoop.Run(form, () =>
{
// Time in seconds
var totalSeconds = clock.ElapsedTicks / clockFrequency;
#region FPS and title update
fpsFrames++;
if (fpsTimer.ElapsedMilliseconds > 1000)
{
fps = 1000.0 * fpsFrames / fpsTimer.ElapsedMilliseconds;
// Update window title with FPS once every second
form.Text = string.Format("D3DRendering D3D11.1 - FPS: {0:F2} ({1:F2}ms/frame)", fps, (float)fpsTimer.ElapsedMilliseconds / fpsFrames);
// Restart the FPS counter
fpsTimer.Reset();
fpsTimer.Start();
fpsFrames = 0;
}
#endregion
// Execute rendering commands here...
device.ImmediateContext.ClearRenderTargetView(
renderTargetView,
Color.LightBlue);
// Present the frame
swapChain.Present(0, PresentFlags.None, new PresentParameters());
// Determine the time it took to render the frame
deltaTime = (clock.ElapsedTicks / clockFrequency) - totalSeconds;
});
#endregion
#region Direct3D Cleanup
// Release the device and any other resources created
renderTargetView.Dispose();
backBuffer.Dispose();
device.Dispose();
swapChain.Dispose();
#endregion
}
示例2: Main
//.........這裏部分代碼省略.........
});
// Create Constant Buffer
var contantBuffer = new Buffer(device, Utilities.SizeOf<Matrix>(), ResourceUsage.Default, BindFlags.ConstantBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0);
// Create Depth Buffer & View
var depthBuffer = new Texture2D(device, new Texture2DDescription()
{
Format = Format.D32_Float_S8X24_UInt,
ArraySize = 1,
MipLevels = 1,
Width = form.ClientSize.Width,
Height = form.ClientSize.Height,
SampleDescription = new SampleDescription(1, 0),
Usage = ResourceUsage.Default,
BindFlags = BindFlags.DepthStencil,
CpuAccessFlags = CpuAccessFlags.None,
OptionFlags = ResourceOptionFlags.None
});
var depthView = new DepthStencilView(device, depthBuffer);
// Load texture and create sampler
var texture = Texture2D.FromFile<Texture2D>(device, "GeneticaMortarlessBlocks.jpg");
var textureView = new ShaderResourceView(device, texture);
var sampler = new SamplerState(device, new SamplerStateDescription()
{
Filter = Filter.MinMagMipLinear,
AddressU = TextureAddressMode.Wrap,
AddressV = TextureAddressMode.Wrap,
AddressW = TextureAddressMode.Wrap,
BorderColor = Color.Black,
ComparisonFunction = Comparison.Never,
MaximumAnisotropy = 16,
MipLodBias = 0,
MinimumLod = 0,
MaximumLod = 16,
});
// Prepare All the stages
context.InputAssembler.InputLayout = layout;
context.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;
context.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(vertices, Utilities.SizeOf<Vector4>() + Utilities.SizeOf<Vector2>(), 0));
context.VertexShader.SetConstantBuffer(0, contantBuffer);
context.VertexShader.Set(vertexShader);
context.Rasterizer.SetViewports(new Viewport(0, 0, form.ClientSize.Width, form.ClientSize.Height, 0.0f, 1.0f));
context.PixelShader.Set(pixelShader);
context.PixelShader.SetSampler(0, sampler);
context.PixelShader.SetShaderResource(0, textureView);
context.OutputMerger.SetTargets(depthView, renderView);
// Prepare matrices
var view = Matrix.LookAtLH(new Vector3(0, 0, -5), new Vector3(0, 0, 0), Vector3.UnitY);
var proj = Matrix.PerspectiveFovLH((float)Math.PI / 4.0f, form.ClientSize.Width / (float)form.ClientSize.Height, 0.1f, 100.0f);
var viewProj = Matrix.Multiply(view, proj);
// Use clock
var clock = new Stopwatch();
clock.Start();
// Main loop
RenderLoop.Run(form, () =>
{
var time = clock.ElapsedMilliseconds / 1000.0f;
// Clear views
context.ClearDepthStencilView(depthView, DepthStencilClearFlags.Depth, 1.0f, 0);
context.ClearRenderTargetView(renderView, Color.Black);
// Update WorldViewProj Matrix
var worldViewProj = Matrix.RotationX(time) * Matrix.RotationY(time * 2) * Matrix.RotationZ(time * .7f) * viewProj;
worldViewProj.Transpose();
context.UpdateSubresource(ref worldViewProj, contantBuffer);
// Draw the cube
context.Draw(36, 0);
// Present!
swapChain.Present(0, PresentFlags.None);
});
// Release all resources
vertexShaderByteCode.Dispose();
vertexShader.Dispose();
pixelShaderByteCode.Dispose();
pixelShader.Dispose();
vertices.Dispose();
layout.Dispose();
renderView.Dispose();
backBuffer.Dispose();
context.ClearState();
context.Flush();
device.Dispose();
context.Dispose();
swapChain.Dispose();
factory.Dispose();
}
示例3: Main
private static void Main()
{
var form = new RenderForm("SharpDX - MiniTri Direct3D 11 Sample using Effects11 system");
Configuration.EnableObjectTracking = true;
// SwapChain description
var desc = new SwapChainDescription()
{
BufferCount = 1,
ModeDescription =
new ModeDescription(form.ClientSize.Width, form.ClientSize.Height,
new Rational(60, 1), Format.R8G8B8A8_UNorm),
IsWindowed = true,
OutputHandle = form.Handle,
SampleDescription = new SampleDescription(1, 0),
SwapEffect = SwapEffect.Discard,
Usage = Usage.RenderTargetOutput
};
// Create Device and SwapChain
Device device;
SwapChain swapChain;
Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.None, desc, out device, out swapChain);
var context = device.ImmediateContext;
// Ignore all windows events
var factory = swapChain.GetParent<Factory>();
factory.MakeWindowAssociation(form.Handle, WindowAssociationFlags.IgnoreAll);
// New RenderTargetView from the backbuffer
var backBuffer = Texture2D.FromSwapChain<Texture2D>(swapChain, 0);
var renderView = new RenderTargetView(device, backBuffer);
// Compile Vertex and Pixel shaders
var effectByteCode = ShaderBytecode.CompileFromFile("MiniTri.fx", "fx_5_0", ShaderFlags.None, EffectFlags.None);
var effect = new Effect(device, effectByteCode);
var technique = effect.GetTechniqueByIndex(0);
var pass = technique.GetPassByIndex(0);
// Layout from VertexShader input signature
var passSignature = pass.Description.Signature;
var layout = new InputLayout(device, passSignature, new[]
{
new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0),
new InputElement("COLOR", 0, Format.R32G32B32A32_Float, 16, 0)
});
// Instantiate Vertex buffer from vertex data
var vertices = Buffer.Create(device, BindFlags.VertexBuffer, new[]
{
new Vector4(0.0f, 0.5f, 0.5f, 1.0f), new Vector4(1.0f, 0.0f, 0.0f, 1.0f),
new Vector4(0.5f, -0.5f, 0.5f, 1.0f), new Vector4(0.0f, 1.0f, 0.0f, 1.0f),
new Vector4(-0.5f, -0.5f, 0.5f, 1.0f), new Vector4(0.0f, 0.0f, 1.0f, 1.0f)
});
// Prepare All the stages
context.InputAssembler.InputLayout = layout;
context.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;
context.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(vertices, 32, 0));
context.Rasterizer.SetViewport(new Viewport(0, 0, form.ClientSize.Width, form.ClientSize.Height, 0.0f, 1.0f));
context.OutputMerger.SetTargets(renderView);
// Main loop
RenderLoop.Run(form, () =>
{
context.ClearRenderTargetView(renderView, Color.Black);
for (int i = 0; i < technique.Description.PassCount; ++i)
{
pass.Apply(context);
context.Draw(3, 0);
}
swapChain.Present(0, PresentFlags.None);
});
// Release all resources
passSignature.Dispose();
effect.Dispose();
effectByteCode.Dispose();
vertices.Dispose();
layout.Dispose();
renderView.Dispose();
backBuffer.Dispose();
device.Dispose();
swapChain.Dispose();
factory.Dispose();
}
示例4: Main
//.........這裏部分代碼省略.........
String directory = null;
if (scene != null)
{
directory = Path.GetDirectoryName(path);
}
else
{
MessageBox.Show("Failed to open file: " + path + ". Either Assimp screwed up or the path is not valid.");
Application.Exit();
}
SceneLoader SceneLoader = new SceneLoader();
List<model> models = new List<model>();
for (int i = 0; i < scene.mNumMeshes; i++)
{
models.Add(SceneLoader.CreateMesh(device, scene.mMeshes[i], scene.mMaterials,directory));
}
//////////////////////////////
// Compile Vertex and Pixel shaders
var effectByteCode = ShaderBytecode.CompileFromFile("MiniTri.fx", "fx_5_0", ShaderFlags.None, EffectFlags.None);
var effect = new Effect(device, effectByteCode);
var technique = effect.GetTechniqueByIndex(0);
var pass = technique.GetPassByIndex(0);
// Layout from VertexShader input signature
var passSignature = pass.Description.Signature;
// Layout from VertexShader input signature
var layout = new InputLayout(
device,
passSignature,
new[]
{
new InputElement("POSITION", 0, Format.R32G32B32_Float, 0, 0),
new InputElement("TEXCOORD", 0, Format.R32G32_Float, 12, 0)
});
// Prepare All the stages
context.Rasterizer.SetViewports(new Viewport(0, 0, form.ClientSize.Width, form.ClientSize.Height, 0.0f, 1.0f));
context.OutputMerger.DepthStencilState = depthStencilState;
Input input = new Input(form);
//FreeLook FreeLook = new SharpExamples.FreeLook(input, (float)form.Width / (float)form.Height);
CameraFirstPerson FreeLook = new CameraFirstPerson(input, 0, 0, Vector3.Zero, form.Width, form.Height);
//FreeLook.SetEyeTarget(new Vector3(300), Vector3.Zero);
Clock Clock = new SharpExamples.Clock();
Clock.Start();
effect.GetVariableByName("projection").AsMatrix().SetMatrix(FreeLook.Projection);
// Main loop
RenderLoop.Run(form, () =>
{
foreach (var item in models)
{
context.InputAssembler.InputLayout = layout;
context.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;
int SizeInBytes = Marshal.SizeOf(typeof(VertexPostitionTexture));
context.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(item.vertex, SizeInBytes, 0));
context.InputAssembler.SetIndexBuffer(item.indices, Format.R32_UInt, 0);
float elapsed = Clock.Update();
FreeLook.Update(elapsed);
effect.GetVariableByName("view").AsMatrix().SetMatrix(FreeLook.View);
effect.GetVariableByName("World").AsMatrix().SetMatrix(Matrix.Scaling(5));
effect.GetVariableByName("tex0").AsShaderResource().SetResource(item.ShaderResourceView);
context.ClearRenderTargetView(renderView, Colors.Black);
context.ClearDepthStencilView(depthStencilView, DepthStencilClearFlags.Depth | DepthStencilClearFlags.Stencil, 1, 0);
for (int i = 0; i < technique.Description.PassCount; ++i)
{
pass.Apply(context);
context.DrawIndexed(item.numberIndices, 0, 0);
}
swapChain.Present(0, PresentFlags.None);
}
});
// Release all resources
foreach (var item in models)
{
item.vertex.Dispose();
item.indices.Dispose();
item.ShaderResourceView.Dispose();
}
layout.Dispose();
renderView.Dispose();
backBuffer.Dispose();
context.ClearState();
context.Flush();
device.Dispose();
context.Dispose();
swapChain.Dispose();
factory.Dispose();
}
示例5: Main
static void Main()
{
var form = new RenderForm("FUN STUFF PHYSXS");
// SwapChain description
var desc = new SwapChainDescription()
{
BufferCount = 1,
ModeDescription =
new ModeDescription(form.ClientSize.Width, form.ClientSize.Height,
new Rational(60, 1), Format.R8G8B8A8_UNorm),
IsWindowed = true,
OutputHandle = form.Handle,
SampleDescription = new SampleDescription(1, 0),
SwapEffect = SwapEffect.Discard,
Usage = Usage.RenderTargetOutput
};
// Create Device and SwapChain
Device device;
SwapChain swapChain;
Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.BgraSupport, new SharpDX.Direct3D.FeatureLevel[] { SharpDX.Direct3D.FeatureLevel.Level_10_0 }, desc, out device, out swapChain);
var d2dFactory = new SharpDX.Direct2D1.Factory();
// Ignore all windows events
Factory factory = swapChain.GetParent<Factory>();
factory.MakeWindowAssociation(form.Handle, WindowAssociationFlags.IgnoreAll);
// New RenderTargetView from the backbuffer
Texture2D backBuffer = Texture2D.FromSwapChain<Texture2D>(swapChain, 0);
var renderView = new RenderTargetView(device, backBuffer);
Surface surface = backBuffer.QueryInterface<Surface>();
var d2dRenderTarget = new RenderTarget(d2dFactory, surface,
new RenderTargetProperties(new PixelFormat(Format.Unknown, AlphaMode.Premultiplied)));
var solidColorBrush = new SolidColorBrush(d2dRenderTarget, Color.White);
var sim = new Simulation();
// Main loop
RenderLoop.Run(form, () =>
{
d2dRenderTarget.BeginDraw();
int width = form.ClientSize.Width;
int height = form.ClientSize.Height;
var simScaleX = sim.SimBounds.MaxX - sim.SimBounds.MinX;
var simScaleY = sim.SimBounds.MaxY - sim.SimBounds.MinY;
var viewScaleX = sim.ViewBounds.MaxX - sim.ViewBounds.MinX;
var viewScaleY = sim.ViewBounds.MaxY - sim.ViewBounds.MinY;
d2dRenderTarget.Clear(Color.Black);
foreach (Body body in sim.State)
{
var x = Convert.ToSingle((body.Pos.X - sim.SimBounds.MinX) * width / simScaleX);
var y = Convert.ToSingle((body.Pos.Y - sim.SimBounds.MinY) * height / simScaleY);
var xRad = Convert.ToSingle((body.Radius) * width / viewScaleX);
var yRad = Convert.ToSingle((body.Radius) * height / viewScaleY);
var elipse = new EllipseGeometry(d2dFactory, new Ellipse(new Vector2(x, y), xRad, yRad));
d2dRenderTarget.FillGeometry(elipse, solidColorBrush, null);
}
d2dRenderTarget.EndDraw();
sim.Step();
swapChain.Present(0, PresentFlags.None);
});
// Release all resources
renderView.Dispose();
backBuffer.Dispose();
device.ImmediateContext.ClearState();
device.ImmediateContext.Flush();
device.Dispose();
device.Dispose();
swapChain.Dispose();
factory.Dispose();
}
示例6: Main
private static void Main()
{
var form = new RenderForm("SharpDX - MiniTri Direct2D - Direct3D 10 Sample");
// SwapChain description
var desc = new SwapChainDescription()
{
BufferCount = 1,
ModeDescription =
new ModeDescription(form.ClientSize.Width, form.ClientSize.Height,
new Rational(60, 1), Format.R8G8B8A8_UNorm),
IsWindowed = true,
OutputHandle = form.Handle,
SampleDescription = new SampleDescription(1, 0),
SwapEffect = SwapEffect.Discard,
Usage = Usage.RenderTargetOutput
};
// Create Device and SwapChain
Device device;
SwapChain swapChain;
Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.BgraSupport, new SharpDX.Direct3D.FeatureLevel[] { SharpDX.Direct3D.FeatureLevel.Level_10_0 }, desc, out device, out swapChain);
var d2dFactory = new SharpDX.Direct2D1.Factory();
int width = form.ClientSize.Width;
int height = form.ClientSize.Height;
var rectangleGeometry = new RoundedRectangleGeometry(d2dFactory, new RoundedRectangle() { RadiusX = 32, RadiusY = 32, Rect = new RectangleF(128, 128, width - 128 * 2, height-128 * 2) });
// Ignore all windows events
Factory factory = swapChain.GetParent<Factory>();
factory.MakeWindowAssociation(form.Handle, WindowAssociationFlags.IgnoreAll);
// New RenderTargetView from the backbuffer
Texture2D backBuffer = Texture2D.FromSwapChain<Texture2D>(swapChain, 0);
var renderView = new RenderTargetView(device, backBuffer);
Surface surface = backBuffer.QueryInterface<Surface>();
var d2dRenderTarget = new RenderTarget(d2dFactory, surface,
new RenderTargetProperties(new PixelFormat(Format.Unknown, AlphaMode.Premultiplied)));
var solidColorBrush = new SolidColorBrush(d2dRenderTarget, Color.White);
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
// Main loop
RenderLoop.Run(form, () =>
{
d2dRenderTarget.BeginDraw();
d2dRenderTarget.Clear(Color.Black);
solidColorBrush.Color = new Color4(1, 1, 1, (float)Math.Abs(Math.Cos(stopwatch.ElapsedMilliseconds * .001)));
d2dRenderTarget.FillGeometry(rectangleGeometry, solidColorBrush, null);
d2dRenderTarget.EndDraw();
swapChain.Present(0, PresentFlags.None);
});
// Release all resources
renderView.Dispose();
backBuffer.Dispose();
device.ImmediateContext.ClearState();
device.ImmediateContext.Flush();
device.Dispose();
device.Dispose();
swapChain.Dispose();
factory.Dispose();
}
示例7: Program
//.........這裏部分代碼省略.........
var sampler = new SamplerState(device, new SamplerStateDescription()
{
Filter = Filter.MinMagMipLinear,
AddressU = TextureAddressMode.Wrap,
AddressV = TextureAddressMode.Wrap,
AddressW = TextureAddressMode.Wrap,
BorderColor = Color.Black,
ComparisonFunction = Comparison.Never,
MaximumAnisotropy = 16,
MipLodBias = 0,
MinimumLod = 0,
MaximumLod = 16,
});
// Create Constant Buffers
var vertexConstantBuffer = new Buffer(device, Utilities.SizeOf<VertexShaderData>(), ResourceUsage.Default, BindFlags.ConstantBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0);
var pixelConstantBuffer = new Buffer(device, Utilities.SizeOf<PixelShaderData>(), ResourceUsage.Default, BindFlags.ConstantBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0);
// Prepare some of the stages. The others are mesh dependent and will be set later
context.VertexShader.Set(vertexShader);
context.Rasterizer.SetViewport(new Viewport(0, 0, form.Width, form.Height, 0.0f, 1.0f));
context.PixelShader.Set(pixelShader);
context.PixelShader.SetSampler(0, sampler);
context.OutputMerger.SetTargets(depthView, renderView);
context.VertexShader.SetConstantBuffer(0, vertexConstantBuffer);
context.PixelShader.SetConstantBuffer(0, pixelConstantBuffer);
//allocate data structs for the vertex and pixel constant buffers
VertexShaderData vsData = new VertexShaderData();
PixelShaderData psData = new PixelShaderData();
//set light position
psData.lightPos = new Vector4(0, 2.5f, 0, 0);
//update pixel shader constant buffer. Only need to do this once.
context.UpdateSubresource(ref psData, pixelConstantBuffer);
// Calc projection matrix
var proj = Matrix.PerspectiveFovLH((float)Math.PI / 4.0f, form.Width / (float)form.Height, 0.1f, 100.0f);
// Use clock
var clock = new Stopwatch();
clock.Start();
// Main loop
RenderLoop.Run(form, () =>
{
var time = clock.ElapsedMilliseconds / 1000.0f;
// Prepare matrices
Vector3 cameraPosition = new Vector3(0, 3, 5.0f);
Vector3 cameraLookAt = new Vector3(0, 2.0f, 0);
//rotate camera
Vector4 tempPos = Vector3.Transform(cameraPosition, Matrix.RotationY(0.2f * time));
cameraPosition = new Vector3(tempPos.X, tempPos.Y, tempPos.Z);
//calculate the view matrix
var view = Matrix.LookAtLH(cameraPosition, cameraLookAt, Vector3.UnitY);
var viewProj = Matrix.Multiply(view, proj);
// Clear views
context.ClearDepthStencilView(depthView, DepthStencilClearFlags.Depth, 1.0f, 0);
context.ClearRenderTargetView(renderView, Color.Black);
// Update transformation matrices
vsData.world = Matrix.Translation(0, -model.AABoxCentre.Y / 2, 0);
vsData.worldView = vsData.world * view;
vsData.worldViewProj = vsData.world * viewProj;
//transpose matrices before sending them to the shader
vsData.world.Transpose();
vsData.worldView.Transpose();
vsData.worldViewProj.Transpose();
//update vertex shader constant buffer
context.UpdateSubresource(ref vsData, vertexConstantBuffer);
// Draw the cube
model.Render(context);
// Present
swapChain.Present(0, PresentFlags.None);
});
// Release all resources
model.Dispose();
vertexShaderByteCode.Dispose();
vertexShader.Dispose();
pixelShaderByteCode.Dispose();
pixelShader.Dispose();
renderView.Dispose();
backBuffer.Dispose();
context.ClearState();
context.Flush();
device.Dispose();
context.Dispose();
swapChain.Dispose();
factory.Dispose();
}
示例8: InitializeOculus
//.........這裏部分代碼省略.........
int textureIndex = eyeTexture.SwapTextureSet.CurrentIndex++;
immediateContext.OutputMerger.SetRenderTargets(eyeTexture.DepthStencilView, eyeTexture.RenderTargetViews[textureIndex]);
immediateContext.ClearRenderTargetView(eyeTexture.RenderTargetViews[textureIndex], Color.Black);
immediateContext.ClearDepthStencilView(eyeTexture.DepthStencilView, DepthStencilClearFlags.Depth | DepthStencilClearFlags.Stencil, 1.0f, 0);
immediateContext.Rasterizer.SetViewport(eyeTexture.Viewport);
//added a custom rasterizer
immediateContext.Rasterizer.State = rasterizerState;
// Retrieve the eye rotation quaternion and use it to calculate the LookAt direction and the LookUp direction.
Quaternion rotationQuaternion = SharpDXHelpers.ToQuaternion(eyePoses[eyeIndex].Orientation);
Matrix rotationMatrix = Matrix.RotationQuaternion(rotationQuaternion);
Vector3 lookUp = Vector3.Transform(new Vector3(0, -1, 0), rotationMatrix).ToVector3();
Vector3 lookAt = Vector3.Transform(new Vector3(0, 0, 1), rotationMatrix).ToVector3();
Vector3 viewPosition = position - eyePoses[eyeIndex].Position.ToVector3();
//use this to get the first rotation to goal
Matrix world = Matrix.Scaling(1.0f) /** Matrix.RotationX(timeSinceStart*0.2f) */* Matrix.RotationY(timeSinceStart * 2 / 10f) /** Matrix.RotationZ(timeSinceStart*3/10f)*/;
Matrix viewMatrix = Matrix.LookAtRH(viewPosition, viewPosition + lookAt, lookUp);
Matrix projectionMatrix = OVR.ovrMatrix4f_Projection(eyeTexture.FieldOfView, 0.1f, 10.0f, OVR.ProjectionModifier.None).ToMatrix();
projectionMatrix.Transpose();
Matrix worldViewProjection = world * viewMatrix * projectionMatrix;
worldViewProjection.Transpose();
// Update the transformation matrix.
immediateContext.UpdateSubresource(ref worldViewProjection, constantBuffer);
// Draw the cube
//immediateContext.Draw(vertices.Length/2, 0);
immediateContext.DrawIndexed(indices.Length, 0, 0);
}
hmd.SubmitFrame(0, layers);
immediateContext.CopyResource(mirrorTextureD3D11, backBuffer);
swapChain.Present(0, PresentFlags.None);
if (newTextureArrived == true)
{
newTextureArrived = false;
DataBox map = device.ImmediateContext.MapSubresource(myTexture, 0, MapMode.WriteDiscard, SharpDX.Direct3D11.MapFlags.None);
//load the BitMapSource with appropriate formating (Format32bppPRGBA)
SharpDX.WIC.BitmapSource bitMap = LoadBitmap(new SharpDX.WIC.ImagingFactory(), streamTexture);
//string newFile = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName) + @"\img_merged.jpg";
//SharpDX.WIC.BitmapSource bitMap = LoadBitmapFromFile(new SharpDX.WIC.ImagingFactory(), newFile);
int width = bitMap.Size.Width;
int height = bitMap.Size.Height;
int stride = bitMap.Size.Width * 4;
bitMap.CopyPixels(stride, map.DataPointer, height * stride);
device.ImmediateContext.UnmapSubresource(myTexture, 0);
//bitMap.Dispose();
streamTexture.Seek(0, SeekOrigin.Begin);
}
});
#endregion
// Release all resources
inputLayout.Dispose();
constantBuffer.Dispose();
indexBuffer.Dispose();
vertexBuffer.Dispose();
inputLayout.Dispose();
shaderSignature.Dispose();
pixelShader.Dispose();
pixelShaderByteCode.Dispose();
vertexShader.Dispose();
vertexShaderByteCode.Dispose();
mirrorTextureD3D11.Dispose();
layers.Dispose();
eyeTextures[0].Dispose();
eyeTextures[1].Dispose();
immediateContext.ClearState();
immediateContext.Flush();
immediateContext.Dispose();
depthStencilState.Dispose();
depthStencilView.Dispose();
depthBuffer.Dispose();
backBufferRenderTargetView.Dispose();
backBuffer.Dispose();
swapChain.Dispose();
factory.Dispose();
// Disposing the device, before the hmd, will cause the hmd to fail when disposing.
// Disposing the device, after the hmd, will cause the dispose of the device to fail.
// It looks as if the hmd steals ownership of the device and destroys it, when it's shutting down.
// device.Dispose();
hmd.Dispose();
oculus.Dispose();
}